String decoding for text only vs any MIME type

Follow-up to:
3d68c496f1

StringDecoder can be created in text-only vs "*/*" mode which in turn
allows a more intuitive order of client side decoders, e.g. SSE does
not have to be ahead of StringDecoder.

The commit also explicitly disables String from the supported types in
Jackson2Decoder leaving it to the StringDecoder in "*/*" mode which
comes after. This does not change the current arrangement since the
the StringDecoder ahead having "*/*" picks up JSON content just the
same.

From a broader perspective this change allows any decoder to deal with
String if it wants to after examining the content type be it the SSE
or another, custom decoder. For Jackson there is very little value in
decoding to String which works only if the output contains a single
JSON string but will fail to parse anything else (JSON object/array)
while StringDecoder in "*/*" mode will not fail.

Issue: SPR-15374
This commit is contained in:
Rossen Stoyanchev
2017-03-23 16:23:34 -04:00
parent a287e67992
commit 0662dbf044
17 changed files with 75 additions and 49 deletions

View File

@@ -58,21 +58,13 @@ public class StringDecoder extends AbstractDecoder<String> {
private final boolean splitOnNewline;
/**
* Create a {@code StringDecoder} that decodes a bytes stream to a String stream
* <p>By default, this decoder will split along new lines.
*/
public StringDecoder() {
this(true);
}
/**
* Create a {@code StringDecoder} that decodes a bytes stream to a String stream
* @param splitOnNewline whether this decoder should split the received data buffers
* along newline characters
*/
public StringDecoder(boolean splitOnNewline) {
super(new MimeType("text", "*", DEFAULT_CHARSET), MimeTypeUtils.ALL);
private StringDecoder(boolean splitOnNewline, MimeType... mimeTypes) {
super(mimeTypes);
this.splitOnNewline = splitOnNewline;
}
@@ -136,4 +128,22 @@ public class StringDecoder extends AbstractDecoder<String> {
}
}
/**
* Create a {@code StringDecoder} for {@code "text/plain"}.
* @param splitOnNewline whether to split the byte stream into lines
*/
public static StringDecoder textPlainOnly(boolean splitOnNewline) {
return new StringDecoder(splitOnNewline, new MimeType("text", "plain", DEFAULT_CHARSET));
}
/**
* Create a {@code StringDecoder} that supports all MIME types.
* @param splitOnNewline whether to split the byte stream into lines
*/
public static StringDecoder allMimeTypes(boolean splitOnNewline) {
return new StringDecoder(splitOnNewline,
new MimeType("text", "plain", DEFAULT_CHARSET), MimeTypeUtils.ALL);
}
}