Search in sources :

Example 1 with SockJsTransportFailureException

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

the class HtmlFileTransportHandler method handleRequestInternal.

@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response, AbstractHttpSockJsSession sockJsSession) throws SockJsException {
    String callback = getCallbackParam(request);
    if (!StringUtils.hasText(callback)) {
        response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        try {
            response.getBody().write("\"callback\" parameter required".getBytes(StandardCharsets.UTF_8));
        } catch (IOException ex) {
            sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
            throw new SockJsTransportFailureException("Failed to write to response", sockJsSession.getId(), ex);
        }
        return;
    }
    super.handleRequestInternal(request, response, sockJsSession);
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) IOException(java.io.IOException)

Example 2 with SockJsTransportFailureException

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

the class JsonpPollingTransportHandler method handleRequestInternal.

@Override
public void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response, AbstractHttpSockJsSession sockJsSession) throws SockJsException {
    try {
        String callback = getCallbackParam(request);
        if (!StringUtils.hasText(callback)) {
            response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
            response.getBody().write("\"callback\" parameter required".getBytes(StandardCharsets.UTF_8));
            return;
        }
    } catch (Throwable ex) {
        sockJsSession.tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
        throw new SockJsTransportFailureException("Failed to send error", sockJsSession.getId(), ex);
    }
    super.handleRequestInternal(request, response, sockJsSession);
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException)

Example 3 with SockJsTransportFailureException

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

the class UndertowXhrTransport method executeRequest.

protected ResponseEntity<String> executeRequest(URI url, HttpString method, HttpHeaders headers, String body) {
    CountDownLatch latch = new CountDownLatch(1);
    List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    try {
        ClientConnection connection = this.httpClient.connect(url, this.worker, this.bufferPool, this.optionMap).get();
        try {
            ClientRequest request = new ClientRequest().setMethod(method).setPath(url.getPath());
            request.getRequestHeaders().add(HttpString.tryFromString(HttpHeaders.HOST), url.getHost());
            if (body != null && !body.isEmpty()) {
                HttpString headerName = HttpString.tryFromString(HttpHeaders.CONTENT_LENGTH);
                request.getRequestHeaders().add(headerName, body.length());
            }
            addHttpHeaders(request, headers);
            connection.sendRequest(request, createRequestCallback(body, responses, latch));
            latch.await();
            ClientResponse response = responses.iterator().next();
            HttpStatus status = HttpStatus.valueOf(response.getResponseCode());
            HttpHeaders responseHeaders = toHttpHeaders(response.getResponseHeaders());
            String responseBody = response.getAttachment(RESPONSE_BODY);
            return (responseBody != null ? new ResponseEntity<>(responseBody, responseHeaders, status) : new ResponseEntity<>(responseHeaders, status));
        } finally {
            IoUtils.safeClose(connection);
        }
    } catch (IOException ex) {
        throw new SockJsTransportFailureException("Failed to execute request to " + url, ex);
    } catch (InterruptedException ex) {
        throw new SockJsTransportFailureException("Interrupted while processing request to " + url, ex);
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) ResponseEntity(org.springframework.http.ResponseEntity) ClientConnection(io.undertow.client.ClientConnection) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HttpString(io.undertow.util.HttpString)

Example 4 with SockJsTransportFailureException

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

the class AbstractHttpSockJsSession method handleSuccessiveRequest.

/**
	 * Handle all requests, except the first one, to receive messages on a SockJS
	 * HTTP transport based session.
	 * <p>Long polling-based transports (e.g. "xhr", "jsonp") complete the request
	 * after writing any buffered message frames (or the next one). 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 handleSuccessiveRequest(ServerHttpRequest request, ServerHttpResponse response, SockJsFrameFormat frameFormat) throws SockJsException {
    synchronized (this.responseLock) {
        try {
            if (isClosed()) {
                response.getBody().write(SockJsFrame.closeFrameGoAway().getContentBytes());
                return;
            }
            this.response = response;
            this.frameFormat = frameFormat;
            this.asyncRequestControl = request.getAsyncRequestControl(response);
            this.asyncRequestControl.start(-1);
            disableShallowEtagHeaderFilter(request);
            handleRequestInternal(request, response, false);
            this.readyToSend = isActive();
        } catch (Throwable ex) {
            tryCloseWithSockJsTransportError(ex, CloseStatus.SERVER_ERROR);
            throw new SockJsTransportFailureException("Failed to handle SockJS receive request", getId(), ex);
        }
    }
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException)

Example 5 with SockJsTransportFailureException

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

the class SockJsSessionTests method writeFrameIoException.

@Test
public void writeFrameIoException() throws Exception {
    this.session.setExceptionOnWrite(new IOException());
    this.session.delegateConnectionEstablished();
    try {
        this.session.writeFrame(SockJsFrame.openFrame());
        fail("expected exception");
    } catch (SockJsTransportFailureException ex) {
        assertEquals(CloseStatus.SERVER_ERROR, this.session.getCloseStatus());
        verify(this.webSocketHandler).afterConnectionClosed(this.session, CloseStatus.SERVER_ERROR);
    }
}
Also used : SockJsTransportFailureException(org.springframework.web.socket.sockjs.SockJsTransportFailureException) IOException(java.io.IOException) Test(org.junit.Test)

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