58a4b334a7642fbaf2cc92f039cc2116ede0ea5d
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020-2022 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 lombok.Generated;
31 import org.apache.commons.lang3.StringUtils;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 /**
36  * This class reads and handles command line parameters to the Apex CLI editor.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 @Data
41 @Generated
42 public class ApexEditorParameters {
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
45
46     /** The default port for connecting to the Web editor on. */
47     public static final int DEFAULT_REST_PORT = 18989;
48
49     /** The connection is held up until killed on demand. */
50     public static final int INFINITY_TIME_TO_LIVE = -1;
51
52     // Base URI the HTTP server will listen on
53     private static final String DEFAULT_SERVER_URI_PREFIX = "http://";
54     /** The server listens on localhost by default. */
55     public static final String DEFAULT_SERVER_URI_ROOT = "localhost";
56     private static final String DEFAULT_REST_PATH = "apexservices/";
57     private static final String DEFAULT_STATIC_PATH = "/";
58
59     // Constants for port checks
60     private static final int MIN_USER_PORT = 1024;
61     private static final int MAX_USER_PORT = 65535;
62
63     // Package that will field REST requests
64     private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.gui.editors.apex.rest" };
65
66     // The editor parameters
67     private boolean help = false;
68     private int restPort = DEFAULT_REST_PORT;
69     private long timeToLive = INFINITY_TIME_TO_LIVE;
70     private String listenAddress = DEFAULT_SERVER_URI_ROOT;
71     private String uploadUrl = null;
72     private String uploadUserid = null;
73
74     /**
75      * Validate.
76      *
77      * @return the string
78      */
79     public String validate() {
80         return validatePort() + validateTimeToLive() + validateUrl() + validateUploadUrl() + validateUploadUserid();
81     }
82
83     /**
84      * Gets the base URI.
85      *
86      * @return the base URI
87      */
88     public URI getBaseUri() {
89         return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + "/" + DEFAULT_REST_PATH);
90     }
91
92     /**
93      * Gets the REST packages.
94      *
95      * @return the REST packages
96      */
97     public String[] getRestPackages() {
98         return DEFAULT_PACKAGES;
99     }
100
101     /**
102      * Gets the static path.
103      *
104      * @return the static path
105      */
106     public String getStaticPath() {
107         return DEFAULT_STATIC_PATH;
108     }
109
110     /**
111      * Validate port.
112      *
113      * @return a warning string, or an empty string
114      */
115     private String validatePort() {
116         if (restPort < MIN_USER_PORT || restPort > MAX_USER_PORT) {
117             return "port must be between " + MIN_USER_PORT + " and " + MAX_USER_PORT + "\n";
118         } else {
119             return "";
120         }
121     }
122
123     /**
124      * Validate URL.
125      *
126      * @return a warning string, or an empty string
127      */
128     private String validateUrl() {
129         try {
130             new URI(getBaseUri().toString()).parseServerAuthority();
131             return "";
132         } catch (final URISyntaxException e) {
133             String message = "listen address is not valid. " + e.getMessage() + "\n";
134             LOGGER.warn(message, e);
135             return message;
136         }
137     }
138
139     /**
140      * Validate time to live.
141      *
142      * @return the string
143      */
144     private String validateTimeToLive() {
145         if (timeToLive < -1) {
146             return "time to live must be greater than -1 (set to -1 to wait forever)\n";
147         } else {
148             return "";
149         }
150     }
151
152     private String validateUploadUrl() {
153         if (!StringUtils.isBlank(uploadUrl)) {
154             try {
155                 new URL(uploadUrl);
156             } catch (MalformedURLException murle) {
157                 return "Specified upload-url parameter is an invalid URL" + murle.getMessage() + "\n";
158             }
159         }
160         return "";
161     }
162
163     private String validateUploadUserid() {
164         if (!StringUtils.isEmpty(uploadUrl) && StringUtils.isEmpty(uploadUserid)) {
165             return "upload-userid parameter must be specified if the upload-url parameter is specified\n";
166         } else {
167             return "";
168         }
169     }
170 }