Changes for checkstyle 8.32
[policy/apex-pdp.git] / client / client-editor / src / main / java / org / onap / policy / apex / client / editor / rest / ApexEditorParameterParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.editor.rest;
22
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.Arrays;
26 import org.apache.commons.cli.CommandLine;
27 import org.apache.commons.cli.DefaultParser;
28 import org.apache.commons.cli.HelpFormatter;
29 import org.apache.commons.cli.Option;
30 import org.apache.commons.cli.Options;
31 import org.apache.commons.cli.ParseException;
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 public class ApexEditorParameterParser {
39     // Apache Commons CLI options
40     private Options options;
41
42     private static final int COMMAND_HELP_MAX_LINE_WIDTH = 120;
43
44     /**
45      * Construct the options for the CLI editor.
46      */
47     public ApexEditorParameterParser() {
48         options = new Options();
49         options.addOption("h", "help", false, "outputs the usage of this command");
50         options.addOption(
51                 Option.builder("p").longOpt("port").desc("port to use for the Apex RESTful editor REST calls.").hasArg()
52                         .argName("PORT").required(false).type(Number.class).build());
53         options.addOption(Option.builder("t").longOpt("time-to-live")
54                 .desc("the amount of time in seconds that the server will run for before terminating. "
55                         + "Default value is " + ApexEditorParameters.INFINITY_TIME_TO_LIVE + " to run indefinitely.")
56                 .hasArg().argName("TIME_TO_LIVE").required(false).type(Number.class).build());
57         options.addOption(Option.builder("l").longOpt("listen")
58                 .desc("the IP address to listen on.  Default value is " + ApexEditorParameters.DEFAULT_SERVER_URI_ROOT
59                         + " to restrict access to the local machine only.")
60                 .hasArg().argName("ADDRESS").required(false).type(String.class).build());
61     }
62
63     /**
64      * Parse the command line options.
65      *
66      * @param args The arguments
67      * @return the apex editor parameters
68      */
69     public ApexEditorParameters parse(final String[] args) {
70         CommandLine commandLine = null;
71         try {
72             commandLine = new DefaultParser().parse(options, args);
73         } catch (final ParseException e) {
74             throw new ApexEditorParameterException("invalid command line arguments specified : " + e.getMessage());
75         }
76
77         final ApexEditorParameters parameters = new ApexEditorParameters();
78         final String[] remainingArgs = commandLine.getArgs();
79
80         if (commandLine.getArgs().length > 0) {
81             throw new ApexEditorParameterException(
82                     "too many command line arguments specified : " + Arrays.toString(remainingArgs));
83         }
84
85         if (commandLine.hasOption('h')) {
86             parameters.setHelp(true);
87         }
88         try {
89             if (commandLine.hasOption('p')) {
90                 parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue());
91             }
92         } catch (final ParseException e) {
93             throw new ApexEditorParameterException("error parsing argument \"port\" :" + e.getMessage(), e);
94         }
95         try {
96             if (commandLine.hasOption('t')) {
97                 parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue());
98             }
99         } catch (final ParseException e) {
100             throw new ApexEditorParameterException("error parsing argument \"time-to-live\" :" + e.getMessage(), e);
101         }
102         try {
103             if (commandLine.hasOption('l')) {
104                 parameters.setListenAddress(commandLine.getParsedOptionValue("listen").toString());
105             }
106         } catch (final ParseException e) {
107             throw new ApexEditorParameterException("error parsing argument \"listen-address\" :" + e.getMessage(), e);
108         }
109
110         return parameters;
111     }
112
113     /**
114      * Get help information.
115      *
116      * @param mainClassName the main class name
117      * @return the help
118      */
119     public String getHelp(final String mainClassName) {
120         final StringWriter stringWriter = new StringWriter();
121         final PrintWriter stringPrintWriter = new PrintWriter(stringWriter);
122
123         final HelpFormatter helpFormatter = new HelpFormatter();
124         helpFormatter.printHelp(stringPrintWriter, COMMAND_HELP_MAX_LINE_WIDTH, mainClassName + " [options...] ", null,
125                 options, 0, 1, "");
126
127         return stringWriter.toString();
128     }
129 }