Skip to content

Commit

Permalink
Merge pull request #118 from swisspost/use-apache-decoder
Browse files Browse the repository at this point in the history
use apache base64 decoder
  • Loading branch information
dominik-cnx authored May 12, 2023
2 parents a8d51a2 + 4e03b5e commit cc3d6c8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,6 +28,7 @@ public class HttpServerRequestUtil {
private static final String CONTENT_LENGTH = "Content-Length";
private static final String CONTENT_TYPE = "content-type";
private static final String APPLICATION_JSON = "application/json";
private static final Base64 apacheDecoder = new Base64();

/**
* Evaluates whether the provided request contains the provided url parameter and the value is either an empty
Expand Down Expand Up @@ -134,9 +136,9 @@ public static String decode(String encoded) {
String value = header.getString(1);
if (key.equalsIgnoreCase(CONTENT_TYPE) && (value.contains("text/") || value.contains(APPLICATION_JSON))) {
try {
object.put(PAYLOAD_OBJECT, new JsonObject(new String(object.getBinary(PAYLOAD), StandardCharsets.UTF_8)));
object.put(PAYLOAD_OBJECT, new JsonObject(new String(apacheDecoder.decode(object.getString(PAYLOAD)), StandardCharsets.UTF_8)));
} catch (DecodeException e) {
object.put(PAYLOAD_STRING, new String(object.getBinary(PAYLOAD), StandardCharsets.UTF_8));
object.put(PAYLOAD_STRING, new String(apacheDecoder.decode(object.getString(PAYLOAD)), StandardCharsets.UTF_8));
}
object.remove(PAYLOAD);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,39 @@ public void testExtractJsonArrayFromBody(TestContext context){
context.assertTrue(result.isErr());
context.assertEquals("no array called 'locks' found", result.getErr());
}

@Test
public void testBase64Decoder(TestContext context){
String testDataUrlEncoded = "{\n" +
" \"headers\": [\n" +
" [\n" +
" \"Content-Type\",\n" +
" \"application/json\"\n" +
" ],\n" +
" [\n" +
" \"Accept-Charset\",\n" +
" \"utf-8, iso-8859-1, utf-16, *;q=0.7\"\n" +
" ]\n" +
" ],\n" +
" \"queueTimestamp\": 1477983671291,\n" +
" \"payload\": \"5rWL6K-V\"\n" +
"}";
String testDataEncoded = "{\n" +
" \"headers\": [\n" +
" [\n" +
" \"Content-Type\",\n" +
" \"application/json\"\n" +
" ],\n" +
" [\n" +
" \"Accept-Charset\",\n" +
" \"utf-8, iso-8859-1, utf-16, *;q=0.7\"\n" +
" ]\n" +
" ],\n" +
" \"queueTimestamp\": 1477983671291,\n" +
" \"payload\": \"5rWL6K+V\"\n" +
"}";
String decodedUrlPayload = HttpServerRequestUtil.decode(testDataUrlEncoded);
String decodedPayload = HttpServerRequestUtil.decode(testDataEncoded);
context.assertEquals(decodedUrlPayload, decodedPayload);
}
}

0 comments on commit cc3d6c8

Please sign in to comment.