Repository: camel
Updated Branches:
  refs/heads/camel-2.18.x 5eb6868f6 -> f325b67fa


CAMEL-10679

Producer does not populate attachments from response

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

Branch: refs/heads/camel-2.18.x
Commit: 18fe8329ef46a4d84e0914052dfbcc93664941ab
Parents: 94b4e6f
Author: marcusmesserkewill <marcus.mes...@kewill.com>
Authored: Fri Jan 6 10:30:48 2017 +0100
Committer: marcusmesserkewill <marcus.mes...@kewill.com>
Committed: Fri Jan 6 10:30:48 2017 +0100

----------------------------------------------------------------------
 .../ws/SpringWebserviceConfiguration.java       | 21 ++++++-
 .../spring/ws/SpringWebserviceProducer.java     | 41 ++++++++++---
 .../ws/SoapAttachmentResponseProcessor.java     | 37 +++++++++++
 .../spring/ws/SoapResponseAttachmentTest.java   | 64 ++++++++++++++++++++
 .../ws/SoapResponseAttachmentTest-context.xml   | 53 ++++++++++++++++
 5 files changed, 205 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/18fe8329/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceConfiguration.java
 
b/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceConfiguration.java
index ae61fcc..ebaec3d 100644
--- 
a/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceConfiguration.java
+++ 
b/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceConfiguration.java
@@ -72,7 +72,9 @@ public class SpringWebserviceConfiguration {
     private int timeout = -1;
     @UriParam(label = "producer")
     private boolean allowResponseHeaderOverride;
-
+    @UriParam(label = "producer")
+    private boolean allowResponseAttachmentOverride;
+    
     /* Consumer configuration */
     @UriParam(label = "consumer")
     private EndpointMappingKey endpointMappingKey;
@@ -401,5 +403,22 @@ public class SpringWebserviceConfiguration {
     public void setAllowResponseHeaderOverride(boolean 
allowResponseHeaderOverride) {
         this.allowResponseHeaderOverride = allowResponseHeaderOverride;
     }
+    /**
+     * @return boolean - true, will override attachments with spring-ws 
response message attachments
+     */
+    public boolean isAllowResponseAttachmentOverride() {
+        return allowResponseAttachmentOverride;
+    }
 
+    /**
+     * Option to override soap response attachments in in/out exchange with 
attachments info the actual service layer.
+     * If the invoked service appends or rewrites the soap attachments this 
option when set to true, allows the modified
+     * soap attachments to be overwritten in in/out message attachments
+     * 
+     * @param allowResponseAttachmentOverride
+     *            - true, will override attachments with spring-ws response 
message attachments
+     */
+    public void setAllowResponseAttachmentOverride(boolean 
allowResponseAttachmentOverride) {
+        this.allowResponseAttachmentOverride = allowResponseAttachmentOverride;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/18fe8329/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceProducer.java
 
b/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceProducer.java
index 489397c..b1a4a16 100644
--- 
a/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceProducer.java
+++ 
b/components/camel-spring-ws/src/main/java/org/apache/camel/component/spring/ws/SpringWebserviceProducer.java
@@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.ws.WebServiceMessage;
 import org.springframework.ws.client.core.WebServiceMessageCallback;
 import org.springframework.ws.client.core.WebServiceTemplate;
+import org.springframework.ws.mime.Attachment;
 import org.springframework.ws.soap.SoapHeader;
 import org.springframework.ws.soap.SoapHeaderElement;
 import org.springframework.ws.soap.SoapMessage;
@@ -98,21 +99,30 @@ public class SpringWebserviceProducer extends 
DefaultProducer {
             public void doWithMessage(WebServiceMessage responseMessage) 
throws IOException, TransformerException {
                 SoapMessage soapMessage = (SoapMessage) responseMessage;
                 if (ExchangeHelper.isOutCapable(exchange)) {
-                    exchange.getOut().copyFromWithNewBody(exchange.getIn(), 
responseMessage.getPayloadSource());
-                    if (soapMessage.getSoapHeader() != null && 
getEndpoint().getConfiguration().isAllowResponseHeaderOverride()) {
-                        populateMessageHeaderFromResponse(exchange.getOut(), 
soapMessage.getSoapHeader());
-                    }
+                    exchange.getOut().copyFromWithNewBody(exchange.getIn(), 
soapMessage.getPayloadSource());
+                    processHeaderAndAttachments(exchange.getOut(), 
soapMessage);
                 } else {
-                    
exchange.getIn().setBody(responseMessage.getPayloadSource());
-                    if (soapMessage.getSoapHeader() != null && 
getEndpoint().getConfiguration().isAllowResponseHeaderOverride()) {
-                        populateMessageHeaderFromResponse(exchange.getIn(), 
soapMessage.getSoapHeader());
-                    }
+                    exchange.getIn().setBody(soapMessage.getPayloadSource());
+                    processHeaderAndAttachments(exchange.getIn(), soapMessage);
                 }
 
             }
         });
     }
-
+    /**
+     * Populates soap message headers and attachments from soap response
+     * @param inOrOut {@link Message}
+     * @param soapMessage {@link SoapMessage}
+     */
+    private void processHeaderAndAttachments(Message inOrOut, SoapMessage 
soapMessage) {
+        if (soapMessage.getSoapHeader() != null && 
getEndpoint().getConfiguration().isAllowResponseHeaderOverride()) { 
+                populateMessageHeaderFromResponse(inOrOut, 
soapMessage.getSoapHeader());
+        }
+        if (soapMessage.getAttachments() != null && 
getEndpoint().getConfiguration().isAllowResponseAttachmentOverride()) {
+                populateMessageAttachmentsFromResponse(inOrOut, 
soapMessage.getAttachments());
+        }
+    }
+    
     /**
      * Populates message headers from soapHeader response
      * 
@@ -139,7 +149,18 @@ public class SpringWebserviceProducer extends 
DefaultProducer {
 
         }
     }
-
+    /**
+     * Populates message attachments from soap response attachments 
+     * @param inOrOut {@link Message}
+     * @param soapMessage {@link SoapMessage}
+     */
+    private void populateMessageAttachmentsFromResponse(Message inOrOut, 
Iterator<Attachment> attachments) {
+       while (attachments.hasNext()) {
+               Attachment attachment = attachments.next();
+               inOrOut.getAttachments().put(attachment.getContentId(), 
attachment.getDataHandler());
+       }
+    }    
+    
     private void prepareMessageSenders(SpringWebserviceConfiguration 
configuration) {
         // Skip this whole thing if none of the relevant config options are 
set.
         if (!(configuration.getTimeout() > -1) && 
configuration.getSslContextParameters() == null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/18fe8329/components/camel-spring-ws/src/test/java/org/apache/camel/component/spring/ws/SoapAttachmentResponseProcessor.java
----------------------------------------------------------------------
diff --git 
a/components/camel-spring-ws/src/test/java/org/apache/camel/component/spring/ws/SoapAttachmentResponseProcessor.java
 
b/components/camel-spring-ws/src/test/java/org/apache/camel/component/spring/ws/SoapAttachmentResponseProcessor.java
new file mode 100644
index 0000000..ad52eb9
--- /dev/null
+++ 
b/components/camel-spring-ws/src/test/java/org/apache/camel/component/spring/ws/SoapAttachmentResponseProcessor.java
@@ -0,0 +1,37 @@
+/**
+ * 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.spring.ws;
+
+import javax.activation.DataHandler;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+/**
+ * Returns the request as the response so it can be analysed (eg. for presence
+ * of SOAP Headers). 
+ * Also adds 2 attachments to the out message, which could be returned in a 
soap message by a ws request.
+ */
+public class SoapAttachmentResponseProcessor implements Processor {
+
+    public void process(Exchange exchange) throws Exception {
+        exchange.setOut(exchange.getIn());
+        exchange.getOut().addAttachment("responseAttachment1.txt", new 
DataHandler("responseAttachment1", "text/plain"));
+        exchange.getOut().addAttachment("responseAttachment2.xml", new 
DataHandler("<responseAttachment2/>", "application/xml"));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/18fe8329/components/camel-spring-ws/src/test/java/org/apache/camel/component/spring/ws/SoapResponseAttachmentTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-spring-ws/src/test/java/org/apache/camel/component/spring/ws/SoapResponseAttachmentTest.java
 
b/components/camel-spring-ws/src/test/java/org/apache/camel/component/spring/ws/SoapResponseAttachmentTest.java
new file mode 100644
index 0000000..6ec1ca8
--- /dev/null
+++ 
b/components/camel-spring-ws/src/test/java/org/apache/camel/component/spring/ws/SoapResponseAttachmentTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.spring.ws;
+
+import static org.junit.Assert.assertNotNull;
+
+import javax.activation.DataHandler;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.junit.Test;
+import org.springframework.test.context.ContextConfiguration;
+import 
org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+@ContextConfiguration
+public class SoapResponseAttachmentTest extends 
AbstractJUnit4SpringContextTests {
+
+    private final String xmlRequestForGoogleStockQuote = "<GetQuote 
xmlns=\"http://www.webserviceX.NET/\";><symbol>GOOG</symbol></GetQuote>";
+    private final String soapHeader = "<h:Header 
xmlns:h=\"http://www.webserviceX.NET/\";><h:MessageID>1234567890</h:MessageID><h:Nested><h:NestedID>1111</h:NestedID></h:Nested></h:Header>";
+
+    @Produce
+    private ProducerTemplate template;
+
+    /**
+     * This tests if attachments, returned by a spring-ws request, are 
populated into the exchange. 
+     * The SOAP attachments are populated by the 
SoapAttachmentResponseProcessor. 
+     * Which adds 2 response attachments.
+     * Note: 'allowResponseAttachmentOverride=true' must be set!
+     *
+     * @throws Exception
+     */
+    @Test()
+    public void consumeStockQuoteWebserviceWithSoapHeader() throws Exception {
+        Exchange result = template.request("direct:stockQuoteWebservice", new 
Processor() {
+
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody(xmlRequestForGoogleStockQuote);
+                
exchange.getIn().setHeader(SpringWebserviceConstants.SPRING_WS_SOAP_HEADER, 
soapHeader);
+                exchange.getIn().addAttachment("requestAttachment1.txt", new 
DataHandler("hello attachment!", "text/plain"));
+            }
+        });
+        assertNotNull(result);
+        assertNotNull(result.getOut().getAttachment("requestAttachment1.txt"));
+        
assertNotNull(result.getOut().getAttachment("responseAttachment1.txt"));
+        
assertNotNull(result.getOut().getAttachment("responseAttachment2.xml"));
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/18fe8329/components/camel-spring-ws/src/test/resources/org/apache/camel/component/spring/ws/SoapResponseAttachmentTest-context.xml
----------------------------------------------------------------------
diff --git 
a/components/camel-spring-ws/src/test/resources/org/apache/camel/component/spring/ws/SoapResponseAttachmentTest-context.xml
 
b/components/camel-spring-ws/src/test/resources/org/apache/camel/component/spring/ws/SoapResponseAttachmentTest-context.xml
new file mode 100644
index 0000000..3bee89e
--- /dev/null
+++ 
b/components/camel-spring-ws/src/test/resources/org/apache/camel/component/spring/ws/SoapResponseAttachmentTest-context.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+         http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
+         http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd";>
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring";>
+        <!-- producer routes (web service clients) -->
+        <route>
+            <from uri="direct:stockQuoteWebservice"/>
+            <to 
uri="spring-ws:http://localhost?webServiceTemplate=#webServiceTemplate&amp;soapAction=http://www.stockquotes.edu/GetQuote&amp;allowResponseAttachmentOverride=true"/>
+        </route>
+
+        <!-- consumer route (providing the actual web service) that responds 
with the request so we can see SOAP headers -->
+        <route>
+            <from 
uri="spring-ws:soapaction:http://www.stockquotes.edu/GetQuote?endpointMapping=#endpointMapping"/>
+            <to uri="responseProcessor"/>
+        </route>
+    </camelContext>
+
+    <bean id="messageFactory" 
class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
+
+    <bean id="endpointMapping"
+          
class="org.apache.camel.component.spring.ws.bean.CamelEndpointMapping"/>
+
+    <bean id="responseProcessor"
+          
class="org.apache.camel.component.spring.ws.SoapAttachmentResponseProcessor"/>
+
+    <bean id="webServiceTemplate" 
class="org.springframework.ws.client.core.WebServiceTemplate">
+        <property name="defaultUri" value="http://localhost"/>
+        <property name="messageSender">
+            <bean 
class="net.javacrumbs.springws.test.helper.InMemoryWebServiceMessageSender"/>
+        </property>
+    </bean>
+
+</beans>
\ No newline at end of file

Reply via email to