2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.editor.rest;
24 import java.net.URISyntaxException;
25 import org.slf4j.ext.XLogger;
26 import org.slf4j.ext.XLoggerFactory;
29 * This class reads and handles command line parameters to the Apex CLI editor.
31 * @author Liam Fallon (liam.fallon@ericsson.com)
33 public class ApexEditorParameters {
34 // Logger for this class
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
37 /** The default port for connecting to the Web editor on. */
38 public static final int DEFAULT_REST_PORT = 18989;
40 /** The connection is held up until killed on demand. */
41 public static final int INFINITY_TIME_TO_LIVE = -1;
43 // Base URI the HTTP server will listen on
44 private static final String DEFAULT_SERVER_URI_PREFIX = "http://";
45 /** The server listens on localhost by default. */
46 public static final String DEFAULT_SERVER_URI_ROOT = "localhost";
47 private static final String DEFAULT_REST_PATH = "/apexservices/";
48 private static final String DEFAULT_STATIC_PATH = "/";
50 // Constants for port checks
51 private static final int MIN_USER_PORT = 1024;
52 private static final int MAX_USER_PORT = 65535;
55 // Package that will field REST requests
56 private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.apex.client.editor.rest" };
58 // The editor parameters
59 private boolean helpSet = false;
60 private int restPort = DEFAULT_REST_PORT;
61 private long timeToLive = INFINITY_TIME_TO_LIVE;
62 private String listenAddress = DEFAULT_SERVER_URI_ROOT;
69 public String validate() {
70 String validationMessage = "";
71 validationMessage += validatePort();
72 validationMessage += validateTimeToLive();
73 validationMessage += validateUrl();
75 return validationMessage;
81 * @return the base URI
83 public URI getBaseUri() {
84 return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + DEFAULT_REST_PATH);
88 * Gets the REST packages.
90 * @return the REST packages
92 public String[] getRestPackages() {
93 return DEFAULT_PACKAGES;
97 * Gets the static path.
99 * @return the static path
101 public String getStaticPath() {
102 return DEFAULT_STATIC_PATH;
108 * @return a warning string, or an empty string
110 private String validatePort() {
111 if (restPort < MIN_USER_PORT || restPort > MAX_USER_PORT) {
112 return "port must be between " + MIN_USER_PORT + " and " + MAX_USER_PORT + "\n";
121 * @return a warning string, or an empty string
123 private String validateUrl() {
125 new URI(getBaseUri().toString()).parseServerAuthority();
127 } catch (final URISyntaxException e) {
128 String message = "listen address is not valid. " + e.getMessage() + "\n";
129 LOGGER.warn(message, e);
135 * Validate time to live.
139 private String validateTimeToLive() {
140 if (timeToLive < -1) {
141 return "time to live must be greater than -1 (set to -1 to wait forever)\n";
148 * Checks if is help set.
150 * @return true, if checks if is help set
152 public boolean isHelpSet() {
159 * @param help the help
161 public void setHelp(final boolean help) {
166 * Gets the REST port.
168 * @return the REST port
170 public int getRestPort() {
175 * Sets the REST port.
177 * @param incomingRestPort the REST port
179 public void setRestPort(final int incomingRestPort) {
180 this.restPort = incomingRestPort;
184 * Gets the time to live.
186 * @return the time to live
188 public long getTimeToLive() {
193 * Sets the time to live.
195 * @param timeToLive the time to live
197 public void setTimeToLive(final long timeToLive) {
198 this.timeToLive = timeToLive;
205 public String toString() {
206 final StringBuilder ret = new StringBuilder();
207 ret.append(this.getClass().getSimpleName()).append(": URI=").append(this.getBaseUri()).append(", TTL=")
208 .append(this.getTimeToLive()).append("sec");
209 return ret.toString();
213 * Gets the base address to listen on.
215 * @return the listenAddress
217 public String getListenAddress() {
218 return listenAddress;
222 * Sets the base address to listen on.
224 * @param listenAddress the new listenAddress
226 public void setListenAddress(final String listenAddress) {
227 this.listenAddress = listenAddress;