camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/3] camel git commit: CAMEL-5723 - set headers while setting partClass and partNamespace properties
Date Thu, 27 Apr 2017 08:40:01 GMT
CAMEL-5723 - set headers while setting partClass and partNamespace properties


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

Branch: refs/heads/master
Commit: 03519184b36a998a5197383f8e37c6c0d8221e21
Parents: 7cd7f48
Author: onders86 <ondersezgin@gmail.com>
Authored: Thu Apr 27 09:16:24 2017 +0300
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Apr 27 10:36:29 2017 +0200

----------------------------------------------------------------------
 .../camel/converter/jaxb/JaxbConstants.java     |  3 +
 .../camel/converter/jaxb/JaxbDataFormat.java    | 28 ++++++-
 .../jaxb/JaxbDataFormatPartClassHeaderTest.java | 80 ++++++++++++++++++++
 3 files changed, 108 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/03519184/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConstants.java
b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConstants.java
index a1183cd..f9e7115 100644
--- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConstants.java
+++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbConstants.java
@@ -25,5 +25,8 @@ public interface JaxbConstants {
      * Key to identify custom marshaller properties in the exchange specific to JAX-RI implementation
      */
     String JAXB_PROVIDER_PROPERTIES = "CamelJaxbProviderProperties";
+    
+    String JAXB_PART_CLASS = "CamelJaxbPartClass";
+    String JAXB_PART_NAMESPACE = "CamelJaxbPartNamespace";
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/03519184/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
index 9506b5e..4950950 100644
--- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
+++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
@@ -183,8 +183,22 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat,
DataFo
         throws XMLStreamException, JAXBException, NoTypeConversionAvailableException, IOException,
InvalidPayloadException {
 
         Object element = graph;
-        if (partialClass != null && getPartNamespace() != null) {
-            element = new JAXBElement<Object>(getPartNamespace(), partialClass, graph);
+        QName  partNamespaceOnDataFormat = getPartNamespace();
+        String partClassFromHeader = (String)exchange.getIn().getHeader(JaxbConstants.JAXB_PART_CLASS);
+        String partNamespaceFromHeader = (String)exchange.getIn().getHeader(JaxbConstants.JAXB_PART_NAMESPACE);
+        if ((partialClass != null || partClassFromHeader != null)
+                && (partNamespaceOnDataFormat != null || partNamespaceFromHeader
!= null)) {
+            if (partClassFromHeader != null) {
+                try {
+                    partialClass = camelContext.getClassResolver().resolveMandatoryClass(partClassFromHeader,
Object.class);
+                } catch (ClassNotFoundException e) {
+                    throw new JAXBException(e);
+                }
+            }
+            if (partNamespaceFromHeader != null) {
+                partNamespaceOnDataFormat = QName.valueOf(partNamespaceFromHeader);
+            }
+            element = new JAXBElement<Object>(partNamespaceOnDataFormat, partialClass,
graph);
         }
 
         // only marshal if its possible
@@ -256,8 +270,16 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat,
DataFo
             } else {
                 xmlReader = typeConverter.convertTo(XMLStreamReader.class, stream);
             }
-            if (partialClass != null) {
+            String partClassFromHeader = (String)exchange.getIn().getHeader(JaxbConstants.JAXB_PART_CLASS);
+            if (partialClass != null || partClassFromHeader != null) {
                 // partial unmarshalling
+                if (partClassFromHeader != null) {
+                    try {
+                        partialClass = camelContext.getClassResolver().resolveMandatoryClass(partClassFromHeader,
Object.class);
+                    } catch (ClassNotFoundException e) {
+                        throw new JAXBException(e);
+                    }
+                }
                 answer = createUnmarshaller().unmarshal(xmlReader, partialClass);
             } else {
                 answer = createUnmarshaller().unmarshal(xmlReader);

http://git-wip-us.apache.org/repos/asf/camel/blob/03519184/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatPartClassHeaderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatPartClassHeaderTest.java
b/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatPartClassHeaderTest.java
new file mode 100644
index 0000000..8391fb3
--- /dev/null
+++ b/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbDataFormatPartClassHeaderTest.java
@@ -0,0 +1,80 @@
+/**
+ * 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.jaxb;
+
+import javax.xml.namespace.QName;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.example.Address;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JaxbDataFormatPartClassHeaderTest extends CamelTestSupport {
+
+    @EndpointInject(uri = "mock:marshall")
+    private MockEndpoint mockMarshall;
+
+    @Test
+    public void testMarshallMultipleNamespaces() throws Exception {
+        mockMarshall.expectedMessageCount(1);
+
+        Address address = new Address();
+        address.setStreet("Main Street");
+        address.setStreetNumber("3a");
+        address.setZip("65843");
+        address.setCity("Sulzbach");
+        template.sendBody("direct:marshall", address);
+
+        assertMockEndpointsSatisfied();
+
+        String payload = mockMarshall.getExchanges().get(0).getIn().getBody(String.class);
+        log.info(payload);
+
+        assertTrue(payload.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"));
+        assertTrue(payload.contains("<address:address"));
+        assertTrue(payload.contains("<address:street>Main Street</address:street>"));
+        assertTrue(payload.contains("<address:streetNumber>3a</address:streetNumber>"));
+        assertTrue(payload.contains("<address:zip>65843</address:zip>"));
+        assertTrue(payload.contains("<address:city>Sulzbach</address:city>"));
+        assertTrue(payload.contains("</address:address>"));
+
+        // the namespaces
+        assertTrue(payload.contains("xmlns:address=\"http://www.camel.apache.org/jaxb/example/address/1\""));
+        assertTrue(payload.contains("xmlns:order=\"http://www.camel.apache.org/jaxb/example/order/1\""));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                JaxbDataFormat jaxbDataFormat = new JaxbDataFormat();
+                jaxbDataFormat.setContextPath(Address.class.getPackage().getName());
+                jaxbDataFormat.setPartNamespace(new QName("http://www.camel.apache.org/jaxb/example/address/123",
"addressToBeOverriden"));
+                jaxbDataFormat.setPrettyPrint(true);
+
+                from("direct:marshall")
+                        .setHeader(JaxbConstants.JAXB_PART_CLASS, simple("org.apache.camel.example.Address"))
+                        .setHeader(JaxbConstants.JAXB_PART_NAMESPACE, simple("{http://www.camel.apache.org/jaxb/example/address/1}address"))
+                        .marshal(jaxbDataFormat)
+                        .to("mock:marshall");
+            }
+        };
+    }
+}


Mime
View raw message