use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.
the class GraphQLBrokerSubProtocolHandler method handleMessageToClient.
@Override
public void handleMessageToClient(WebSocketSession session, Message<?> message) {
if (!(message.getPayload() instanceof GraphQLMessage)) {
logger.error("Expected OperationMessage. Ignoring " + message + ".");
return;
}
boolean closeWebSocketSession = false;
try {
GraphQLMessage operation = (GraphQLMessage) message.getPayload();
if (GraphQLMessageType.CONNECTION_ACK.equals(operation.getType()))
this.stats.incrementConnectedCount();
byte[] bytes = objectMapper.writer().writeValueAsBytes(message.getPayload());
session.sendMessage(new TextMessage(bytes));
} catch (SessionLimitExceededException ex) {
// Bad session, just get out
throw ex;
} catch (Throwable ex) {
// Could be part of normal workflow (e.g. browser tab closed)
logger.debug("Failed to send WebSocket message to client in session " + session.getId() + ".", ex);
closeWebSocketSession = true;
} finally {
if (closeWebSocketSession) {
try {
session.close(CloseStatus.PROTOCOL_ERROR);
} catch (IOException ex) {
// Ignore
}
}
}
}
use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.
the class GraphQLBrokerSubProtocolHandler method sendErrorMessage.
/**
* Invoked to send an ERROR frame to the client.
*/
protected void sendErrorMessage(WebSocketSession session, Throwable error, GraphQLMessage message) {
this.stats.incrementErrorCount();
GraphQLMessage response = new GraphQLMessage(message.getId(), GraphQLMessageType.CONNECTION_ERROR);
ObjectWriter writer = objectMapper.writer();
try {
byte[] bytes = writer.writeValueAsBytes(response);
session.sendMessage(new TextMessage(bytes));
} catch (Throwable ex) {
// Could be part of normal workflow (e.g. browser tab closed)
logger.debug("Failed to send ERROR to client", ex);
}
}
use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.
the class GraphQLBrokerSubProtocolHandler method createDisconnectMessage.
private Message<GraphQLMessage> createDisconnectMessage(WebSocketSession session) {
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
headerAccessor.setDestination(destination);
headerAccessor.setSessionId(session.getId());
headerAccessor.setSessionAttributes(session.getAttributes());
headerAccessor.setUser(getUser(session));
headerAccessor.setLeaveMutable(false);
GraphQLMessage operation = new GraphQLMessage(null, GraphQLMessageType.CONNECTION_TERMINATE);
return MessageBuilder.createMessage(operation, headerAccessor.getMessageHeaders());
}
use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.
the class GraphQLBrokerMessageHandler method handleMessageInternal.
@SuppressWarnings("unchecked")
@Override
protected void handleMessageInternal(Message<?> message) {
MessageHeaders headers = message.getHeaders();
SimpMessageType messageType = SimpMessageHeaderAccessor.getMessageType(headers);
String destination = SimpMessageHeaderAccessor.getDestination(headers);
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
Principal user = SimpMessageHeaderAccessor.getUser(headers);
updateSessionReadTime(sessionId);
if (!checkDestinationPrefix(destination)) {
return;
}
if (SimpMessageType.MESSAGE.equals(messageType) && message.getPayload() instanceof GraphQLMessage) {
logMessage(message);
Message<GraphQLMessage> graphQLMessage = (Message<GraphQLMessage>) message;
GraphQLMessageType graphQLMessagePayloadType = graphQLMessage.getPayload().getType();
switch(graphQLMessagePayloadType) {
case CONNECTION_INIT:
if (!isBrokerAvailable()) {
sendErrorMessageToClient(BROKER_NOT_AVAILABLE, GraphQLMessageType.CONNECTION_ERROR, message);
return;
}
long[] clientHeartbeat = SimpMessageHeaderAccessor.getHeartbeat(headers);
long[] serverHeartbeat = getHeartbeatValue();
this.sessions.put(sessionId, new SessionInfo(sessionId, user, clientHeartbeat, serverHeartbeat));
handleConnectionInitMessage(graphQLMessage);
break;
case START:
// start subscription
if (!isBrokerAvailable()) {
sendErrorMessageToClient(BROKER_NOT_AVAILABLE, GraphQLMessageType.ERROR, message);
return;
}
handleStartSubscription(graphQLMessage);
break;
case STOP:
// stop subscription
handleStopSubscription(graphQLMessage);
break;
case CONNECTION_TERMINATE:
// end connection
handleConnectionTerminate(graphQLMessage);
break;
default:
break;
}
}
}
use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.
the class GraphQLBrokerMessageHandler method handleQueryOrMutation.
private void handleQueryOrMutation(String id, ExecutionResult result, Message<GraphQLMessage> message) {
Map<String, Object> payload = Collections.singletonMap("data", result.getData());
MessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.getMutableAccessor(message);
GraphQLMessage operationData = new GraphQLMessage(id, GraphQLMessageType.DATA, payload);
Message<?> dataMessage = MessageBuilder.createMessage(operationData, headerAccessor.getMessageHeaders());
// Send data
getClientOutboundChannel().send(dataMessage);
GraphQLMessage completeData = new GraphQLMessage(id, GraphQLMessageType.COMPLETE);
Message<?> completeMessage = MessageBuilder.createMessage(completeData, headerAccessor.getMessageHeaders());
// Send complete
getClientOutboundChannel().send(completeMessage);
}
Aggregations