Search in sources :

Example 21 with GraphQLMessage

use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.

the class GraphQLBrokerMessageHandler method sendErrorMessageToClient.

private void sendErrorMessageToClient(String errorText, GraphQLMessageType type, Message<?> inputMessage) {
    Map<String, Object> payload = Collections.singletonMap("errors", Collections.singletonList(errorText));
    GraphQLMessage inputOperation = (GraphQLMessage) inputMessage.getPayload();
    GraphQLMessage connectionError = new GraphQLMessage(inputOperation.getId(), type, payload);
    MessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.getMutableAccessor(inputMessage);
    Message<GraphQLMessage> errorMessage = MessageBuilder.createMessage(connectionError, headerAccessor.getMessageHeaders());
    getClientOutboundChannel().send(errorMessage);
}
Also used : SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) GraphQLMessage(org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage)

Example 22 with GraphQLMessage

use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.

the class GraphQLBrokerMessageHandler method handleDisconnect.

private void handleDisconnect(String sessionId, Principal user, Message<?> origMessage) {
    this.sessions.remove(sessionId);
    SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
    accessor.setSessionId(sessionId);
    accessor.setUser(user);
    if (origMessage != null) {
        accessor.setHeader(SimpMessageHeaderAccessor.DISCONNECT_MESSAGE_HEADER, origMessage);
    }
    initHeaders(accessor);
    GraphQLMessage disconnect = new GraphQLMessage(null, GraphQLMessageType.CONNECTION_ERROR);
    Message<GraphQLMessage> message = MessageBuilder.createMessage(disconnect, accessor.getMessageHeaders());
    getClientOutboundChannel().send(message);
}
Also used : GraphQLMessage(org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Example 23 with GraphQLMessage

use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.

the class GraphQLBrokerMessageHandler method handleStartSubscription.

@Timed
protected final void handleStartSubscription(Message<GraphQLMessage> message) {
    logger.info("handleStartSubscription for message " + message);
    MessageHeaders headers = message.getHeaders();
    String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
    GraphQLMessage operationPayload = message.getPayload();
    QueryParameters parameters = null;
    try {
        parameters = QueryParameters.from(operationPayload.getPayload());
    } catch (Exception e) {
        sendErrorMessageToClient(e.getMessage(), GraphQLMessageType.ERROR, message);
        return;
    }
    ExecutionResult executionResult = graphQLSubscriptionExecutor.execute(parameters.getQuery(), parameters.getVariables());
    if (executionResult.getErrors().isEmpty()) {
        if (executionResult.getData() == null) {
            sendErrorMessageToClient("Server error!", GraphQLMessageType.ERROR, message);
        } else if (executionResult.getData() instanceof Publisher) {
            Optional.of(executionResult.<Publisher<ExecutionResult>>getData()).ifPresent(data -> {
                MessageChannel outboundChannel = getClientOutboundChannelForSession(sessionId);
                GraphQLBrokerChannelSubscriber subscriber = new GraphQLBrokerChannelSubscriber(message, operationPayload.getId(), outboundChannel, bufferTimeSpanMs, bufferCount);
                graphQLsubscriptionRegistry.subscribe(sessionId, operationPayload.getId(), subscriber, () -> {
                    data.subscribe(subscriber);
                });
            });
        } else {
            handleQueryOrMutation(operationPayload.getId(), executionResult, message);
        }
    } else {
        Map<String, Object> payload = Collections.singletonMap("errors", executionResult.getErrors());
        GraphQLMessage startSubscriptionMessage = message.getPayload();
        GraphQLMessage startSubscriptionErrors = new GraphQLMessage(startSubscriptionMessage.getId(), GraphQLMessageType.ERROR, payload);
        Message<GraphQLMessage> errorMessage = MessageBuilder.createMessage(startSubscriptionErrors, headers);
        getClientOutboundChannel().send(errorMessage);
    }
}
Also used : Arrays(java.util.Arrays) ScheduledFuture(java.util.concurrent.ScheduledFuture) SubscribableChannel(org.springframework.messaging.SubscribableChannel) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) ExecutionResult(graphql.ExecutionResult) GraphQLMessage(org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage) AbstractBrokerMessageHandler(org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler) Map(java.util.Map) Message(org.springframework.messaging.Message) QueryParameters(org.activiti.cloud.services.notifications.graphql.ws.util.QueryParameters) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) SimpMessageType(org.springframework.messaging.simp.SimpMessageType) Publisher(org.reactivestreams.Publisher) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventListener(org.springframework.context.event.EventListener) TaskScheduler(org.springframework.scheduling.TaskScheduler) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) BrokerAvailabilityEvent(org.springframework.messaging.simp.broker.BrokerAvailabilityEvent) Timed(com.codahale.metrics.annotation.Timed) Principal(java.security.Principal) GraphQLMessageType(org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessageType) Optional(java.util.Optional) Collections(java.util.Collections) MessageBuilder(org.springframework.messaging.support.MessageBuilder) MessageHeaderInitializer(org.springframework.messaging.support.MessageHeaderInitializer) Assert(org.springframework.util.Assert) ExecutionResult(graphql.ExecutionResult) QueryParameters(org.activiti.cloud.services.notifications.graphql.ws.util.QueryParameters) Publisher(org.reactivestreams.Publisher) GraphQLMessage(org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) Timed(com.codahale.metrics.annotation.Timed)

Example 24 with GraphQLMessage

use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.

the class GraphQLBrokerSubProtocolHandlerTest method testHandleProtocolErrorMessageToClient.

@Test
public void testHandleProtocolErrorMessageToClient() throws IOException {
    // given
    WebSocketSession session = spy(mockWebSocketSession("sess1"));
    doThrow(RuntimeException.class).when(session).sendMessage(any(TextMessage.class));
    Message<GraphQLMessage> message = connectionAckMessage("operationId", session);
    // when
    testSubject.handleMessageToClient(session, message);
    // then
    verify(session).close(eq(CloseStatus.PROTOCOL_ERROR));
}
Also used : GraphQLMessage(org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage) TextMessage(org.springframework.web.socket.TextMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) StandardWebSocketSession(org.springframework.web.socket.adapter.standard.StandardWebSocketSession) Test(org.junit.jupiter.api.Test)

Example 25 with GraphQLMessage

use of org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage in project activiti-cloud by Activiti.

the class GraphQLBrokerSubProtocolHandlerTest method connectionAckMessage.

private Message<GraphQLMessage> connectionAckMessage(String operationId, WebSocketSession session) {
    SimpMessageHeaderAccessor headerAccessor = simpHeaderAccessor(session);
    GraphQLMessage payload = new GraphQLMessage(operationId, GraphQLMessageType.CONNECTION_ACK);
    return MessageBuilder.createMessage(payload, headerAccessor.getMessageHeaders());
}
Also used : GraphQLMessage(org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor)

Aggregations

GraphQLMessage (org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessage)35 Test (org.junit.jupiter.api.Test)14 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)13 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 WebsocketSender (reactor.netty.http.client.HttpClient.WebsocketSender)6 Date (java.util.Date)5 TextMessage (org.springframework.web.socket.TextMessage)5 ExecutionResult (graphql.ExecutionResult)4 Principal (java.security.Principal)4 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)4 WebSocketSession (org.springframework.web.socket.WebSocketSession)4 Timed (com.codahale.metrics.annotation.Timed)3 HashMap (java.util.HashMap)3 GraphQLMessageType (org.activiti.cloud.services.notifications.graphql.ws.api.GraphQLMessageType)3 Message (org.springframework.messaging.Message)3 MessageHeaders (org.springframework.messaging.MessageHeaders)3 SimpMessageType (org.springframework.messaging.simp.SimpMessageType)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2