Skip to content

Commit

Permalink
Add javadoc and use the parameter converter in the web socket router.
Browse files Browse the repository at this point in the history
Signed-off-by: Clement Escoffier <[email protected]>
  • Loading branch information
cescoffier committed May 31, 2014
1 parent 8867853 commit ddda1c3
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
94 changes: 91 additions & 3 deletions core/router/src/main/java/org/wisdom/router/WebSocketRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,59 @@ public class WebSocketRouter implements WebSocketListener, Publisher {
List<OnMessageWebSocketCallback> listeners = new ArrayList<>();

@Requires(optional = true)
private ContentEngine engine;
private ContentEngine contentEngine;

@Requires(optional = true)
ParameterConverters converter;

@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;
}

/**
Expand Down Expand Up @@ -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<DefaultWebSocketCallback> toRemove = new ArrayList<>();
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit ddda1c3

Please sign in to comment.