Skip to content

Commit

Permalink
feat: add methods for binary content of messages
Browse files Browse the repository at this point in the history
  • Loading branch information
phiz71 committed Jun 3, 2024
1 parent 068ca6c commit 707519e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ content.greeting = 'Hello Universe!'
return JsonOutput.toJson(content)
----

The content of a message can also be accessed directly:

- as a base64 string with `message.contentAsBase64`
- as an array of bytes with `message.contentAsByteArray`

== Errors

=== Sandbox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.gravitee.gateway.api.http.HttpHeaders;
import io.gravitee.gateway.reactive.api.message.Message;
import io.gravitee.policy.groovy.model.BindableHttpHeaders;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -111,6 +112,14 @@ public String getContent() {
return message.content() == null ? "" : content().toString();
}

public String getContentAsBase64() {
return message.content() == null ? "" : Base64.getEncoder().encodeToString(message.content().getBytes());
}

public byte[] getContentAsByteArray() {
return message.content() == null ? new byte[0] : message.content().getBytes();
}

@Override
public Message content(Buffer content) {
throw new UnsupportedOperationException("Setting content must be done returning a value and setting `overrideContent` to true");
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/io/gravitee/policy/groovy/GroovyPolicyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.MaybeTransformer;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.function.Function;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -302,6 +305,24 @@ private static GroovyPolicyConfiguration buildConfig(String script) {
return GroovyPolicyConfiguration.builder().script(loadScript(script)).readContent(true).build();
}

@Test
void should_get_message_binary_content_as_base64() {
var policy = new GroovyPolicy(buildConfig("get_message_binary_content.groovy"));
var message = new DefaultMessage();
byte[] isoEncodedCharacter = "é".getBytes(StandardCharsets.ISO_8859_1);
message.content(Buffer.buffer(isoEncodedCharacter));

when(request.onMessage(onMessageCaptor.capture())).thenReturn(Completable.complete());
policy.onMessageRequest(ctx).test().assertNoValues();

onMessageCaptor.getValue().apply(message).test().assertComplete().assertNoErrors();

assertThat(message.<String>attribute("wronglyBase64EncodedContent"))
.isNotEqualTo(Base64.getEncoder().encodeToString(isoEncodedCharacter));
assertThat(message.<String>attribute("goodBase64Content")).isEqualTo(Base64.getEncoder().encodeToString(isoEncodedCharacter));
assertThat(message.<String>attribute("byteArray")).isEqualTo(Arrays.toString(isoEncodedCharacter));
}

private static String loadScript(String file) {
try {
return new String(getResourceAsStream(file).readAllBytes());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message.attributes.wronglyBase64EncodedContent = message.content.bytes.encodeBase64().toString() // final value is not well encoded because of Charset transformation
message.attributes.goodBase64Content = message.contentAsBase64
message.attributes.byteArray = message.contentAsByteArray.toString()

0 comments on commit 707519e

Please sign in to comment.