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;
27 * This class reads and handles command line parameters to the Apex CLI editor.
29 * @author Liam Fallon (liam.fallon@ericsson.com)
31 public class ApexEditorParameters {
32 /** The default port for connecting to the Web editor on. */
33 public static final int DEFAULT_REST_PORT = 18989;
35 /** The connection is held up until killed on demand. */
36 public static final int INFINITY_TIME_TO_LIVE = -1;
38 // Base URI the HTTP server will listen on
39 private static final String DEFAULT_SERVER_URI_PREFIX = "http://";
40 /** The server listens on localhost by default. */
41 public static final String DEFAULT_SERVER_URI_ROOT = "localhost";
42 private static final String DEFAULT_REST_PATH = "/apexservices/";
43 private static final String DEFAULT_STATIC_PATH = "/";
45 // Constants for port checks
46 private static final int MIN_USER_PORT = 1024;
47 private static final int MAX_USER_PORT = 65535;
50 // Package that will field REST requests
51 private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.apex.client.editor.rest" };
53 // The editor parameters
54 private boolean helpSet = false;
55 private int restPort = DEFAULT_REST_PORT;
56 private long timeToLive = INFINITY_TIME_TO_LIVE;
57 private String listenAddress = DEFAULT_SERVER_URI_ROOT;
64 public String validate() {
65 String validationMessage = "";
66 validationMessage += validatePort();
67 validationMessage += validateTimeToLive();
68 validationMessage += validateUrl();
70 return validationMessage;
76 * @return the base URI
78 public URI getBaseURI() {
79 return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + DEFAULT_REST_PATH);
83 * Gets the REST packages.
85 * @return the REST packages
87 public String[] getRESTPackages() {
88 return DEFAULT_PACKAGES;
92 * Gets the static path.
94 * @return the static path
96 public String getStaticPath() {
97 return DEFAULT_STATIC_PATH;
103 * @return a warning string, or an empty string
105 private String validatePort() {
106 if (restPort < MIN_USER_PORT || restPort > MAX_USER_PORT) {
107 return "port must be between " + MIN_USER_PORT + " and " + MAX_USER_PORT + "\n";
116 * @return a warning string, or an empty string
118 private String validateUrl() {
120 new URI(getBaseURI().toString()).parseServerAuthority();
122 } catch (final URISyntaxException e) {
123 return "listen address is not valid. " + e.getMessage() + "\n";
128 * Validate time to live.
132 private String validateTimeToLive() {
133 if (timeToLive < -1) {
134 return "time to live must be greater than -1 (set to -1 to wait forever)\n";
141 * Checks if is help set.
143 * @return true, if checks if is help set
145 public boolean isHelpSet() {
152 * @param help the help
154 public void setHelp(final boolean help) {
159 * Gets the REST port.
161 * @return the REST port
163 public int getRESTPort() {
168 * Sets the REST port.
170 * @param incomingRestPort the REST port
172 public void setRESTPort(final int incomingRestPort) {
173 this.restPort = incomingRestPort;
177 * Gets the time to live.
179 * @return the time to live
181 public long getTimeToLive() {
186 * Sets the time to live.
188 * @param timeToLive the time to live
190 public void setTimeToLive(final long timeToLive) {
191 this.timeToLive = timeToLive;
197 * @see java.lang.Object#toString()
200 public String toString() {
201 final StringBuilder ret = new StringBuilder();
202 ret.append(this.getClass().getSimpleName()).append(": URI=").append(this.getBaseURI()).append(", TTL=")
203 .append(this.getTimeToLive()).append("sec");
204 return ret.toString();
208 * Gets the base address to listen on.
210 * @return the listenAddress
212 public String getListenAddress() {
213 return listenAddress;
217 * Sets the base address to listen on.
219 * @param listenAddress the new listenAddress
221 public void setListenAddress(final String listenAddress) {
222 this.listenAddress = listenAddress;