659dd77eb8762048c32e38cf2090e2a9ac9ce659
[policy/apex-pdp.git] / tools / simple-wsclient / src / main / java / org / onap / policy / apex / tools / simple / wsclient / SimpleEcho.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.PrintStream;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26
27 import org.apache.commons.lang3.Validate;
28 import org.java_websocket.client.WebSocketClient;
29 import org.java_websocket.framing.CloseFrame;
30 import org.java_websocket.handshake.ServerHandshake;
31
32 /**
33  * Simple WS client as an echo.
34  *
35  * @author Sven van der Meer (sven.van.der.meer@ericsson.com)
36  */
37 public class SimpleEcho extends WebSocketClient {
38
39     /** Application name, used as prompt. */
40     private final String appName;
41
42     // Output and error streams
43     private PrintStream outStream;
44     private PrintStream errStream;
45
46     /**
47      * Creates a new simple echo object.
48      *
49      * @param server the name of the server as either IP address or fully qualified host name, must not be blank
50      * @param port the port to be used, must not be blank
51      * @param appName the application name, used as prompt, must not be blank
52      * @param outStream the stream for message output
53      * @param errStream the stream for error messages
54      * @throws URISyntaxException is URI could not be created from server/port settings
55      * @throws RuntimeException if server or port where blank
56      */
57     public SimpleEcho(final String server, final String port, final String appName, PrintStream outStream,
58                     PrintStream errStream) throws URISyntaxException {
59         super(new URI("ws://" + server + ":" + port));
60         Validate.notBlank(appName, "SimpleEcho: given application name was blank");
61  
62         this.appName = appName;
63         this.outStream = outStream;
64         this.errStream = errStream;
65     }
66
67     @Override
68     public void onClose(final int code, final String reason, final boolean remote) {
69         outStream.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me"));
70         outStream.print(" ==-->> ");
71         switch (code) {
72             case CloseFrame.NORMAL:
73                 outStream.println("normal");
74                 break;
75             case CloseFrame.GOING_AWAY:
76                 outStream.println("APEX going away");
77                 break;
78             case CloseFrame.PROTOCOL_ERROR:
79                 outStream.println("some protocol error");
80                 break;
81             case CloseFrame.REFUSE:
82                 outStream.println("received unacceptable type of data");
83                 break;
84             case CloseFrame.NO_UTF8:
85                 outStream.println("expected UTF-8, found something else");
86                 break;
87             case CloseFrame.TOOBIG:
88                 outStream.println("message too big");
89                 break;
90             case CloseFrame.UNEXPECTED_CONDITION:
91                 outStream.println("unexpected server condition");
92                 break;
93             default:
94                 outStream.println("unkown close frame code");
95                 break;
96         }
97         outStream.print(" ==-->> " + reason);
98     }
99
100     @Override
101     public void onError(final Exception ex) {
102         errStream.println(this.appName + ": " + ex.getMessage());
103     }
104
105     @Override
106     public void onMessage(final String message) {
107         outStream.println(this.appName + ": received");
108         outStream.println("---------------------------------");
109         outStream.println(message);
110         outStream.println("=================================");
111         outStream.println();
112     }
113
114     @Override
115     public void onOpen(final ServerHandshake handshakedata) {
116         outStream.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")");
117     }
118
119 }