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 2009/10/21 02:43:28 UTC

svn commit: r827859 - in /cxf/branches/2.2.x-fixes: ./ api/src/main/java/org/apache/cxf/message/ rt/bindings/coloc/src/main/java/org/apache/cxf/binding/coloc/ rt/core/src/main/java/org/apache/cxf/endpoint/ rt/core/src/main/java/org/apache/cxf/intercept...

Author: dkulp
Date: Wed Oct 21 00:43:27 2009
New Revision: 827859

URL: http://svn.apache.org/viewvc?rev=827859&view=rev
Log:
Merged revisions 827857 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r827857 | dkulp | 2009-10-20 20:33:19 -0400 (Tue, 20 Oct 2009) | 2 lines
  
  [CXF-2015] Add support to add interceptors per request as well as allow
  Destinations to be InterceptorProviders
........

Modified:
    cxf/branches/2.2.x-fixes/   (props changed)
    cxf/branches/2.2.x-fixes/api/src/main/java/org/apache/cxf/message/Message.java
    cxf/branches/2.2.x-fixes/rt/bindings/coloc/src/main/java/org/apache/cxf/binding/coloc/ColocUtil.java
    cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java
    cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/InFaultChainInitiatorObserver.java
    cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutFaultChainInitiatorObserver.java
    cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java
    cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/transport/ChainInitiationObserver.java
    cxf/branches/2.2.x-fixes/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/ClientServerTest.java

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Oct 21 00:43:27 2009
@@ -1 +1 @@
-/cxf/trunk:827711
+/cxf/trunk:827711,827857

Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.2.x-fixes/api/src/main/java/org/apache/cxf/message/Message.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/api/src/main/java/org/apache/cxf/message/Message.java?rev=827859&r1=827858&r2=827859&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/api/src/main/java/org/apache/cxf/message/Message.java (original)
+++ cxf/branches/2.2.x-fixes/api/src/main/java/org/apache/cxf/message/Message.java Wed Oct 21 00:43:27 2009
@@ -99,7 +99,21 @@
     String WSDL_INTERFACE = "javax.xml.ws.wsdl.interface";
     String WSDL_OPERATION = "javax.xml.ws.wsdl.operation";
 
-    
+    /**
+     * Some properties to allow adding interceptors to the chain
+     * on a per-request basis.  All are a Collection<Interceptor> 
+     * These are NOT contextual properties (ie: not searched outside the message).
+     * They must exist on the message itself at time of Chain creation
+     */
+    String IN_INTERCEPTORS = Message.class.getName() + ".IN_INTERCEPTORS";
+    String OUT_INTERCEPTORS = Message.class.getName() + ".OUT_INTERCEPTORS";
+    String FAULT_IN_INTERCEPTORS = Message.class.getName() + ".FAULT_IN_INTERCEPTORS";
+    String FAULT_OUT_INTERCEPTORS = Message.class.getName() + ".FAULT_OUT_INTERCEPTORS";
+    /**
+     * As above, but Collection<InterceptorProvider> 
+     */
+    String INTERCEPTOR_PROVIDERS = Message.class.getName() + ".INTERCEPTOR_PROVIDER";
+
     String getId();
     void setId(String id);
     

Modified: cxf/branches/2.2.x-fixes/rt/bindings/coloc/src/main/java/org/apache/cxf/binding/coloc/ColocUtil.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/bindings/coloc/src/main/java/org/apache/cxf/binding/coloc/ColocUtil.java?rev=827859&r1=827858&r2=827859&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/bindings/coloc/src/main/java/org/apache/cxf/binding/coloc/ColocUtil.java (original)
+++ cxf/branches/2.2.x-fixes/rt/bindings/coloc/src/main/java/org/apache/cxf/binding/coloc/ColocUtil.java Wed Oct 21 00:43:27 2009
@@ -28,10 +28,12 @@
 import org.apache.cxf.Bus;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.interceptor.Interceptor;
 import org.apache.cxf.interceptor.InterceptorChain;
 import org.apache.cxf.interceptor.InterceptorProvider;
 import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 import org.apache.cxf.service.model.FaultInfo;
@@ -91,8 +93,8 @@
                 LOG.fine("Interceptors contributed by databinding: " + il);
             }
             chain.add(il);
-
         }
+        modifyChain(chain, ex, false);
 
         return chain;
     }
@@ -126,10 +128,35 @@
             chain.add(il);
         }
         chain.setFaultObserver(new ColocOutFaultObserver(bus));
-
+        modifyChain(chain, ex, true);
         return chain;
     }    
-    
+    private static void modifyChain(PhaseInterceptorChain chain, Exchange ex, boolean in) {
+        modifyChain(chain, ex.getInMessage(), in);
+        modifyChain(chain, ex.getOutMessage(), in);
+    }
+    private static void modifyChain(PhaseInterceptorChain chain, Message m, boolean in) {
+        if (m == null) {
+            return;
+        }
+        Collection<InterceptorProvider> providers 
+            = CastUtils.cast((Collection<?>)m.get(Message.INTERCEPTOR_PROVIDERS));
+        if (providers != null) {
+            for (InterceptorProvider p : providers) {
+                if (in) {
+                    chain.add(p.getInInterceptors());
+                } else {
+                    chain.add(p.getOutInterceptors());
+                }
+            }
+        }
+        String key = in ? Message.IN_INTERCEPTORS : Message.OUT_INTERCEPTORS;
+        Collection<Interceptor> is = CastUtils.cast((Collection<?>)m.get(key));
+        if (is != null) {
+            chain.add(is);
+        }
+    }
+
     public static boolean isSameOperationInfo(OperationInfo oi1, OperationInfo oi2) {
         return  oi1.getName().equals(oi2.getName())
                 && isSameMessageInfo(oi1.getInput(), oi2.getInput())

Modified: cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java?rev=827859&r1=827858&r2=827859&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java (original)
+++ cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/endpoint/ClientImpl.java Wed Oct 21 00:43:27 2009
@@ -22,6 +22,7 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -408,11 +409,13 @@
             PhaseInterceptorChain chain = setupInterceptorChain(endpoint);
             message.setInterceptorChain(chain);
 
-            modifyChain(chain, reqContext);
             chain.setFaultObserver(outFaultObserver);
 
             // setup conduit selector
             prepareConduitSelector(message);
+            
+            // add additional interceptors and such
+            modifyChain(chain, message, false);
 
             // execute chain
             chain.doIntercept(message);
@@ -468,12 +471,14 @@
             PhaseInterceptorChain chain = setupInterceptorChain(endpoint);
             message.setInterceptorChain(chain);
 
-            modifyChain(chain, reqContext);
             chain.setFaultObserver(outFaultObserver);
 
             // setup conduit selector
             prepareConduitSelector(message);
 
+            // add additional interceptors and such
+            modifyChain(chain, message, false);
+            
             // execute chain
             chain.doIntercept(message);
 
@@ -636,7 +641,9 @@
         message.setInterceptorChain(chain);
 
         chain.setFaultObserver(outFaultObserver);
-
+        modifyChain(chain, message, true);
+        modifyChain(chain, message.getExchange().getOutMessage(), true);
+        
         Bus origBus = BusFactory.getThreadDefaultBus(false);
         BusFactory.setThreadDefaultBus(bus);
         // execute chain
@@ -812,8 +819,26 @@
         return outboundChainCache.get(pm.getOutPhases(), i1, i2, i3, i4);
     }
 
-    protected void modifyChain(InterceptorChain chain, Map<String, Object> ctx) {
-        // no-op
+    protected void modifyChain(InterceptorChain chain, Message ctx, boolean in) {
+        if (ctx == null) {
+            return;
+        }
+        Collection<InterceptorProvider> providers 
+            = CastUtils.cast((Collection<?>)ctx.get(Message.INTERCEPTOR_PROVIDERS));
+        if (providers != null) {
+            for (InterceptorProvider p : providers) {
+                if (in) {
+                    chain.add(p.getInInterceptors());
+                } else {
+                    chain.add(p.getOutInterceptors());
+                }
+            }
+        }
+        String key = in ? Message.IN_INTERCEPTORS : Message.OUT_INTERCEPTORS;
+        Collection<Interceptor> is = CastUtils.cast((Collection<?>)ctx.get(key));
+        if (is != null) {
+            chain.add(is);
+        }
     }
 
     protected void setEndpoint(Endpoint e) {

Modified: cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/InFaultChainInitiatorObserver.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/InFaultChainInitiatorObserver.java?rev=827859&r1=827858&r2=827859&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/InFaultChainInitiatorObserver.java (original)
+++ cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/InFaultChainInitiatorObserver.java Wed Oct 21 00:43:27 2009
@@ -18,12 +18,15 @@
  */
 package org.apache.cxf.interceptor;
 
+import java.util.Collection;
 import java.util.SortedSet;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 import org.apache.cxf.phase.PhaseManager;
@@ -51,6 +54,22 @@
         if (e.getService().getDataBinding() instanceof InterceptorProvider) {
             chain.add(((InterceptorProvider)e.getService().getDataBinding()).getInFaultInterceptors());
         }
+        
+        addToChain(chain, ex.getInFaultMessage());
+        addToChain(chain, ex.getOutMessage());
+    }
+    private void addToChain(PhaseInterceptorChain chain, Message m) {
+        Collection<InterceptorProvider> providers 
+            = CastUtils.cast((Collection<?>)m.get(Message.INTERCEPTOR_PROVIDERS));
+        if (providers != null) {
+            for (InterceptorProvider p : providers) {
+                chain.add(p.getInFaultInterceptors());
+            }
+        }
+        Collection<Interceptor> is = CastUtils.cast((Collection<?>)m.get(Message.FAULT_IN_INTERCEPTORS));
+        if (is != null) {
+            chain.add(is);
+        }
     }
     
     protected SortedSet<Phase> getPhases() {

Modified: cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutFaultChainInitiatorObserver.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutFaultChainInitiatorObserver.java?rev=827859&r1=827858&r2=827859&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutFaultChainInitiatorObserver.java (original)
+++ cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutFaultChainInitiatorObserver.java Wed Oct 21 00:43:27 2009
@@ -18,12 +18,15 @@
  */
 package org.apache.cxf.interceptor;
 
+import java.util.Collection;
 import java.util.SortedSet;
 
 import org.apache.cxf.Bus;
 import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.phase.PhaseInterceptorChain;
 import org.apache.cxf.phase.PhaseManager;
@@ -48,6 +51,25 @@
         if (e.getService().getDataBinding() instanceof InterceptorProvider) {
             chain.add(((InterceptorProvider)e.getService().getDataBinding()).getOutFaultInterceptors());
         }
+        
+        addToChain(chain, ex.getInMessage());
+        addToChain(chain, ex.getOutFaultMessage());
+    }
+    private void addToChain(PhaseInterceptorChain chain, Message m) {
+        Collection<InterceptorProvider> providers 
+            = CastUtils.cast((Collection<?>)m.get(Message.INTERCEPTOR_PROVIDERS));
+        if (providers != null) {
+            for (InterceptorProvider p : providers) {
+                chain.add(p.getOutFaultInterceptors());
+            }
+        }
+        Collection<Interceptor> is = CastUtils.cast((Collection<?>)m.get(Message.FAULT_OUT_INTERCEPTORS));
+        if (is != null) {
+            chain.add(is);
+        }
+        if (m.getDestination() instanceof InterceptorProvider) {
+            chain.add(((InterceptorProvider)m.getDestination()).getOutFaultInterceptors());
+        }
     }
     
     protected SortedSet<Phase> getPhases() {

Modified: cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java?rev=827859&r1=827858&r2=827859&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java (original)
+++ cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/interceptor/OutgoingChainInterceptor.java Wed Oct 21 00:43:27 2009
@@ -20,6 +20,7 @@
 package org.apache.cxf.interceptor;
 
 import java.io.IOException;
+import java.util.Collection;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -30,6 +31,7 @@
 import org.apache.cxf.endpoint.ConduitSelector;
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.endpoint.PreexistingConduitSelector;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.message.Exchange;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.phase.AbstractPhaseInterceptor;
@@ -123,10 +125,33 @@
             }
             chain.add(il);
         }
+        modifyChain(chain, ex);
         chain.setFaultObserver(ep.getOutFaultObserver());
         return chain;
     }
-
+    private static void modifyChain(PhaseInterceptorChain chain, Exchange ex) {
+        modifyChain(chain, ex.getInMessage());
+        modifyChain(chain, ex.getOutMessage());
+    }
+    private static void modifyChain(PhaseInterceptorChain chain, Message m) {
+        if (m == null) {
+            return;
+        }
+        Collection<InterceptorProvider> providers 
+            = CastUtils.cast((Collection<?>)m.get(Message.INTERCEPTOR_PROVIDERS));
+        if (providers != null) {
+            for (InterceptorProvider p : providers) {
+                chain.add(p.getOutInterceptors());
+            }
+        }
+        Collection<Interceptor> is = CastUtils.cast((Collection<?>)m.get(Message.OUT_INTERCEPTORS));
+        if (is != null) {
+            chain.add(is);
+        }
+        if (m.getDestination() instanceof InterceptorProvider) {
+            chain.add(((InterceptorProvider)m.getDestination()).getOutInterceptors());
+        }
+    }
     private PhaseInterceptorChain getChain(Exchange ex) {
         Bus bus = ex.get(Bus.class);
         Binding binding = ex.get(Binding.class);
@@ -175,6 +200,7 @@
                                    i1, i2, i3);
         }
         
+        modifyChain(chain, ex);
         chain.setFaultObserver(ep.getOutFaultObserver());
         return chain;
     }

Modified: cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/transport/ChainInitiationObserver.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/transport/ChainInitiationObserver.java?rev=827859&r1=827858&r2=827859&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/transport/ChainInitiationObserver.java (original)
+++ cxf/branches/2.2.x-fixes/rt/core/src/main/java/org/apache/cxf/transport/ChainInitiationObserver.java Wed Oct 21 00:43:27 2009
@@ -22,6 +22,7 @@
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.Collection;
 
 import javax.xml.namespace.QName;
 
@@ -29,6 +30,8 @@
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.binding.Binding;
 import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.interceptor.Interceptor;
 import org.apache.cxf.interceptor.InterceptorChain;
 import org.apache.cxf.interceptor.InterceptorProvider;
 import org.apache.cxf.message.Exchange;
@@ -101,12 +104,29 @@
             
             phaseChain.setFaultObserver(endpoint.getOutFaultObserver());
            
+            addToChain(phaseChain, message);
+            
             phaseChain.doIntercept(message);
         } finally {
             BusFactory.setThreadDefaultBus(origBus);
         }
     }
-
+    private void addToChain(PhaseInterceptorChain chain, Message m) {
+        Collection<InterceptorProvider> providers 
+            = CastUtils.cast((Collection<?>)m.get(Message.INTERCEPTOR_PROVIDERS));
+        if (providers != null) {
+            for (InterceptorProvider p : providers) {
+                chain.add(p.getInInterceptors());
+            }
+        }
+        Collection<Interceptor> is = CastUtils.cast((Collection<?>)m.get(Message.IN_INTERCEPTORS));
+        if (is != null) {
+            chain.add(is);
+        }
+        if (m.getDestination() instanceof InterceptorProvider) {
+            chain.add(((InterceptorProvider)m.getDestination()).getInInterceptors());
+        }
+    }
 
     protected Binding getBinding() {
         return endpoint.getBinding();

Modified: cxf/branches/2.2.x-fixes/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/ClientServerTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/ClientServerTest.java?rev=827859&r1=827858&r2=827859&view=diff
==============================================================================
--- cxf/branches/2.2.x-fixes/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/ClientServerTest.java (original)
+++ cxf/branches/2.2.x-fixes/rt/frontend/simple/src/test/java/org/apache/cxf/frontend/spring/ClientServerTest.java Wed Oct 21 00:43:27 2009
@@ -17,9 +17,18 @@
  * under the License.
  */
 package org.apache.cxf.frontend.spring;
+import java.util.Arrays;
+
 import junit.framework.Assert;
 
 import org.apache.cxf.BusFactory;
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.frontend.ClientProxy;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.interceptor.Interceptor;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
 import org.apache.cxf.service.factory.HelloService;
 import org.junit.Test;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -39,6 +48,39 @@
         
         String result = greeter.sayHello();
         assertEquals("We get the wrong sayHello result", result, "hello");
+        
+        
+        Client c = ClientProxy.getClient(greeter);
+        TestInterceptor out = new TestInterceptor();
+        TestInterceptor in = new TestInterceptor();
+        c.getRequestContext().put(Message.OUT_INTERCEPTORS, Arrays.asList(new Interceptor[] {out}));
+        result = greeter.sayHello();
+        assertTrue(out.wasCalled());
+        out.reset();
+
+        c.getRequestContext().put(Message.IN_INTERCEPTORS, Arrays.asList(new Interceptor[] {in}));
+        result = greeter.sayHello();
+        assertTrue(out.wasCalled());
+        assertTrue(in.wasCalled());
+    }
+    
+    private class TestInterceptor extends AbstractPhaseInterceptor<Message> {
+        boolean called;
+        
+        public TestInterceptor() {
+            super(Phase.USER_LOGICAL);
+        }
+        
+        public void handleMessage(Message message) throws Fault {
+            called = true;
+        }
+        public boolean wasCalled() {
+            return called;
+        }
+        public void reset() {
+            called = false;
+        }
     }
+    
 
 }