openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1861218 - in /openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front: ProxyServlet.java cdi/CDIProxyServlet.java cdi/event/OnResponse.java cdi/func/IOConsumer.java cdi/func/IORunnable.java
Date Thu, 13 Jun 2019 06:07:15 GMT
Author: rmannibucau
Date: Thu Jun 13 06:07:15 2019
New Revision: 1861218

URL: http://svn.apache.org/viewvc?rev=1861218&view=rev
Log:
MEECROWAVE-197 (proxy) ensure the response payload can be rewritten easily

Added:
    openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/func/IOConsumer.java
Removed:
    openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/func/IORunnable.java
Modified:
    openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/ProxyServlet.java
    openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/CDIProxyServlet.java
    openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/event/OnResponse.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/ProxyServlet.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/ProxyServlet.java?rev=1861218&r1=1861217&r2=1861218&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/ProxyServlet.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/ProxyServlet.java
Thu Jun 13 06:07:15 2019
@@ -22,6 +22,7 @@ import static java.util.Collections.list
 import static java.util.Optional.empty;
 import static java.util.Optional.ofNullable;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static java.util.function.Function.identity;
 import static java.util.stream.Collectors.toMap;
 
 import java.io.IOException;
@@ -34,6 +35,7 @@ import java.util.Enumeration;
 import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.CompletionStage;
+import java.util.function.Function;
 import java.util.stream.Stream;
 
 import javax.servlet.AsyncContext;
@@ -86,7 +88,7 @@ public class ProxyServlet extends HttpSe
                     onError(route, req, resp, error);
                 } else {
                     try {
-                        forwardResponse(route, response, req, resp);
+                        forwardResponse(route, response, req, resp, identity());
                     } catch (final IOException e) {
                         onError(route, req, resp, e);
                     }
@@ -172,7 +174,7 @@ public class ProxyServlet extends HttpSe
         if (WebApplicationException.class.isInstance(error)) {
             final WebApplicationException wae = WebApplicationException.class.cast(error);
             if (wae.getResponse() != null) {
-                forwardResponse(route, wae.getResponse(), request, resp);
+                forwardResponse(route, wae.getResponse(), request, resp, identity());
                 return;
             }
         }
@@ -185,7 +187,8 @@ public class ProxyServlet extends HttpSe
     }
 
     protected void forwardResponse(final Routes.Route route, final Response response,
-                                   final HttpServletRequest request, final HttpServletResponse
resp) throws IOException {
+                                   final HttpServletRequest request, final HttpServletResponse
resp,
+                                   final Function<InputStream, InputStream> responseRewriter)
throws IOException {
         final int status = response.getStatus();
         resp.setStatus(status);
         forwardHeaders(route, response, resp);
@@ -193,7 +196,7 @@ public class ProxyServlet extends HttpSe
             resp.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0);
         }
         forwardCookies(route, response, resp);
-        writeOutput(resp, response.readEntity(InputStream.class));
+        writeOutput(resp, responseRewriter.apply(response.readEntity(InputStream.class)));
     }
 
     protected void forwardCookies(final Routes.Route route, final Response response, final
HttpServletResponse resp) {

Modified: openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/CDIProxyServlet.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/CDIProxyServlet.java?rev=1861218&r1=1861217&r2=1861218&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/CDIProxyServlet.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/CDIProxyServlet.java
Thu Jun 13 06:07:15 2019
@@ -18,9 +18,13 @@
  */
 package org.apache.meecrowave.proxy.servlet.front.cdi;
 
+import static java.util.function.Function.identity;
+
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionStage;
+import java.util.function.Function;
 
 import javax.enterprise.event.Event;
 import javax.inject.Inject;
@@ -99,15 +103,17 @@ public class CDIProxyServlet extends Pro
 
     @Override
     protected void forwardResponse(final Routes.Route route, final Response response,
-                                   final HttpServletRequest request, final HttpServletResponse
resp) throws IOException {
+                                   final HttpServletRequest request, final HttpServletResponse
resp,
+                                   final Function<InputStream, InputStream> responseRewriter)
throws IOException {
         if (spy.isHasOnResponseEvent()) {
-            final OnResponse onResponse = new OnResponse(request, resp, response, () ->
super.forwardResponse(route, response, request, resp));
+            final OnResponse onResponse = new OnResponse(request, resp, response, responseRewriter,
+                    rewriter -> super.forwardResponse(route, response, request, resp,
rewriter));
             onResponseEvent.fire(onResponse);
             if (!onResponse.isProceeded()) {
                 onResponse.proceed();
             }
         } else {
-            super.forwardResponse(route, response, request, resp);
+            super.forwardResponse(route, response, request, resp, identity());
         }
     }
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/event/OnResponse.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/event/OnResponse.java?rev=1861218&r1=1861217&r2=1861218&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/event/OnResponse.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/event/OnResponse.java
Thu Jun 13 06:07:15 2019
@@ -19,31 +19,40 @@
 package org.apache.meecrowave.proxy.servlet.front.cdi.event;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.function.Function;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.core.Response;
 
-import org.apache.meecrowave.proxy.servlet.front.cdi.func.IORunnable;
+import org.apache.meecrowave.proxy.servlet.front.cdi.func.IOConsumer;
 
 public class OnResponse extends BaseEvent {
     private final Response clientResponse;
-    private final IORunnable delegate;
+    private final IOConsumer<Function<InputStream, InputStream>> delegate;
+    private Function<InputStream, InputStream> payloadRewriter;
     private boolean proceeded;
 
     public OnResponse(final HttpServletRequest request, final HttpServletResponse response,
-                      final Response clientResponse, final IORunnable delegate) {
+                      final Response clientResponse, final Function<InputStream, InputStream>
payloadRewriter,
+                      final IOConsumer<Function<InputStream, InputStream>> delegate)
{
         super(request, response);
         this.clientResponse = clientResponse;
         this.delegate = delegate;
+        this.payloadRewriter = payloadRewriter;
     }
 
     public Response getClientResponse() {
         return clientResponse;
     }
 
+    public void setPayloadRewriter(final Function<InputStream, InputStream> payloadRewriter)
{
+        this.payloadRewriter = payloadRewriter;
+    }
+
     public void proceed() throws IOException {
-        delegate.run();
+        delegate.accept(payloadRewriter);
         proceeded = true;
     }
 

Added: openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/func/IOConsumer.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/func/IOConsumer.java?rev=1861218&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/func/IOConsumer.java
(added)
+++ openwebbeans/meecrowave/trunk/meecrowave-proxy/src/main/java/org/apache/meecrowave/proxy/servlet/front/cdi/func/IOConsumer.java
Thu Jun 13 06:07:15 2019
@@ -0,0 +1,26 @@
+/*
+ * 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.meecrowave.proxy.servlet.front.cdi.func;
+
+import java.io.IOException;
+
+@FunctionalInterface
+public interface IOConsumer<T> {
+    void accept(T parameter) throws IOException;
+}



Mime
View raw message