Example usage for org.springframework.http HttpInputMessage getHeaders

List of usage examples for org.springframework.http HttpInputMessage getHeaders

Introduction

In this page you can find the example usage for org.springframework.http HttpInputMessage getHeaders.

Prototype

HttpHeaders getHeaders();

Source Link

Document

Return the headers of this message.

Usage

From source file:org.ayfaar.app.spring.handler.RestExceptionHandler.java

@SuppressWarnings("unchecked")
protected 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  w  w .j  a  v  a2s  . com

    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:org.nobel.highriseapi.mapper.SimpleXmlHttpMessageConverter.java

@Override
protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {

    Reader source = new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders()));

    try {//from  w w  w. j  ava  2s  .  com
        Object result = this.serializer.read(clazz, source);
        if (!clazz.isInstance(result)) {
            throw new TypeMismatchException(result, clazz);
        }
        return result;
    } catch (Exception ex) {
        throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", ex);
    }
}

From source file:nl.flotsam.calendar.web.UriHttpMessageConverter.java

@Override
protected URI readInternal(Class<? extends URI> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    InputStream in = null;/*ww w  .j  a v  a  2 s  .  c  om*/
    try {
        in = inputMessage.getBody();
        String uri = IOUtils.toString(in);
        if (inputMessage.getHeaders().getContentType() == MediaType.APPLICATION_FORM_URLENCODED) {
            uri = URLDecoder.decode(uri, "UTF-8");
        }
        return new URI(uri);
    } catch (URISyntaxException urie) {
        throw new HttpMessageNotReadableException("Failed to parse incoming String into a URI.", urie);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.epam.ta.reportportal.commons.exception.rest.RestExceptionHandler.java

@SuppressWarnings({ "rawtypes", "unchecked", "resource" })
private ModelAndView handleResponseBody(Object body, ServletWebRequest webRequest)
        throws HttpMessageNotWritableException, 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  .  c  o  m*/

    MediaType.sortByQualityValue(acceptedMediaTypes);

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

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

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

    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:org.wcy123.ProtobufMessageConverter.java

@Override
protected Message readInternal(Class<? extends Message> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {

    MediaType contentType = inputMessage.getHeaders().getContentType();
    contentType = (contentType != null ? contentType : PROTOBUF);

    Charset charset = getCharset(inputMessage.getHeaders());
    InputStreamReader reader = new InputStreamReader(inputMessage.getBody(), charset);

    try {//from  w  ww  .  j  a v  a 2  s .c om
        Message.Builder builder = getMessageBuilder(clazz);

        if (MediaType.APPLICATION_JSON.isCompatibleWith(contentType)) {
            parser.merge(reader, builder);
        } else if (MediaType.TEXT_PLAIN.isCompatibleWith(contentType)) {
            TextFormat.merge(reader, this.extensionRegistry, builder);
        } else if (MediaType.APPLICATION_XML.isCompatibleWith(contentType)) {
            throw new UnsupportedOperationException("not supported yet");
        } else {
            builder.mergeFrom(inputMessage.getBody(), this.extensionRegistry);
        }
        return builder.build();
    } catch (Exception e) {
        throw new HttpMessageNotReadableException("Could not read Protobuf message: " + e.getMessage(), e);
    }
}

From source file:org.cloudfoundry.identity.uaa.error.ConvertingExceptionView.java

@SuppressWarnings("unchecked")
private void writeWithMessageConverters(Object returnValue, HttpInputMessage inputMessage,
        HttpOutputMessage outputMessage) throws IOException, HttpMediaTypeNotAcceptableException {
    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }//  w w  w.  j a  va2s.c o m
    MediaType.sortByQualityValue(acceptedMediaTypes);
    Class<?> returnValueType = returnValue.getClass();
    List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
    if (messageConverters != null) {
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            for (@SuppressWarnings("rawtypes")
            HttpMessageConverter messageConverter : messageConverters) {
                if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
                    messageConverter.write(returnValue, acceptedMediaType, outputMessage);
                    if (logger.isDebugEnabled()) {
                        MediaType contentType = outputMessage.getHeaders().getContentType();
                        if (contentType == null) {
                            contentType = acceptedMediaType;
                        }
                        logger.debug("Written [" + returnValue + "] as \"" + contentType + "\" using ["
                                + messageConverter + "]");
                    }
                    // this.responseArgumentUsed = true;
                    return;
                }
            }
        }
        for (@SuppressWarnings("rawtypes")
        HttpMessageConverter messageConverter : messageConverters) {
            allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
        }
    }
    throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}

From source file:com.cookbook.cq.dao.util.GsonHttpMessageConverter.java

@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {

    Reader json = new InputStreamReader(inputMessage.getBody(), getCharset(inputMessage.getHeaders()));

    try {//from w w w  .j  av  a2s.  c  o  m
        Type typeOfT = getType();
        if (typeOfT != null) {
            return this.gson.fromJson(json, typeOfT);
        } else {
            return this.gson.fromJson(json, clazz);
        }
    } catch (JsonSyntaxException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    } catch (JsonIOException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    } catch (JsonParseException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }
}

From source file:com.p5solutions.core.json.JsonHttpMessageConverter.java

/**
 * Deserialize.//from ww  w.  jav  a  2  s.  c  om
 * 
 * @param clazz
 *          the clazz
 * @param bindObject
 *          the bind object
 * @param inputMessage
 *          the input message
 * @return the object
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unchecked")
protected Object deserialize(Class<?> clazz, Object bindObject, WebRequest webRequest, WebDataBinder binder,
        HttpInputMessage inputMessage) throws IOException {

    ///

    HttpHeaders headers = inputMessage.getHeaders();
    MediaType mediaType = headers.getContentType();
    Charset charset = mediaType.getCharSet();
    InputStream input = inputMessage.getBody();
    Object value = deserializer.deserialize((Class<Object>) clazz, bindObject, webRequest, binder, input,
            charset);

    return value;
}

From source file:it.unitn.disi.smatch.web.server.api.handlers.ExceptionDetailsExceptionResolver.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  .  ja  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 (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.wisemapping.rest.DebugMappingJacksonHttpMessageConverter.java

@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
        throws IOException, JsonHttpMessageNotReadableException {
    try {/*ww w .j  a va  2 s .com*/
        final byte[] bytes = IOUtils.toByteArray(inputMessage.getBody());
        final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        final WrapHttpInputMessage wrap = new WrapHttpInputMessage(bais, inputMessage.getHeaders());

        return super.readInternal(clazz, wrap);

    } catch (org.springframework.http.converter.HttpMessageNotReadableException e) {
        throw new JsonHttpMessageNotReadableException("Request Body could not be read", e);
    } catch (IOException e) {
        throw new JsonHttpMessageNotReadableException("Request Body could not be read", e);
    }
}