Example usage for org.springframework.http.server ServerHttpResponse getBody

List of usage examples for org.springframework.http.server ServerHttpResponse getBody

Introduction

In this page you can find the example usage for org.springframework.http.server ServerHttpResponse getBody.

Prototype

OutputStream getBody() throws IOException;

Source Link

Document

Return the body of the message as an output stream.

Usage

From source file:org.springframework.web.socket.sockjs.support.AbstractSockJsService.java

/**
 * This method determines the SockJS path and handles SockJS static URLs.
 * Session URLs and raw WebSocket requests are delegated to abstract methods.
 *//*from   w  w w .  j  a v  a2 s . co m*/
@Override
public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
        @Nullable String sockJsPath, WebSocketHandler wsHandler) throws SockJsException {

    if (sockJsPath == null) {
        if (logger.isWarnEnabled()) {
            logger.warn("Expected SockJS path. Failing request: " + request.getURI());
        }
        response.setStatusCode(HttpStatus.NOT_FOUND);
        return;
    }

    try {
        request.getHeaders();
    } catch (InvalidMediaTypeException ex) {
        // As per SockJS protocol content-type can be ignored (it's always json)
    }

    String requestInfo = (logger.isDebugEnabled() ? request.getMethod() + " " + request.getURI() : null);

    try {
        if (sockJsPath.equals("") || sockJsPath.equals("/")) {
            if (requestInfo != null) {
                logger.debug("Processing transport request: " + requestInfo);
            }
            response.getHeaders().setContentType(new MediaType("text", "plain", StandardCharsets.UTF_8));
            response.getBody().write("Welcome to SockJS!\n".getBytes(StandardCharsets.UTF_8));
        }

        else if (sockJsPath.equals("/info")) {
            if (requestInfo != null) {
                logger.debug("Processing transport request: " + requestInfo);
            }
            this.infoHandler.handle(request, response);
        }

        else if (sockJsPath.matches("/iframe[0-9-.a-z_]*.html")) {
            if (!this.allowedOrigins.isEmpty() && !this.allowedOrigins.contains("*")) {
                if (requestInfo != null) {
                    logger.debug("Iframe support is disabled when an origin check is required. "
                            + "Ignoring transport request: " + requestInfo);
                }
                response.setStatusCode(HttpStatus.NOT_FOUND);
                return;
            }
            if (this.allowedOrigins.isEmpty()) {
                response.getHeaders().add(XFRAME_OPTIONS_HEADER, "SAMEORIGIN");
            }
            if (requestInfo != null) {
                logger.debug("Processing transport request: " + requestInfo);
            }
            this.iframeHandler.handle(request, response);
        }

        else if (sockJsPath.equals("/websocket")) {
            if (isWebSocketEnabled()) {
                if (requestInfo != null) {
                    logger.debug("Processing transport request: " + requestInfo);
                }
                handleRawWebSocketRequest(request, response, wsHandler);
            } else if (requestInfo != null) {
                logger.debug("WebSocket disabled. Ignoring transport request: " + requestInfo);
            }
        }

        else {
            String[] pathSegments = StringUtils.tokenizeToStringArray(sockJsPath.substring(1), "/");
            if (pathSegments.length != 3) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Invalid SockJS path '" + sockJsPath + "' - required to have 3 path segments");
                }
                if (requestInfo != null) {
                    logger.debug("Ignoring transport request: " + requestInfo);
                }
                response.setStatusCode(HttpStatus.NOT_FOUND);
                return;
            }

            String serverId = pathSegments[0];
            String sessionId = pathSegments[1];
            String transport = pathSegments[2];

            if (!isWebSocketEnabled() && transport.equals("websocket")) {
                if (requestInfo != null) {
                    logger.debug("WebSocket disabled. Ignoring transport request: " + requestInfo);
                }
                response.setStatusCode(HttpStatus.NOT_FOUND);
                return;
            } else if (!validateRequest(serverId, sessionId, transport) || !validatePath(request)) {
                if (requestInfo != null) {
                    logger.debug("Ignoring transport request: " + requestInfo);
                }
                response.setStatusCode(HttpStatus.NOT_FOUND);
                return;
            }

            if (requestInfo != null) {
                logger.debug("Processing transport request: " + requestInfo);
            }
            handleTransportRequest(request, response, wsHandler, sessionId, transport);
        }
        response.close();
    } catch (IOException ex) {
        throw new SockJsException("Failed to write to the response", null, ex);
    }
}

From source file:org.springframework.web.socket.sockjs.transport.AbstractHttpReceivingTransportHandler.java

protected void sendInternalServerError(ServerHttpResponse response, String error, String sessionId)
        throws TransportErrorException {

    try {/*from   w  ww.j a va2  s  .  c  om*/
        response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        response.getBody().write(error.getBytes("UTF-8"));
    } catch (Throwable t) {
        throw new TransportErrorException("Failed to send error message to client", t, sessionId);
    }
}

From source file:org.springframework.web.socket.sockjs.transport.AbstractHttpSendingTransportHandler.java

protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
        AbstractHttpSockJsSession httpServerSession) throws TransportErrorException {

    if (httpServerSession.isNew()) {
        logger.debug("Opening " + getTransportType() + " connection");
        httpServerSession.setInitialRequest(request, response, getFrameFormat(request));
    } else if (!httpServerSession.isActive()) {
        logger.debug("starting " + getTransportType() + " async request");
        httpServerSession.setLongPollingRequest(request, response, getFrameFormat(request));
    } else {//from   w  w w . j a  v  a 2s . c o m
        try {
            logger.debug("another " + getTransportType() + " connection still open: " + httpServerSession);
            SockJsFrame closeFrame = SockJsFrame.closeFrameAnotherConnectionOpen();
            response.getBody().write(getFrameFormat(request).format(closeFrame).getContentBytes());
        } catch (IOException e) {
            throw new TransportErrorException("Failed to send SockJS close frame", e,
                    httpServerSession.getId());
        }
    }
}