de.oliverwetterau.neo4j.websockets.client.WebSocketHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of websockets-client Show documentation
Show all versions of websockets-client Show documentation
This framework allows to connect clients with embedded Neo4j servers through websockets. Furthermore it offers
high availability and load balancing to the clients when using Neo4j in a cluster.
The newest version!
package de.oliverwetterau.neo4j.websockets.client;
import com.fasterxml.jackson.databind.JsonNode;
import de.oliverwetterau.neo4j.websockets.core.data.json.JsonObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import java.io.IOException;
import java.util.Locale;
/**
* A class to process messages through a websocket connection.
*
* @author Oliver Wetterau
* @version 2015-09-01
*/
public class WebSocketHandler extends TextWebSocketHandler {
private static final Logger logger = LoggerFactory.getLogger(WebSocketHandler.class);
protected static JsonObjectMapper jsonObjectMapper;
protected final ClusterListener clusterListener;
protected final ConnectionListener connectionListener;
protected WebSocketSession session;
protected String resultString;
protected byte[] resultBytes;
/** notify object for threads used for waiting for connections */
protected final Object notifyConnectionObject = new Object();
/** notify object for threads used for waiting for results */
protected final Object notifyResultObject = new Object();
/** language settings */
protected Locale locale;
/**
* Constructor
* @param clusterListener cluster availablity listener
* @param connectionListener websocket status listener
*/
public WebSocketHandler(ClusterListener clusterListener, ConnectionListener connectionListener) {
this.clusterListener = clusterListener;
this.connectionListener = connectionListener;
}
/**
* Sets the wrapper for a json object mapper
* @param jsonObjectMapper wrapper for a json object mapper
*/
public static void setJsonObjectMapper(JsonObjectMapper jsonObjectMapper) {
WebSocketHandler.jsonObjectMapper = jsonObjectMapper;
}
/**
* Sets the language settings
* @param locale language settings
*/
public void setLocale(final Locale locale) {
this.locale = locale;
}
/**
* Returns whether this websocket is connected.
* @return is websocket connected?
*/
public boolean isConnected() {
return session != null && session.isOpen();
}
/**
* Is called after a websocket session was established.
* @param webSocketSession websocket session that was established
*/
@Override
public void afterConnectionEstablished(final WebSocketSession webSocketSession) {
logger.debug("[afterConnectionEstablished] id = {}", webSocketSession.getId());
this.session = webSocketSession;
synchronized (notifyConnectionObject) {
notifyConnectionObject.notifyAll();
}
}
/**
* Is called after a websocket session was closed.
* @param webSocketSession websocket session that was closed
* @param status status of the websocket session
*/
@Override
public void afterConnectionClosed(final WebSocketSession webSocketSession, final CloseStatus status) {
logger.debug("[afterConnectionClosed] id = ", webSocketSession.getId());
this.session = null;
if (connectionListener != null) {
connectionListener.onConnectionClosed();
}
}
/*@Override
public void handleTextMessage(final WebSocketSession webSocketSession, final TextMessage message) {
resultString = message.getPayload();
if (clusterListener != null) {
JsonNode jsonNode = null;
try {
jsonNode = jsonObjectMapper.getObjectMapper().readTree(resultString);
}
catch (Exception e) {
logger.error("[handleTextMessage]", e);
}
if (jsonNode != null) {
if (jsonNode.has("available")) {
clusterListener.onServerAvailable(jsonNode.get("available").asText(), jsonNode.get("role").asText());
}
else if (jsonNode.has("unavailable")) {
clusterListener.onServerUnavailable(jsonNode.get("unavailable").asText());
}
}
}
synchronized (notifyResultObject) {
notifyResultObject.notifyAll();
}
}*/
/**
* Handles incoming messages.
* @param webSocketSession websocket session the message was received from
* @param message received message
*/
@Override
public void handleBinaryMessage(final WebSocketSession webSocketSession, final BinaryMessage message) {
resultBytes = new byte[message.getPayloadLength()];
message.getPayload().get(resultBytes);
if (clusterListener != null) {
JsonNode jsonNode = null;
try {
jsonNode = jsonObjectMapper.getObjectMapper().readTree(resultBytes);
}
catch (Exception e) {
logger.error("[handleBinaryMessage]", e);
}
if (jsonNode != null) {
if (jsonNode.has("available")) {
clusterListener.onServerAvailable(jsonNode.get("available").asText(), jsonNode.get("role").asText());
}
else if (jsonNode.has("unavailable")) {
clusterListener.onServerUnavailable(jsonNode.get("unavailable").asText());
}
}
}
synchronized (notifyResultObject) {
notifyResultObject.notifyAll();
}
}
/**
* Gets the result that was received in {@link #handleBinaryMessage}
* @return result that was received in {@link #handleBinaryMessage}
*/
public byte[] getResultBytes() {
return resultBytes;
}
/**
* Handles websocket transport errors
* @param webSocketSession websocket session where the error appeared
* @param exception exception that occured
* @throws Exception transport error exception
*/
@Override
public void handleTransportError(final WebSocketSession webSocketSession, final Throwable exception) throws Exception {
if (exception != null) {
logger.error("[handleTransportError]", exception);
}
}
/**
* Sends a binary message using this object's websocket session.
* @param message json binary message
*/
public void sendMessage(final byte[] message) {
try {
session.sendMessage(new BinaryMessage(message));
}
catch (IOException e) {
logger.error("[sendMessage(byte[])]", e);
}
}
/**
* Returns the synchronisation object used for connections.
* @return synchronisation object used for connections
*/
public Object getNotifyConnectionObject() {
return notifyConnectionObject;
}
/**
* Returns the synchronisation object used for results of a websocket message.
* @return synchronisation object used for results of a websocket message
*/
public Object getNotifyResultObject() {
return notifyResultObject;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy