Example usage for org.springframework.http HttpInputMessage HttpInputMessage

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

Introduction

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

Prototype

HttpInputMessage

Source Link

Usage

From source file:org.geoserver.opensearch.rest.AbstractOpenSearchController.java

@SuppressWarnings("unchecked")
protected <T> T parseJSON(Class<T> clazz, byte[] rawData) throws IOException {
    T links = (T) jsonConverter.read(clazz, new HttpInputMessage() {

        @Override/*from w  w  w. j a  v  a  2  s.c  o m*/
        public HttpHeaders getHeaders() {
            return new HttpHeaders();
        }

        @Override
        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream(rawData);
        }
    });
    return links;
}

From source file:org.springframework.data.keyvalue.riak.core.AbstractRiakTemplate.java

@SuppressWarnings({ "unchecked" })
protected <T> RiakValue<T> extractValue(final ResponseEntity<?> response, Class<?> origType,
        Class<T> requiredType) throws IOException {
    if (response.hasBody()) {
        RiakMetaData meta = extractMetaData(response.getHeaders());
        Object o = response.getBody();
        if (!origType.equals(requiredType)) {
            if (conversionService.canConvert(origType, requiredType)) {
                o = conversionService.convert(o, requiredType);
            } else {
                if (o instanceof byte[] || o instanceof String) {
                    // Peek inside, see if it's a string of something we recognize
                    String s = (o instanceof byte[] ? new String((byte[]) o) : (String) o);
                    if (s.charAt(0) == '{' || s.charAt(0) == '[') {
                        // Looks like it might be a JSON string. Use the JSON converter
                        for (HttpMessageConverter conv : getRestTemplate().getMessageConverters()) {
                            if (conv instanceof MappingJacksonHttpMessageConverter) {
                                o = conv.read(requiredType, new HttpInputMessage() {
                                    public InputStream getBody() throws IOException {
                                        Object body = response.getBody();
                                        return new ByteArrayInputStream((body instanceof byte[] ? (byte[]) body
                                                : ((String) body).getBytes()));
                                    }/*w  w w.  j ava  2s .c o m*/

                                    public HttpHeaders getHeaders() {
                                        return response.getHeaders();
                                    }
                                });
                                break;
                            }
                        }

                    }
                } else {
                    throw new DataStoreOperationException(
                            "Cannot convert object of type " + origType + " to type " + requiredType);
                }
            }
        }
        return new RiakValue<T>((T) o, meta);
    }
    return null;
}