cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [cxf] branch master updated: CXF-8356: JAXRS Multipart-Handling broken for InputStream/Datasource parameters (#719)
Date Tue, 03 Nov 2020 23:23:57 GMT
This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new c8c2202  CXF-8356: JAXRS Multipart-Handling broken for InputStream/Datasource parameters
(#719)
c8c2202 is described below

commit c8c2202136ad0b9174c9a21ac3dfa7fc471347f0
Author: Andriy Redko <drreta@gmail.com>
AuthorDate: Tue Nov 3 18:23:45 2020 -0500

    CXF-8356: JAXRS Multipart-Handling broken for InputStream/Datasource parameters (#719)
---
 .../org/apache/cxf/jaxrs/utils/JAXRSUtils.java     | 11 ++-
 .../jaxrs/JAXRSMultipartLocalTransportTest.java    | 88 ++++++++++++++++++++++
 .../apache/cxf/systest/jaxrs/MultipartStore.java   | 18 +++++
 3 files changed, 114 insertions(+), 3 deletions(-)

diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
index df314d2..0518d69 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
@@ -876,11 +876,16 @@ public final class JAXRSUtils {
             contentType = defaultCt == null ? MediaType.APPLICATION_OCTET_STREAM : defaultCt;
         }
 
-        MessageContext mc = new MessageContextImpl(message);
+        final MediaType contentTypeMt = toMediaType(contentType);
+        final MessageContext mc = new MessageContextImpl(message);
+
         MediaType mt = mc.getHttpHeaders().getMediaType();
+        if (mt == null) {
+            mt = contentTypeMt;
+        }
 
         InputStream is;
-        if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
+        if (mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
             is = copyAndGetEntityStream(message);
         } else {
             is = message.getContent(InputStream.class);
@@ -897,7 +902,7 @@ public final class JAXRSUtils {
                                    parameterType,
                                    parameterAnns,
                                    is,
-                                   toMediaType(contentType),
+                                   contentTypeMt,
                                    ori,
                                    message);
     }
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartLocalTransportTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartLocalTransportTest.java
new file mode 100644
index 0000000..5d74dd9
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSMultipartLocalTransportTest.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.jaxrs;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
+
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+public class JAXRSMultipartLocalTransportTest {
+    private static final String ENDPOINT_ADDRESS = "local://test-multipart";
+    private static final String TEMPLATE = "{ "
+        + "\"id\": %s, "
+        + "\"name\": \"Book #%s\", "
+        + "\"class\": \"org.apache.cxf.systest.jaxrs.Book\" }";
+    private static Server server;
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+        sf.setResourceClasses(MultipartStore.class);
+        sf.setResourceProvider(MultipartStore.class, new SingletonResourceProvider(new MultipartStore(),
true));
+        sf.setAddress(ENDPOINT_ADDRESS);
+        server = sf.create();
+    }
+    
+    @AfterClass
+    public static void stopServers() throws Exception {
+        if (server != null) {
+            server.stop();
+            server.destroy();
+        }
+    }
+
+    @Test
+    public void testBookAsMultipleInputStreams() throws Exception {
+        final MultipartStore store = JAXRSClientFactory.create(ENDPOINT_ADDRESS, MultipartStore.class,

+            Collections.singletonList(new JacksonJaxbJsonProvider()));
+        
+        final byte[] books1 = generateBooks(40000).getBytes(StandardCharsets.UTF_8); 
+        final byte[] books2 = generateBooks(2000).getBytes(StandardCharsets.UTF_8);
+        
+        final List<Book> books = store.addBookJsonTypeFromStreams(new ByteArrayInputStream(books1),

+            new ByteArrayInputStream(books2));
+        
+        assertThat(books.size(), equalTo(42000));
+    }
+
+    private String generateBooks(int size) {
+        return "[" + IntStream
+            .range(0, size)
+            .mapToObj(id -> String.format(TEMPLATE, id, id))
+            .collect(Collectors.joining(",")) + "]";
+    }
+}
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
index a1a7c60..b9c244b 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/MultipartStore.java
@@ -23,10 +23,13 @@ package org.apache.cxf.systest.jaxrs;
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
@@ -49,6 +52,8 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.transform.stream.StreamSource;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
@@ -493,6 +498,19 @@ public class MultipartStore {
         b1.setId(124);
         return Response.ok(b1).build();
     }
+    
+    @POST
+    @Path("/books/jsonstream")
+    @Produces("text/xml")
+    @Consumes(MediaType.MULTIPART_FORM_DATA)
+    public List<Book> addBookJsonTypeFromStreams(
+            @Multipart(value = "part1", type = "application/octet-stream") InputStream in1,
+            @Multipart(value = "part2", type = "application/octet-stream") InputStream in2)
throws Exception {
+        final ObjectMapper mapper = new ObjectMapper();
+        final Book[] array1 = mapper.readValue(in1, Book[].class);
+        final Book[] array2 = mapper.readValue(in2, Book[].class);
+        return Stream.concat(Arrays.stream(array1), Arrays.stream(array2)).collect(Collectors.toList());
+    }
 
     @POST
     @Path("/books/filesform")


Mime
View raw message