Search in sources :

Example 6 with CloseStatus

use of org.springframework.web.socket.CloseStatus in project spring-framework by spring-projects.

the class UndertowXhrTransport method createReceiveCallback.

private ClientCallback<ClientExchange> createReceiveCallback(final TransportRequest transportRequest, final URI url, final HttpHeaders headers, final XhrClientSockJsSession sockJsSession, final SettableListenableFuture<WebSocketSession> connectFuture) {
    return new ClientCallback<ClientExchange>() {

        @Override
        public void completed(final ClientExchange exchange) {
            exchange.setResponseListener(new ClientCallback<ClientExchange>() {

                @Override
                public void completed(ClientExchange result) {
                    ClientResponse response = result.getResponse();
                    if (response.getResponseCode() != 200) {
                        HttpStatus status = HttpStatus.valueOf(response.getResponseCode());
                        IoUtils.safeClose(result.getConnection());
                        onFailure(new HttpServerErrorException(status, "Unexpected XHR receive status"));
                    } else {
                        SockJsResponseListener listener = new SockJsResponseListener(transportRequest, result.getConnection(), url, headers, sockJsSession, connectFuture);
                        listener.setup(result.getResponseChannel());
                    }
                    if (logger.isTraceEnabled()) {
                        logger.trace("XHR receive headers: " + toHttpHeaders(response.getResponseHeaders()));
                    }
                    try {
                        StreamSinkChannel channel = result.getRequestChannel();
                        channel.shutdownWrites();
                        if (!channel.flush()) {
                            channel.getWriteSetter().set(ChannelListeners.<StreamSinkChannel>flushingChannelListener(null, null));
                            channel.resumeWrites();
                        }
                    } catch (IOException exc) {
                        IoUtils.safeClose(result.getConnection());
                        onFailure(exc);
                    }
                }

                @Override
                public void failed(IOException exc) {
                    IoUtils.safeClose(exchange.getConnection());
                    onFailure(exc);
                }
            });
        }

        @Override
        public void failed(IOException exc) {
            onFailure(exc);
        }

        private void onFailure(Throwable failure) {
            if (connectFuture.setException(failure)) {
                return;
            }
            if (sockJsSession.isDisconnected()) {
                sockJsSession.afterTransportClosed(null);
            } else {
                sockJsSession.handleTransportError(failure);
                sockJsSession.afterTransportClosed(new CloseStatus(1006, failure.getMessage()));
            }
        }
    };
}
Also used : ClientExchange(io.undertow.client.ClientExchange) ClientResponse(io.undertow.client.ClientResponse) ClientCallback(io.undertow.client.ClientCallback) HttpStatus(org.springframework.http.HttpStatus) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) IOException(java.io.IOException) CloseStatus(org.springframework.web.socket.CloseStatus) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException)

Example 7 with CloseStatus

use of org.springframework.web.socket.CloseStatus in project spring-framework by spring-projects.

the class AbstractClientSockJsSession method handleOpenFrame.

private void handleOpenFrame() {
    if (logger.isDebugEnabled()) {
        logger.debug("Processing SockJS open frame in " + this);
    }
    if (this.state == State.NEW) {
        this.state = State.OPEN;
        try {
            this.webSocketHandler.afterConnectionEstablished(this);
            this.connectFuture.set(this);
        } catch (Throwable ex) {
            if (logger.isErrorEnabled()) {
                logger.error("WebSocketHandler.afterConnectionEstablished threw exception in " + this, ex);
            }
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Open frame received in " + getId() + " but we're not connecting (current state " + this.state + "). The server might have been restarted and lost track of the session.");
        }
        closeInternal(new CloseStatus(1006, "Server lost session"));
    }
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus)

Example 8 with CloseStatus

use of org.springframework.web.socket.CloseStatus in project spring-framework by spring-projects.

the class AbstractClientSockJsSession method handleCloseFrame.

private void handleCloseFrame(SockJsFrame frame) {
    CloseStatus closeStatus = CloseStatus.NO_STATUS_CODE;
    try {
        String[] data = getMessageCodec().decode(frame.getFrameData());
        if (data.length == 2) {
            closeStatus = new CloseStatus(Integer.valueOf(data[0]), data[1]);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Processing SockJS close frame with " + closeStatus + " in " + this);
        }
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Failed to decode data for " + frame + " in " + this, ex);
        }
    }
    closeInternal(closeStatus);
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus) IOException(java.io.IOException)

Example 9 with CloseStatus

use of org.springframework.web.socket.CloseStatus in project spring-framework by spring-projects.

the class RestTemplateXhrTransportTests method connectReceiveAndCloseWithStompFrame.

@Test
public void connectReceiveAndCloseWithStompFrame() throws Exception {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.SEND);
    accessor.setDestination("/destination");
    MessageHeaders headers = accessor.getMessageHeaders();
    Message<byte[]> message = MessageBuilder.createMessage("body".getBytes(StandardCharsets.UTF_8), headers);
    byte[] bytes = new StompEncoder().encode(message);
    TextMessage textMessage = new TextMessage(bytes);
    SockJsFrame frame = SockJsFrame.messageFrame(new Jackson2SockJsMessageCodec(), textMessage.getPayload());
    String body = "o\n" + frame.getContent() + "\n" + "c[3000,\"Go away!\"]";
    ClientHttpResponse response = response(HttpStatus.OK, body);
    connect(response);
    verify(this.webSocketHandler).afterConnectionEstablished(any());
    verify(this.webSocketHandler).handleMessage(any(), eq(textMessage));
    verify(this.webSocketHandler).afterConnectionClosed(any(), eq(new CloseStatus(3000, "Go away!")));
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : Jackson2SockJsMessageCodec(org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) CloseStatus(org.springframework.web.socket.CloseStatus) SockJsFrame(org.springframework.web.socket.sockjs.frame.SockJsFrame) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 10 with CloseStatus

use of org.springframework.web.socket.CloseStatus in project spring-framework by spring-projects.

the class ClientSockJsSessionTests method closeWithStatusOutOfRange.

@Test
public void closeWithStatusOutOfRange() throws Exception {
    this.session.handleFrame(SockJsFrame.openFrame().getContent());
    this.thrown.expect(IllegalArgumentException.class);
    this.thrown.expectMessage("Invalid close status");
    this.session.close(new CloseStatus(2999, "reason"));
}
Also used : CloseStatus(org.springframework.web.socket.CloseStatus) Test(org.junit.Test)

Aggregations

CloseStatus (org.springframework.web.socket.CloseStatus)13 Test (org.junit.Test)9 IOException (java.io.IOException)3 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)3 TextMessage (org.springframework.web.socket.TextMessage)3 ClientCallback (io.undertow.client.ClientCallback)1 ClientExchange (io.undertow.client.ClientExchange)1 ClientResponse (io.undertow.client.ClientResponse)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)1 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)1 SimpUser (org.springframework.messaging.simp.user.SimpUser)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1 Jackson2SockJsMessageCodec (org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec)1 SockJsFrame (org.springframework.web.socket.sockjs.frame.SockJsFrame)1 StreamSinkChannel (org.xnio.channels.StreamSinkChannel)1