c4c36ff81368526329723cc4d3dc164d96f647dc
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.gui.editors.apex.rest;
23
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import org.slf4j.ext.XLogger;
27 import org.slf4j.ext.XLoggerFactory;
28
29 /**
30  * This class reads and handles command line parameters to the Apex CLI editor.
31  *
32  * @author Liam Fallon (liam.fallon@ericsson.com)
33  */
34 public class ApexEditorParameters {
35     // Logger for this class
36     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
37
38     /** The default port for connecting to the Web editor on. */
39     public static final int DEFAULT_REST_PORT = 18989;
40
41     /** The connection is held up until killed on demand. */
42     public static final int INFINITY_TIME_TO_LIVE = -1;
43
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 = "/";
50
51     // Constants for port checks
52     private static final int MIN_USER_PORT = 1024;
53     private static final int MAX_USER_PORT = 65535;
54
55     // Package that will field REST requests
56     private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.gui.editors.apex.rest" };
57
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;
63
64     /**
65      * Validate.
66      *
67      * @return the string
68      */
69     public String validate() {
70         String validationMessage = "";
71         validationMessage += validatePort();
72         validationMessage += validateTimeToLive();
73         validationMessage += validateUrl();
74
75         return validationMessage;
76     }
77
78     /**
79      * Gets the base URI.
80      *
81      * @return the base URI
82      */
83     public URI getBaseUri() {
84         return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + DEFAULT_REST_PATH);
85     }
86
87     /**
88      * Gets the REST packages.
89      *
90      * @return the REST packages
91      */
92     public String[] getRestPackages() {
93         return DEFAULT_PACKAGES;
94     }
95
96     /**
97      * Gets the static path.
98      *
99      * @return the static path
100      */
101     public String getStaticPath() {
102         return DEFAULT_STATIC_PATH;
103     }
104
105     /**
106      * Validate port.
107      *
108      * @return a warning string, or an empty string
109      */
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";
113         } else {
114             return "";
115         }
116     }
117
118     /**
119      * Validate URL.
120      *
121      * @return a warning string, or an empty string
122      */
123     private String validateUrl() {
124         try {
125             new URI(getBaseUri().toString()).parseServerAuthority();
126             return "";
127         } catch (final URISyntaxException e) {
128             String message = "listen address is not valid. " + e.getMessage() + "\n";
129             LOGGER.warn(message, e);
130             return message;
131         }
132     }
133
134     /**
135      * Validate time to live.
136      *
137      * @return the string
138      */
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";
142         } else {
143             return "";
144         }
145     }
146
147     /**
148      * Checks if is help set.
149      *
150      * @return true, if checks if is help set
151      */
152     public boolean isHelpSet() {
153         return helpSet;
154     }
155
156     /**
157      * Sets the help.
158      *
159      * @param help the help
160      */
161     public void setHelp(final boolean help) {
162         this.helpSet = help;
163     }
164
165     /**
166      * Gets the REST port.
167      *
168      * @return the REST port
169      */
170     public int getRestPort() {
171         return restPort;
172     }
173
174     /**
175      * Sets the REST port.
176      *
177      * @param incomingRestPort the REST port
178      */
179     public void setRestPort(final int incomingRestPort) {
180         this.restPort = incomingRestPort;
181     }
182
183     /**
184      * Gets the time to live.
185      *
186      * @return the time to live
187      */
188     public long getTimeToLive() {
189         return timeToLive;
190     }
191
192     /**
193      * Sets the time to live.
194      *
195      * @param timeToLive the time to live
196      */
197     public void setTimeToLive(final long timeToLive) {
198         this.timeToLive = timeToLive;
199     }
200
201     /**
202      * {@inheritDoc}.
203      */
204     @Override
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();
210     }
211
212     /**
213      * Gets the base address to listen on.
214      *
215      * @return the listenAddress
216      */
217     public String getListenAddress() {
218         return listenAddress;
219     }
220
221     /**
222      * Sets the base address to listen on.
223      *
224      * @param listenAddress the new listenAddress
225      */
226     public void setListenAddress(final String listenAddress) {
227         this.listenAddress = listenAddress;
228     }
229 }