Changes for checkstyle 8.32
[policy/apex-pdp.git] / tools / simple-wsclient / src / main / java / org / onap / policy / apex / tools / simple / wsclient / SimpleConsole.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.tools.simple.wsclient;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.PrintStream;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.nio.channels.NotYetConnectedException;
30 import org.apache.commons.lang3.Validate;
31 import org.java_websocket.client.WebSocketClient;
32 import org.java_websocket.framing.CloseFrame;
33 import org.java_websocket.handshake.ServerHandshake;
34
35 /**
36  * Simple WS client with a console for events.
37  *
38  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
39  */
40 public class SimpleConsole extends WebSocketClient {
41
42     /** Application name, used as prompt. */
43     private final String appName;
44     
45     // Output and error streams
46     private PrintStream outStream;
47     private PrintStream errStream;
48
49     /**
50      * Creates a new simple echo object.
51      *
52      * @param server the name of the server as either IP address or fully qualified host name, must not be blank
53      * @param port the port to be used, must not be blank
54      * @param appName the application name, used as prompt, must not be blank
55      * @param outStream the stream for message output
56      * @param errStream the stream for error messages
57      * @throws URISyntaxException is URI could not be created from server/port settings
58      * @throws RuntimeException if server or port where blank
59      */
60     public SimpleConsole(final String server, final String port, final String appName, PrintStream outStream,
61                     PrintStream errStream) throws URISyntaxException {
62         super(new URI("ws://" + server + ":" + port));
63         Validate.notBlank(appName, "SimpleConsole: given application name was blank");
64
65         this.appName = appName;
66         this.outStream = outStream;
67         this.errStream = errStream;
68     }
69
70     @Override
71     public void onClose(final int code, final String reason, final boolean remote) {
72         outStream.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
73         outStream.print(" ==-->> ");
74         switch (code) {
75             case CloseFrame.NORMAL:
76                 outStream.println("normal");
77                 break;
78             case CloseFrame.GOING_AWAY:
79                 outStream.println("APEX going away");
80                 break;
81             case CloseFrame.PROTOCOL_ERROR:
82                 outStream.println("some protocol error");
83                 break;
84             case CloseFrame.REFUSE:
85                 outStream.println("received unacceptable type of data");
86                 break;
87             case CloseFrame.NO_UTF8:
88                 outStream.println("expected UTF-8, found something else");
89                 break;
90             case CloseFrame.TOOBIG:
91                 outStream.println("message too big");
92                 break;
93             case CloseFrame.UNEXPECTED_CONDITION:
94                 outStream.println("unexpected server condition");
95                 break;
96             default:
97                 outStream.println("unkown close frame code");
98                 break;
99         }
100         outStream.print(" ==-->> " + reason);
101     }
102
103     @Override
104     public void onError(final Exception ex) {
105         errStream.println(this.appName + ": " + ex.getMessage());
106     }
107
108     @Override
109     public void onMessage(final String message) {
110         // this client does not expect messages
111     }
112
113     @Override
114     public void onOpen(final ServerHandshake handshakedata) {
115         outStream.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
116     }
117
118     /**
119      * Runs the console client. In particular, it starts a new thread for the Websocket connection and then reads from
120      * standard input.
121      *
122      * @throws NotYetConnectedException if not connected to server when sending events
123      * @throws IOException on an IO problem on standard in
124      */
125     public void runClient() throws IOException {
126         final Thread thread = new Thread() {
127             @Override
128             public void run() {
129                 connect();
130             }
131         };
132         thread.setName("ClientThread");
133         thread.start();
134         
135         final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
136         String event = "";
137         String line;
138         while ((line = in.readLine()) != null) {
139             if ("exit".equals(line)) {
140                 break;
141             }
142
143             final String current = line.trim();
144             if ("".equals(current)) {
145                 this.send(event);
146                 event = "";
147             } else {
148                 event += current;
149             }
150         }
151
152         thread.interrupt();
153         this.close(CloseFrame.NORMAL);
154     }
155
156 }