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
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 lombok.Generated;
31 import org.apache.commons.lang3.StringUtils;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * This class reads and handles command line parameters to the Apex CLI editor.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class ApexEditorParameters {
43 // Logger for this class
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexEditorParameters.class);
46 /** The default port for connecting to the Web editor on. */
47 public static final int DEFAULT_REST_PORT = 18989;
49 /** The connection is held up until killed on demand. */
50 public static final int INFINITY_TIME_TO_LIVE = -1;
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 = "/";
59 // Constants for port checks
60 private static final int MIN_USER_PORT = 1024;
61 private static final int MAX_USER_PORT = 65535;
63 // Package that will field REST requests
64 private static final String[] DEFAULT_PACKAGES = new String[] { "org.onap.policy.gui.editors.apex.rest" };
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;
79 public String validate() {
80 return validatePort() + validateTimeToLive() + validateUrl() + validateUploadUrl() + validateUploadUserid();
86 * @return the base URI
88 public URI getBaseUri() {
89 return URI.create(DEFAULT_SERVER_URI_PREFIX + listenAddress + ':' + restPort + "/" + DEFAULT_REST_PATH);
93 * Gets the REST packages.
95 * @return the REST packages
97 public String[] getRestPackages() {
98 return DEFAULT_PACKAGES;
102 * Gets the static path.
104 * @return the static path
106 public String getStaticPath() {
107 return DEFAULT_STATIC_PATH;
113 * @return a warning string, or an empty string
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";
126 * @return a warning string, or an empty string
128 private String validateUrl() {
130 new URI(getBaseUri().toString()).parseServerAuthority();
132 } catch (final URISyntaxException e) {
133 String message = "listen address is not valid. " + e.getMessage() + "\n";
134 LOGGER.warn(message, e);
140 * Validate time to live.
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";
152 private String validateUploadUrl() {
153 if (!StringUtils.isBlank(uploadUrl)) {
156 } catch (MalformedURLException murle) {
157 return "Specified upload-url parameter is an invalid URL" + murle.getMessage() + "\n";
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";