55d10c1a15b7ee3751ddea201ec94ccf8cff1b8b
[policy/apex-pdp.git] /
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 /**
27  * This class reads and handles command line parameters to the Apex CLI editor.
28  *
29  * @author Liam Fallon (liam.fallon@ericsson.com)
30  */
31 public class ApexEditorParameters {
32     /** The default port for connecting to the Web editor on. */
33     public static final int DEFAULT_REST_PORT = 18989;
34
35     /** The connection is held up until killed on demand. */
36     public static final int INFINITY_TIME_TO_LIVE = -1;
37
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 = "/";
44
45     // Constants for port checks
46     private static final int MIN_USER_PORT = 1024;
47     private static final int MAX_USER_PORT = 65535;
48
49
50     // Package that will field REST requests
51     private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.apex.client.editor.rest" };
52
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;
58
59     /**
60      * Validate.
61      *
62      * @return the string
63      */
64     public String validate() {
65         String validationMessage = "";
66         validationMessage += validatePort();
67         validationMessage += validateTimeToLive();
68         validationMessage += validateUrl();
69
70         return validationMessage;
71     }
72
73     /**
74      * Gets the base URI.
75      *
76      * @return the base URI
77      */
78     public URI getBaseUri() {
79         return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + DEFAULT_REST_PATH);
80     }
81
82     /**
83      * Gets the REST packages.
84      *
85      * @return the REST packages
86      */
87     public String[] getRestPackages() {
88         return DEFAULT_PACKAGES;
89     }
90
91     /**
92      * Gets the static path.
93      *
94      * @return the static path
95      */
96     public String getStaticPath() {
97         return DEFAULT_STATIC_PATH;
98     }
99
100     /**
101      * Validate port.
102      *
103      * @return a warning string, or an empty string
104      */
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";
108         } else {
109             return "";
110         }
111     }
112
113     /**
114      * Validate URL.
115      *
116      * @return a warning string, or an empty string
117      */
118     private String validateUrl() {
119         try {
120             new URI(getBaseUri().toString()).parseServerAuthority();
121             return "";
122         } catch (final URISyntaxException e) {
123             return "listen address is not valid. " + e.getMessage() + "\n";
124         }
125     }
126
127     /**
128      * Validate time to live.
129      *
130      * @return the string
131      */
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";
135         } else {
136             return "";
137         }
138     }
139
140     /**
141      * Checks if is help set.
142      *
143      * @return true, if checks if is help set
144      */
145     public boolean isHelpSet() {
146         return helpSet;
147     }
148
149     /**
150      * Sets the help.
151      *
152      * @param help the help
153      */
154     public void setHelp(final boolean help) {
155         this.helpSet = help;
156     }
157
158     /**
159      * Gets the REST port.
160      *
161      * @return the REST port
162      */
163     public int getRestPort() {
164         return restPort;
165     }
166
167     /**
168      * Sets the REST port.
169      *
170      * @param incomingRestPort the REST port
171      */
172     public void setRestPort(final int incomingRestPort) {
173         this.restPort = incomingRestPort;
174     }
175
176     /**
177      * Gets the time to live.
178      *
179      * @return the time to live
180      */
181     public long getTimeToLive() {
182         return timeToLive;
183     }
184
185     /**
186      * Sets the time to live.
187      *
188      * @param timeToLive the time to live
189      */
190     public void setTimeToLive(final long timeToLive) {
191         this.timeToLive = timeToLive;
192     }
193
194     /*
195      * (non-Javadoc)
196      *
197      * @see java.lang.Object#toString()
198      */
199     @Override
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();
205     }
206
207     /**
208      * Gets the base address to listen on.
209      *
210      * @return the listenAddress
211      */
212     public String getListenAddress() {
213         return listenAddress;
214     }
215
216     /**
217      * Sets the base address to listen on.
218      *
219      * @param listenAddress the new listenAddress
220      */
221     public void setListenAddress(final String listenAddress) {
222         this.listenAddress = listenAddress;
223     }
224 }