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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.gui.editors.apex.rest;
25 import java.net.MalformedURLException;
27 import java.net.URISyntaxException;
30 import org.apache.commons.lang3.StringUtils;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * This class reads and handles command line parameters to the Apex CLI editor.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class ApexEditorParameters {
41 // Logger for this class
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
44 /** The default port for connecting to the Web editor on. */
45 public static final int DEFAULT_REST_PORT = 18989;
47 /** The connection is held up until killed on demand. */
48 public static final int INFINITY_TIME_TO_LIVE = -1;
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 = "/";
57 // Constants for port checks
58 private static final int MIN_USER_PORT = 1024;
59 private static final int MAX_USER_PORT = 65535;
61 // Package that will field REST requests
62 private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.gui.editors.apex.rest" };
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;
77 public String validate() {
78 return validatePort() + validateTimeToLive() + validateUrl() + validateUploadUrl() + validateUploadUserid();
84 * @return the base URI
86 public URI getBaseUri() {
87 return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + "/" + DEFAULT_REST_PATH);
91 * Gets the REST packages.
93 * @return the REST packages
95 public String[] getRestPackages() {
96 return DEFAULT_PACKAGES;
100 * Gets the static path.
102 * @return the static path
104 public String getStaticPath() {
105 return DEFAULT_STATIC_PATH;
111 * @return a warning string, or an empty string
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";
124 * @return a warning string, or an empty string
126 private String validateUrl() {
128 new URI(getBaseUri().toString()).parseServerAuthority();
130 } catch (final URISyntaxException e) {
131 String message = "listen address is not valid. " + e.getMessage() + "\n";
132 LOGGER.warn(message, e);
138 * Validate time to live.
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";
150 private String validateUploadUrl() {
151 if (!StringUtils.isBlank(uploadUrl)) {
154 } catch (MalformedURLException murle) {
155 return "Specified upload-url parameter is an invalid URL" + murle.getMessage() + "\n";
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";