You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2018/03/16 18:07:51 UTC

[GitHub] dkulp closed pull request #390: [CXF-7676] Create a proxy OutputStream to create an EntityStream that���

dkulp closed pull request #390: [CXF-7676] Create a proxy OutputStream to create an EntityStream that…
URL: https://github.com/apache/cxf/pull/390
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestFilterInterceptor.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestFilterInterceptor.java
index 44430f393ca..d207d76ef87 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestFilterInterceptor.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/spec/ClientRequestFilterInterceptor.java
@@ -19,6 +19,7 @@
 package org.apache.cxf.jaxrs.client.spec;
 
 import java.io.IOException;
+import java.io.OutputStream;
 import java.util.List;
 import java.util.Map;
 
@@ -39,6 +40,7 @@
 import org.apache.cxf.message.MessageImpl;
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.transport.MessageObserver;
+import org.apache.cxf.transport.http.ProxyOutputStream;
 
 public class ClientRequestFilterInterceptor extends AbstractOutDatabindingInterceptor {
 
@@ -52,6 +54,12 @@ public void handleMessage(Message outMessage) throws Fault {
             return;
         }
 
+        // create an empty proxy output stream that the filter can interact with
+        // and save a reference for later
+        ProxyOutputStream pos = new ProxyOutputStream();
+        outMessage.setContent(OutputStream.class, pos);
+        outMessage.setContent(ProxyOutputStream.class, pos);
+
         List<ProviderInfo<ClientRequestFilter>> filters = pf.getClientRequestFilters();
         if (!filters.isEmpty()) {
 
diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
index 4b84aa32212..fda7b8fad29 100644
--- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
+++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
@@ -562,11 +562,21 @@ public void prepare(Message message) throws IOException {
 
         setHeadersByAuthorizationPolicy(message, currentAddress.getURI());
         new Headers(message).setFromClientPolicy(getClient(message));
-        message.setContent(OutputStream.class,
-                           createOutputStream(message,
-                                              needToCacheRequest,
-                                              isChunking,
-                                              chunkThreshold));
+
+        // set the OutputStream on the ProxyOutputStream
+        ProxyOutputStream pos = message.getContent(ProxyOutputStream.class);
+        if (pos != null && message.getContent(OutputStream.class) != null) {
+            pos.setWrappedOutputStream(createOutputStream(message,
+                                                          needToCacheRequest,
+                                                          isChunking,
+                                                          chunkThreshold));
+        } else {
+            message.setContent(OutputStream.class,
+                               createOutputStream(message,
+                                                  needToCacheRequest,
+                                                  isChunking,
+                                                  chunkThreshold));
+        }
         // We are now "ready" to "send" the message.
     }
 
diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/ProxyOutputStream.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/ProxyOutputStream.java
new file mode 100644
index 00000000000..f1d0f4af1f8
--- /dev/null
+++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/ProxyOutputStream.java
@@ -0,0 +1,29 @@
+/**
+ * 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.transport.http;
+
+import java.io.OutputStream;
+
+import org.apache.cxf.io.AbstractWrappedOutputStream;
+
+public class ProxyOutputStream extends AbstractWrappedOutputStream {
+    public void setWrappedOutputStream(OutputStream os) {
+        this.wrappedStream = os;
+    }
+}
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index fe843667392..538b99bcd09 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -1668,6 +1668,14 @@ public Response getBadlyQuotedHeader(@QueryParam("type")int t) {
     public BookSubresource getBookFromSubresource() {
         return new BookSubresourceImpl();
     }
+    
+    @POST
+    @Path("/entityecho")
+    @Consumes("text/plain")
+    @Produces("text/plain")
+    public Response echoEntity(String entity) {
+        return Response.ok().entity(entity).build();
+    }
 
     public final String init() {
         books.clear();
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
index 418408fa23a..f3b37a8c004 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
@@ -834,6 +834,27 @@ public void testUnknownHostException() throws InterruptedException {
                     ExceptionUtils.getRootCause(e) instanceof UnknownHostException);
         }
     }
+    
+    @Test
+    public void testGetSetEntityStream() {
+        String address = "http://localhost:" + PORT + "/bookstore/entityecho";
+        String entity = "BOOKSTORE";
+
+        Client client = ClientBuilder.newClient();
+        client.register(new ClientRequestFilter() {
+            @Override
+            public void filter(ClientRequestContext context) throws IOException {
+                context.setEntityStream(new ReplacingOutputStream(
+                                 context.getEntityStream(), 'X', 'O'));
+            }
+        });
+
+        WebTarget target = client.target(address);
+
+        Response response = target.request().post(
+                Entity.entity(entity.replace('O', 'X'), "text/plain"));
+        assertEquals(entity, response.readEntity(String.class));
+    }
 
     private static class ReplaceBodyFilter implements ClientRequestFilter {
 
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ReplacingOutputStream.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ReplacingOutputStream.java
new file mode 100644
index 00000000000..0668961e47b
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/ReplacingOutputStream.java
@@ -0,0 +1,45 @@
+/**
+ * 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.IOException;
+import java.io.OutputStream;
+
+import org.apache.cxf.io.AbstractWrappedOutputStream;
+
+public class ReplacingOutputStream extends AbstractWrappedOutputStream {
+
+    private char oldChar;
+    private char newChar;
+
+    public ReplacingOutputStream(OutputStream wrappedStream, char oldChar, char newChar) {
+        super(wrappedStream);
+        this.oldChar = oldChar;
+        this.newChar = newChar;
+    }
+
+    @Override
+    public void write(byte[] b) throws IOException {
+        replace(b);
+    }
+
+    private void replace(byte[] b) throws IOException {
+        this.wrappedStream.write(new String(b).replace(this.oldChar, this.newChar).getBytes());
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services