You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/10/26 15:36:01 UTC

[2/2] git commit: CAMEL-6883: New name strategy for came-soap that perfer xml root namespace. Thanks to Tom Ziemer for the patch.

CAMEL-6883: New name strategy for came-soap that perfer xml root namespace. Thanks to Tom Ziemer for the patch.


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

Branch: refs/heads/camel-2.12.x
Commit: 21b8aae5840e53fbd710a6739e325f356034d82f
Parents: 5758c7c
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 26 15:36:09 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 26 15:36:34 2013 +0200

----------------------------------------------------------------------
 ...ootElementPreferringElementNameStrategy.java | 76 ++++++++++++++++++++
 ...lementPreferringElementNameStrategyTest.java | 65 +++++++++++++++++
 .../name/testpackage/RequestWithDefaultNs.java  | 25 +++++++
 .../soap/name/testpackage/package-info.java     | 18 +++++
 4 files changed, 184 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/21b8aae5/components/camel-soap/src/main/java/org/apache/camel/dataformat/soap/name/XmlRootElementPreferringElementNameStrategy.java
----------------------------------------------------------------------
diff --git a/components/camel-soap/src/main/java/org/apache/camel/dataformat/soap/name/XmlRootElementPreferringElementNameStrategy.java b/components/camel-soap/src/main/java/org/apache/camel/dataformat/soap/name/XmlRootElementPreferringElementNameStrategy.java
new file mode 100644
index 0000000..590d420
--- /dev/null
+++ b/components/camel-soap/src/main/java/org/apache/camel/dataformat/soap/name/XmlRootElementPreferringElementNameStrategy.java
@@ -0,0 +1,76 @@
+/**
+ * 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.dataformat.soap.name;
+
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlSchema;
+import javax.xml.bind.annotation.XmlType;
+import javax.xml.namespace.QName;
+
+import org.apache.camel.util.ObjectHelper;
+
+public class XmlRootElementPreferringElementNameStrategy implements ElementNameStrategy {
+
+    private static final String DEFAULT_NS = "##default";
+
+    @Override
+    public QName findQNameForSoapActionOrType(String soapAction, Class<?> type) {
+        XmlType xmlType = type.getAnnotation(XmlType.class);
+        if (xmlType == null || xmlType.name() == null) {
+            throw new RuntimeException("The type " + type.getName() + " needs to have an XmlType annotation with name");
+        }
+        // prefer name+ns from the XmlRootElement, and fallback to XmlType
+        String localName = null;
+        String nameSpace = null;
+
+        XmlRootElement root = type.getAnnotation(XmlRootElement.class);
+        if (root != null) {
+            localName = ObjectHelper.isEmpty(localName) ? root.name() : localName;
+            nameSpace = isInValidNamespace(nameSpace) ? root.namespace() : nameSpace;
+        }
+
+        if (ObjectHelper.isEmpty(localName)) {
+            localName = xmlType.name();
+        }
+
+        if (isInValidNamespace(nameSpace)) {
+            XmlSchema xmlSchema = type.getPackage().getAnnotation(XmlSchema.class);
+            if (xmlSchema != null) {
+                nameSpace = xmlSchema.namespace();
+            }
+        }
+
+        if (isInValidNamespace(nameSpace)) {
+            nameSpace = xmlType.namespace();
+        }
+
+        if (ObjectHelper.isEmpty(localName) || isInValidNamespace(nameSpace)) {
+            throw new IllegalStateException("Unable to determine localName or namespace for type <" + type.getName() + ">");
+        }
+        return new QName(nameSpace, localName);
+    }
+
+    private boolean isInValidNamespace(String namespace) {
+        return ObjectHelper.isEmpty(namespace) || DEFAULT_NS.equalsIgnoreCase(namespace);
+    }
+
+    @Override
+    public Class<? extends Exception> findExceptionForFaultName(QName faultName) {
+        throw new UnsupportedOperationException("Exception lookup is not supported");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/21b8aae5/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/XmlRootElementPreferringElementNameStrategyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/XmlRootElementPreferringElementNameStrategyTest.java b/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/XmlRootElementPreferringElementNameStrategyTest.java
new file mode 100644
index 0000000..2a9c325
--- /dev/null
+++ b/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/XmlRootElementPreferringElementNameStrategyTest.java
@@ -0,0 +1,65 @@
+/**
+ * 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.converter.soap.name;
+
+import java.io.Serializable;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+import javax.xml.namespace.QName;
+
+import org.apache.camel.converter.soap.name.testpackage.RequestWithDefaultNs;
+import org.apache.camel.dataformat.soap.name.XmlRootElementPreferringElementNameStrategy;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class XmlRootElementPreferringElementNameStrategyTest {
+
+    private static final String DEFAULT_NS = "##default";
+    private static final String CUSTOM_NS = "http://test.com/sample";
+    private static final String LOCAL_NAME = "sample";
+    private XmlRootElementPreferringElementNameStrategy ens = new XmlRootElementPreferringElementNameStrategy();
+
+    @Test
+    public void testFindQNameForSoapActionOrTypeWithXmlSchemaPresent() throws Exception {
+        QName qname = ens.findQNameForSoapActionOrType("abc", RequestWithDefaultNs.class);
+        assertEquals("local names must match", "foo", qname.getLocalPart());
+        assertEquals("namespace must match", "baz", qname.getNamespaceURI());
+    }
+
+    @Test
+    public void testFindQNameForSoapActionOrType() throws Exception {
+        QName qname = ens.findQNameForSoapActionOrType(DEFAULT_NS, Request.class);
+        assertEquals("local names must match", LOCAL_NAME, qname.getLocalPart());
+        assertEquals("namespace must match", CUSTOM_NS, qname.getNamespaceURI());
+
+        qname = ens.findQNameForSoapActionOrType(CUSTOM_NS, Request.class);
+        assertEquals("local names must match", LOCAL_NAME, qname.getLocalPart());
+        assertEquals("namespace must match", CUSTOM_NS, qname.getNamespaceURI());
+    }
+
+    @Test(expected = UnsupportedOperationException.class)
+    public void testFindExceptionForFaultName() throws Exception {
+        ens.findExceptionForFaultName(new QName(LOCAL_NAME, CUSTOM_NS));
+    }
+
+    @XmlType(name = "", propOrder = {LOCAL_NAME})
+    @XmlRootElement(name = LOCAL_NAME, namespace = CUSTOM_NS)
+    public class Request implements Serializable {
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/21b8aae5/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/testpackage/RequestWithDefaultNs.java
----------------------------------------------------------------------
diff --git a/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/testpackage/RequestWithDefaultNs.java b/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/testpackage/RequestWithDefaultNs.java
new file mode 100644
index 0000000..2f759e5
--- /dev/null
+++ b/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/testpackage/RequestWithDefaultNs.java
@@ -0,0 +1,25 @@
+/**
+ * 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.converter.soap.name.testpackage;
+
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+@XmlType(name = "one", namespace = "two")
+@XmlRootElement(name = "foo")
+public class RequestWithDefaultNs {
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/21b8aae5/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/testpackage/package-info.java
----------------------------------------------------------------------
diff --git a/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/testpackage/package-info.java b/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/testpackage/package-info.java
new file mode 100755
index 0000000..c7c28f6
--- /dev/null
+++ b/components/camel-soap/src/test/java/org/apache/camel/converter/soap/name/testpackage/package-info.java
@@ -0,0 +1,18 @@
+/**
+ * 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.
+ */
+@javax.xml.bind.annotation.XmlSchema(namespace = "baz", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
+package org.apache.camel.converter.soap.name.testpackage;
\ No newline at end of file