Example usage for org.springframework.http HttpInputMessage getBody

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

Introduction

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

Prototype

InputStream getBody() throws IOException;

Source Link

Document

Return the body of the message as an input stream.

Usage

From source file:com.wq.common.web.springmvc.MappingJackson2HttpMessageConverter.java

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

    JavaType javaType = getJavaType(clazz);
    try {/*from w ww . java2s.  c o m*/
        return this.objectMapper.readValue(inputMessage.getBody(), javaType);
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }
}

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

/**
 * Deserialize.//from   www.ja va2 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:com.github.cherimojava.data.spring.EntityConverter.java

@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    // as this method is only called after we decided that we can decode the requested type, we only need to check
    // what we have (plain entity/list of entities)
    if (type instanceof Class) {
        // simple class
        return factory.readEntity((Class<? extends Entity>) type, IOUtils.toString(inputMessage.getBody()));
    } else {/*from  ww w. j a  va2  s . c o m*/
        // collection
        return factory.readList(
                (Class<? extends Entity>) ((ParameterizedType) type).getActualTypeArguments()[0],
                IOUtils.toString(inputMessage.getBody()));
    }
}

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 w  w.  ja v a2  s  . c  o  m
        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:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java

@Test
public void testRead() throws Exception {

    // Setup//from w w  w .j a va  2  s  . co m
    JSOG expected = new JSOG("foobar");

    MediaType contentType = JSON_CONTENT_TYPE;

    HttpHeaders headers = createMock(HttpHeaders.class);
    expect(headers.getContentType()).andReturn(contentType);

    HttpInputMessage message = createMock(HttpInputMessage.class);
    expect(message.getHeaders()).andReturn(headers);

    expect(message.getBody()).andReturn(new ByteArrayInputStream(expected.toString().getBytes("ISO-8859-1")));

    // Execute
    replay(headers, message);
    JSOG actual = instance.read(JSOG.class, message);

    // Verify
    verify(message);
    assertEquals(expected, actual);
}

From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java

@Test
public void testReadObject() throws Exception {

    // Setup//from  w  w w . j  a va  2s  .  c o m
    JSOG expected = JSOG.object("foo", "bar");

    MediaType contentType = JSON_CONTENT_TYPE;

    HttpHeaders headers = createMock(HttpHeaders.class);
    expect(headers.getContentType()).andReturn(contentType);

    HttpInputMessage message = createMock(HttpInputMessage.class);
    expect(message.getHeaders()).andReturn(headers);

    expect(message.getBody()).andReturn(new ByteArrayInputStream(expected.toString().getBytes("ISO-8859-1")));

    // Execute
    replay(headers, message);
    JSOG actual = instance.read(JSOG.class, message);

    // Verify
    verify(message);
    assertEquals(expected, actual);
}

From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java

@Test
public void testReadArray() throws Exception {

    // Setup/*www  .ja v  a  2s .c om*/
    JSOG expected = JSOG.array("foo", "bar");

    MediaType contentType = JSON_CONTENT_TYPE;

    HttpHeaders headers = createMock(HttpHeaders.class);
    expect(headers.getContentType()).andReturn(contentType);

    HttpInputMessage message = createMock(HttpInputMessage.class);
    expect(message.getHeaders()).andReturn(headers);

    expect(message.getBody()).andReturn(new ByteArrayInputStream(expected.toString().getBytes("ISO-8859-1")));

    // Execute
    replay(headers, message);
    JSOG actual = instance.read(JSOG.class, message);

    // Verify
    verify(message);
    assertEquals(expected, actual);
}

From source file:net.sf.jsog.spring.StringJsogHttpMessageConverterTest.java

@Test(expected = HttpMessageNotReadableException.class)
public void testReadBad() throws Exception {

    // Setup//from  w w w.j  a  v  a2  s. c om
    MediaType contentType = JSON_CONTENT_TYPE;

    HttpHeaders headers = createMock(HttpHeaders.class);
    expect(headers.getContentType()).andReturn(contentType);

    HttpInputMessage message = createMock(HttpInputMessage.class);
    expect(message.getHeaders()).andReturn(headers);

    expect(message.getBody()).andReturn(new ByteArrayInputStream("[".toString().getBytes("ISO-8859-1")));

    // Execute
    replay(headers, message);
    try {
        instance.read(JSOG.class, message);
    } finally {

        // Verify
        verify(message);
    }
}

From source file:net.sf.jsog.spring.StringJsogHttpMessageConverter.java

@Override
public JSOG read(Class<? extends JSOG> clazz, HttpInputMessage input)
        throws IOException, HttpMessageNotReadableException {
    HttpHeaders headers = input.getHeaders();
    MediaType contentType = headers.getContentType();
    Charset encoding = contentType.getCharSet();

    if (encoding == null) {
        encoding = this.encoding;
    }/*from w  w  w .j  a  v  a2  s .  co  m*/

    // Read in the JSON
    String json = IOUtils.toString(input.getBody(), encoding.name());

    // Parse the JSON and return a JSOG.
    try {
        return JSOG.parse(json);
    } catch (IOException e) {
        throw new HttpMessageNotReadableException("Unable to parse JSON.", e);
    }
}

From source file:com.fiadot.springjsoncrypt.json.CryptMappingJacson2HttpMessageConverter.java

private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
    try {/* ww  w  .ja v a 2  s.  co  m*/
        CipherDecryptUtils cryptoUtil = new CipherDecryptUtils("AES", "AES/CBC/PKCS7Padding",
                "ls4h+XaXU+A5m72HRpwkeQ==", "W46YspHuEiQlKDcLTqoySw==");

        BufferedReader in = new BufferedReader(new InputStreamReader(inputMessage.getBody(), "UTF-8"));

        String raw_message = in.readLine();

        //byte[] data = new byte[4096]; 
        //inputMessage.getBody().read(data);

        //String raw_message = data.toString();
        logger.info("encoded=" + raw_message);

        String decoded_str = cryptoUtil.decrypt(raw_message);
        logger.info("decoded=" + decoded_str);

        ReqDto obj = this.objectMapper.readValue(decoded_str, ReqDto.class);
        // mapper.readValue(str_res, AppHashAddRes.class);
        //Object obj =  this.objectMapper.readValue(decoded_str, javaType); 

        return obj;

        // TODO : make input stream 
        //InputStream inputStream = new ByteArrayInputStream(encStr.getBytes());
        //return this.objectMapper.readValue(inputStream, javaType);
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    } catch (Exception ex) {
        throw new HttpMessageNotReadableException("Could not read JSON(crypto): " + ex.getMessage(), ex);
    }
}