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);
}
}
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);
});
}
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);
}
});
}
}
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);
}
}
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);
}
}
Aggregations