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;
26 import org.slf4j.ext.XLogger;
27 import org.slf4j.ext.XLoggerFactory;
30 * This class reads and handles command line parameters to the Apex CLI editor.
32 * @author Liam Fallon (liam.fallon@ericsson.com)
34 public class ApexEditorParameters {
35 // Logger for this class
36 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
38 /** The default port for connecting to the Web editor on. */
39 public static final int DEFAULT_REST_PORT = 18989;
41 /** The connection is held up until killed on demand. */
42 public static final int INFINITY_TIME_TO_LIVE = -1;
44 // Base URI the HTTP server will listen on
45 private static final String DEFAULT_SERVER_URI_PREFIX = "http://";
46 /** The server listens on localhost by default. */
47 public static final String DEFAULT_SERVER_URI_ROOT = "localhost";
48 private static final String DEFAULT_REST_PATH = "/apexservices/";
49 private static final String DEFAULT_STATIC_PATH = "/";
51 // Constants for port checks
52 private static final int MIN_USER_PORT = 1024;
53 private static final int MAX_USER_PORT = 65535;
56 // Package that will field REST requests
57 private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.apex.client.editor.rest" };
59 // The editor parameters
60 private boolean helpSet = false;
61 private int restPort = DEFAULT_REST_PORT;
62 private long timeToLive = INFINITY_TIME_TO_LIVE;
63 private String listenAddress = DEFAULT_SERVER_URI_ROOT;
70 public String validate() {
71 String validationMessage = "";
72 validationMessage += validatePort();
73 validationMessage += validateTimeToLive();
74 validationMessage += validateUrl();
76 return validationMessage;
82 * @return the base URI
84 public URI getBaseUri() {
85 return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + DEFAULT_REST_PATH);
89 * Gets the REST packages.
91 * @return the REST packages
93 public String[] getRestPackages() {
94 return DEFAULT_PACKAGES;
98 * Gets the static path.
100 * @return the static path
102 public String getStaticPath() {
103 return DEFAULT_STATIC_PATH;
109 * @return a warning string, or an empty string
111 private String validatePort() {
112 if (restPort < MIN_USER_PORT || restPort > MAX_USER_PORT) {
113 return "port must be between " + MIN_USER_PORT + " and " + MAX_USER_PORT + "\n";
122 * @return a warning string, or an empty string
124 private String validateUrl() {
126 new URI(getBaseUri().toString()).parseServerAuthority();
128 } catch (final URISyntaxException e) {
129 String message = "listen address is not valid. " + e.getMessage() + "\n";
130 LOGGER.warn(message, e);
136 * Validate time to live.
140 private String validateTimeToLive() {
141 if (timeToLive < -1) {
142 return "time to live must be greater than -1 (set to -1 to wait forever)\n";
149 * Checks if is help set.
151 * @return true, if checks if is help set
153 public boolean isHelpSet() {
160 * @param help the help
162 public void setHelp(final boolean help) {
167 * Gets the REST port.
169 * @return the REST port
171 public int getRestPort() {
176 * Sets the REST port.
178 * @param incomingRestPort the REST port
180 public void setRestPort(final int incomingRestPort) {
181 this.restPort = incomingRestPort;
185 * Gets the time to live.
187 * @return the time to live
189 public long getTimeToLive() {
194 * Sets the time to live.
196 * @param timeToLive the time to live
198 public void setTimeToLive(final long timeToLive) {
199 this.timeToLive = timeToLive;
206 public String toString() {
207 final StringBuilder ret = new StringBuilder();
208 ret.append(this.getClass().getSimpleName()).append(": URI=").append(this.getBaseUri()).append(", TTL=")
209 .append(this.getTimeToLive()).append("sec");
210 return ret.toString();
214 * Gets the base address to listen on.
216 * @return the listenAddress
218 public String getListenAddress() {
219 return listenAddress;
223 * Sets the base address to listen on.
225 * @param listenAddress the new listenAddress
227 public void setListenAddress(final String listenAddress) {
228 this.listenAddress = listenAddress;