This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 802e8cb  SLING-12441 Add support for binary request parameters (#54)
802e8cb is described below

commit 802e8cb1bf40dfc84559df410de435dd94cec1dc
Author: Konrad Windszus <[email protected]>
AuthorDate: Fri Oct 11 17:02:03 2024 +0200

    SLING-12441 Add support for binary request parameters (#54)
---
 pom.xml                                            |  3 +-
 .../apache/sling/api/request/builder/Builders.java | 22 +++++++++--
 .../request/builder/impl/RequestParameterImpl.java | 45 ++++++++++++++--------
 .../sling/api/request/builder/package-info.java    |  2 +-
 .../sling/api/request/builder/BuildersTest.java    | 19 ++++++++-
 .../builder/impl/RequestParameterImplTest.java     | 32 +++++++++++++--
 6 files changed, 95 insertions(+), 28 deletions(-)

diff --git a/pom.xml b/pom.xml
index ab0d995..fdc9c39 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.api</artifactId>
-    <version>2.27.7-SNAPSHOT</version>
+    <version>2.28.0-SNAPSHOT</version>
 
     <name>Apache Sling API</name>
     <description>
@@ -46,7 +46,6 @@
         
<connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git</connection>
         
<developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git</developerConnection>
         <url>https://github.com/apache/sling-org-apache-sling-api.git</url>
-    <tag>org.apache.sling.api-2.26.0</tag>
   </scm>
 
     <properties>
diff --git a/src/main/java/org/apache/sling/api/request/builder/Builders.java 
b/src/main/java/org/apache/sling/api/request/builder/Builders.java
index 0a1518a..a58b6fa 100644
--- a/src/main/java/org/apache/sling/api/request/builder/Builders.java
+++ b/src/main/java/org/apache/sling/api/request/builder/Builders.java
@@ -69,19 +69,19 @@ public final class Builders {
     }
 
     /**
-     * Creates a new request parameter
+     * Creates a new {@code application/x-www-form-urlencoded} request 
parameter with UTF-8 encoding
      *
      * @param name the parameter name
      * @param value the parameter value
      * @return a request parameter
      * @since 1.2 (Sling API Bundle 2.26.2)
      */
-    public static @NotNull RequestParameter newRequestParameter(String name, 
String value) {
+    public static @NotNull RequestParameter newRequestParameter(@NotNull 
String name, @NotNull String value) {
         return new RequestParameterImpl(name, value);
     }
 
     /**
-     * Creates a new request parameter
+     * Creates a new {@code application/x-www-form-urlencoded} request 
parameter with the given encoding
      *
      * @param name the parameter name
      * @param value the parameter value
@@ -89,7 +89,21 @@ public final class Builders {
      * @return a request parameter
      * @since 1.2 (Sling API Bundle 2.26.2)
      */
-    public static @NotNull RequestParameter newRequestParameter(String name, 
String value, Charset encoding) {
+    public static @NotNull RequestParameter newRequestParameter(@NotNull 
String name, @NotNull String value, @NotNull Charset encoding) {
         return new RequestParameterImpl(name, value, encoding);
     }
+
+    /**
+     * Creates a new binary request parameter
+     *
+     * @param name the parameter name
+     * @param value the parameter value
+     * @param fileName the file name (may be {@code null})
+     * @param contentType the content type (may be {@code null})
+     * @return a request parameter
+     * @since 1.3 (Sling API Bundle 2.28.0)
+     */
+    public static @NotNull RequestParameter newRequestParameter(@NotNull 
String name, byte @NotNull[] value, String fileName, String contentType) {
+        return new RequestParameterImpl(name, value, fileName, contentType);
+    }
 }
diff --git 
a/src/main/java/org/apache/sling/api/request/builder/impl/RequestParameterImpl.java
 
b/src/main/java/org/apache/sling/api/request/builder/impl/RequestParameterImpl.java
index ce07c22..09b77f9 100644
--- 
a/src/main/java/org/apache/sling/api/request/builder/impl/RequestParameterImpl.java
+++ 
b/src/main/java/org/apache/sling/api/request/builder/impl/RequestParameterImpl.java
@@ -25,41 +25,54 @@ import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 
 import org.apache.sling.api.request.RequestParameter;
+import org.jetbrains.annotations.NotNull;
 
 /**
  * Implementation of {@link RequestParameter}.
  */
 public class RequestParameterImpl implements RequestParameter {
 
-    private static final String CONTENT_TYPE = "text/plain";
+    private final @NotNull String name;
+    private final byte @NotNull[] value;
+    private final @NotNull Charset encoding;
+    private final String contentType;
+    private final String fileName;
+    private final boolean isFormField;
 
-    private final String name;
-    private final String value;
-    private final Charset encoding;
-
-    public RequestParameterImpl(final String name, final String value) {
+    public RequestParameterImpl(@NotNull final String name, @NotNull final 
String value) {
         this(name, value, StandardCharsets.UTF_8);
     }
 
-    public RequestParameterImpl(final String name, final String value, final 
Charset encoding) {
+    public RequestParameterImpl(@NotNull final String name, @NotNull final 
String value, @NotNull final Charset encoding) {
+        this(name, value.getBytes(encoding), encoding, null, null, true); // 
by default application/x-www-form-urlencoded is returning null for content type 
in Sling Engine
+    }
+
+    public RequestParameterImpl(@NotNull final String name, byte @NotNull[] 
value, String fileName, String contentType) {
+        this(name, value, StandardCharsets.UTF_8, fileName, contentType, 
false);
+    }
+
+    private RequestParameterImpl(@NotNull final String name, byte @NotNull [] 
value, @NotNull final Charset encoding, String fileName, final String 
contentType, boolean isFormField) {
         this.name = name;
         this.value = value;
+        this.contentType = contentType;
         this.encoding = encoding;
+        this.fileName = fileName;
+        this.isFormField = isFormField;
     }
 
     @Override
-    public String getName() {
+    public @NotNull String getName() {
         return this.name;
     }
 
     @Override
     public byte[] get() {
-        return this.value.getBytes(encoding);
+        return this.value;
     }
 
     @Override
     public String getContentType() {
-        return CONTENT_TYPE;
+        return contentType;
     }
 
     @Override
@@ -69,7 +82,7 @@ public class RequestParameterImpl implements RequestParameter 
{
 
     @Override
     public String getFileName() {
-        return null;
+        return fileName;
     }
 
     @Override
@@ -78,18 +91,18 @@ public class RequestParameterImpl implements 
RequestParameter {
     }
 
     @Override
-    public String getString() {
-        return this.value;
+    public @NotNull String getString() {
+        return new String(this.value, encoding);
     }
 
     @Override
-    public String getString(final String encoding) throws 
UnsupportedEncodingException {
-        return new String(this.get(), encoding);
+    public @NotNull String getString(final @NotNull String encoding) throws 
UnsupportedEncodingException {
+        return new String(this.value, encoding);
     }
 
     @Override
     public boolean isFormField() {
-        return true;
+        return isFormField;
     }
 
     @Override
diff --git 
a/src/main/java/org/apache/sling/api/request/builder/package-info.java 
b/src/main/java/org/apache/sling/api/request/builder/package-info.java
index 9a8c0d9..e818dbc 100644
--- a/src/main/java/org/apache/sling/api/request/builder/package-info.java
+++ b/src/main/java/org/apache/sling/api/request/builder/package-info.java
@@ -16,5 +16,5 @@
  * specific language governing permissions and limitations
  * under the License.
  */
[email protected]("1.2")
[email protected]("1.3")
 package org.apache.sling.api.request.builder;
diff --git 
a/src/test/java/org/apache/sling/api/request/builder/BuildersTest.java 
b/src/test/java/org/apache/sling/api/request/builder/BuildersTest.java
index 1d19071..21ca6d4 100644
--- a/src/test/java/org/apache/sling/api/request/builder/BuildersTest.java
+++ b/src/test/java/org/apache/sling/api/request/builder/BuildersTest.java
@@ -23,9 +23,12 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.request.RequestParameter;
 import org.apache.sling.api.resource.Resource;
@@ -89,11 +92,25 @@ public class BuildersTest {
     @Test
     public void createRequestParameterWithCharset() throws 
UnsupportedEncodingException {
         @NotNull
-        RequestParameter rp = Builders.newRequestParameter("key", new 
String("value".getBytes(StandardCharsets.UTF_16), StandardCharsets.UTF_16), 
StandardCharsets.UTF_16);
+        RequestParameter rp = Builders.newRequestParameter("key", "value", 
StandardCharsets.UTF_16);
         assertNotNull(rp);
         assertEquals("key", rp.getName());
         assertEquals("value", rp.getString());
         assertEquals("value", rp.getString(StandardCharsets.UTF_16.name()));
     }
 
+    @Test
+    public void createBinaryRequestParameter() throws IOException {
+        byte[] value = new byte[] {1, 2, 3};
+        RequestParameter rp = Builders.newRequestParameter("key", value, 
"filename", "application/octet-stream");
+        assertNotNull(rp);
+        assertEquals("key", rp.getName());
+        try (InputStream is = rp.getInputStream()) {
+            byte[] actualValue = IOUtils.toByteArray(is);
+            assertArrayEquals(value, actualValue);
+        }
+        assertEquals(value.length, rp.getSize());
+        assertEquals("filename", rp.getFileName());
+        assertEquals("application/octet-stream", rp.getContentType());
+    }
 }
diff --git 
a/src/test/java/org/apache/sling/api/request/builder/impl/RequestParameterImplTest.java
 
b/src/test/java/org/apache/sling/api/request/builder/impl/RequestParameterImplTest.java
index 43d4feb..695c137 100644
--- 
a/src/test/java/org/apache/sling/api/request/builder/impl/RequestParameterImplTest.java
+++ 
b/src/test/java/org/apache/sling/api/request/builder/impl/RequestParameterImplTest.java
@@ -20,23 +20,31 @@ package org.apache.sling.api.request.builder.impl;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.UnsupportedEncodingException;
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 
+import org.apache.commons.io.IOUtils;
 import org.junit.Test;
 
 public class RequestParameterImplTest {
-    
-    @Test public void testParameter() throws UnsupportedEncodingException {
+
+    @Test 
+    public void testTextualParameter() throws IOException {
         final RequestParameterImpl param = new RequestParameterImpl("foo", 
"bar");
         assertEquals("foo", param.getName());
         assertEquals("bar", param.getString());
         assertArrayEquals("bar".getBytes(StandardCharsets.UTF_8), param.get());
-        assertEquals("text/plain", param.getContentType());
+        assertNull(param.getContentType());  // compare with 
https://github.com/apache/sling-org-apache-sling-engine/blob/8b6b5b273f667b7e156b37a791493b00b87cc733/src/main/java/org/apache/sling/engine/impl/parameters/ContainerRequestParameter.java#L73
+        try (InputStream is = param.getInputStream()) {
+            byte[] actualValue = IOUtils.toByteArray(is);
+            assertArrayEquals("bar".getBytes(StandardCharsets.UTF_8), 
actualValue);
+        }
         assertNotNull(param.getInputStream());
         assertNull(param.getFileName());
         assertEquals(3L, param.getSize());
@@ -44,4 +52,20 @@ public class RequestParameterImplTest {
         assertTrue(param.isFormField());
         assertEquals(param.getString(), param.toString());
     }
+
+    @Test 
+    public void testBinaryParameter() throws IOException {
+        byte[] value = new byte[] { 1, 2, 3 };
+        final RequestParameterImpl param = new RequestParameterImpl("foo", 
value, "filename", null);
+        assertEquals("foo", param.getName());
+        assertArrayEquals(value, param.get());
+        assertNull(param.getContentType());
+        try (InputStream is = param.getInputStream()) {
+            byte[] actualValue = IOUtils.toByteArray(is);
+            assertArrayEquals(value, actualValue);
+        }
+        assertEquals("filename", param.getFileName());
+        assertEquals(3L, param.getSize());
+        assertFalse(param.isFormField());
+    }
 }

Reply via email to