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