Fixed bunch of camel-cxf unit test errors when using CXF 2.7.7-SNAPSHOT

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

Branch: refs/heads/camel-2.12.x
Commit: 4a8526325a5d189910f5a470f6872575a278b76c
Parents: b3f2593
Author: Willem Jiang <willem.ji...@gmail.com>
Authored: Fri Sep 6 15:20:36 2013 +0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Fri Sep 6 15:36:21 2013 +0800

----------------------------------------------------------------------
 .../cxf/feature/RAWDataFormatFeature.java       |  11 +-
 .../RawMessageWSDLGetInterceptor.java           | 100 +++++++++++++++++++
 .../RawMessageWSDLGetOutInterceptor.java        |  66 ++++++++++++
 .../cxf/CxfWsdlFirstPayloadModeTest.java        |  12 +++
 4 files changed, 183 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4a852632/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/RAWDataFormatFeature.java
----------------------------------------------------------------------
diff --git 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/RAWDataFormatFeature.java
 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/RAWDataFormatFeature.java
index 276cd2b..ead2489 100644
--- 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/RAWDataFormatFeature.java
+++ 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/feature/RAWDataFormatFeature.java
@@ -18,10 +18,10 @@
 package org.apache.camel.component.cxf.feature;
 
 import 
org.apache.camel.component.cxf.interceptors.RawMessageContentRedirectInterceptor;
+import 
org.apache.camel.component.cxf.interceptors.RawMessageWSDLGetInterceptor;
 import org.apache.cxf.Bus;
 import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.endpoint.Server;
-import org.apache.cxf.frontend.WSDLGetInterceptor;
 import org.apache.cxf.interceptor.LoggingOutInterceptor;
 import org.apache.cxf.phase.Phase;
 import org.slf4j.Logger;
@@ -69,10 +69,7 @@ public class RAWDataFormatFeature extends 
AbstractDataFormatFeature {
     public void initialize(Server server, Bus bus) {
         // currently we do not filter the bus
         // remove the interceptors
-        
-        // keep the WSDLGetInterceptor
-        getInInterceptorNames().add(WSDLGetInterceptor.class.getName());
-        
+
         
removeInterceptorWhichIsOutThePhases(server.getEndpoint().getService().getInInterceptors(),
 REMAINING_IN_PHASES, getInInterceptorNames());
         
removeInterceptorWhichIsOutThePhases(server.getEndpoint().getInInterceptors(), 
REMAINING_IN_PHASES, getInInterceptorNames());
 
@@ -89,7 +86,9 @@ public class RAWDataFormatFeature extends 
AbstractDataFormatFeature {
         // Do not use the binding interceptor any more
         server.getEndpoint().getBinding().getOutInterceptors().clear();
         server.getEndpoint().getOutInterceptors().add(new 
RawMessageContentRedirectInterceptor());
-        
+
+        // setup the RawMessageWSDLGetInterceptor
+        
server.getEndpoint().getInInterceptors().add(RawMessageWSDLGetInterceptor.INSTANCE);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/4a852632/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageWSDLGetInterceptor.java
----------------------------------------------------------------------
diff --git 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageWSDLGetInterceptor.java
 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageWSDLGetInterceptor.java
new file mode 100644
index 0000000..5e9ca39
--- /dev/null
+++ 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageWSDLGetInterceptor.java
@@ -0,0 +1,100 @@
+package org.apache.camel.component.cxf.interceptors;
+
+import org.apache.cxf.binding.soap.interceptor.EndpointSelectionInterceptor;
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.common.util.UrlUtils;
+import org.apache.cxf.endpoint.Endpoint;
+import org.apache.cxf.frontend.WSDLGetUtils;
+import org.apache.cxf.interceptor.*;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.service.model.EndpointInfo;
+import org.w3c.dom.Document;
+
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * Just copy the from WSDLGetInterceptor to provide backward compatible 
support for 2.7.x
+ */
+public class RawMessageWSDLGetInterceptor extends 
AbstractPhaseInterceptor<Message> {
+    public static final RawMessageWSDLGetInterceptor INSTANCE = new 
RawMessageWSDLGetInterceptor();
+    public static final String DOCUMENT_HOLDER = 
RawMessageWSDLGetInterceptor.class.getName() + ".documentHolder";
+
+    public RawMessageWSDLGetInterceptor() {
+        super(Phase.READ);
+        getAfter().add(EndpointSelectionInterceptor.class.getName());
+    }
+
+    public void handleMessage(Message message) throws Fault {
+        String method = (String)message.get(Message.HTTP_REQUEST_METHOD);
+        String query = (String)message.get(Message.QUERY_STRING);
+
+        if (!"GET".equals(method) || StringUtils.isEmpty(query)) {
+            return;
+        }
+
+        String baseUri = (String)message.get(Message.REQUEST_URL);
+        String ctx = (String)message.get(Message.PATH_INFO);
+
+        Map<String, String> map = UrlUtils.parseQueryString(query);
+        if (isRecognizedQuery(map, baseUri, ctx, 
message.getExchange().getEndpoint().getEndpointInfo())) {
+            Document doc = getDocument(message, baseUri, map, ctx);
+
+            Endpoint e = message.getExchange().get(Endpoint.class);
+            Message mout = new MessageImpl();
+            mout.setExchange(message.getExchange());
+            mout = e.getBinding().createMessage(mout);
+            
mout.setInterceptorChain(OutgoingChainInterceptor.getOutInterceptorChain(message.getExchange()));
+            message.getExchange().setOutMessage(mout);
+
+            mout.put(DOCUMENT_HOLDER, doc);
+
+            Iterator<Interceptor<? extends Message>> iterator = 
mout.getInterceptorChain().iterator();
+            while (iterator.hasNext()) {
+                Interceptor<? extends Message> inInterceptor = iterator.next();
+                if (inInterceptor instanceof AbstractPhaseInterceptor) {
+                    AbstractPhaseInterceptor interceptor = 
(AbstractPhaseInterceptor)inInterceptor;
+                    if (interceptor.getPhase().equals(Phase.PREPARE_SEND)
+                        || interceptor.getPhase().equals(Phase.PRE_STREAM)) {
+                        // just make sure we keep the right interceptors
+                        continue;
+                    }
+                }
+                mout.getInterceptorChain().remove(inInterceptor);
+            }
+
+            // notice this is being added after the purge above, don't swap 
the order!
+            
mout.getInterceptorChain().add(RawMessageWSDLGetOutInterceptor.INSTANCE);
+
+            // skip the service executor and goto the end of the chain.
+            message.getInterceptorChain().doInterceptStartingAt(
+                    message,
+                    OutgoingChainInterceptor.class.getName());
+        }
+    }
+
+    private Document getDocument(Message message, String base, Map<String, 
String> params, String ctxUri) {
+        // cannot have two wsdl's being generated for the same endpoint at the 
same
+        // time as the addresses may get mixed up
+        // For WSDL's the WSDLWriter does not share any state between 
documents.
+        // For XSD's, the WSDLGetUtils makes a copy of any XSD schema 
documents before updating
+        // any addresses and returning them, so for both WSDL and XSD this is 
the only part that needs
+        // to be synchronized.
+        synchronized (message.getExchange().getEndpoint()) {
+            return new WSDLGetUtils().getDocument(message, base, params, 
ctxUri,
+                    message.getExchange().getEndpoint().getEndpointInfo());
+        }
+    }
+
+    private boolean isRecognizedQuery(Map<String, String> map, String baseUri, 
String ctx,
+                                      EndpointInfo endpointInfo) {
+
+        if (map.containsKey("wsdl") || map.containsKey("xsd")) {
+            return true;
+        }
+        return false;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4a852632/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageWSDLGetOutInterceptor.java
----------------------------------------------------------------------
diff --git 
a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageWSDLGetOutInterceptor.java
 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageWSDLGetOutInterceptor.java
new file mode 100644
index 0000000..5c081bc
--- /dev/null
+++ 
b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/interceptors/RawMessageWSDLGetOutInterceptor.java
@@ -0,0 +1,66 @@
+/**
+ * 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.camel.component.cxf.interceptors;
+
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.w3c.dom.Document;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import java.io.OutputStream;
+
+public class RawMessageWSDLGetOutInterceptor extends 
AbstractPhaseInterceptor<Message> {
+    public static final RawMessageWSDLGetOutInterceptor INSTANCE = new 
RawMessageWSDLGetOutInterceptor();
+
+    public RawMessageWSDLGetOutInterceptor() {
+        super(Phase.PRE_STREAM);
+    }
+
+    public void handleMessage(Message message) throws Fault {
+
+        Document doc = 
(Document)message.get(RawMessageWSDLGetInterceptor.DOCUMENT_HOLDER);
+        if (doc == null) {
+            return;
+        }
+        message.remove(RawMessageWSDLGetInterceptor.DOCUMENT_HOLDER);
+
+        OutputStream out = message.getContent(OutputStream.class);
+
+        String enc = null;
+        try {
+            enc = doc.getXmlEncoding();
+        } catch (Exception ex) {
+            //ignore - not dom level 3
+        }
+        if (enc == null) {
+            enc = "utf-8";
+        }
+
+        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out, enc);
+        try {
+            StaxUtils.writeNode(doc, writer, true);
+            writer.flush();
+        } catch (XMLStreamException e) {
+            throw new Fault(e);
+        }
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4a852632/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfWsdlFirstPayloadModeTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfWsdlFirstPayloadModeTest.java
 
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfWsdlFirstPayloadModeTest.java
index c1ad6ac..772dd25 100644
--- 
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfWsdlFirstPayloadModeTest.java
+++ 
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/CxfWsdlFirstPayloadModeTest.java
@@ -17,6 +17,8 @@
 package org.apache.camel.component.cxf;
 
 import javax.xml.ws.Endpoint;
+
+import org.apache.camel.wsdl_first.JaxwsTestHandler;
 import org.apache.camel.wsdl_first.PersonImpl;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -47,6 +49,16 @@ public class CxfWsdlFirstPayloadModeTest extends 
AbstractCxfWsdlFirstTest {
     public void testInvokingServiceWithCamelProducer() throws Exception {
         // this test does not apply to PAYLOAD mode
     }
+
+    protected void verifyJaxwsHandlers(JaxwsTestHandler fromHandler, 
JaxwsTestHandler toHandler) {
+        assertEquals(2, fromHandler.getFaultCount());
+        assertEquals(4, fromHandler.getMessageCount());
+        // Since CXF 2.2.7 there are some performance improvement to use the 
stax as much as possible
+        // which causes the XML validate doesn't work on the from endpoint
+        // So we skip the toHandler messageCount here
+        //assertEquals(3, toHandler.getMessageCount());
+        assertEquals(1, toHandler.getFaultCount());
+    }
     
 
 

Reply via email to