Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
import static io.servicetalk.utils.internal.CharsetUtils.standardCharsets;
import static java.util.Collections.emptyMap;

final class FormUrlEncodedSerializer implements SerializerDeserializer<Map<String, List<String>>> {
/**
* Serialize/deserialize key-values {@link Map}s to/from urlencoded forms.
*/
public final class FormUrlEncodedSerializer implements SerializerDeserializer<Map<String, List<String>>> {
private static final Map<Charset, byte[]> CONTINUATIONS_SEPARATORS;
private static final Map<Charset, byte[]> KEYVALUE_SEPARATORS;
static {
Expand All @@ -62,7 +65,7 @@ final class FormUrlEncodedSerializer implements SerializerDeserializer<Map<Strin
private final byte[] continuationSeparator;
private final byte[] keyValueSeparator;

FormUrlEncodedSerializer(final Charset charset) {
private FormUrlEncodedSerializer(final Charset charset) {
this.charset = charset;
byte[] continuationSeparator = CONTINUATIONS_SEPARATORS.get(charset);
if (continuationSeparator == null) {
Expand All @@ -74,6 +77,16 @@ final class FormUrlEncodedSerializer implements SerializerDeserializer<Map<Strin
}
}

/**
* Create a new instance.
*
* @param charset The charset used for encoding.
* @return A serializer that uses {@code charset} for encoding.
*/
public static SerializerDeserializer<Map<String, List<String>>> formUrlEncodedSerializer(final Charset charset) {
return new FormUrlEncodedSerializer(charset);
}

@Override
public Map<String, List<String>> deserialize(final Buffer serializedData, final BufferAllocator allocator) {
return deserialize(serializedData, charset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public final class HttpSerializers {
newAsciiString(APPLICATION_TEXT_VARINT + "; charset=US-ASCII");

private static final HttpSerializerDeserializer<Map<String, List<String>>> FORM_ENCODED_UTF_8 =
new DefaultHttpSerializerDeserializer<>(new FormUrlEncodedSerializer(UTF_8),
new DefaultHttpSerializerDeserializer<>(FormUrlEncodedSerializer.formUrlEncodedSerializer(UTF_8),
headers -> headers.set(CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED_UTF_8)
.set(ACCEPT, APPLICATION_X_WWW_FORM_URLENCODED),
headers -> hasContentType(headers, APPLICATION_X_WWW_FORM_URLENCODED, UTF_8));
Expand Down Expand Up @@ -130,7 +130,7 @@ public static HttpSerializerDeserializer<Map<String, List<String>>> formUrlEncod
}
final CharSequence contentType = newAsciiString(APPLICATION_X_WWW_FORM_URLENCODED + "; charset=" +
charset.name());
return new DefaultHttpSerializerDeserializer<>(new FormUrlEncodedSerializer(charset),
return new DefaultHttpSerializerDeserializer<>(FormUrlEncodedSerializer.formUrlEncodedSerializer(charset),
headers -> headers.set(CONTENT_TYPE, contentType)
.set(ACCEPT, APPLICATION_X_WWW_FORM_URLENCODED),
headers -> hasContentType(headers, APPLICATION_X_WWW_FORM_URLENCODED, charset));
Expand Down
Loading