You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2015/03/23 16:18:35 UTC

[2/2] cxf git commit: Add a ThrottlingFeature to make configuring throttling easier

Add a ThrottlingFeature to make configuring throttling easier


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

Branch: refs/heads/master
Commit: ed18c008f05af7063d5929d072b86048d12dab4c
Parents: eb56937
Author: Daniel Kulp <dk...@apache.org>
Authored: Mon Mar 23 11:05:59 2015 -0400
Committer: Daniel Kulp <dk...@apache.org>
Committed: Mon Mar 23 11:05:59 2015 -0400

----------------------------------------------------------------------
 .../java/demo/throttling/server/Server.java     | 19 ++++---
 .../cxf/throttling/ThrottlingFeature.java       | 53 ++++++++++++++++++++
 .../cxf/throttling/ThrottlingInterceptor.java   |  2 +-
 .../cxf/throttling/ThrottlingManager.java       | 13 ++++-
 4 files changed, 79 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/ed18c008/distribution/src/main/release/samples/throttling/src/main/java/demo/throttling/server/Server.java
----------------------------------------------------------------------
diff --git a/distribution/src/main/release/samples/throttling/src/main/java/demo/throttling/server/Server.java b/distribution/src/main/release/samples/throttling/src/main/java/demo/throttling/server/Server.java
index 926ae31..28600fa 100644
--- a/distribution/src/main/release/samples/throttling/src/main/java/demo/throttling/server/Server.java
+++ b/distribution/src/main/release/samples/throttling/src/main/java/demo/throttling/server/Server.java
@@ -19,7 +19,9 @@
 
 package demo.throttling.server;
 
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.xml.ws.Endpoint;
@@ -31,7 +33,7 @@ import org.apache.cxf.BusFactory;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.metrics.MetricsFeature;
 import org.apache.cxf.phase.Phase;
-import org.apache.cxf.throttling.ThrottlingInterceptor;
+import org.apache.cxf.throttling.ThrottlingFeature;
 import org.apache.cxf.throttling.ThrottlingManager;
 
 public class Server {
@@ -51,22 +53,27 @@ public class Server {
         
         ThrottlingManager manager = new ThrottlingManager() {
             @Override
-            public long getThrottleDelay(Message m) {
+            public long getThrottleDelay(String phase, Message m) {
                 if (m.get("THROTTLED") != null) {
                     return 0;
                 }
                 m.put("THROTTLED", true);
                 Customer c = m.getExchange().get(Customer.class);
                 return c.throttle(m);
-            }           
+            }
+
+            @Override
+            public List<String> getDecisionPhases() {
+                return Collections.singletonList(Phase.PRE_STREAM);
+            }
         };
         b.getInInterceptors().add(new CustomerMetricsInterceptor(registry, customers));
-        b.getInInterceptors().add(new ThrottlingInterceptor(Phase.PRE_STREAM, manager));
-        //add Throttling
         
         Object implementor = new GreeterImpl();
         String address = "http://localhost:9001/SoapContext/SoapPort";
-        Endpoint.publish(address, implementor, new MetricsFeature());
+        Endpoint.publish(address, implementor, 
+                         new MetricsFeature(),
+                         new ThrottlingFeature(manager));
     }
 
     public static void main(String args[]) throws Exception {

http://git-wip-us.apache.org/repos/asf/cxf/blob/ed18c008/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingFeature.java
----------------------------------------------------------------------
diff --git a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingFeature.java b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingFeature.java
new file mode 100644
index 0000000..3580213
--- /dev/null
+++ b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingFeature.java
@@ -0,0 +1,53 @@
+/**
+ * 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.throttling;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.feature.AbstractFeature;
+import org.apache.cxf.interceptor.InterceptorProvider;
+
+/**
+ * 
+ */
+public class ThrottlingFeature extends AbstractFeature {
+    final ThrottlingManager manager;
+    
+    public ThrottlingFeature() {
+        manager = null;
+    }
+
+    public ThrottlingFeature(ThrottlingManager manager) {
+        this.manager = manager;
+    }
+    
+    @Override
+    protected void initializeProvider(InterceptorProvider provider, Bus bus) {
+        ThrottlingManager m = manager;
+        if (m == null) {
+            m = bus.getExtension(ThrottlingManager.class);
+        }
+        if (m == null) {
+            throw new IllegalArgumentException("ThrottlingManager must not be null");
+        }
+        for (String p : m.getDecisionPhases()) {
+            provider.getInInterceptors().add(new ThrottlingInterceptor(p, m));
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/ed18c008/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 f35e144..d56e286 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
@@ -42,7 +42,7 @@ public class ThrottlingInterceptor extends AbstractPhaseInterceptor<Message> {
 
     @Override
     public void handleMessage(Message message) throws Fault {
-        long l = manager.getThrottleDelay(message);
+        long l = manager.getThrottleDelay(getPhase(), message);
         if (l > 0) {
             ContinuationProvider cp = message.get(ContinuationProvider.class);
             if (cp == null) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/ed18c008/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingManager.java
----------------------------------------------------------------------
diff --git a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingManager.java b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingManager.java
index ea7c841..59d7289 100644
--- a/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingManager.java
+++ b/rt/features/throttling/src/main/java/org/apache/cxf/throttling/ThrottlingManager.java
@@ -19,6 +19,8 @@
 
 package org.apache.cxf.throttling;
 
+import java.util.List;
+
 import org.apache.cxf.message.Message;
 
 /**
@@ -27,9 +29,18 @@ import org.apache.cxf.message.Message;
 public interface ThrottlingManager {
     
     /**
+     * Get the list of phases where this manager will expect to have to make throttling decisions.
+     * For example: using BasicAuth or other protocol based header, it can be a very early in the 
+     * chain, but for WS-Security based authentication, it would be later. 
+     * @return
+     */
+    List<String> getDecisionPhases();
+    
+    /**
      * Returns the number of milliseconds the request should be delayed before further processing
+     * @param phase
      * @param m
      * @return
      */
-    long getThrottleDelay(Message m);
+    long getThrottleDelay(String phase, Message m);
 }