Changes for checkstyle 8.32
[policy/apex-pdp.git] / client / client-editor / src / main / java / org / onap / policy / apex / client / editor / rest / ApexEditorParameters.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.editor.rest;
22
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import org.slf4j.ext.XLogger;
26 import org.slf4j.ext.XLoggerFactory;
27
28 /**
29  * This class reads and handles command line parameters to the Apex CLI editor.
30  *
31  * @author Liam Fallon (liam.fallon@ericsson.com)
32  */
33 public class ApexEditorParameters {
34     // Logger for this class
35     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
36
37     /** The default port for connecting to the Web editor on. */
38     public static final int DEFAULT_REST_PORT = 18989;
39
40     /** The connection is held up until killed on demand. */
41     public static final int INFINITY_TIME_TO_LIVE = -1;
42
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 = "/";
49
50     // Constants for port checks
51     private static final int MIN_USER_PORT = 1024;
52     private static final int MAX_USER_PORT = 65535;
53
54
55     // Package that will field REST requests
56     private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.apex.client.editor.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 }