Example usage for org.springframework.http.converter HttpMessageNotWritableException HttpMessageNotWritableException

List of usage examples for org.springframework.http.converter HttpMessageNotWritableException HttpMessageNotWritableException

Introduction

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

Prototype

public HttpMessageNotWritableException(String msg, @Nullable Throwable cause) 

Source Link

Document

Create a new HttpMessageNotWritableException.

Usage

From source file:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.java

@SuppressWarnings("deprecation")
@Override// w ww  .  j  a  v  a2s . c om
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();
    }

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

From source file:demo.SourceHttpMessageConverter.java

@Override
protected void writeInternal(T t, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    try {//from   ww  w  .  java  2s  .c o m
        Result result = new StreamResult(outputMessage.getBody());
        transform(t, result);
    } catch (TransformerException ex) {
        throw new HttpMessageNotWritableException("Could not transform [" + t + "] to output message", ex);
    }
}

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

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

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    // The following has been deprecated as late as Jackson 2.2 (April 2013);
    // preserved for the time being, for Jackson 2.0/2.1 compatibility.
    @SuppressWarnings("deprecation")
    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();
    }/*  w  w w .j  ava 2s. c  om*/

    try {
        if (this.jsonPrefix != null) {
            jsonGenerator.writeRaw(this.jsonPrefix);
        }

        // original source
        // jsonGenerator.
        //this.objectMapper.writeValue(jsonGenerator, object);

        CipherEncryptUtils cryptoUtil = new CipherEncryptUtils("AES", "AES/CBC/PKCS7Padding",
                "ls4h+XaXU+A5m72HRpwkeQ==", "W46YspHuEiQlKDcLTqoySw==");
        String encStr = null;
        try {
            encStr = cryptoUtil.encrypt(this.objectMapper.writeValueAsString(object));
            logger.info("MessageMapper::WriteInternal() encStr=" + encStr);
        } catch (Exception e) {

        }

        outputMessage.getBody().write(encStr.getBytes());
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.alfresco.repo.publishing.JaxbHttpMessageConverter.java

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
    try {//from  www.  j  a v a2  s . com
        Class<?> clazz = ClassUtils.getUserClass(o);
        Marshaller marshaller = createMarshaller(clazz);
        setCharset(headers.getContentType(), marshaller);
        marshaller.marshal(o, result);
    } catch (MarshalException ex) {
        throw new HttpMessageNotWritableException("Could not marshal [" + o + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}

From source file:org.craftercms.commons.jackson.mvc.CrafterJackson2MessageConverter.java

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

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.getObjectMapper().getFactory().createGenerator(outputMessage.getBody(),
            encoding);//from   w w w.jav a 2 s  .c o m
    // 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();
    }

    try {
        if (this.jsonPrefix != null) {
            jsonGenerator.writeRaw(this.jsonPrefix);
        }

        runAnnotations(object);

        ObjectWriter writer = this.getObjectMapper().writer(filter);
        writer.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.molgenis.util.GsonHttpMessageConverter.java

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

    OutputStreamWriter writer = new OutputStreamWriter(outputMessage.getBody(),
            getCharset(outputMessage.getHeaders()));

    String callback = getCallbackParam();
    if (callback != null) {
        // this is a JSONP (JSON with padding) request
        writer.append(callback).append('(');
    }/* w  w  w. j  a  va2  s. c o  m*/

    try {
        Type typeOfSrc = getType();

        if (LOG.isTraceEnabled()) {
            StringBuilder sb = new StringBuilder();
            if (this.prefixJson) {
                sb.append("{} && ");
            }

            if (typeOfSrc != null) {
                sb.append(gson.toJson(o, typeOfSrc));
            } else {
                sb.append(gson.toJson(o));
            }

            LOG.debug("Json response:\n" + sb.toString());
            writer.write(sb.toString());
        } else {
            if (this.prefixJson) {
                writer.append("{} && ");
            }

            if (typeOfSrc != null) {
                this.gson.toJson(o, typeOfSrc, writer);
            } else {
                this.gson.toJson(o, writer);
            }
        }

        if (callback != null) {
            // this is a JSONP (JSON with padding) request
            writer.append(')');
        }
    } catch (JsonIOException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(writer);
    }

}

From source file:org.springframework.flex.http.AmfHttpMessageConverter.java

private void writeActionMessage(ActionMessage message, HttpOutputMessage outputMessage, AmfTrace trace)
        throws IOException {
    AmfMessageSerializer serializer = new AmfMessageSerializer();
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    serializer.setVersion(message.getVersion());
    serializer.initialize(new SerializationContext(), outBuffer, trace);

    try {/*from  w  ww.  j a v a2s.  c  om*/
        ActionContext context = new ActionContext();
        context.setVersion(message.getVersion());
        context.setResponseMessage(message);
        serializer.writeMessage(message);
        outBuffer.flush();
        outBuffer.close();
        outputMessage.getHeaders().setContentLength(outBuffer.size());
        outBuffer.writeTo(outputMessage.getBody());
    } catch (SerializationException se) {
        throw new HttpMessageNotWritableException("Could not write " + message + " as AMF message.", se);
    }
}

From source file:org.springframework.flex.http.AmfHttpMessageConverter.java

private void writeObject(Object data, HttpOutputMessage outputMessage, AmfTrace trace) throws IOException {
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    Amf3Output serializer = new Amf3Output(new SerializationContext());
    serializer.setOutputStream(outBuffer);
    serializer.setDebugTrace(trace);//from   w w w  .  j  a  v a2  s . com
    try {
        serializer.writeObject(data);
        outBuffer.flush();
        outBuffer.close();
        outputMessage.getHeaders().setContentLength(outBuffer.size());
        outBuffer.writeTo(outputMessage.getBody());
    } catch (SerializationException se) {
        throw new HttpMessageNotWritableException("Could not write " + data + " as AMF message.", se);
    }
}