You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by jl...@apache.org on 2007/06/19 11:55:47 UTC

svn commit: r548673 - in /incubator/cxf/trunk: rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/ rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/ rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/ systests/src/t...

Author: jliu
Date: Tue Jun 19 02:55:46 2007
New Revision: 548673

URL: http://svn.apache.org/viewvc?view=rev&rev=548673
Log:
CXF-732: Dispatch does not work with LogicalHandlers

Added:
    incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/DispatchLogicalHandlerOutInterceptor.java
Modified:
    incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/DispatchImpl.java
    incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchOutInterceptor.java
    incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationUsingAddNumbersTest.java

Modified: incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/DispatchImpl.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/DispatchImpl.java?view=diff&rev=548673&r1=548672&r2=548673
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/DispatchImpl.java (original)
+++ incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/DispatchImpl.java Tue Jun 19 02:55:46 2007
@@ -31,10 +31,13 @@
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import javax.activation.DataSource;
 import javax.xml.bind.JAXBContext;
 import javax.xml.soap.SOAPException;
 import javax.xml.soap.SOAPFactory;
 import javax.xml.soap.SOAPFault;
+import javax.xml.soap.SOAPMessage;
+import javax.xml.transform.Source;
 import javax.xml.ws.AsyncHandler;
 import javax.xml.ws.Binding;
 import javax.xml.ws.BindingProvider;
@@ -56,7 +59,7 @@
 import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.interceptor.Interceptor;
 import org.apache.cxf.interceptor.MessageSenderInterceptor;
-import org.apache.cxf.jaxws.handler.logical.LogicalHandlerOutInterceptor;
+import org.apache.cxf.jaxws.handler.logical.DispatchLogicalHandlerOutInterceptor;
 import org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor;
 import org.apache.cxf.jaxws.interceptors.DispatchInInterceptor;
 import org.apache.cxf.jaxws.interceptors.DispatchOutInterceptor;
@@ -139,6 +142,15 @@
         setExchangeProperties(exchange, endpoint);
 
         message.setContent(Object.class, obj);
+        
+        if (obj instanceof SOAPMessage) {
+            message.setContent(SOAPMessage.class, obj);
+        } else if (obj instanceof Source) {
+            message.setContent(Source.class, obj);
+        } else if (obj instanceof DataSource) {
+            message.setContent(DataSource.class, obj);
+        }
+  
         message.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
 
         PhaseInterceptorChain chain = getDispatchOutChain(endpoint);
@@ -228,9 +240,8 @@
                 chain.add(new SOAPHandlerInterceptor(jaxwsBinding));
             } else {
                 // TODO: what for non soap bindings?
-            }
-            //endpoint.getInInterceptors().add(new LogicalHandlerInInterceptor(jaxwsBinding));         
-            chain.add(new LogicalHandlerOutInterceptor(jaxwsBinding));
+            }       
+            chain.add(new DispatchLogicalHandlerOutInterceptor(jaxwsBinding));
         }   
         
         chain.add(new MessageSenderInterceptor());

Added: incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/DispatchLogicalHandlerOutInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/DispatchLogicalHandlerOutInterceptor.java?view=auto&rev=548673
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/DispatchLogicalHandlerOutInterceptor.java (added)
+++ incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/handler/logical/DispatchLogicalHandlerOutInterceptor.java Tue Jun 19 02:55:46 2007
@@ -0,0 +1,81 @@
+/**
+ * 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.jaxws.handler.logical;
+
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.ws.Binding;
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.jaxws.handler.AbstractJAXWSHandlerInterceptor;
+import org.apache.cxf.jaxws.handler.HandlerChainInvoker;
+import org.apache.cxf.jaxws.support.ContextPropertiesMapping;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.phase.PhaseInterceptorChain;
+import org.apache.cxf.transport.MessageObserver;
+
+
+public class DispatchLogicalHandlerOutInterceptor<T extends Message> 
+    extends AbstractJAXWSHandlerInterceptor<T> {
+    
+    public static final String ORIGINAL_WRITER = "original_writer";
+     
+    public DispatchLogicalHandlerOutInterceptor(Binding binding) {
+        super(binding, Phase.PRE_MARSHAL);
+    }
+    
+    public void handleMessage(T message) throws Fault {
+        HandlerChainInvoker invoker = getInvoker(message);
+        if (invoker.getLogicalHandlers().isEmpty()) {
+            return;
+        }            
+
+        LogicalMessageContextImpl lctx = new LogicalMessageContextImpl(message);
+        invoker.setLogicalMessageContext(lctx);
+        boolean requestor = isRequestor(message);
+        
+        ContextPropertiesMapping.mapCxf2Jaxws(message.getExchange(), lctx, requestor);          
+        
+        if (!invoker.invokeLogicalHandlers(requestor, lctx)) {
+            if (requestor) {
+                // client side - abort
+                message.getInterceptorChain().abort();
+                Endpoint e = message.getExchange().get(Endpoint.class);
+                Message responseMsg = e.getBinding().createMessage();            
+
+                MessageObserver observer = (MessageObserver)message.getExchange()
+                            .get(MessageObserver.class);
+                if (observer != null) {
+                    //client side outbound, the request message becomes the response message
+                    responseMsg.setContent(XMLStreamReader.class, message
+                        .getContent(XMLStreamReader.class));                        
+                    
+                    message.getExchange().setInMessage(responseMsg);
+                    responseMsg.put(PhaseInterceptorChain.STARTING_AT_INTERCEPTOR_ID,
+                                    LogicalHandlerInInterceptor.class.getName());
+                    observer.onMessage(responseMsg);
+                }
+            } else {
+                // server side - abort
+                //System.out.println("Logical handler server side aborting");
+            }
+        }
+    }
+}

Modified: incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchOutInterceptor.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchOutInterceptor.java?view=diff&rev=548673&r1=548672&r2=548673
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchOutInterceptor.java (original)
+++ incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/DispatchOutInterceptor.java Tue Jun 19 02:55:46 2007
@@ -52,9 +52,11 @@
 
 public class DispatchOutInterceptor extends AbstractOutDatabindingInterceptor {
     private static final Logger LOG = LogUtils.getL7dLogger(DispatchOutInterceptor.class);
+    private DispatchOutEndingInterceptor ending;
 
     public DispatchOutInterceptor() {
         super(Phase.WRITE);
+        ending = new DispatchOutEndingInterceptor();
     }
 
     public void handleMessage(Message message) throws Fault {
@@ -71,24 +73,24 @@
         try {
             if (message instanceof SoapMessage) {
                 if (m == Service.Mode.MESSAGE) {
-                    if (obj instanceof SOAPMessage) {
-                        ((SOAPMessage)obj).writeTo(os);
-                    } else if (obj instanceof Source) {
-                        doTransform(obj, os);
-                    } else if (obj instanceof DataSource) {
+                    if (obj instanceof DataSource) {
                         throw new RuntimeException(obj.getClass()
                                                    + " is not valid in Message mode for SOAP/HTTP");
                     }
                 } else if (m == Service.Mode.PAYLOAD) {
-                    SOAPMessage msg = initSOAPMessage();
-                    DataWriter<Node> dataWriter = getDataWriter(message, service, Node.class);
                     if (obj instanceof SOAPMessage || obj instanceof DataSource) {
                         throw new RuntimeException(obj.getClass()
                                                    + " is not valid in PAYLOAD mode with SOAP/HTTP");
-                    } else {
-                        dataWriter.write(obj, msg.getSOAPBody());
                     }
-                    msg.writeTo(os);
+                    
+                    //Convert Source or JAXB element to SOAPMessage
+                    SOAPMessage msg = initSOAPMessage();
+                    DataWriter<Node> dataWriter = getDataWriter(message, service, Node.class);
+                    dataWriter.write(obj, msg.getSOAPBody());
+                    message.setContent(Object.class, msg);
+                    message.setContent(SOAPMessage.class, msg);   
+                    msg.writeTo(System.out);
+                    //msg.writeTo(os);
                 }
             } else if (message instanceof XMLMessage) {
                 if (m == Service.Mode.MESSAGE
@@ -99,30 +101,58 @@
                                || obj instanceof DataSource)) {
                     throw new RuntimeException(obj.getClass()
                                                + " is not valid in PAYLOAD mode with XML/HTTP");
+                }                
+
+                DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message, service,
+                                                                       XMLStreamWriter.class);
+                XMLStreamWriter xmlWriter = message.getContent(XMLStreamWriter.class);
+                if (xmlWriter == null) {
+                    xmlWriter = StaxUtils.createXMLStreamWriter(os, "UTF-8");
                 }
-                
-                if (obj instanceof Source || obj instanceof DataSource) {
-                    doTransform(obj, os);
-                } else {
-                    DataWriter<XMLStreamWriter> dataWriter = getDataWriter(message, 
-                                                                           service, 
-                                                                           XMLStreamWriter.class);
-                    XMLStreamWriter xmlWriter = message.getContent(XMLStreamWriter.class);
-                    if (xmlWriter == null) {
-                        xmlWriter = StaxUtils.createXMLStreamWriter(os, "UTF-8");
-                    }
-                    dataWriter.write(obj, xmlWriter);
-                    
-                    xmlWriter.flush();
-                }
+                dataWriter.write(obj, xmlWriter);
+                message.setContent(XMLStreamWriter.class, xmlWriter);
+                // xmlWriter.flush();
+
             }
             // Finish the message processing, do flush
-            os.flush();
+            // os.flush();
         } catch (Exception ex) {
             throw new Fault(new org.apache.cxf.common.i18n.Message("EXCEPTION_WRITING_OBJECT", LOG));
         }
+        
+        message.getInterceptorChain().add(ending);
+    }
+    
+    private class DispatchOutEndingInterceptor extends AbstractOutDatabindingInterceptor {
+        public DispatchOutEndingInterceptor() {
+            super(Phase.WRITE_ENDING);
+        }
+        
+        public void handleMessage(Message message) throws Fault {
+            OutputStream os = message.getContent(OutputStream.class);
+            Object obj = message.getContent(Object.class);
+            XMLStreamWriter xmlWriter = message.getContent(XMLStreamWriter.class);
+            
+            try {
+                if (xmlWriter != null) {
+                    xmlWriter.flush();
+                } else if (obj instanceof SOAPMessage) {
+                    ((SOAPMessage)obj).writeTo(os);
+                } else if (obj instanceof Source || obj instanceof DataSource) {
+                    doTransform(obj, os);
+                }
+
+                // Finish the message processing, do flush
+                os.flush();
+            } catch (Exception ex) {
+                throw new Fault(new org.apache.cxf.common.i18n.Message("EXCEPTION_WRITING_OBJECT", LOG));
+            }
+
+        }
+      
     }
 
+        
     private SOAPMessage initSOAPMessage() throws SOAPException {
         SOAPMessage msg = MessageFactory.newInstance().createMessage();
         msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");

Modified: incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationUsingAddNumbersTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationUsingAddNumbersTest.java?view=diff&rev=548673&r1=548672&r2=548673
==============================================================================
--- incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationUsingAddNumbersTest.java (original)
+++ incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/handlers/HandlerInvocationUsingAddNumbersTest.java Tue Jun 19 02:55:46 2007
@@ -26,10 +26,13 @@
 import javax.xml.bind.JAXBElement;
 import javax.xml.namespace.QName;
 
+import javax.xml.soap.SOAPMessage;
 import javax.xml.ws.BindingProvider;
 import javax.xml.ws.Dispatch;
 import javax.xml.ws.Service;
 import javax.xml.ws.handler.Handler;
+import javax.xml.ws.handler.MessageContext;
+import javax.xml.ws.handler.soap.SOAPMessageContext;
 
 
 import org.apache.cxf.Bus;
@@ -41,7 +44,6 @@
 import org.apache.handlers.types.AddNumbersResponse;
 import org.apache.handlers.types.ObjectFactory;
 import org.junit.BeforeClass;
-import org.junit.Ignore;
 import org.junit.Test;
 
 
@@ -85,7 +87,6 @@
     }
     
     @Test
-    @Ignore("This does not work. CXF-732")
     public void testInvokeFromDispatchWithJAXBPayload() throws Exception {
         URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
         assertNotNull(wsdl);
@@ -97,8 +98,26 @@
         Dispatch<Object> disp = service.createDispatch(portName, jc, Service.Mode.PAYLOAD);
  
         SmallNumberHandler sh = new SmallNumberHandler();
-        addHandlersProgrammatically(disp, sh);
-        
+        TestSOAPHandler soapHandler = new TestSOAPHandler<SOAPMessageContext>(false) {
+            public boolean handleMessage(SOAPMessageContext ctx) {
+                super.handleMessage(ctx);
+                Boolean outbound = (Boolean)ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
+                if (outbound) {
+                    try {
+                        SOAPMessage msg = ctx.getMessage();
+                        //System.out.println("aaaaaaaaaaaa");
+                        //msg.writeTo(System.out);
+                        assertNotNull(msg);
+                    } catch (Exception e) {
+                        e.printStackTrace();
+                        fail(e.toString());
+                    }
+                }
+                return true;
+            }
+        };
+        addHandlersProgrammatically(disp, sh, soapHandler);
+      
         org.apache.handlers.types.AddNumbers req = new org.apache.handlers.types.AddNumbers();        
         req.setArg0(10);
         req.setArg1(20);