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