Search in sources :

Example 6 with SockJsTransportFailureException

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

the class JettyXhrTransport method executeRequest.

protected ResponseEntity<String> executeRequest(URI url, HttpMethod method, HttpHeaders headers, String body) {
    Request httpRequest = this.httpClient.newRequest(url).method(method);
    addHttpHeaders(httpRequest, headers);
    if (body != null) {
        httpRequest.content(new StringContentProvider(body));
    }
    ContentResponse response;
    try {
        response = httpRequest.send();
    } catch (Exception ex) {
        throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
    }
    HttpStatus status = HttpStatus.valueOf(response.getStatus());
    HttpHeaders responseHeaders = toHttpHeaders(response.getHeaders());
    return (response.getContent() != null ? new ResponseEntity<>(response.getContentAsString(), responseHeaders, status) : new ResponseEntity<>(responseHeaders, status));
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpStatus(org.springframework.http.HttpStatus) Request(org.eclipse.jetty.client.api.Request) SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) SockJsException(org.springframework.web.socket.sockjs.SockJsException)

Example 7 with SockJsTransportFailureException

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

the class UndertowXhrTransport method executeReceiveRequest.

private void executeReceiveRequest(final TransportRequest transportRequest, final URI url, final HttpHeaders headers, final XhrClientSockJsSession session, final SettableListenableFuture<WebSocketSession> connectFuture) {
    if (logger.isTraceEnabled()) {
        logger.trace("Starting XHR receive request for " + url);
    }
    ClientCallback<ClientConnection> clientCallback = new ClientCallback<ClientConnection>() {

        @Override
        public void completed(ClientConnection connection) {
            ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(url.getPath());
            HttpString headerName = HttpString.tryFromString(HttpHeaders.HOST);
            request.getRequestHeaders().add(headerName, url.getHost());
            addHttpHeaders(request, headers);
            HttpHeaders httpHeaders = transportRequest.getHttpRequestHeaders();
            connection.sendRequest(request, createReceiveCallback(transportRequest, url, httpHeaders, session, connectFuture));
        }

        @Override
        public void failed(IOException ex) {
            throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
        }
    };
    this.httpClient.connect(clientCallback, url, this.worker, this.bufferPool, this.optionMap);
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) ClientCallback(io.undertow.client.ClientCallback) HttpHeaders(org.springframework.http.HttpHeaders) ClientConnection(io.undertow.client.ClientConnection) IOException(java.io.IOException) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString)

Example 8 with SockJsTransportFailureException

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

the class WebSocketTransportHandler method handleRequest.

@Override
public void handleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {
    WebSocketServerSockJsSession sockJsSession = (WebSocketServerSockJsSession) wsSession;
    try {
        wsHandler = new SockJsWebSocketHandler(getServiceConfig(), wsHandler, sockJsSession);
        this.handshakeHandler.doHandshake(request, response, wsHandler, sockJsSession.getAttributes());
    } catch (Throwable ex) {
        sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
        throw new SockJsTransportFailureException("WebSocket handshake failure", wsSession.getId(), ex);
    }
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) WebSocketServerSockJsSession(org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession)

Example 9 with SockJsTransportFailureException

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

the class AbstractHttpSockJsSession method handleInitialRequest.

/**
	 * Handle the first request for receiving messages on a SockJS HTTP transport
	 * based session.
	 * <p>Long polling-based transports (e.g. "xhr", "jsonp") complete the request
	 * after writing the open frame. Streaming-based transports ("xhr_streaming",
	 * "eventsource", and "htmlfile") leave the response open longer for further
	 * streaming of message frames but will also close it eventually after some
	 * amount of data has been sent.
	 * @param request the current request
	 * @param response the current response
	 * @param frameFormat the transport-specific SocksJS frame format to use
	 */
public void handleInitialRequest(ServerHttpRequest request, ServerHttpResponse response, SockJsFrameFormat frameFormat) throws SockJsException {
    this.uri = request.getURI();
    this.handshakeHeaders = request.getHeaders();
    this.principal = request.getPrincipal();
    try {
        this.localAddress = request.getLocalAddress();
    } catch (Exception ex) {
    // Ignore
    }
    try {
        this.remoteAddress = request.getRemoteAddress();
    } catch (Exception ex) {
    // Ignore
    }
    synchronized (this.responseLock) {
        try {
            this.response = response;
            this.frameFormat = frameFormat;
            this.asyncRequestControl = request.getAsyncRequestControl(response);
            this.asyncRequestControl.start(-1);
            disableShallowEtagHeaderFilter(request);
            // Let "our" handler know before sending the open frame to the remote handler
            delegateConnectionEstablished();
            handleRequestInternal(request, response, true);
            // Request might have been reset (e.g. polling sessions do after writing)
            this.readyToSend = isActive();
        } catch (Throwable ex) {
            tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
            throw new SockJsTransportFailureException("Failed to open session", getId(), ex);
        }
    }
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) SockJsException(org.springframework.web.socket.sockjs.SockJsException) IOException(java.io.IOException)

Example 10 with SockJsTransportFailureException

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

the class HttpSendingTransportHandlerTests method testJsonpTransport.

private void testJsonpTransport(String callbackValue, boolean expectSuccess) throws Exception {
    JsonpPollingTransportHandler transportHandler = new JsonpPollingTransportHandler();
    transportHandler.initialize(this.sockJsConfig);
    PollingSockJsSession session = transportHandler.createSession("1", this.webSocketHandler, null);
    resetRequestAndResponse();
    setRequest("POST", "/");
    if (callbackValue != null) {
        // need to encode the query parameter
        this.servletRequest.setQueryString("c=" + UriUtils.encodeQueryParam(callbackValue, "UTF-8"));
        this.servletRequest.addParameter("c", callbackValue);
    }
    try {
        transportHandler.handleRequest(this.request, this.response, this.webSocketHandler, session);
    } catch (SockJsTransportFailureException ex) {
        if (expectSuccess) {
            throw new AssertionError("Unexpected transport failure", ex);
        }
    }
    if (expectSuccess) {
        assertEquals(200, this.servletResponse.getStatus());
        assertEquals("application/javascript;charset=UTF-8", this.response.getHeaders().getContentType().toString());
        verify(this.webSocketHandler).afterConnectionEstablished(session);
    } else {
        assertEquals(500, this.servletResponse.getStatus());
        verifyNoMoreInteractions(this.webSocketHandler);
    }
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) PollingSockJsSession(org.springframework.web.socket.sockjs.transport.session.PollingSockJsSession)

Aggregations

SockJsTransportFailureException (org.springframework.web.socket.sockjs.SockJsTransportFailureException)10 IOException (java.io.IOException)5 HttpHeaders (org.springframework.http.HttpHeaders)3 ClientConnection (io.undertow.client.ClientConnection)2 ClientRequest (io.undertow.client.ClientRequest)2 HttpString (io.undertow.util.HttpString)2 HttpStatus (org.springframework.http.HttpStatus)2 ResponseEntity (org.springframework.http.ResponseEntity)2 SockJsException (org.springframework.web.socket.sockjs.SockJsException)2 ClientCallback (io.undertow.client.ClientCallback)1 ClientResponse (io.undertow.client.ClientResponse)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 Request (org.eclipse.jetty.client.api.Request)1 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)1 Test (org.junit.Test)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1 PollingSockJsSession (org.springframework.web.socket.sockjs.transport.session.PollingSockJsSession)1 WebSocketServerSockJsSession (org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession)1