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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.tools.simple.wsclient;
23 import java.io.IOException;
24 import java.io.PrintStream;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.net.URISyntaxException;
28 import java.nio.channels.NotYetConnectedException;
30 import org.apache.commons.cli.CommandLine;
31 import org.apache.commons.cli.HelpFormatter;
32 import org.apache.commons.lang3.Validate;
33 import org.onap.policy.apex.tools.common.CliOptions;
34 import org.onap.policy.apex.tools.common.CliParser;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Simple console application with main method.
41 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
43 public final class WsClientMain {
44 // Get a reference to the logger
45 private static final Logger LOGGER = LoggerFactory.getLogger(WsClientMain.class);
48 private static final String APP_NAME = "ws-client";
49 private static final String APP_DESCRIPTION = "takes events from stdin and sends via WS to APEX"
50 + " and/or receives events from APEX via WS and prints them to standard out";
55 * @param args the command line arguments
56 * @param outStream stream for output
58 WsClientMain(final String[] args, final PrintStream outStream) {
59 boolean console = false;
61 final CliParser cli = new CliParser();
62 cli.addOption(CliOptions.HELP);
63 cli.addOption(CliOptions.VERSION);
64 cli.addOption(CliOptions.CONSOLE);
65 cli.addOption(CliOptions.SERVER);
66 cli.addOption(CliOptions.PORT);
68 final CommandLine cmd = cli.parseCli(args);
70 // help is an exit option, print usage and exit
71 if (cmd == null || cmd.hasOption('h') || cmd.hasOption("help")) {
72 outStream.println(getHelpString(cli));
77 if (cmd.hasOption('c') || cmd.hasOption("console")) {
81 // version is an exit option, print version and exit
82 if (cmd.hasOption('v') || cmd.hasOption("version")) {
83 outStream.println(APP_NAME + " " + cli.getAppVersion());
88 runConsoleOrEcho(console, cmd, outStream);
92 * Run the console or echo.
94 * @param console if true, run the console otherwise run echo
95 * @param cmd the command line to run
96 * @param outStream stream for output
98 private static void runConsoleOrEcho(final boolean console, final CommandLine cmd, final PrintStream outStream) {
99 String server = cmd.getOptionValue('s');
100 if (server == null) {
101 server = cmd.getOptionValue("server");
103 if (server == null) {
104 server = "localhost";
107 String port = cmd.getOptionValue('p');
109 port = cmd.getOptionValue("port");
116 runConsole(server, port, outStream);
118 runEcho(server, port, outStream);
123 * Runs the simple echo client.
125 * @param server the server, must not be blank
126 * @param port the port, must not be blank
127 * @param outStream stream for output
129 public static void runEcho(final String server, final String port, final PrintStream outStream) {
130 Validate.notBlank(server);
131 Validate.notBlank(port);
134 outStream.println(APP_NAME + ": starting simple event echo");
135 outStream.println(" --> server: " + server);
136 outStream.println(" --> port: " + port);
138 outStream.println("Once started, the application will simply print out all received events to standard out.");
139 outStream.println("Each received event will be prefixed by '---' and suffixed by '===='");
144 final SimpleEcho simpleEcho = new SimpleEcho(server, port, APP_NAME, outStream, outStream);
145 simpleEcho.connect();
146 } catch (final URISyntaxException uex) {
147 String message = APP_NAME + ": URI exception, could not create URI from server and port settings";
148 outStream.println(message);
149 LOGGER.warn(message, uex);
150 } catch (final NullPointerException nex) {
151 String message = APP_NAME + ": null pointer, server or port were null";
152 outStream.println(message);
153 LOGGER.warn(message, nex);
154 } catch (final IllegalArgumentException iex) {
155 String message = APP_NAME + ": illegal argument, server or port were blank";
156 outStream.println(message);
157 LOGGER.warn(message, iex);
162 * Runs the simple console.
164 * @param server the server, must not be blank
165 * @param port the port, must not be blank
166 * @param outStream stream for output
168 public static void runConsole(final String server, final String port, final PrintStream outStream) {
169 Validate.notBlank(server);
170 Validate.notBlank(port);
171 Validate.notBlank(APP_NAME);
174 outStream.println(APP_NAME + ": starting simple event console");
175 outStream.println(" --> server: " + server);
176 outStream.println(" --> port: " + port);
178 outStream.println(" - terminate the application typing 'exit<enter>' or using 'CTRL+C'");
179 outStream.println(" - events are created by a non-blank starting line and terminated by a blank line");
184 final SimpleConsole simpleConsole = new SimpleConsole(server, port, APP_NAME, outStream, outStream);
185 simpleConsole.runClient();
186 } catch (final URISyntaxException uex) {
187 String message = APP_NAME + ": URI exception, could not create URI from server and port settings";
188 outStream.println(message);
189 LOGGER.warn(message, uex);
190 } catch (final NullPointerException nex) {
191 String message = APP_NAME + ": null pointer, server or port were null";
192 outStream.println(message);
193 LOGGER.warn(message, nex);
194 } catch (final IllegalArgumentException iex) {
195 String message = APP_NAME + ": illegal argument, server or port were blank";
196 outStream.println(message);
197 LOGGER.warn(message, iex);
198 } catch (final NotYetConnectedException nex) {
199 String message = APP_NAME + ": not yet connected, connection to server took too long";
200 outStream.println(message);
201 LOGGER.warn(message, nex);
202 } catch (final IOException ioe) {
203 String message = APP_NAME + ": IO exception, something went wrong on the standard input";
204 outStream.println(message);
205 LOGGER.warn(message, ioe);
210 * Get the help string for the application.
212 * @param cli the command line options
213 * @return the help string
215 private String getHelpString(final CliParser cli) {
216 HelpFormatter formatter = new HelpFormatter();
218 final StringWriter helpStringWriter = new StringWriter();
219 final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
221 formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
223 return helpStringWriter.toString();
227 * The main method for the WS applications.
229 * @param args command line argument s
231 public static void main(final String[] args) {
232 new WsClientMain(args, System.out);