Search in sources :

Example 1 with CodecException

use of org.springframework.core.codec.CodecException in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReader method decodeData.

private Object decodeData(String data, ResolvableType dataType, Map<String, Object> hints) {
    if (String.class.isAssignableFrom(dataType.getRawClass())) {
        return data.substring(0, data.length() - 1);
    }
    byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
    Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes));
    return this.dataDecoders.stream().filter(e -> e.canDecode(dataType, MimeTypeUtils.APPLICATION_JSON)).findFirst().orElseThrow(() -> new CodecException("No suitable decoder found!")).decodeToMono(input, dataType, MimeTypeUtils.APPLICATION_JSON, hints).block(Duration.ZERO);
}
Also used : CodecException(org.springframework.core.codec.CodecException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 2 with CodecException

use of org.springframework.core.codec.CodecException in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageWriter method applyEncoder.

@SuppressWarnings("unchecked")
private <T> Flux<DataBuffer> applyEncoder(Object data, DataBufferFactory bufferFactory, Map<String, Object> hints) {
    ResolvableType elementType = ResolvableType.forClass(data.getClass());
    Optional<Encoder<?>> encoder = dataEncoders.stream().filter(e -> e.canEncode(elementType, MimeTypeUtils.APPLICATION_JSON)).findFirst();
    return ((Encoder<T>) encoder.orElseThrow(() -> new CodecException("No suitable encoder found!"))).encode(Mono.just((T) data), bufferFactory, elementType, MimeTypeUtils.APPLICATION_JSON, hints).concatWith(encodeString("\n", bufferFactory));
}
Also used : Publisher(org.reactivestreams.Publisher) MediaType(org.springframework.http.MediaType) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ArrayList(java.util.ArrayList) CodecException(org.springframework.core.codec.CodecException) Flux(reactor.core.publisher.Flux) List(java.util.List) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) Map(java.util.Map) Optional(java.util.Optional) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) Encoder(org.springframework.core.codec.Encoder) Assert(org.springframework.util.Assert) Encoder(org.springframework.core.codec.Encoder) CodecException(org.springframework.core.codec.CodecException) ResolvableType(org.springframework.core.ResolvableType)

Example 3 with CodecException

use of org.springframework.core.codec.CodecException in project spring-framework by spring-projects.

the class Jaxb2XmlDecoder method unmarshal.

private Object unmarshal(List<XMLEvent> events, Class<?> outputClass) {
    try {
        Unmarshaller unmarshaller = this.jaxbContexts.createUnmarshaller(outputClass);
        XMLEventReader eventReader = StaxUtils.createXMLEventReader(events);
        if (outputClass.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(eventReader);
        } else {
            JAXBElement<?> jaxbElement = unmarshaller.unmarshal(eventReader, outputClass);
            return jaxbElement.getValue();
        }
    } catch (JAXBException ex) {
        throw new CodecException(ex.getMessage(), ex);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) CodecException(org.springframework.core.codec.CodecException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 4 with CodecException

use of org.springframework.core.codec.CodecException in project spring-framework by spring-projects.

the class Jackson2JsonDecoder method decodeInternal.

private Flux<Object> decodeInternal(JsonObjectDecoder objectDecoder, Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
    Assert.notNull(inputStream, "'inputStream' must not be null");
    Assert.notNull(elementType, "'elementType' must not be null");
    MethodParameter methodParam = (elementType.getSource() instanceof MethodParameter ? (MethodParameter) elementType.getSource() : null);
    Class<?> contextClass = (methodParam != null ? methodParam.getContainingClass() : null);
    JavaType javaType = getJavaType(elementType.getType(), contextClass);
    ObjectReader reader;
    Class<?> jsonView = (Class<?>) hints.get(AbstractJackson2Codec.JSON_VIEW_HINT);
    if (jsonView != null) {
        reader = this.mapper.readerWithView(jsonView).forType(javaType);
    } else {
        reader = this.mapper.readerFor(javaType);
    }
    return objectDecoder.decode(inputStream, elementType, mimeType, hints).map(dataBuffer -> {
        try {
            Object value = reader.readValue(dataBuffer.asInputStream());
            DataBufferUtils.release(dataBuffer);
            return value;
        } catch (IOException ex) {
            throw new CodecException("Error while reading the data", ex);
        }
    });
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) CodecException(org.springframework.core.codec.CodecException) IOException(java.io.IOException) MethodParameter(org.springframework.core.MethodParameter)

Example 5 with CodecException

use of org.springframework.core.codec.CodecException in project spring-framework by spring-projects.

the class Jackson2JsonEncoder method encodeValue.

private DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType type, Map<String, Object> hints) {
    TypeFactory typeFactory = this.mapper.getTypeFactory();
    JavaType javaType = typeFactory.constructType(type.getType());
    if (type.isInstance(value)) {
        javaType = getJavaType(type.getType(), null);
    }
    ObjectWriter writer;
    Class<?> jsonView = (Class<?>) hints.get(AbstractJackson2Codec.JSON_VIEW_HINT);
    if (jsonView != null) {
        writer = this.mapper.writerWithView(jsonView);
    } else {
        writer = this.mapper.writer();
    }
    if (javaType != null && javaType.isContainerType()) {
        writer = writer.forType(javaType);
    }
    Boolean sse = (Boolean) hints.get(ServerSentEventHttpMessageWriter.SSE_CONTENT_HINT);
    SerializationConfig config = writer.getConfig();
    if (Boolean.TRUE.equals(sse) && config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        writer = writer.with(this.ssePrettyPrinter);
    }
    DataBuffer buffer = bufferFactory.allocateBuffer();
    OutputStream outputStream = buffer.asOutputStream();
    try {
        writer.writeValue(outputStream, value);
    } catch (IOException ex) {
        throw new CodecException("Error while writing the data", ex);
    }
    return buffer;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) CodecException(org.springframework.core.codec.CodecException) IOException(java.io.IOException) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

CodecException (org.springframework.core.codec.CodecException)5 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 JavaType (com.fasterxml.jackson.databind.JavaType)2 IOException (java.io.IOException)2 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 OutputStream (java.io.OutputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 Publisher (org.reactivestreams.Publisher)1