Example usage for org.springframework.http HttpOutputMessage getBody

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

Introduction

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

Prototype

OutputStream getBody() throws IOException;

Source Link

Document

Return the body of the message as an output stream.

Usage

From source file:com.iflytek.edu.cloud.frame.spring.MappingJackson2HttpMessageConverterExt.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    outputMessage.getHeaders().setContentType(MediaType.parseMediaType("application/json"));
    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.getObjectMapper().getFactory().createGenerator(outputMessage.getBody(),
            encoding);/*from w  ww.  j  a  va2s  .c om*/

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.getObjectMapper().isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }

    String callBack = (String) RestContextHolder.getContext().getParam(Constants.SYS_PARAM_KEY_CALLBACK);
    try {
        if (StringUtils.hasText(callBack)) {
            String json = this.getObjectMapper().writeValueAsString(object);
            json = callBack + "( " + json + " )";
            outputMessage.getBody().write(json.getBytes(Charset.forName("UTF-8")));
            outputMessage.getBody().flush();
        } else {
            this.getObjectMapper().writeValue(jsonGenerator, object);
        }
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.ccntgrid.extend.spring.http.converter.json.MappingJackson2HttpMessageConverter.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);
    try {/*w w w.j  a  va2s .  com*/
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:sys.core.jackson.MappingJacksonHttpMessageConverter.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    System.out.println("Entro al writeInternal");
    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);
    try {// ww  w  .java  2s  . c o m
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:tuwien.aic.crowdsourcing.util.MappingJackson2HttpMessageConverter.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getFactory().createJsonGenerator(outputMessage.getBody(),
            encoding);/* w w w. ja  va2  s . c  o m*/
    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        if (this.prettyPrint) {
            jsonGenerator.useDefaultPrettyPrinter();
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.cloudfoundry.client.lib.util.StringHttpMessageConverterWithoutMediaType.java

@Override
protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
    if (writeAcceptCharset) {
        outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
    }// w  w w.j  a v a 2 s .co  m
    MediaType contentType = outputMessage.getHeaders().getContentType();
    Charset charset = contentType != null && contentType.getCharSet() != null ? contentType.getCharSet()
            : DEFAULT_CHARSET;
    FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset));
}

From source file:org.resthub.web.converter.MappingJackson2XmlHttpMessageConverter.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }/*from w ww  .j  av  a2 s  .  co m*/

    try {
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write XML: " + ex.getMessage(), ex);
    }
}

From source file:org.cloudfoundry.client.lib.util.UploadApplicationPayloadHttpMessageConverter.java

public void write(UploadApplicationPayload t, MediaType contentType, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    HttpHeaders headers = outputMessage.getHeaders();
    if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
        contentType = MediaType.APPLICATION_OCTET_STREAM;
    }//from w  w w  . ja  v  a 2 s  .c om
    if (contentType != null) {
        headers.setContentType(contentType);
    }
    FileCopyUtils.copy(t.getInputStream(), outputMessage.getBody());
    outputMessage.getBody().flush();
}

From source file:org.appverse.web.framework.backend.frontfacade.json.controllers.CustomMappingJacksonHttpMessageConverter.java

@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    JsonEncoding encoding = getEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);
    if (this.prefixJson) {
        jsonGenerator.writeRaw("{} && ");
    }/*from   w  w  w .j ava  2  s. c o m*/
    this.objectMapper.writeValue(jsonGenerator, o);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator baosjsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(baos, encoding);
    this.objectMapper.writeValue(baosjsonGenerator, o);
    logger.debug("Middleware returns " + o.getClass().getName() + " " + baos.toString());
}

From source file:org.resthub.web.converter.MappingJackson2JsonHttpMessageConverter.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }/*from  w  w  w  .  j ava2 s  .com*/

    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

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

/**
 * Write internal.//from   ww w.  j  a v  a  2 s . com
 * 
 * @param t
 *          the t
 * @param outputMessage
 *          the output message
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 * @throws HttpMessageNotWritableException
 *           the http message not writable exception
 * @see org.springframework.http.converter.AbstractHttpMessageConverter#writeInternal
 *      (java.lang.Object, org.springframework.http.HttpOutputMessage)
 */
@Override
protected void writeInternal(Object t, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    HttpHeaders headers = outputMessage.getHeaders();
    MediaType mediaType = headers.getContentType();
    Charset charset = mediaType.getCharSet();
    OutputStream output = outputMessage.getBody();
    serializer.serialize(t, output, charset);
}