Example usage for org.springframework.http.server ServletServerHttpRequest ServletServerHttpRequest

List of usage examples for org.springframework.http.server ServletServerHttpRequest ServletServerHttpRequest

Introduction

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

Prototype

public ServletServerHttpRequest(HttpServletRequest servletRequest) 

Source Link

Document

Construct a new instance of the ServletServerHttpRequest based on the given HttpServletRequest .

Usage

From source file:com.oolong.platform.web.error.RestExceptionHandler.java

@SuppressWarnings("unchecked")
private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest)
        throws ServletException, IOException {

    HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());

    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }//from   ww w  . j  a va 2 s. c  o  m

    MediaType.sortByQualityValue(acceptedMediaTypes);

    HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());

    Class<?> bodyType = body.getClass();

    List<HttpMessageConverter<?>> converters = this.allMessageConverters;

    if (converters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (HttpMessageConverter messageConverter : converters) {
                if (messageConverter.canWrite(bodyType, acceptedMediaType)) {
                    messageConverter.write(body, acceptedMediaType, outputMessage);
                    // return empty model and view to short circuit the
                    // iteration and to let
                    // Spring know that we've rendered the view ourselves:
                    return new ModelAndView();
                }
            }
        }
    }

    if (logger.isWarnEnabled()) {
        logger.warn("Could not find HttpMessageConverter that supports return type [" + bodyType + "] and "
                + acceptedMediaTypes);
    }
    return null;
}

From source file:com.novation.eligibility.rest.spring.web.servlet.handler.RestExceptionHandler.java

@SuppressWarnings("unchecked")
private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest)
        throws ServletException, IOException {

    HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());

    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }/* w ww  .ja v  a 2 s  .c  om*/

    MediaType.sortByQualityValue(acceptedMediaTypes);

    HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());

    Class<?> bodyType = body.getClass();

    List<HttpMessageConverter<?>> converters = this.allMessageConverters;

    if (converters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (HttpMessageConverter messageConverter : converters) {
                if (messageConverter.canWrite(bodyType, acceptedMediaType)) {
                    messageConverter.write(body, acceptedMediaType, outputMessage);
                    //return empty model and view to short circuit the iteration and to let
                    //Spring know that we've rendered the view ourselves:
                    return new ModelAndView();
                }
            }
        }
    }

    if (logger.isWarnEnabled()) {
        logger.warn("Could not find HttpMessageConverter that supports return type [" + bodyType + "] and "
                + acceptedMediaTypes);
    }
    return null;
}

From source file:com.yang.oa.commons.exception.RestExceptionHandler.java

@SuppressWarnings("unchecked")
private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest)
        throws ServletException, IOException {

    HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());

    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }//w w w. j av a2 s  . co m

    MediaType.sortByQualityValue(acceptedMediaTypes);

    HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());

    Class<?> bodyType = body.getClass();

    List<HttpMessageConverter<?>> converters = this.allMessageConverters;

    if (converters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (HttpMessageConverter messageConverter : converters) {
                if (messageConverter.canWrite(bodyType, acceptedMediaType)) {
                    messageConverter.write(body, acceptedMediaType, outputMessage);
                    //return empty model and view to short circuit the iteration and to let
                    //Spring know that we've rendered the view ourselves:
                    return new ModelAndView();
                }
            }
        }
    }

    return null;
}

From source file:org.jlot.web.api.error.RestExceptionHandler.java

@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest)
        throws ServletException, IOException {

    HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());

    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }//from  www. j a v a  2s. c  om

    MediaType.sortByQualityValue(acceptedMediaTypes);

    HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());

    Class<?> bodyType = body.getClass();

    List<HttpMessageConverter<?>> converters = this.allMessageConverters;

    if (converters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (HttpMessageConverter messageConverter : converters) {
                if (messageConverter.canWrite(bodyType, acceptedMediaType)) {
                    messageConverter.write(body, acceptedMediaType, outputMessage);
                    // return empty model and view to short circuit the
                    // iteration and to let
                    // Spring know that we've rendered the view ourselves:

                    return new ModelAndView();
                }
            }
        }
    }

    if (logger.isWarnEnabled()) {
        logger.warn("Could not find HttpMessageConverter that supports return type [" + bodyType + "] and "
                + acceptedMediaTypes);
    }
    return null;
}

From source file:com.sti.rest.error.RestExceptionHandler.java

@SuppressWarnings({ "unchecked", "resource" })
private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest)
        throws ServletException, IOException {

    HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());

    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }//from w  ww . j  a v  a  2  s  .c o m

    MediaType.sortByQualityValue(acceptedMediaTypes);

    HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());

    Class<?> bodyType = body.getClass();

    List<HttpMessageConverter<?>> converters = this.allMessageConverters;

    if (converters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (@SuppressWarnings("rawtypes")
            HttpMessageConverter messageConverter : converters) {
                if (messageConverter.canWrite(bodyType, acceptedMediaType)) {
                    messageConverter.write(body, acceptedMediaType, outputMessage);
                    //return empty model and view to short circuit the iteration and to let
                    //Spring know that we've rendered the view ourselves:
                    return new ModelAndView();
                }
            }
        }
    }

    if (logger.isWarnEnabled()) {
        logger.warn("Could not find HttpMessageConverter that supports return type [" + bodyType + "] and "
                + acceptedMediaTypes);
    }

    return null;
}

From source file:de.zib.vold.userInterface.RESTController.java

MultiValueMap<String, String> getBody() throws IOException {
    ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes();
    HttpServletRequest request = requestAttributes.getRequest();

    if (request.getContentLength() <= 0)
        return null;

    HttpInputMessage inputMessage = new ServletServerHttpRequest(request);

    return (MultiValueMap<String, String>) converter.read(LinkedMultiValueMap.class, inputMessage);
}

From source file:morph.plugin.views.annotation.AnnotationMethodHandlerAdapter.java

/**
 * Template method for creating a new HttpInputMessage instance.
 * <p>The default implementation creates a standard {@link ServletServerHttpRequest}.
 * This can be overridden for custom {@code HttpInputMessage} implementations
 * @param servletRequest current HTTP request
 * @return the HttpInputMessage instance to use
 * @throws Exception in case of errors//from   w  w w .ja va  2s.c om
 */
protected HttpInputMessage createHttpInputMessage(HttpServletRequest servletRequest) throws Exception {
    return new ServletServerHttpRequest(servletRequest);
}

From source file:org.infoscoop.api.oauth2.provider.ISOAuth2ExceptionRenderer.java

private HttpInputMessage createHttpInputMessage(NativeWebRequest webRequest) throws Exception {
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    return new ServletServerHttpRequest(servletRequest);
}

From source file:org.springframework.flex.security3.FlexAuthenticationEntryPoint.java

/**
 * If the incoming message is an {@link ActionMessage}, indicating a standard Flex Remoting or Messaging 
 * request, invokes Spring BlazeDS's {@link ExceptionTranslator}s with the {@link AuthenticationException} and 
 * sends the resulting {@link MessageException} as an AMF response to the client.
 * /*from   ww  w.  ja v  a 2  s. c  o m*/
 * <p>If the request is unabled to be deserialized to AMF, if the resulting deserialized object is not an 
 * <code>ActionMessage</code>, or if no appropriate <code>ExceptionTranslator</code> is found, will simply 
 * delegate to the parent class to return a 403 response.
 */
public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException {

    if (CollectionUtils.isEmpty(this.exceptionTranslators)) {
        exceptionTranslators = Collections.singleton(DEFAULT_TRANSLATOR);
    }

    HttpInputMessage inputMessage = new ServletServerHttpRequest(request);
    HttpOutputMessage outputMessage = new ServletServerHttpResponse(response);

    if (!converter.canRead(Object.class, inputMessage.getHeaders().getContentType())) {
        super.commence(request, response, authException);
        return;
    }

    ActionMessage deserializedInput = null;
    try {
        deserializedInput = (ActionMessage) this.converter.read(ActionMessage.class, inputMessage);
    } catch (HttpMessageNotReadableException ex) {
        log.info("Authentication failure detected, but request could not be read as AMF.", ex);
        super.commence(request, response, authException);
        return;
    }

    if (deserializedInput instanceof ActionMessage) {
        for (ExceptionTranslator translator : this.exceptionTranslators) {
            if (translator.handles(authException.getClass())) {
                MessageException result = translator.translate(authException);
                ErrorMessage err = result.createErrorMessage();
                MessageBody body = (MessageBody) ((ActionMessage) deserializedInput).getBody(0);
                Message amfInputMessage = body.getDataAsMessage();
                err.setCorrelationId(amfInputMessage.getMessageId());
                err.setDestination(amfInputMessage.getDestination());
                err.setClientId(amfInputMessage.getClientId());
                ActionMessage responseMessage = new ActionMessage();
                responseMessage.setVersion(((ActionMessage) deserializedInput).getVersion());
                MessageBody responseBody = new MessageBody();
                responseMessage.addBody(responseBody);
                responseBody.setData(err);
                responseBody.setTargetURI(body.getResponseURI());
                responseBody.setReplyMethod(MessageIOConstants.STATUS_METHOD);
                converter.write(responseMessage, amfMediaType, outputMessage);
                response.flushBuffer();
                return;
            }
        }
    }
    super.commence(request, response, authException);
}

From source file:org.springframework.web.cors.DefaultCorsProcessor.java

@Override
@SuppressWarnings("resource")
public boolean processRequest(@Nullable CorsConfiguration config, HttpServletRequest request,
        HttpServletResponse response) throws IOException {

    if (!CorsUtils.isCorsRequest(request)) {
        return true;
    }/*from  w w  w .j av a 2s.com*/

    ServletServerHttpResponse serverResponse = new ServletServerHttpResponse(response);
    if (responseHasCors(serverResponse)) {
        logger.debug("Skip CORS processing: response already contains \"Access-Control-Allow-Origin\" header");
        return true;
    }

    ServletServerHttpRequest serverRequest = new ServletServerHttpRequest(request);
    if (WebUtils.isSameOrigin(serverRequest)) {
        logger.debug("Skip CORS processing: request is from same origin");
        return true;
    }

    boolean preFlightRequest = CorsUtils.isPreFlightRequest(request);
    if (config == null) {
        if (preFlightRequest) {
            rejectRequest(serverResponse);
            return false;
        } else {
            return true;
        }
    }

    return handleInternal(serverRequest, serverResponse, config, preFlightRequest);
}