summaryrefslogtreecommitdiffstats
path: root/tools/simple-wsclient
diff options
context:
space:
mode:
Diffstat (limited to 'tools/simple-wsclient')
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java84
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java41
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java49
3 files changed, 106 insertions, 68 deletions
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java
index 7c7d7e9c6..e0b819f60 100644
--- a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/Application.java
@@ -21,6 +21,7 @@
package org.onap.policy.apex.tools.simple.wsclient;
import java.io.IOException;
+import java.io.PrintStream;
import java.net.URISyntaxException;
import java.nio.channels.NotYetConnectedException;
@@ -41,10 +42,16 @@ public final class Application {
// Get a reference to the logger
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
+ // Input and output streams
+ private static final PrintStream OUT_STREAM = System.out;
+ private static final PrintStream ERR_STREAM = System.err;
+
/**
* Private constructor prevents subclassing.
*/
- private Application() {}
+ private Application() {
+ // Prevent subclassing
+ }
/**
* The main method for the WS applications.
@@ -74,19 +81,29 @@ public final class Application {
// help is an exit option, print usage and exit
if (cmd.hasOption('h') || cmd.hasOption("help")) {
final HelpFormatter formatter = new HelpFormatter();
- System.out.println(appName + " v" + cli.getAppVersion() + " - " + appDescr);
+ OUT_STREAM.println(appName + " v" + cli.getAppVersion() + " - " + appDescr);
formatter.printHelp(appName, cli.getOptions());
- System.out.println();
+ OUT_STREAM.println();
return;
}
// version is an exit option, print version and exit
if (cmd.hasOption('v') || cmd.hasOption("version")) {
- System.out.println(appName + " " + cli.getAppVersion());
- System.out.println();
+ OUT_STREAM.println(appName + " " + cli.getAppVersion());
+ OUT_STREAM.println();
return;
}
+ runConsoleOrEcho(appName, console, cmd);
+ }
+
+ /**
+ * Run the console or echo.
+ * @param appName the application name
+ * @param console if true, run the console otherwise run echo
+ * @param cmd the command line to run
+ */
+ private static void runConsoleOrEcho(String appName, boolean console, final CommandLine cmd) {
String server = cmd.getOptionValue('s');
if (server == null) {
server = cmd.getOptionValue("server");
@@ -108,7 +125,6 @@ public final class Application {
} else {
runEcho(server, port, appName);
}
-
}
/**
@@ -123,30 +139,30 @@ public final class Application {
Validate.notBlank(port);
Validate.notBlank(appName);
- System.out.println();
- System.out.println(appName + ": starting simple event echo");
- System.out.println(" --> server: " + server);
- System.out.println(" --> port: " + port);
- System.out.println();
- System.out.println("Once started, the application will simply print out all received events to standard out.");
- System.out.println("Each received event will be prefixed by '---' and suffixed by '===='");
- System.out.println();
- System.out.println();
+ OUT_STREAM.println();
+ OUT_STREAM.println(appName + ": starting simple event echo");
+ OUT_STREAM.println(" --> server: " + server);
+ OUT_STREAM.println(" --> port: " + port);
+ OUT_STREAM.println();
+ OUT_STREAM.println("Once started, the application will simply print out all received events to standard out.");
+ OUT_STREAM.println("Each received event will be prefixed by '---' and suffixed by '===='");
+ OUT_STREAM.println();
+ OUT_STREAM.println();
try {
- final SimpleEcho simpleEcho = new SimpleEcho(server, port, appName);
+ final SimpleEcho simpleEcho = new SimpleEcho(server, port, appName, OUT_STREAM, ERR_STREAM);
simpleEcho.connect();
} catch (final URISyntaxException uex) {
String message = appName + ": URI exception, could not create URI from server and port settings";
- System.err.println(message);
+ ERR_STREAM.println(message);
LOGGER.warn(message, uex);
} catch (final NullPointerException nex) {
String message = appName + ": null pointer, server or port were null";
- System.err.println(message);
+ ERR_STREAM.println(message);
LOGGER.warn(message, nex);
} catch (final IllegalArgumentException iex) {
String message = appName + ": illegal argument, server or port were blank";
- System.err.println(message);
+ ERR_STREAM.println(message);
LOGGER.warn(message, iex);
}
}
@@ -163,38 +179,38 @@ public final class Application {
Validate.notBlank(port);
Validate.notBlank(appName);
- System.out.println();
- System.out.println(appName + ": starting simple event console");
- System.out.println(" --> server: " + server);
- System.out.println(" --> port: " + port);
- System.out.println();
- System.out.println(" - terminate the application typing 'exit<enter>' or using 'CTRL+C'");
- System.out.println(" - events are created by a non-blank starting line and terminated by a blank line");
- System.out.println();
- System.out.println();
+ OUT_STREAM.println();
+ OUT_STREAM.println(appName + ": starting simple event console");
+ OUT_STREAM.println(" --> server: " + server);
+ OUT_STREAM.println(" --> port: " + port);
+ OUT_STREAM.println();
+ OUT_STREAM.println(" - terminate the application typing 'exit<enter>' or using 'CTRL+C'");
+ OUT_STREAM.println(" - events are created by a non-blank starting line and terminated by a blank line");
+ OUT_STREAM.println();
+ OUT_STREAM.println();
try {
- final SimpleConsole simpleConsole = new SimpleConsole(server, port, appName);
+ final SimpleConsole simpleConsole = new SimpleConsole(server, port, appName, OUT_STREAM, ERR_STREAM);
simpleConsole.runClient();
} catch (final URISyntaxException uex) {
String message = appName + ": URI exception, could not create URI from server and port settings";
- System.err.println(message);
+ ERR_STREAM.println(message);
LOGGER.warn(message, uex);
} catch (final NullPointerException nex) {
String message = appName + ": null pointer, server or port were null";
- System.err.println(message);
+ ERR_STREAM.println(message);
LOGGER.warn(message, nex);
} catch (final IllegalArgumentException iex) {
String message = appName + ": illegal argument, server or port were blank";
- System.err.println(message);
+ ERR_STREAM.println(message);
LOGGER.warn(message, iex);
} catch (final NotYetConnectedException nex) {
String message = appName + ": not yet connected, connection to server took too long";
- System.err.println(message);
+ ERR_STREAM.println(message);
LOGGER.warn(message, nex);
} catch (final IOException ioe) {
String message = appName + ": IO exception, something went wrong on the standard input";
- System.err.println(message);
+ ERR_STREAM.println(message);
LOGGER.warn(message, ioe);
}
}
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java
index bb38a37a0..28c494207 100644
--- a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java
@@ -23,6 +23,7 @@ package org.onap.policy.apex.tools.simple.wsclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.NotYetConnectedException;
@@ -41,6 +42,10 @@ public class SimpleConsole extends WebSocketClient {
/** Application name, used as prompt. */
private final String appName;
+
+ // Output and error streams
+ private PrintStream outStream;
+ private PrintStream errStream;
/**
* Creates a new simple echo object.
@@ -48,51 +53,57 @@ public class SimpleConsole extends WebSocketClient {
* @param server the name of the server as either IP address or fully qualified host name, must not be blank
* @param port the port to be used, must not be blank
* @param appName the application name, used as prompt, must not be blank
+ * @param outStream the stream for message output
+ * @param errStream the stream for error messages
* @throws URISyntaxException is URI could not be created from server/port settings
* @throws RuntimeException if server or port where blank
*/
- public SimpleConsole(final String server, final String port, final String appName) throws URISyntaxException {
+ public SimpleConsole(final String server, final String port, final String appName, PrintStream outStream,
+ PrintStream errStream) throws URISyntaxException {
super(new URI("ws://" + server + ":" + port));
Validate.notBlank(appName, "SimpleConsole: given application name was blank");
+
this.appName = appName;
+ this.outStream = outStream;
+ this.errStream = errStream;
}
@Override
public void onClose(final int code, final String reason, final boolean remote) {
- System.out.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
- System.out.print(" ==-->> ");
+ outStream.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
+ outStream.print(" ==-->> ");
switch (code) {
case CloseFrame.NORMAL:
- System.out.println("normal");
+ outStream.println("normal");
break;
case CloseFrame.GOING_AWAY:
- System.out.println("APEX going away");
+ outStream.println("APEX going away");
break;
case CloseFrame.PROTOCOL_ERROR:
- System.out.println("some protocol error");
+ outStream.println("some protocol error");
break;
case CloseFrame.REFUSE:
- System.out.println("received unacceptable type of data");
+ outStream.println("received unacceptable type of data");
break;
case CloseFrame.NO_UTF8:
- System.out.println("expected UTF-8, found something else");
+ outStream.println("expected UTF-8, found something else");
break;
case CloseFrame.TOOBIG:
- System.out.println("message too big");
+ outStream.println("message too big");
break;
case CloseFrame.UNEXPECTED_CONDITION:
- System.out.println("unexpected server condition");
+ outStream.println("unexpected server condition");
break;
default:
- System.out.println("unkown close frame code");
+ outStream.println("unkown close frame code");
break;
}
- System.out.print(" ==-->> " + reason);
+ outStream.print(" ==-->> " + reason);
}
@Override
public void onError(final Exception ex) {
- System.err.println(this.appName + ": " + ex.getMessage());
+ errStream.println(this.appName + ": " + ex.getMessage());
}
@Override
@@ -102,7 +113,7 @@ public class SimpleConsole extends WebSocketClient {
@Override
public void onOpen(final ServerHandshake handshakedata) {
- System.out.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
+ outStream.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
}
/**
@@ -125,7 +136,7 @@ public class SimpleConsole extends WebSocketClient {
String event = "";
String line;
while ((line = in.readLine()) != null) {
- if (line.equals("exit")) {
+ if ("exit".equals(line)) {
break;
}
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java
index 4b67cb862..659dd77eb 100644
--- a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleEcho.java
@@ -20,6 +20,7 @@
package org.onap.policy.apex.tools.simple.wsclient;
+import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
@@ -38,71 +39,81 @@ public class SimpleEcho extends WebSocketClient {
/** Application name, used as prompt. */
private final String appName;
+ // Output and error streams
+ private PrintStream outStream;
+ private PrintStream errStream;
+
/**
* Creates a new simple echo object.
*
* @param server the name of the server as either IP address or fully qualified host name, must not be blank
* @param port the port to be used, must not be blank
* @param appName the application name, used as prompt, must not be blank
+ * @param outStream the stream for message output
+ * @param errStream the stream for error messages
* @throws URISyntaxException is URI could not be created from server/port settings
* @throws RuntimeException if server or port where blank
*/
- public SimpleEcho(final String server, final String port, final String appName) throws URISyntaxException {
+ public SimpleEcho(final String server, final String port, final String appName, PrintStream outStream,
+ PrintStream errStream) throws URISyntaxException {
super(new URI("ws://" + server + ":" + port));
Validate.notBlank(appName, "SimpleEcho: given application name was blank");
+
this.appName = appName;
+ this.outStream = outStream;
+ this.errStream = errStream;
}
@Override
public void onClose(final int code, final String reason, final boolean remote) {
- System.out.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
- System.out.print(" ==-->> ");
+ outStream.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
+ outStream.print(" ==-->> ");
switch (code) {
case CloseFrame.NORMAL:
- System.out.println("normal");
+ outStream.println("normal");
break;
case CloseFrame.GOING_AWAY:
- System.out.println("APEX going away");
+ outStream.println("APEX going away");
break;
case CloseFrame.PROTOCOL_ERROR:
- System.out.println("some protocol error");
+ outStream.println("some protocol error");
break;
case CloseFrame.REFUSE:
- System.out.println("received unacceptable type of data");
+ outStream.println("received unacceptable type of data");
break;
case CloseFrame.NO_UTF8:
- System.out.println("expected UTF-8, found something else");
+ outStream.println("expected UTF-8, found something else");
break;
case CloseFrame.TOOBIG:
- System.out.println("message too big");
+ outStream.println("message too big");
break;
case CloseFrame.UNEXPECTED_CONDITION:
- System.out.println("unexpected server condition");
+ outStream.println("unexpected server condition");
break;
default:
- System.out.println("unkown close frame code");
+ outStream.println("unkown close frame code");
break;
}
- System.out.print(" ==-->> " + reason);
+ outStream.print(" ==-->> " + reason);
}
@Override
public void onError(final Exception ex) {
- System.err.println(this.appName + ": " + ex.getMessage());
+ errStream.println(this.appName + ": " + ex.getMessage());
}
@Override
public void onMessage(final String message) {
- System.out.println(this.appName + ": received");
- System.out.println("---------------------------------");
- System.out.println(message);
- System.out.println("=================================");
- System.out.println();
+ outStream.println(this.appName + ": received");
+ outStream.println("---------------------------------");
+ outStream.println(message);
+ outStream.println("=================================");
+ outStream.println();
}
@Override
public void onOpen(final ServerHandshake handshakedata) {
- System.out.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
+ outStream.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
}
}