private Mono<?> decodeData(String data, ResolvableType dataType, Map<String, Object> hints) { if (String.class == dataType.resolve()) { return Mono.just(data.substring(0, data.length() - 1)); } if (this.decoder == null) { return Mono.error(new CodecException("No SSE decoder configured and the data is not String.")); } byte[] bytes = data.getBytes(StandardCharsets.UTF_8); Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes)); return this.decoder.decodeToMono(input, dataType, MediaType.TEXT_EVENT_STREAM, hints); }
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); } }
private Mono<?> decodeData(String data, ResolvableType dataType, Map<String, Object> hints) { if (String.class == dataType.resolve()) { return Mono.just(data.substring(0, data.length() - 1)); } if (this.decoder == null) { return Mono.error(new CodecException("No SSE decoder configured and the data is not String.")); } byte[] bytes = data.getBytes(StandardCharsets.UTF_8); Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes)); return this.decoder.decodeToMono(input, dataType, MediaType.TEXT_EVENT_STREAM, hints); }
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); } }
@SuppressWarnings("unchecked") private <T> Flux<DataBuffer> encodeData(@Nullable T data, ResolvableType valueType, MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints) { if (data == null) { return Flux.empty(); } if (data instanceof String) { String text = (String) data; return Flux.from(encodeText(StringUtils.replace(text, "\n", "\ndata:") + "\n", mediaType, factory)); } if (this.encoder == null) { return Flux.error(new CodecException("No SSE encoder configured and the data is not String.")); } return ((Encoder<T>) this.encoder) .encode(Mono.just(data), factory, valueType, mediaType, hints) .concatWith(encodeText("\n", mediaType, factory)); }
return Flux.error(new CodecException("Invalid JAXB configuration", ex));
throw new CodecException("Type definition error: " + ex.getType(), ex);
return Flux.error(new CodecException("No suitable writer found for part: " + name));
@SuppressWarnings("unchecked") private <T> Flux<DataBuffer> encodeData(@Nullable T data, ResolvableType valueType, MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints) { if (data == null) { return Flux.empty(); } if (data instanceof String) { String text = (String) data; return Flux.from(encodeText(StringUtils.replace(text, "\n", "\ndata:") + "\n", mediaType, factory)); } if (this.encoder == null) { return Flux.error(new CodecException("No SSE encoder configured and the data is not String.")); } return ((Encoder<T>) this.encoder) .encode(Mono.just(data), factory, valueType, mediaType, hints) .concatWith(encodeText("\n", mediaType, factory)); }
return Mono.error(new CodecException("Type definition error: " + ex.getType(), ex));
throw new CodecException("Type definition error: " + ex.getType(), ex);
return Flux.error(new CodecException("Invalid JAXB configuration", ex));
throw new CodecException("Type definition error: " + ex.getType(), ex);
return Flux.error(new CodecException("No suitable writer found for part: " + name));
private Mono<?> decodeData(String data, ResolvableType dataType, Map<String, Object> hints) { if (String.class == dataType.resolve()) { return Mono.just(data.substring(0, data.length() - 1)); } if (this.decoder == null) { return Mono.error(new CodecException("No SSE decoder configured and the data is not String.")); } byte[] bytes = data.getBytes(StandardCharsets.UTF_8); Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes)); return this.decoder.decodeToMono(input, dataType, MediaType.TEXT_EVENT_STREAM, hints); }
private Mono<?> decodeData(String data, ResolvableType dataType, Map<String, Object> hints) { if (String.class == dataType.resolve()) { return Mono.just(data.substring(0, data.length() - 1)); } if (this.decoder == null) { return Mono.error(new CodecException("No SSE decoder configured and the data is not String.")); } byte[] bytes = data.getBytes(StandardCharsets.UTF_8); Mono<DataBuffer> input = Mono.just(bufferFactory.wrap(bytes)); return this.decoder.decodeToMono(input, dataType, MediaType.TEXT_EVENT_STREAM, hints); }
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); } }
@Override protected Flux<DataBuffer> encode(Object value, DataBufferFactory dataBufferFactory, ResolvableType type, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { try { DataBuffer buffer = dataBufferFactory.allocateBuffer(1024); OutputStream outputStream = buffer.asOutputStream(); Class<?> clazz = ClassUtils.getUserClass(value); Marshaller marshaller = this.jaxbContexts.createMarshaller(clazz); marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name()); marshaller.marshal(value, outputStream); return Flux.just(buffer); } catch (MarshalException ex) { return Flux.error(new EncodingException("Could not marshal " + value.getClass() + " to XML", ex)); } catch (JAXBException ex) { return Flux.error(new CodecException("Invalid JAXB configuration", ex)); } }
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 (UnmarshalException ex) { throw new DecodingException("Could not unmarshal XML to " + outputClass, ex); } catch (JAXBException ex) { throw new CodecException("Invalid JAXB configuration", ex); } }
private DataBuffer encodeValue(Object value, @Nullable MimeType mimeType, DataBufferFactory bufferFactory, ResolvableType elementType, @Nullable Map<String, Object> hints, JsonEncoding encoding) { JavaType javaType = getJavaType(elementType.getType(), null); Class<?> jsonView = (hints != null ? (Class<?>) hints.get(Jackson2CodecSupport.JSON_VIEW_HINT) : null); ObjectWriter writer = (jsonView != null ? getObjectMapper().writerWithView(jsonView) : getObjectMapper().writer()); if (javaType.isContainerType()) { writer = writer.forType(javaType); } writer = customizeWriter(writer, mimeType, elementType, hints); DataBuffer buffer = bufferFactory.allocateBuffer(); OutputStream outputStream = buffer.asOutputStream(); try { JsonGenerator generator = getObjectMapper().getFactory().createGenerator(outputStream, encoding); writer.writeValue(generator, value); } catch (InvalidDefinitionException ex) { throw new CodecException("Type definition error: " + ex.getType(), ex); } catch (JsonProcessingException ex) { throw new EncodingException("JSON encoding error: " + ex.getOriginalMessage(), ex); } catch (IOException ex) { throw new IllegalStateException("Unexpected I/O error while writing to data buffer", ex); } return buffer; }