3f988b59a5b263aa81f5e1aca60cebea1d2ef07f
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2021 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.gui.editors.apex.rest;
24
25 import java.net.MalformedURLException;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.net.URL;
29 import lombok.Data;
30 import org.apache.commons.lang3.StringUtils;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
33
34 /**
35  * This class reads and handles command line parameters to the Apex CLI editor.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 @Data
40 public class ApexEditorParameters {
41     // Logger for this class
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
43
44     /** The default port for connecting to the Web editor on. */
45     public static final int DEFAULT_REST_PORT = 18989;
46
47     /** The connection is held up until killed on demand. */
48     public static final int INFINITY_TIME_TO_LIVE = -1;
49
50     // Base URI the HTTP server will listen on
51     private static final String DEFAULT_SERVER_URI_PREFIX = "http://";
52     /** The server listens on localhost by default. */
53     public static final String DEFAULT_SERVER_URI_ROOT = "localhost";
54     private static final String DEFAULT_REST_PATH = "apexservices/";
55     private static final String DEFAULT_STATIC_PATH = "/";
56
57     // Constants for port checks
58     private static final int MIN_USER_PORT = 1024;
59     private static final int MAX_USER_PORT = 65535;
60
61     // Package that will field REST requests
62     private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.gui.editors.apex.rest" };
63
64     // The editor parameters
65     private boolean help = false;
66     private int restPort = DEFAULT_REST_PORT;
67     private long timeToLive = INFINITY_TIME_TO_LIVE;
68     private String listenAddress = DEFAULT_SERVER_URI_ROOT;
69     private String uploadUrl = null;
70     private String uploadUserid = null;
71
72     /**
73      * Validate.
74      *
75      * @return the string
76      */
77     public String validate() {
78         return validatePort() + validateTimeToLive() + validateUrl() + validateUploadUrl() + validateUploadUserid();
79     }
80
81     /**
82      * Gets the base URI.
83      *
84      * @return the base URI
85      */
86     public URI getBaseUri() {
87         return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + "/" + DEFAULT_REST_PATH);
88     }
89
90     /**
91      * Gets the REST packages.
92      *
93      * @return the REST packages
94      */
95     public String[] getRestPackages() {
96         return DEFAULT_PACKAGES;
97     }
98
99     /**
100      * Gets the static path.
101      *
102      * @return the static path
103      */
104     public String getStaticPath() {
105         return DEFAULT_STATIC_PATH;
106     }
107
108     /**
109      * Validate port.
110      *
111      * @return a warning string, or an empty string
112      */
113     private String validatePort() {
114         if (restPort < MIN_USER_PORT || restPort > MAX_USER_PORT) {
115             return "port must be between " + MIN_USER_PORT + " and " + MAX_USER_PORT + "\n";
116         } else {
117             return "";
118         }
119     }
120
121     /**
122      * Validate URL.
123      *
124      * @return a warning string, or an empty string
125      */
126     private String validateUrl() {
127         try {
128             new URI(getBaseUri().toString()).parseServerAuthority();
129             return "";
130         } catch (final URISyntaxException e) {
131             String message = "listen address is not valid. " + e.getMessage() + "\n";
132             LOGGER.warn(message, e);
133             return message;
134         }
135     }
136
137     /**
138      * Validate time to live.
139      *
140      * @return the string
141      */
142     private String validateTimeToLive() {
143         if (timeToLive < -1) {
144             return "time to live must be greater than -1 (set to -1 to wait forever)\n";
145         } else {
146             return "";
147         }
148     }
149
150     private String validateUploadUrl() {
151         if (!StringUtils.isBlank(uploadUrl)) {
152             try {
153                 new URL(uploadUrl);
154             } catch (MalformedURLException murle) {
155                 return "Specified upload-url parameter is an invalid URL" + murle.getMessage() + "\n";
156             }
157         }
158         return "";
159     }
160
161     private String validateUploadUserid() {
162         if (!StringUtils.isEmpty(uploadUrl) && StringUtils.isEmpty(uploadUserid)) {
163             return "upload-userid parameter must be specified if the upload-url parameter is specified\n";
164         } else {
165             return "";
166         }
167     }
168 }