You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by lf...@apache.org on 2026/08/19 07:57:30 UTC
(ofbiz-framework) 01/02: Implemented: GZip and deflate compression for rest-api (OFBIZ-13496)
This is an automated email from the ASF dual-hosted git repository.
Lukas-Finster pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
commit 7727f5fff37c6cd8c2203429365db9f2a51a56d2
Author: Lukas Finster <lu...@ecomify.de>
AuthorDate: Tue Aug 18 13:04:52 2026 +0200
Implemented: GZip and deflate compression for rest-api (OFBIZ-13496)
---
.../apache/ofbiz/ws/rs/core/OFBizApiConfig.java | 5 ++
.../ofbiz/ws/rs/test/RestTestHttpRequest.java | 54 ++++++++++++++++++++++
2 files changed, 59 insertions(+)
diff --git a/framework/rest-api/src/main/java/org/apache/ofbiz/ws/rs/core/OFBizApiConfig.java b/framework/rest-api/src/main/java/org/apache/ofbiz/ws/rs/core/OFBizApiConfig.java
index cfc12e5bdc..1933019158 100644
--- a/framework/rest-api/src/main/java/org/apache/ofbiz/ws/rs/core/OFBizApiConfig.java
+++ b/framework/rest-api/src/main/java/org/apache/ofbiz/ws/rs/core/OFBizApiConfig.java
@@ -40,7 +40,10 @@ import org.apache.ofbiz.ws.rs.process.ServiceRequestHandler;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
+import org.glassfish.jersey.message.DeflateEncoder;
+import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.server.filter.EncodingFilter;
import org.glassfish.jersey.server.model.Resource;
import org.glassfish.jersey.server.model.ResourceMethod;
@@ -73,6 +76,8 @@ public class OFBizApiConfig extends ResourceConfig {
LoggingFeature.Verbosity.PAYLOAD_ANY, 10000));
}
registerDSLResources();
+ EncodingFilter.enableFor(this, GZipEncoder.class);
+ EncodingFilter.enableFor(this, DeflateEncoder.class);
}
/**
diff --git a/framework/rest-api/src/test/java/org/apache/ofbiz/ws/rs/test/RestTestHttpRequest.java b/framework/rest-api/src/test/java/org/apache/ofbiz/ws/rs/test/RestTestHttpRequest.java
index 29e5df6176..b7c60ec79e 100644
--- a/framework/rest-api/src/test/java/org/apache/ofbiz/ws/rs/test/RestTestHttpRequest.java
+++ b/framework/rest-api/src/test/java/org/apache/ofbiz/ws/rs/test/RestTestHttpRequest.java
@@ -18,9 +18,15 @@
*******************************************************************************/
package org.apache.ofbiz.ws.rs.test;
+import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
import java.util.Base64;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.InflaterInputStream;
+import java.util.zip.ZipException;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.HttpClient;
@@ -173,6 +179,54 @@ class RestTestHttpRequest implements JupiterTestHelper {
assertEquals("fr", localeSentViaAcceptLanguageHeader);
}
+ @Test
+ void testGZipCompression() throws Exception {
+ HttpClient client = initHttpClient();
+ client.setHeader("Content-Type", "application/json");
+ client.setHeader("Authorization", "Bearer " + accessToken);
+ client.setHeader("Accept-Language", "fr");
+ client.setHeader("Accept-Encoding", "gzip");
+ client.setUrl(BASE_URL + "/exampleApi/returnSuccess");
+
+ byte[] rawBytes;
+ try (InputStream responseStream = client.postStream()) {
+ rawBytes = responseStream.readAllBytes();
+ }
+
+ // magic bytes for gzip
+ assertEquals((byte) 0x1f, rawBytes[0]);
+ assertEquals((byte) 0x8b, rawBytes[1]);
+
+ try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(rawBytes))) {
+ JsonNode root = mapper.readTree(gis);
+ assertEquals(200, root.path("statusCode").asInt());
+ } catch (JsonProcessingException e) {
+ fail("Error parsing rest auth response: " + e.getMessage());
+ }
+ }
+
+ @Test
+ void testDeflateCompression() throws Exception {
+ HttpClient client = initHttpClient();
+ client.setHeader("Content-Type", "application/json");
+ client.setHeader("Authorization", "Bearer " + accessToken);
+ client.setHeader("Accept-Language", "fr");
+ client.setHeader("Accept-Encoding", "deflate");
+ client.setUrl(BASE_URL + "/exampleApi/returnSuccess");
+
+ byte[] rawBytes;
+ try (InputStream responseStream = client.postStream()) {
+ rawBytes = responseStream.readAllBytes();
+ }
+
+ try (InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(rawBytes))) {
+ JsonNode root = mapper.readTree(iis);
+ assertEquals(200, root.path("statusCode").asInt());
+ } catch (JsonProcessingException | ZipException e) {
+ fail("Error parsing rest auth response: " + e.getMessage());
+ }
+ }
+
@Test
void testServiceInputParametersAsPath() throws Exception {
HttpClient client = initHttpClient();