diff options
author | adheli.tavares <adheli.tavares@est.tech> | 2023-08-10 16:58:48 +0100 |
---|---|---|
committer | adheli.tavares <adheli.tavares@est.tech> | 2023-09-22 16:01:00 +0100 |
commit | a3a032d5b902c3e125dccd2bc2b41f22861decc1 (patch) | |
tree | 481b0faa916f39da096fe5c935c630b1598f0f7d /tools/simple-wsclient/src | |
parent | 097618956f681eee12a23d7d9dc4f4f22f5347cc (diff) |
Java 17 Upgrade
Updated dependencies to latest versions
Made Java 17 changes
Moved apex dependencies versions to main apex pom
Issue-ID: POLICY-4675
Change-Id: Ia5cd5670a1f024f5402cbd7371162ce3313930ef
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'tools/simple-wsclient/src')
3 files changed, 143 insertions, 62 deletions
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 8e607ebd4..2e6714fcc 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 @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +22,17 @@ package org.onap.policy.apex.tools.simple.wsclient; +import jakarta.websocket.ClientEndpoint; +import jakarta.websocket.CloseReason; +import jakarta.websocket.CloseReason.CloseCodes; +import jakarta.websocket.ContainerProvider; +import jakarta.websocket.DeploymentException; +import jakarta.websocket.OnClose; +import jakarta.websocket.OnError; +import jakarta.websocket.OnMessage; +import jakarta.websocket.OnOpen; +import jakarta.websocket.Session; +import jakarta.websocket.WebSocketContainer; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -29,16 +41,14 @@ import java.net.URI; import java.net.URISyntaxException; import java.nio.channels.NotYetConnectedException; import org.apache.commons.lang3.Validate; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.framing.CloseFrame; -import org.java_websocket.handshake.ServerHandshake; /** * Simple WS client with a console for events. * * @author Sven van der Meer (sven.van.der.meer@ericsson.com) */ -public class SimpleConsole extends WebSocketClient { +@ClientEndpoint +public class SimpleConsole { /** Application name, used as prompt. */ private final String appName; @@ -46,6 +56,8 @@ public class SimpleConsole extends WebSocketClient { // Output and error streams private PrintStream outStream; private PrintStream errStream; + // The Websocket Session + private Session userSession; /** * Creates a new simple echo object. @@ -56,42 +68,54 @@ public class SimpleConsole extends WebSocketClient { * @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 IOException on IO exceptions + * @throws DeploymentException on deployment exceptions * @throws RuntimeException if server or port where blank */ public SimpleConsole(final String server, final String port, final String appName, PrintStream outStream, - PrintStream errStream) throws URISyntaxException { - super(new URI("ws://" + server + ":" + port)); + PrintStream errStream) throws URISyntaxException, DeploymentException, IOException { Validate.notBlank(appName, "SimpleConsole: given application name was blank"); + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); + container.connectToServer(this, new URI("ws://" + server + ":" + port)); + this.appName = appName; this.outStream = outStream; this.errStream = errStream; } - @Override - public void onClose(final int code, final String reason, final boolean remote) { - outStream.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me")); + /** + * Callback hook for Connection close events. + * + * @param userSession the userSession which is getting closed. + * @param reason the reason for connection close + */ + @OnClose + public void onClose(Session userSession, CloseReason reason) { + outStream.println(this.appName + ": Connection closed"); + outStream.print(" ==-->> "); - switch (code) { - case CloseFrame.NORMAL: + CloseCodes reasonCloseCode = CloseCodes.valueOf(reason.getCloseCode().toString()); + switch (reasonCloseCode) { + case NORMAL_CLOSURE: outStream.println("normal"); break; - case CloseFrame.GOING_AWAY: + case GOING_AWAY: outStream.println("APEX going away"); break; - case CloseFrame.PROTOCOL_ERROR: + case PROTOCOL_ERROR: outStream.println("some protocol error"); break; - case CloseFrame.REFUSE: + case CANNOT_ACCEPT: outStream.println("received unacceptable type of data"); break; - case CloseFrame.NO_UTF8: + case CLOSED_ABNORMALLY: outStream.println("expected UTF-8, found something else"); break; - case CloseFrame.TOOBIG: + case TOO_BIG: outStream.println("message too big"); break; - case CloseFrame.UNEXPECTED_CONDITION: + case UNEXPECTED_CONDITION: outStream.println("unexpected server condition"); break; default: @@ -101,19 +125,32 @@ public class SimpleConsole extends WebSocketClient { outStream.print(" ==-->> " + reason); } - @Override + + @OnError public void onError(final Exception ex) { errStream.println(this.appName + ": " + ex.getMessage()); } - @Override - public void onMessage(final String message) { + /** + * Callback hook for Message Events. This method will be invoked when a client send a message. + * + * @param message The text message + */ + @OnMessage + public void onMessage(String message) { // this client does not expect messages } - @Override - public void onOpen(final ServerHandshake handshakedata) { - outStream.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")"); + /** + * Callback hook for Connection open events. + * + * @param userSession the userSession which is opened. + */ + @OnOpen + public void onOpen(Session userSession) { + outStream.println(this.appName + ": opened connection to APEX (" + userSession.getRequestURI() + ")"); + + this.userSession = userSession; } /** @@ -124,15 +161,6 @@ public class SimpleConsole extends WebSocketClient { * @throws IOException on an IO problem on standard in */ public void runClient() throws IOException { - final Thread thread = new Thread() { - @Override - public void run() { - connect(); - } - }; - thread.setName("ClientThread"); - thread.start(); - final var in = new BufferedReader(new InputStreamReader(System.in)); var event = new StringBuilder(); String line; @@ -143,15 +171,15 @@ public class SimpleConsole extends WebSocketClient { final String current = line.trim(); if ("".equals(current)) { - this.send(event.toString()); + this.userSession.getBasicRemote().sendText(event.toString()); event = new StringBuilder(); } else { event.append(current); } } - thread.interrupt(); - this.close(CloseFrame.NORMAL); + this.userSession.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Normal Closure")); } + } 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 672594c52..8c7aec008 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,20 +21,30 @@ package org.onap.policy.apex.tools.simple.wsclient; +import jakarta.websocket.ClientEndpoint; +import jakarta.websocket.CloseReason; +import jakarta.websocket.CloseReason.CloseCodes; +import jakarta.websocket.ContainerProvider; +import jakarta.websocket.DeploymentException; +import jakarta.websocket.OnClose; +import jakarta.websocket.OnError; +import jakarta.websocket.OnMessage; +import jakarta.websocket.OnOpen; +import jakarta.websocket.Session; +import jakarta.websocket.WebSocketContainer; +import java.io.IOException; import java.io.PrintStream; import java.net.URI; import java.net.URISyntaxException; import org.apache.commons.lang3.Validate; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.framing.CloseFrame; -import org.java_websocket.handshake.ServerHandshake; /** * Simple WS client as an echo. * * @author Sven van der Meer (sven.van.der.meer@ericsson.com) */ -public class SimpleEcho extends WebSocketClient { +@ClientEndpoint +public class SimpleEcho { /** Application name, used as prompt. */ private final String appName; @@ -51,42 +62,54 @@ public class SimpleEcho extends WebSocketClient { * @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 IOException on IO exceptions + * @throws DeploymentException on deployment exceptions * @throws RuntimeException if server or port where blank */ public SimpleEcho(final String server, final String port, final String appName, PrintStream outStream, - PrintStream errStream) throws URISyntaxException { - super(new URI("ws://" + server + ":" + port)); + PrintStream errStream) throws URISyntaxException, DeploymentException, IOException { + Validate.notBlank(appName, "SimpleEcho: given application name was blank"); - + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); + container.connectToServer(this, new URI("ws://" + server + ":" + port)); + this.appName = appName; this.outStream = outStream; this.errStream = errStream; } - @Override - public void onClose(final int code, final String reason, final boolean remote) { - outStream.println(this.appName + ": Connection closed by " + (remote ? "APEX" : "me")); + /** + * Callback hook for Connection close events. + * + * @param userSession the userSession which is getting closed. + * @param reason the reason for connection close + */ + @OnClose + public void onClose(Session userSession, CloseReason reason) { + outStream.println(this.appName + ": Connection closed"); outStream.print(" ==-->> "); - switch (code) { - case CloseFrame.NORMAL: + + CloseCodes reasonCloseCode = CloseCodes.valueOf(reason.getCloseCode().toString()); + switch (reasonCloseCode) { + case NORMAL_CLOSURE: outStream.println("normal"); break; - case CloseFrame.GOING_AWAY: + case GOING_AWAY: outStream.println("APEX going away"); break; - case CloseFrame.PROTOCOL_ERROR: + case PROTOCOL_ERROR: outStream.println("some protocol error"); break; - case CloseFrame.REFUSE: + case CANNOT_ACCEPT: outStream.println("received unacceptable type of data"); break; - case CloseFrame.NO_UTF8: + case CLOSED_ABNORMALLY: outStream.println("expected UTF-8, found something else"); break; - case CloseFrame.TOOBIG: + case TOO_BIG: outStream.println("message too big"); break; - case CloseFrame.UNEXPECTED_CONDITION: + case UNEXPECTED_CONDITION: outStream.println("unexpected server condition"); break; default: @@ -96,13 +119,18 @@ public class SimpleEcho extends WebSocketClient { outStream.print(" ==-->> " + reason); } - @Override + @OnError public void onError(final Exception ex) { errStream.println(this.appName + ": " + ex.getMessage()); } - @Override - public void onMessage(final String message) { + /** + * Callback hook for Message Events. This method will be invoked when a client send a message. + * + * @param message The text message + */ + @OnMessage + public void onMessage(String message) { outStream.println(this.appName + ": received"); outStream.println("---------------------------------"); outStream.println(message); @@ -110,9 +138,13 @@ public class SimpleEcho extends WebSocketClient { outStream.println(); } - @Override - public void onOpen(final ServerHandshake handshakedata) { - outStream.println(this.appName + ": opened connection to APEX (" + handshakedata.getHttpStatusMessage() + ")"); + /** + * Callback hook for Connection open events. + * + * @param userSession the userSession which is opened. + */ + @OnOpen + public void onOpen(Session userSession) { + outStream.println(this.appName + ": opened connection to APEX (" + userSession.getRequestURI() + ")"); } - } diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java index e3af4dcc1..405013be4 100644 --- a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java +++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +22,7 @@ package org.onap.policy.apex.tools.simple.wsclient; +import jakarta.websocket.DeploymentException; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; @@ -141,8 +143,7 @@ public final class WsClientMain { outStream.println(); try { - final var simpleEcho = new SimpleEcho(server, port, APP_NAME, outStream, outStream); - simpleEcho.connect(); + new SimpleEcho(server, port, APP_NAME, outStream, outStream); } catch (final URISyntaxException uex) { String message = APP_NAME + ": URI exception, could not create URI from server and port settings"; outStream.println(message); @@ -155,6 +156,18 @@ public final class WsClientMain { String message = APP_NAME + ": illegal argument, server or port were blank"; outStream.println(message); LOGGER.warn(message, iex); + } catch (DeploymentException dex) { + String message = APP_NAME + ": could not deploy client"; + outStream.println(message); + LOGGER.warn(message, dex); + } catch (IOException iox) { + String message = APP_NAME + ": illegal argument, IO execption on client"; + outStream.println(message); + LOGGER.warn(message, iox); + } catch (Exception xex) { + String message = APP_NAME + ": Unknown execption on client"; + outStream.println(message); + LOGGER.warn(message, xex); } } @@ -199,10 +212,18 @@ public final class WsClientMain { String message = APP_NAME + ": not yet connected, connection to server took too long"; outStream.println(message); LOGGER.warn(message, nex); + } catch (DeploymentException dex) { + String message = APP_NAME + ": could not deploy client"; + outStream.println(message); + LOGGER.warn(message, dex); } catch (final IOException ioe) { String message = APP_NAME + ": IO exception, something went wrong on the standard input"; outStream.println(message); LOGGER.warn(message, ioe); + } catch (Exception xex) { + String message = APP_NAME + ": Unknown execption on client"; + outStream.println(message); + LOGGER.warn(message, xex); } } |