Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 14374182B5 for ; Mon, 11 May 2015 08:47:18 +0000 (UTC) Received: (qmail 71714 invoked by uid 500); 11 May 2015 08:47:18 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 71656 invoked by uid 500); 11 May 2015 08:47:17 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 71647 invoked by uid 99); 11 May 2015 08:47:17 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 May 2015 08:47:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A034CDFCB8; Mon, 11 May 2015 08:47:17 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ay@apache.org To: commits@cxf.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer 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 +0000 (UTC) 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 Authored: Wed May 6 23:25:19 2015 +0200 Committer: Akitoshi Yoshida 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 bodyNC = addNC ? new HashMap() : 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 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 = + ("" + + "" + + "" + + "" + + "").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 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 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; + } + +}