You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2016/02/01 15:19:44 UTC

cxf git commit: [CXF-6610] Aborting the call if ThrottledResponse statusis >= 300

Repository: cxf
Updated Branches:
  refs/heads/master 3e843f03b -> 9039c28b2


[CXF-6610] Aborting the call if ThrottledResponse statusis >= 300


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/9039c28b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/9039c28b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/9039c28b

Branch: refs/heads/master
Commit: 9039c28b2017380ee67f4d1bed90da2adbab6d31
Parents: 3e843f0
Author: Sergey Beryozkin <sb...@gmail.com>
Authored: Mon Feb 1 14:19:28 2016 +0000
Committer: Sergey Beryozkin <sb...@gmail.com>
Committed: Mon Feb 1 14:19:28 2016 +0000

----------------------------------------------------------------------
 .../apache/cxf/throttling/ThrottleResponse.java | 16 +++-
 .../cxf/throttling/ThrottlingInterceptor.java   | 20 ++++-
 .../ThrottlingResponseInterceptor.java          |  6 ++
 systests/jaxrs/pom.xml                          |  5 ++
 .../cxf/systest/jaxrs/BookServerThrottled.java  | 92 ++++++++++++++++++++
 .../jaxrs/JAXRSClientServerThrottledTest.java   | 59 +++++++++++++
 6 files changed, 196 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/9039c28b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottleResponse.java
----------------------------------------------------------------------
diff --git a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottleResponse.java b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottleResponse.java
index 34ba5d5..af0bcbd 100644
--- a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottleResponse.java
+++ b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottleResponse.java
@@ -31,6 +31,18 @@ public class ThrottleResponse {
     protected int responseCode = -1;
     protected String errorMessage;
     
+    public ThrottleResponse() {
+        
+    }
+    
+    public ThrottleResponse(int responceCode) {
+        this.responseCode = responceCode;
+    }
+    
+    public ThrottleResponse(int responceCode, long delay) {
+        this(responceCode);
+        this.delay = delay;
+    }
 
     public Map<String, String> getResponseHeaders() {
         return responseHeaders;
@@ -61,7 +73,9 @@ public class ThrottleResponse {
     }
 
     /**
-     * Delay processing for specified milliseconds.  Should be "small" to prevent the client from timing out.
+     * Delay processing for specified milliseconds.  
+     * Should be "small" to prevent the client from timing out unless the client request is
+     * aborted with the HTTP error code.
      * @return
      */
     public long getDelay() {

http://git-wip-us.apache.org/repos/asf/cxf/blob/9039c28b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingInterceptor.java b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingInterceptor.java
index e0f598c..a6cf392 100644
--- a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingInterceptor.java
+++ b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingInterceptor.java
@@ -24,7 +24,9 @@ import java.util.logging.Logger;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.continuations.Continuation;
 import org.apache.cxf.continuations.ContinuationProvider;
+import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.interceptor.OutgoingChainInterceptor;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
 
@@ -47,6 +49,13 @@ public class ThrottlingInterceptor extends AbstractPhaseInterceptor<Message> {
             return;
         }
         message.getExchange().put(ThrottleResponse.class, rsp);
+        if (rsp.getResponseCode() >= 300) {
+            createOutMessage(message);
+            message.getInterceptorChain().doInterceptStartingAt(message,
+                                                                OutgoingChainInterceptor.class.getName());
+            return;
+        }
+        
         long l = rsp.getDelay();
         if (l > 0) {
             ContinuationProvider cp = message.get(ContinuationProvider.class);
@@ -63,5 +72,14 @@ public class ThrottlingInterceptor extends AbstractPhaseInterceptor<Message> {
             c.suspend(l);
         }
     }
-
+    private Message createOutMessage(Message inMessage) {
+        Endpoint e = inMessage.getExchange().getEndpoint();
+        Message mout = e.getBinding().createMessage();
+        mout.setExchange(inMessage.getExchange());
+        mout.setInterceptorChain(
+             OutgoingChainInterceptor.getOutInterceptorChain(inMessage.getExchange()));
+        inMessage.getExchange().setOutMessage(mout);
+        inMessage.getExchange().put("cxf.io.cacheinput", Boolean.FALSE);
+        return mout;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9039c28b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingResponseInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingResponseInterceptor.java b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingResponseInterceptor.java
index 5604c9a..9992abd 100644
--- a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingResponseInterceptor.java
+++ b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingResponseInterceptor.java
@@ -20,6 +20,7 @@
 package org.apache.cxf.throttling;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeMap;
@@ -62,6 +63,11 @@ public class ThrottlingResponseInterceptor extends AbstractPhaseInterceptor<Mess
                 }
                 r.add(e.getValue());
             }
+            if (rsp.getResponseCode() == 503 && rsp.getDelay() > 0 
+                && !rsp.getResponseHeaders().containsKey("Retry-After")) {
+                String retryAfter = Long.toString(rsp.getDelay() / 1000);
+                headers.put("Retry-After", Collections.singletonList(retryAfter));
+            }
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/9039c28b/systests/jaxrs/pom.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/pom.xml b/systests/jaxrs/pom.xml
index 39a3cfb..1733af0 100644
--- a/systests/jaxrs/pom.xml
+++ b/systests/jaxrs/pom.xml
@@ -246,6 +246,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-features-throttling</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-rt-transports-jms</artifactId>
             <version>${project.version}</version>
         </dependency>

http://git-wip-us.apache.org/repos/asf/cxf/blob/9039c28b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerThrottled.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerThrottled.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerThrottled.java
new file mode 100644
index 0000000..aa61a70
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServerThrottled.java
@@ -0,0 +1,92 @@
+/**
+ * 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.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.cxf.configuration.security.AuthorizationPolicy;
+import org.apache.cxf.feature.Feature;
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.throttling.ThrottleResponse;
+import org.apache.cxf.throttling.ThrottlingFeature;
+import org.apache.cxf.throttling.ThrottlingManager;
+
+    
+public class BookServerThrottled extends AbstractBusTestServerBase {
+    public static final String PORT = allocatePort(BookServerThrottled.class);
+     
+    org.apache.cxf.endpoint.Server server;
+    public BookServerThrottled() {
+    }
+    protected void run() {
+        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+        sf.setResourceClasses(BookStore.class);
+        List<Feature> features = new ArrayList<Feature>();
+        ThrottlingFeature tf = new ThrottlingFeature(new ThrottlingManagerImpl()); 
+        features.add(tf);
+        sf.setFeatures(features);
+        sf.setResourceProvider(BookStore.class,
+                               new SingletonResourceProvider(new BookStore(), true));
+        sf.setAddress("http://localhost:" + PORT + "/");
+        server = sf.create();
+    }
+    
+    public void tearDown() throws Exception {
+        server.stop();
+        server.destroy();
+        server = null;
+    }
+
+    public static void main(String[] args) {
+        try {
+            BookServerThrottled s = new BookServerThrottled();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+    private static class ThrottlingManagerImpl implements ThrottlingManager {
+
+        @Override
+        public List<String> getDecisionPhases() {
+            return CastUtils.cast(Collections.singletonList(Phase.PRE_STREAM));
+        }
+
+        @Override
+        public ThrottleResponse getThrottleResponse(String phase, Message m) {
+            AuthorizationPolicy ap = m.get(AuthorizationPolicy.class);
+            if (ap != null && "alice".equals(ap.getUserName())) {
+                return null;
+            }
+            return new ThrottleResponse(503, 2000);
+        }
+        
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/9039c28b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerThrottledTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerThrottledTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerThrottledTest.java
new file mode 100644
index 0000000..5a804a0
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerThrottledTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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 javax.ws.rs.core.Response;
+
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JAXRSClientServerThrottledTest extends AbstractBusClientServerTestBase {
+    public static final String PORT = BookServerThrottled.PORT;
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        AbstractResourceInfo.clearAllMaps();
+        assertTrue("server did not launch correctly",
+                   launchServer(BookServerThrottled.class, true));
+        createStaticBus();
+    }
+    
+    @Test
+    public void testGetBookRetryAfter() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/books/query/default";
+        WebClient wc = WebClient.create(address);
+        Response r = wc.get();
+        assertEquals(503, r.getStatus());
+        assertEquals("2", r.getHeaderString("Retry-After"));
+    }
+    @Test
+    public void testGetBookOK() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/books/query/default";
+        WebClient wc = WebClient.create(address, "alice", "password", null);
+        //WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000);
+        Response r = wc.get();
+        assertEquals(200, r.getStatus());
+        assertEquals(123L, r.readEntity(Book.class).getId());
+    }
+}