Search in sources :

Example 1 with DecodingException

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

the class ProtobufDecoder method decode.

@Override
public Message decode(DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) throws DecodingException {
    try {
        Message.Builder builder = getMessageBuilder(targetType.toClass());
        ByteBuffer buffer = dataBuffer.asByteBuffer();
        builder.mergeFrom(CodedInputStream.newInstance(buffer), this.extensionRegistry);
        return builder.build();
    } catch (IOException ex) {
        throw new DecodingException("I/O error while parsing input stream", ex);
    } catch (Exception ex) {
        throw new DecodingException("Could not read Protobuf message: " + ex.getMessage(), ex);
    } finally {
        DataBufferUtils.release(dataBuffer);
    }
}
Also used : Message(com.google.protobuf.Message) DecodingException(org.springframework.core.codec.DecodingException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) DecodingException(org.springframework.core.codec.DecodingException) IOException(java.io.IOException) DataBufferLimitException(org.springframework.core.io.buffer.DataBufferLimitException)

Example 2 with DecodingException

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

the class DefaultPartHttpMessageReader method read.

@Override
public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
    return Flux.defer(() -> {
        byte[] boundary = boundary(message);
        if (boundary == null) {
            return Flux.error(new DecodingException("No multipart boundary found in Content-Type: \"" + message.getHeaders().getContentType() + "\""));
        }
        Flux<MultipartParser.Token> tokens = MultipartParser.parse(message.getBody(), boundary, this.maxHeadersSize, this.headersCharset);
        return PartGenerator.createParts(tokens, this.maxParts, this.maxInMemorySize, this.maxDiskUsagePerPart, this.streaming, this.fileStorage.directory(), this.blockingOperationScheduler);
    });
}
Also used : DecodingException(org.springframework.core.codec.DecodingException)

Example 3 with DecodingException

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

the class XmlEventDecoder method decode.

@Override
public Flux<XMLEvent> decode(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
    if (this.useAalto) {
        AaltoDataBufferToXmlEvent mapper = new AaltoDataBufferToXmlEvent(this.maxInMemorySize);
        return Flux.from(input).flatMapIterable(mapper).doFinally(signalType -> mapper.endOfInput());
    } else {
        return DataBufferUtils.join(input, this.maxInMemorySize).flatMapIterable(buffer -> {
            try {
                InputStream is = buffer.asInputStream();
                Iterator<Object> eventReader = inputFactory.createXMLEventReader(is);
                List<XMLEvent> result = new ArrayList<>();
                eventReader.forEachRemaining(event -> result.add((XMLEvent) event));
                return result;
            } catch (XMLStreamException ex) {
                throw new DecodingException(ex.getMessage(), ex);
            } finally {
                DataBufferUtils.release(buffer);
            }
        });
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) XMLEvent(javax.xml.stream.events.XMLEvent) ArrayList(java.util.ArrayList) DecodingException(org.springframework.core.codec.DecodingException)

Example 4 with DecodingException

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

the class Jaxb2XmlDecoder method unmarshal.

private Object unmarshal(List<XMLEvent> events, Class<?> outputClass) {
    try {
        Unmarshaller unmarshaller = initUnmarshaller(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 (UnmarshalException ex) {
        throw new DecodingException("Could not unmarshal XML to " + outputClass, ex);
    } catch (JAXBException ex) {
        throw new CodecException("Invalid JAXB configuration", ex);
    }
}
Also used : UnmarshalException(jakarta.xml.bind.UnmarshalException) JAXBException(jakarta.xml.bind.JAXBException) XMLEventReader(javax.xml.stream.XMLEventReader) DecodingException(org.springframework.core.codec.DecodingException) CodecException(org.springframework.core.codec.CodecException) Unmarshaller(jakarta.xml.bind.Unmarshaller)

Example 5 with DecodingException

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

the class Jaxb2XmlDecoder method decode.

@Override
public Object decode(DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) throws DecodingException {
    try {
        Iterator<Object> eventReader = inputFactory.createXMLEventReader(dataBuffer.asInputStream());
        List<XMLEvent> events = new ArrayList<>();
        eventReader.forEachRemaining(event -> events.add((XMLEvent) event));
        return unmarshal(events, targetType.toClass());
    } catch (XMLStreamException ex) {
        throw new DecodingException(ex.getMessage(), ex);
    } catch (Throwable ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof XMLStreamException) {
            throw new DecodingException(cause.getMessage(), cause);
        } else {
            throw Exceptions.propagate(ex);
        }
    } finally {
        DataBufferUtils.release(dataBuffer);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) ArrayList(java.util.ArrayList) DecodingException(org.springframework.core.codec.DecodingException)

Aggregations

DecodingException (org.springframework.core.codec.DecodingException)5 ArrayList (java.util.ArrayList)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 XMLEvent (javax.xml.stream.events.XMLEvent)2 Message (com.google.protobuf.Message)1 JAXBException (jakarta.xml.bind.JAXBException)1 UnmarshalException (jakarta.xml.bind.UnmarshalException)1 Unmarshaller (jakarta.xml.bind.Unmarshaller)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 CodecException (org.springframework.core.codec.CodecException)1 DataBufferLimitException (org.springframework.core.io.buffer.DataBufferLimitException)1