diff --git a/core/router/src/main/java/org/wisdom/router/DefaultWebSocketCallback.java b/core/router/src/main/java/org/wisdom/router/DefaultWebSocketCallback.java index 6f6df546f..6e398219f 100644 --- a/core/router/src/main/java/org/wisdom/router/DefaultWebSocketCallback.java +++ b/core/router/src/main/java/org/wisdom/router/DefaultWebSocketCallback.java @@ -149,33 +149,22 @@ public void invoke(String uri, String client, byte[] content) throws } } else { // Body - parameters[i] = transform(argument.getRawType(), content); + parameters[i] = transform(argument, content); } } getMethod().invoke(getController(), parameters); } - private Object transform(Class type, byte[] content) { - //TODO Change this to use the parameter converter - if (type.equals(String.class)) { - return new String(content, Charset.defaultCharset()); - } - if (type.equals(Integer.class)) { - // Parse as string, wrap as boolean. - String s = new String(content, Charset.defaultCharset()); - return Integer.parseInt(s); - } - if (type.equals(Boolean.class)) { - // Parse as string, wrap as boolean. - String s = new String(content, Charset.defaultCharset()); - return Boolean.parseBoolean(s); - } - // Byte Array - if (type.isArray() && type.getComponentType().equals(Byte.TYPE)) { - return content; + private Object transform(ActionParameter parameter, byte[] content) { + String data = new String(content, Charset.defaultCharset()); + try { + return router.converter().convertValue(data, parameter.getRawType(), parameter.getGenericType(), null); + } catch (IllegalArgumentException e) { //NOSONAR + // Cannot convert. } + // For all the other cases, we need a binder, however, we have no idea about the type of message, // for now we suppose it's json. - return router.engine().getBodyParserEngineForContentType(MimeTypes.JSON).invoke(content, type); + return router.engine().getBodyParserEngineForContentType(MimeTypes.JSON).invoke(content, parameter.getRawType()); } } diff --git a/core/router/src/main/java/org/wisdom/router/WebSocketRouter.java b/core/router/src/main/java/org/wisdom/router/WebSocketRouter.java index 6e86ce7fa..13c5ed57e 100644 --- a/core/router/src/main/java/org/wisdom/router/WebSocketRouter.java +++ b/core/router/src/main/java/org/wisdom/router/WebSocketRouter.java @@ -60,7 +60,7 @@ public class WebSocketRouter implements WebSocketListener, Publisher { List listeners = new ArrayList<>(); @Requires(optional = true) - private ContentEngine engine; + private ContentEngine contentEngine; @Requires(optional = true) ParameterConverters converter; @@ -68,32 +68,51 @@ public class WebSocketRouter implements WebSocketListener, Publisher { @Requires AkkaSystemService akka; - + /** + * @return the logger. + */ public static Logger getLogger() { return LOGGER; } + /** + * Registers the current router in the dispatcher. + */ @Validate public void start() { dispatcher.register(this); } + /** + * Unregisters the current router in the dispatcher. + */ @Invalidate public void stop() { dispatcher.unregister(this); } + /** + * Binds a new controller. + * + * @param controller the new controller + */ @Bind(aggregate = true) public synchronized void bindController(Controller controller) { analyze(controller); } + /** + * @return the parameter converter. + */ public ParameterConverters converter() { return converter; } + /** + * @return the content engine. + */ public ContentEngine engine() { - return engine; + return contentEngine; } /** @@ -134,6 +153,11 @@ private void analyze(Controller controller) { } + /** + * Unbinds the controller. + * + * @param controller the leaving controller + */ @Unbind public synchronized void unbindController(Controller controller) { List toRemove = new ArrayList<>(); @@ -161,6 +185,13 @@ public synchronized void unbindController(Controller controller) { listeners.removeAll(toRemove); } + /** + * Handles the reception of a message. + * + * @param uri the url of the web socket + * @param from the client having sent the message (octal id). + * @param content the received content + */ @Override public void received(final String uri, final String from, final byte[] content) { for (final OnMessageWebSocketCallback listener : listeners) { @@ -187,6 +218,12 @@ public Void call() throws Exception { } } + /** + * Handles the registration of a new client on the web socket. + * + * @param uri the url of the web socket + * @param client the client id + */ @Override public void opened(String uri, String client) { for (DefaultWebSocketCallback open : opens) { @@ -206,6 +243,12 @@ public void opened(String uri, String client) { } } + /** + * Handles the disconnection of a client from the web socket. + * + * @param uri the url of the web socket + * @param client the client id + */ @Override public void closed(String uri, String client) { for (DefaultWebSocketCallback close : closes) { @@ -225,6 +268,13 @@ public void closed(String uri, String client) { } } + /** + * Publishes a text message on the web socket. + * All client's connected to the given websocket receive the message. + * + * @param uri the websocket's url + * @param message the message (text) + */ @Override public void publish(String uri, String message) { if (message == null) { @@ -234,6 +284,13 @@ public void publish(String uri, String message) { dispatcher.publish(uri, message); } + /** + * Publishes a binary message on the web socket. + * All client's connected to the given websocket receive the message. + * + * @param uri the websocket's url + * @param message the data (binary) + */ @Override public void publish(String uri, byte[] message) { if (message == null) { @@ -243,6 +300,13 @@ public void publish(String uri, byte[] message) { dispatcher.publish(uri, message); } + /** + * Publishes a JSON message on the web socket. + * All client's connected to the given websocket receive the message. + * + * @param uri the websocket's url + * @param message the data (JSON) + */ @Override public void publish(String uri, JsonNode message) { if (message == null) { @@ -252,6 +316,14 @@ public void publish(String uri, JsonNode message) { } } + /** + * Sends the given text message to the identified client. If the client is not connected on the web socket, + * nothing happens. + * + * @param uri the websocket's url + * @param client the client that is going to receive the message + * @param message the data (text) + */ @Override public void send(String uri, String client, String message) { if (message == null || client == null) { @@ -262,6 +334,14 @@ public void send(String uri, String client, String message) { dispatcher.send(uri, client, message); } + /** + * Sends the given json message to the identified client. If the client is not connected on the web socket, + * nothing happens. + * + * @param uri the websocket's url + * @param client the client that is going to receive the message + * @param message the data (json) + */ @Override public void send(String uri, String client, JsonNode message) { if (message == null) { @@ -271,6 +351,14 @@ public void send(String uri, String client, JsonNode message) { } } + /** + * Sends the given binary message to the identified client. If the client is not connected on the web socket, + * nothing happens. + * + * @param uri the websocket's url + * @param client the client that is going to receive the message + * @param message the data (binary) + */ @Override public void send(String uri, String client, byte[] message) { if (message == null || client == null) {