2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.gui.editors.apex.rest;
24 import java.net.MalformedURLException;
26 import java.net.URISyntaxException;
29 import org.apache.commons.lang3.StringUtils;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * This class reads and handles command line parameters to the Apex CLI editor.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class ApexEditorParameters {
40 // Logger for this class
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
43 /** The default port for connecting to the Web editor on. */
44 public static final int DEFAULT_REST_PORT = 18989;
46 /** The connection is held up until killed on demand. */
47 public static final int INFINITY_TIME_TO_LIVE = -1;
49 // Base URI the HTTP server will listen on
50 private static final String DEFAULT_SERVER_URI_PREFIX = "http://";
51 /** The server listens on localhost by default. */
52 public static final String DEFAULT_SERVER_URI_ROOT = "localhost";
53 private static final String DEFAULT_REST_PATH = "apexservices/";
54 private static final String DEFAULT_STATIC_PATH = "/";
56 // Constants for port checks
57 private static final int MIN_USER_PORT = 1024;
58 private static final int MAX_USER_PORT = 65535;
60 // Package that will field REST requests
61 private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.gui.editors.apex.rest" };
63 // The editor parameters
64 private boolean help = false;
65 private int restPort = DEFAULT_REST_PORT;
66 private long timeToLive = INFINITY_TIME_TO_LIVE;
67 private String listenAddress = DEFAULT_SERVER_URI_ROOT;
68 private String uploadUrl = null;
69 private String uploadUserid = null;
76 public String validate() {
77 String validationMessage = "";
78 validationMessage += validatePort();
79 validationMessage += validateTimeToLive();
80 validationMessage += validateUrl();
81 validationMessage += validateUploadUrl();
82 validationMessage += validateUploadUserid();
84 return validationMessage;
90 * @return the base URI
92 public URI getBaseUri() {
93 return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + "/" + DEFAULT_REST_PATH);
97 * Gets the REST packages.
99 * @return the REST packages
101 public String[] getRestPackages() {
102 return DEFAULT_PACKAGES;
106 * Gets the static path.
108 * @return the static path
110 public String getStaticPath() {
111 return DEFAULT_STATIC_PATH;
117 * @return a warning string, or an empty string
119 private String validatePort() {
120 if (restPort < MIN_USER_PORT || restPort > MAX_USER_PORT) {
121 return "port must be between " + MIN_USER_PORT + " and " + MAX_USER_PORT + "\n";
130 * @return a warning string, or an empty string
132 private String validateUrl() {
134 new URI(getBaseUri().toString()).parseServerAuthority();
136 } catch (final URISyntaxException e) {
137 String message = "listen address is not valid. " + e.getMessage() + "\n";
138 LOGGER.warn(message, e);
144 * Validate time to live.
148 private String validateTimeToLive() {
149 if (timeToLive < -1) {
150 return "time to live must be greater than -1 (set to -1 to wait forever)\n";
156 private String validateUploadUrl() {
157 if (!StringUtils.isBlank(uploadUrl)) {
160 } catch (MalformedURLException murle) {
161 return "Specified upload-url parameter is an invalid URL" + murle.getMessage() + "\n";
167 private String validateUploadUserid() {
168 if (!StringUtils.isEmpty(uploadUrl) && StringUtils.isEmpty(uploadUserid)) {
169 return "upload-userid parameter must be specified if the upload-url parameter is specified\n";