cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject cxf git commit: [CXF-6394] Add an option to put the namespace map of the soap body element into the message
Date Mon, 11 May 2015 08:47:17 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes 07c41afc4 -> 47a3323b8


[CXF-6394] Add an option to put the namespace map of the soap body element into the message


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

Branch: refs/heads/2.7.x-fixes
Commit: 47a3323b85ee2c2ba4ee8dbf51a6766c09e88dde
Parents: 07c41af
Author: Akitoshi Yoshida <ay@apache.org>
Authored: Wed May 6 23:25:19 2015 +0200
Committer: Akitoshi Yoshida <ay@apache.org>
Committed: Mon May 11 10:25:04 2015 +0200

----------------------------------------------------------------------
 .../interceptor/ReadHeadersInterceptor.java     | 30 ++++++-
 .../interceptor/ReadHeadersInterceptorTest.java | 83 ++++++++++++++++++++
 2 files changed, 112 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/47a3323b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptor.java
----------------------------------------------------------------------
diff --git a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptor.java
b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptor.java
index f8c2c66..8acbfb2 100644
--- a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptor.java
+++ b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptor.java
@@ -22,7 +22,9 @@ package org.apache.cxf.binding.soap.interceptor;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.xml.namespace.QName;
@@ -56,6 +58,7 @@ import org.apache.cxf.headers.HeaderProcessor;
 import org.apache.cxf.helpers.DOMUtils;
 import org.apache.cxf.helpers.ServiceUtils;
 import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.MessageUtils;
 import org.apache.cxf.phase.Phase;
 import org.apache.cxf.staxutils.PartialXMLStreamReader;
 import org.apache.cxf.staxutils.StaxUtils;
@@ -133,7 +136,8 @@ public class ReadHeadersInterceptor extends AbstractSoapInterceptor {
         message.setVersion(soapVersion);
         return soapVersion;
     }
-    
+
+    //CHECKSTYLE:OFF MethodLength
     public void handleMessage(SoapMessage message) {
         if (isGET(message)) {
             LOG.fine("ReadHeadersInterceptor skipped in HTTP GET method");
@@ -186,6 +190,14 @@ public class ReadHeadersInterceptor extends AbstractSoapInterceptor {
                     doc = (Document)nd;
                     StaxUtils.readDocElements(doc, doc, filteredReader, false, false);
                 } else {
+                    final boolean addNC = 
+                        MessageUtils.getContextualBoolean(
+                            message, "org.apache.cxf.binding.soap.addNamespaceContext", false);
+                    Map<String, String> bodyNC = addNC ? new HashMap<String, String>()
: null;
+                    if (addNC) {
+                        // add the Envelope-Level declarations
+                        addCurrentNamespaceDecls(xmlReader, bodyNC);
+                    }
                     HeadersProcessor processor = new HeadersProcessor(soapVersion);
                     doc = processor.process(filteredReader);
                     if (doc != null) {
@@ -194,6 +206,11 @@ public class ReadHeadersInterceptor extends AbstractSoapInterceptor {
                         message.put(ENVELOPE_EVENTS, processor.getEnvAttributeAndNamespaceEvents());
                         message.put(BODY_EVENTS, processor.getBodyAttributeAndNamespaceEvents());
                     }
+                    if (addNC) {
+                        // add the Body-level declarations
+                        addCurrentNamespaceDecls(xmlReader, bodyNC);
+                        message.put("soap.body.ns.context", bodyNC);
+                    }
                 }
 
                 // Find header
@@ -271,6 +288,17 @@ public class ReadHeadersInterceptor extends AbstractSoapInterceptor {
         }
     }
 
+    private void addCurrentNamespaceDecls(XMLStreamReader xmlReader, Map<String, String>
bodyNsMap) {
+        for (int i = 0; i < xmlReader.getNamespaceCount(); i++) {
+            String nsuri = xmlReader.getNamespaceURI(i);
+            if (!Soap11.SOAP_NAMESPACE.equals(nsuri) && !Soap12.SOAP_NAMESPACE.equals(nsuri))
{
+                bodyNsMap.put(xmlReader.getNamespacePrefix(i), nsuri);
+            } else if ("".equals(nsuri)) {
+                bodyNsMap.remove("");
+            }
+        }
+    }
+
     /**
      * A convenient class for parsing the message header stream into a DOM document;
      * the document is created only if a SOAP Header is actually found, keeping the

http://git-wip-us.apache.org/repos/asf/cxf/blob/47a3323b/rt/bindings/soap/src/test/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptorTest.java
----------------------------------------------------------------------
diff --git a/rt/bindings/soap/src/test/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptorTest.java
b/rt/bindings/soap/src/test/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptorTest.java
new file mode 100644
index 0000000..e2fd16c
--- /dev/null
+++ b/rt/bindings/soap/src/test/java/org/apache/cxf/binding/soap/interceptor/ReadHeadersInterceptorTest.java
@@ -0,0 +1,83 @@
+/**
+ * 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.binding.soap.interceptor;
+
+import java.io.ByteArrayInputStream;
+import java.util.Map;
+
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.cxf.binding.soap.Soap11;
+import org.apache.cxf.binding.soap.SoapMessage;
+import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.staxutils.StaxUtils;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * 
+ */
+public class ReadHeadersInterceptorTest extends Assert {
+    private static final byte[] TEST_SOAP = 
+        ("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"
+            + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"
+            + " xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:bar='tmp:bar'>"
+            + "<soap:Body>"
+            + "<ns2:payload xmlns:ns2='urn:tmp:foo'/>"
+            + "</soap:Body>"
+            + "</soap:Envelope>").getBytes();
+    
+    private ReadHeadersInterceptor interceptor;
+    
+    @Before
+    public void setUp() {
+        interceptor = new ReadHeadersInterceptor(null);
+    }
+
+    @Test
+    public void testNotAddNSContext() throws Exception {
+        SoapMessage message = setUpMessage();
+        interceptor.handleMessage(message);
+        Map<String, String> nsc = CastUtils.cast((Map<?, ?>)message.get("soap.body.ns.context"));

+        assertNull(nsc);
+    }
+
+    @Test
+    public void testAddNSContext() throws Exception {
+        SoapMessage message = setUpMessage();
+        message.put("org.apache.cxf.binding.soap.addNamespaceContext", "true");
+        interceptor.handleMessage(message);
+        Map<String, String> nsc = CastUtils.cast((Map<?, ?>)message.get("soap.body.ns.context"));
+        assertNotNull(nsc);
+        assertEquals("http://www.w3.org/2001/XMLSchema-instance", nsc.get("xsi"));
+        assertEquals("http://www.w3.org/2001/XMLSchema", nsc.get("xs"));
+        assertEquals("tmp:bar", nsc.get("bar"));
+        
+    }
+
+    private SoapMessage setUpMessage() throws Exception {
+        SoapMessage message = new SoapMessage(Soap11.getInstance());
+        message.setContent(XMLStreamReader.class, StaxUtils.createXMLStreamReader(new ByteArrayInputStream(TEST_SOAP)));
+        return message;
+    }
+
+}


Mime
View raw message