override cert stores
[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
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
56     // Package that will field REST requests
57     private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.apex.client.editor.rest" };
58
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;
64
65     /**
66      * Validate.
67      *
68      * @return the string
69      */
70     public String validate() {
71         String validationMessage = "";
72         validationMessage += validatePort();
73         validationMessage += validateTimeToLive();
74         validationMessage += validateUrl();
75
76         return validationMessage;
77     }
78
79     /**
80      * Gets the base URI.
81      *
82      * @return the base URI
83      */
84     public URI getBaseUri() {
85         return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + DEFAULT_REST_PATH);
86     }
87
88     /**
89      * Gets the REST packages.
90      *
91      * @return the REST packages
92      */
93     public String[] getRestPackages() {
94         return DEFAULT_PACKAGES;
95     }
96
97     /**
98      * Gets the static path.
99      *
100      * @return the static path
101      */
102     public String getStaticPath() {
103         return DEFAULT_STATIC_PATH;
104     }
105
106     /**
107      * Validate port.
108      *
109      * @return a warning string, or an empty string
110      */
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";
114         } else {
115             return "";
116         }
117     }
118
119     /**
120      * Validate URL.
121      *
122      * @return a warning string, or an empty string
123      */
124     private String validateUrl() {
125         try {
126             new URI(getBaseUri().toString()).parseServerAuthority();
127             return "";
128         } catch (final URISyntaxException e) {
129             String message = "listen address is not valid. " + e.getMessage() + "\n";
130             LOGGER.warn(message, e);
131             return message;
132         }
133     }
134
135     /**
136      * Validate time to live.
137      *
138      * @return the string
139      */
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";
143         } else {
144             return "";
145         }
146     }
147
148     /**
149      * Checks if is help set.
150      *
151      * @return true, if checks if is help set
152      */
153     public boolean isHelpSet() {
154         return helpSet;
155     }
156
157     /**
158      * Sets the help.
159      *
160      * @param help the help
161      */
162     public void setHelp(final boolean help) {
163         this.helpSet = help;
164     }
165
166     /**
167      * Gets the REST port.
168      *
169      * @return the REST port
170      */
171     public int getRestPort() {
172         return restPort;
173     }
174
175     /**
176      * Sets the REST port.
177      *
178      * @param incomingRestPort the REST port
179      */
180     public void setRestPort(final int incomingRestPort) {
181         this.restPort = incomingRestPort;
182     }
183
184     /**
185      * Gets the time to live.
186      *
187      * @return the time to live
188      */
189     public long getTimeToLive() {
190         return timeToLive;
191     }
192
193     /**
194      * Sets the time to live.
195      *
196      * @param timeToLive the time to live
197      */
198     public void setTimeToLive(final long timeToLive) {
199         this.timeToLive = timeToLive;
200     }
201
202     /**
203      * {@inheritDoc}.
204      */
205     @Override
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();
211     }
212
213     /**
214      * Gets the base address to listen on.
215      *
216      * @return the listenAddress
217      */
218     public String getListenAddress() {
219         return listenAddress;
220     }
221
222     /**
223      * Sets the base address to listen on.
224      *
225      * @param listenAddress the new listenAddress
226      */
227     public void setListenAddress(final String listenAddress) {
228         this.listenAddress = listenAddress;
229     }
230 }