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.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.PrintStream;
28 import java.net.URISyntaxException;
29 import java.nio.channels.NotYetConnectedException;
31 import org.apache.commons.lang3.Validate;
32 import org.java_websocket.client.WebSocketClient;
33 import org.java_websocket.framing.CloseFrame;
34 import org.java_websocket.handshake.ServerHandshake;
37 * Simple WS client with a console for events.
39 * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
41 public class SimpleConsole extends WebSocketClient {
43 /** Application name, used as prompt. */
44 private final String appName;
46 // Output and error streams
47 private PrintStream outStream;
48 private PrintStream errStream;
51 * Creates a new simple echo object.
53 * @param server the name of the server as either IP address or fully qualified host name, must not be blank
54 * @param port the port to be used, must not be blank
55 * @param appName the application name, used as prompt, must not be blank
56 * @param outStream the stream for message output
57 * @param errStream the stream for error messages
58 * @throws URISyntaxException is URI could not be created from server/port settings
59 * @throws RuntimeException if server or port where blank
61 public SimpleConsole(final String server, final String port, final String appName, PrintStream outStream,
62 PrintStream errStream) throws URISyntaxException {
63 super(new URI("ws://" + server + ":" + port));
64 Validate.notBlank(appName, "SimpleConsole: given application name was blank");
66 this.appName = appName;
67 this.outStream = outStream;
68 this.errStream = errStream;
72 public void onClose(final int code, final String reason, final boolean remote) {
73 outStream.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
74 outStream.print(" ==-->> ");
76 case CloseFrame.NORMAL:
77 outStream.println("normal");
79 case CloseFrame.GOING_AWAY:
80 outStream.println("APEX going away");
82 case CloseFrame.PROTOCOL_ERROR:
83 outStream.println("some protocol error");
85 case CloseFrame.REFUSE:
86 outStream.println("received unacceptable type of data");
88 case CloseFrame.NO_UTF8:
89 outStream.println("expected UTF-8, found something else");
91 case CloseFrame.TOOBIG:
92 outStream.println("message too big");
94 case CloseFrame.UNEXPECTED_CONDITION:
95 outStream.println("unexpected server condition");
98 outStream.println("unkown close frame code");
101 outStream.print(" ==-->> " + reason);
105 public void onError(final Exception ex) {
106 errStream.println(this.appName + ": " + ex.getMessage());
110 public void onMessage(final String message) {
111 // this client does not expect messages
115 public void onOpen(final ServerHandshake handshakedata) {
116 outStream.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
120 * Runs the console client. In particular, it starts a new thread for the Websocket connection and then reads from
123 * @throws NotYetConnectedException if not connected to server when sending events
124 * @throws IOException on an IO problem on standard in
126 public void runClient() throws IOException {
127 final Thread thread = new Thread() {
133 thread.setName("ClientThread");
136 final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
139 while ((line = in.readLine()) != null) {
140 if ("exit".equals(line)) {
144 final String current = line.trim();
145 if ("".equals(current)) {
154 this.close(CloseFrame.NORMAL);