Add WebFlux support for Smile streaming
The commit brings following changes: - Move getDecodableMimeTypes() to AbstractJackson2Decoder - Move getEncodableMimeTypes() to AbstractJackson2Encoder - Add support for application/stream+x-jackson-smile - Avoid streaming line separator when Smile encoder is used - Use double null token in Jackson2Tokenizer to identify documents Issue: SPR-16151
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.http.codec.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
@@ -133,6 +134,13 @@ public abstract class AbstractJackson2Decoder extends Jackson2CodecSupport imple
|
||||
return getHints(actualType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MimeType> getDecodableMimeTypes() {
|
||||
return getMimeTypes();
|
||||
}
|
||||
|
||||
// Jackson2CodecSupport ...
|
||||
|
||||
@Override
|
||||
protected <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType) {
|
||||
return parameter.getParameterAnnotation(annotType);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -60,6 +60,8 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
|
||||
|
||||
protected final List<MediaType> streamingMediaTypes = new ArrayList<>(1);
|
||||
|
||||
protected boolean streamingLineSeparator = true;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor with a Jackson {@link ObjectMapper} to use.
|
||||
@@ -104,7 +106,9 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
|
||||
else if (this.streamingMediaTypes.stream().anyMatch(mediaType -> mediaType.isCompatibleWith(mimeType))) {
|
||||
return Flux.from(inputStream).map(value -> {
|
||||
DataBuffer buffer = encodeValue(value, mimeType, bufferFactory, elementType, hints);
|
||||
buffer.write(new byte[]{'\n'});
|
||||
if (streamingLineSeparator) {
|
||||
buffer.write(new byte[]{'\n'});
|
||||
}
|
||||
return buffer;
|
||||
});
|
||||
}
|
||||
@@ -156,6 +160,11 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
|
||||
|
||||
// HttpMessageEncoder...
|
||||
|
||||
@Override
|
||||
public List<MimeType> getEncodableMimeTypes() {
|
||||
return getMimeTypes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MediaType> getStreamingMediaTypes() {
|
||||
return Collections.unmodifiableList(this.streamingMediaTypes);
|
||||
@@ -168,6 +177,8 @@ public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport imple
|
||||
return (actualType != null ? getHints(actualType) : Collections.emptyMap());
|
||||
}
|
||||
|
||||
// Jackson2CodecSupport ...
|
||||
|
||||
@Override
|
||||
protected <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType) {
|
||||
return parameter.getMethodAnnotation(annotType);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.http.codec.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
@@ -42,10 +40,4 @@ public class Jackson2JsonDecoder extends AbstractJackson2Decoder {
|
||||
super(mapper, mimeTypes);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MimeType> getDecodableMimeTypes() {
|
||||
return getMimeTypes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -44,7 +44,7 @@ import org.springframework.util.MimeType;
|
||||
* @see Jackson2JsonDecoder
|
||||
*/
|
||||
public class Jackson2JsonEncoder extends AbstractJackson2Encoder {
|
||||
|
||||
|
||||
@Nullable
|
||||
private final PrettyPrinter ssePrettyPrinter;
|
||||
|
||||
@@ -76,9 +76,4 @@ public class Jackson2JsonEncoder extends AbstractJackson2Encoder {
|
||||
writer.with(this.ssePrettyPrinter) : writer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MimeType> getEncodableMimeTypes() {
|
||||
return getMimeTypes();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.http.codec.json;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -38,11 +40,13 @@ import org.springframework.util.MimeType;
|
||||
*/
|
||||
public class Jackson2SmileDecoder extends AbstractJackson2Decoder {
|
||||
|
||||
private static final MimeType SMILE_MIME_TYPE = new MediaType("application", "x-jackson-smile");
|
||||
private static final MimeType[] DEFAULT_SMILE_MIME_TYPES = new MimeType[] {
|
||||
new MimeType("application", "x-jackson-smile", StandardCharsets.UTF_8),
|
||||
new MimeType("application", "*+x-jackson-smile", StandardCharsets.UTF_8)};
|
||||
|
||||
|
||||
public Jackson2SmileDecoder() {
|
||||
this(Jackson2ObjectMapperBuilder.smile().build(), SMILE_MIME_TYPE);
|
||||
this(Jackson2ObjectMapperBuilder.smile().build(), DEFAULT_SMILE_MIME_TYPES);
|
||||
}
|
||||
|
||||
public Jackson2SmileDecoder(ObjectMapper mapper, MimeType... mimeTypes) {
|
||||
@@ -50,9 +54,4 @@ public class Jackson2SmileDecoder extends AbstractJackson2Decoder {
|
||||
Assert.isAssignable(SmileFactory.class, mapper.getFactory().getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MimeType> getDecodableMimeTypes() {
|
||||
return Collections.singletonList(SMILE_MIME_TYPE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.http.codec.json;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -39,23 +40,20 @@ import org.springframework.util.MimeType;
|
||||
*/
|
||||
public class Jackson2SmileEncoder extends AbstractJackson2Encoder {
|
||||
|
||||
private static final MimeType SMILE_MIME_TYPE = new MediaType("application", "x-jackson-smile");
|
||||
private static final MimeType[] DEFAULT_SMILE_MIME_TYPES = new MimeType[] {
|
||||
new MimeType("application", "x-jackson-smile", StandardCharsets.UTF_8),
|
||||
new MimeType("application", "*+x-jackson-smile", StandardCharsets.UTF_8)};
|
||||
|
||||
|
||||
public Jackson2SmileEncoder() {
|
||||
this(Jackson2ObjectMapperBuilder.smile().build(), new MediaType("application", "x-jackson-smile"));
|
||||
this(Jackson2ObjectMapperBuilder.smile().build(), DEFAULT_SMILE_MIME_TYPES);
|
||||
}
|
||||
|
||||
public Jackson2SmileEncoder(ObjectMapper mapper, MimeType... mimeTypes) {
|
||||
super(mapper, mimeTypes);
|
||||
Assert.isAssignable(SmileFactory.class, mapper.getFactory().getClass());
|
||||
this.streamingMediaTypes.add(new MediaType("application", "stream+x-jackson-smile"));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<MimeType> getEncodableMimeTypes() {
|
||||
return Collections.singletonList(SMILE_MIME_TYPE);
|
||||
this.streamingLineSeparator = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -125,7 +125,9 @@ class Jackson2Tokenizer {
|
||||
|
||||
while (true) {
|
||||
JsonToken token = this.parser.nextToken();
|
||||
if (token == null || token == JsonToken.NOT_AVAILABLE) {
|
||||
// SPR-16151: Smile data format uses null to separate documents
|
||||
if ((token == JsonToken.NOT_AVAILABLE) ||
|
||||
(token == null && (token = this.parser.nextToken()) == null)) {
|
||||
break;
|
||||
}
|
||||
updateDepth(token);
|
||||
|
||||
Reference in New Issue
Block a user