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);
}
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);
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations