Search in sources :

Example 1 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project spring-framework by spring-projects.

the class AbstractJackson2HttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    try {
        writePrefix(generator, object);
        Class<?> serializationView = null;
        FilterProvider filters = null;
        Object value = object;
        JavaType javaType = null;
        if (object instanceof MappingJacksonValue) {
            MappingJacksonValue container = (MappingJacksonValue) object;
            value = container.getValue();
            serializationView = container.getSerializationView();
            filters = container.getFilters();
        }
        if (type != null && value != null && TypeUtils.isAssignable(type, value.getClass())) {
            javaType = getJavaType(type, null);
        }
        ObjectWriter objectWriter;
        if (serializationView != null) {
            objectWriter = this.objectMapper.writerWithView(serializationView);
        } else if (filters != null) {
            objectWriter = this.objectMapper.writer(filters);
        } else {
            objectWriter = this.objectMapper.writer();
        }
        if (javaType != null && javaType.isContainerType()) {
            objectWriter = objectWriter.forType(javaType);
        }
        SerializationConfig config = objectWriter.getConfig();
        if (contentType != null && contentType.isCompatibleWith(MediaType.TEXT_EVENT_STREAM) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            objectWriter = objectWriter.with(this.ssePrettyPrinter);
        }
        objectWriter.writeValue(generator, value);
        writeSuffix(generator, object);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Example 2 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project spring-framework by spring-projects.

the class Jaxb2RootElementHttpMessageConverter method writeToResult.

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
    try {
        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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) MarshalException(javax.xml.bind.MarshalException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JAXBException(javax.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException)

Example 3 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project incubator-servicecomb-java-chassis by apache.

the class TestCseHttpMessageConverter method testAll.

@Test
public void testAll() {
    MockUtil.getInstance().mockReflectionUtils();
    MockUtil.getInstance().mockCseClientHttpRequest();
    CseHttpMessageConverter lCseHttpMessageConverter = new CseHttpMessageConverter();
    lCseHttpMessageConverter.canWrite(null, null);
    lCseHttpMessageConverter.getSupportedMediaTypes();
    try {
        lCseHttpMessageConverter.read(this.getClass(), null);
    } catch (HttpMessageNotReadableException | IOException ignored) {
    }
    try {
        HttpOutputMessage httpOutputMessage = Mockito.mock(CseClientHttpRequest.class);
        lCseHttpMessageConverter.write(null, null, httpOutputMessage);
    } catch (HttpMessageNotWritableException | IOException ignored) {
    }
    Assert.assertEquals(true, lCseHttpMessageConverter.canRead(null, null));
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpOutputMessage(org.springframework.http.HttpOutputMessage) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project ma-core-public by infiniteautomation.

the class ExceptionCsvMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
    try {
        CsvSchema schema;
        if (object instanceof AbstractRestV2Exception) {
            schema = this.restExceptionSchema;
            object = new CsvRestException((AbstractRestV2Exception) object);
        } else {
            schema = this.exceptionSchema;
        }
        writePrefix(generator, object);
        ObjectWriter objectWriter = this.objectMapper.writer().with(schema);
        objectWriter.writeValue(generator, object);
        writeSuffix(generator, object);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 5 with HttpMessageNotWritableException

use of org.springframework.http.converter.HttpMessageNotWritableException in project ma-modules-public by infiniteautomation.

the class PointValueTimeStreamCsvMessageConverter method writeInternal.

@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    MediaType contentType = outputMessage.getHeaders().getContentType();
    JsonEncoding encoding = getJsonEncoding(contentType);
    try {
        PointValueTimeStream<?, ?> stream = (PointValueTimeStream<?, ?>) object;
        stream.setContentType(StreamContentType.CSV);
        JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
        // Set the schema
        CsvSchema.Builder builder = CsvSchema.builder();
        builder.setUseHeader(true);
        // Setup our rendering parameters
        LatestQueryInfo info = stream.getQueryInfo();
        if (stream instanceof MultiPointTimeRangeDatabaseStream || stream instanceof MultiPointLatestDatabaseStream) {
            if (info.isSingleArray()) {
                if (info.isMultiplePointsPerArray()) {
                    Map<Integer, DataPointVO> voMap = stream.getVoMap();
                    Iterator<Integer> it = voMap.keySet().iterator();
                    boolean firstTimestamp = true;
                    while (it.hasNext()) {
                        String xid = voMap.get(it.next()).getXid();
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.TIMESTAMP) {
                                if (firstTimestamp)
                                    field.createColumn(builder, xid);
                                firstTimestamp = false;
                            } else
                                field.createColumn(builder, xid);
                        }
                    }
                } else {
                    for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                }
            } else {
                for (PointValueField field : info.getFields()) field.createColumn(builder, null);
            }
        } else if (stream instanceof MultiDataPointStatisticsQuantizerStream) {
            if (stream.getQueryInfo().isSingleArray()) {
                if (stream.getQueryInfo().isMultiplePointsPerArray()) {
                    Map<Integer, DataPointVO> voMap = stream.getVoMap();
                    Iterator<Integer> it = voMap.keySet().iterator();
                    boolean firstTimestamp = true;
                    while (it.hasNext()) {
                        String xid = voMap.get(it.next()).getXid();
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.TIMESTAMP) {
                                if (firstTimestamp)
                                    field.createColumn(builder, xid);
                                firstTimestamp = false;
                            } else if (field == PointValueField.VALUE) {
                                if (info.getRollup() == RollupEnum.ALL) {
                                    for (RollupEnum rollup : getAllRollups()) {
                                        builder.addColumn(xid + PointValueTimeWriter.DOT + rollup.name(), ColumnType.NUMBER_OR_STRING);
                                    }
                                } else {
                                    field.createColumn(builder, xid);
                                }
                            } else {
                                field.createColumn(builder, xid);
                            }
                        }
                    }
                } else {
                    // Single array
                    if (info.getRollup() == RollupEnum.ALL) {
                        for (RollupEnum rollup : getAllRollups()) {
                            builder.addColumn(rollup.name(), ColumnType.NUMBER_OR_STRING);
                        }
                        for (PointValueField field : info.getFields()) {
                            if (field == PointValueField.VALUE)
                                continue;
                            field.createColumn(builder, null);
                        }
                    } else {
                        for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                    }
                }
            } else {
                if (info.getRollup() == RollupEnum.ALL) {
                    for (RollupEnum rollup : getAllRollups()) {
                        builder.addColumn(rollup.name(), ColumnType.NUMBER_OR_STRING);
                    }
                    for (PointValueField field : info.getFields()) {
                        if (field == PointValueField.VALUE)
                            continue;
                        field.createColumn(builder, null);
                    }
                } else {
                    for (PointValueField field : info.getFields()) field.createColumn(builder, null);
                }
            }
        }
        generator.setSchema(builder.build());
        PointValueTimeWriter writer = new PointValueTimeCsvWriter(stream.getQueryInfo(), stream.getVoMap().size(), generator);
        stream.start(writer);
        stream.streamData(writer);
        stream.finish(writer);
        generator.flush();
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
    }
}
Also used : PointValueTimeStream(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeStream) MultiDataPointStatisticsQuantizerStream(com.infiniteautomation.mango.rest.v2.model.pointValue.quantize.MultiDataPointStatisticsQuantizerStream) LatestQueryInfo(com.infiniteautomation.mango.rest.v2.model.pointValue.query.LatestQueryInfo) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) PointValueTimeCsvWriter(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeCsvWriter) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) MultiPointTimeRangeDatabaseStream(com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointTimeRangeDatabaseStream) Iterator(java.util.Iterator) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) RollupEnum(com.serotonin.m2m2.web.mvc.rest.v1.model.time.RollupEnum) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MultiPointLatestDatabaseStream(com.infiniteautomation.mango.rest.v2.model.pointValue.query.MultiPointLatestDatabaseStream) DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) PointValueTimeWriter(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueTimeWriter) Map(java.util.Map) PointValueField(com.infiniteautomation.mango.rest.v2.model.pointValue.PointValueField)

Aggregations

HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)27 MediaType (org.springframework.http.MediaType)9 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)8 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 IOException (java.io.IOException)5 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)4 JsonEncoding (org.codehaus.jackson.JsonEncoding)4 JsonGenerator (org.codehaus.jackson.JsonGenerator)4 HttpOutputMessage (org.springframework.http.HttpOutputMessage)4 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 OutputStreamWriter (java.io.OutputStreamWriter)3 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)3 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)2 JsonIOException (com.google.gson.JsonIOException)2 Charset (java.nio.charset.Charset)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2