1 Reply Latest reply: Jul 4, 2012 2:50 AM by gimbal2 RSS

    Spring-WS with MTOM Hello World

    ziggy
      I am trying to create a simple Spring webservice which when called returns a file attachement as part of the SOAP response. The Enpoint class is shown below:


      And finally the endpoint
          @PayloadRoot(namespace="http://ws.mypackage.com", localPart="downloadMessageRequest")
               @ResponsePayload
               public JAXBElement<DownloadResponseType> invoke(@RequestPayload DownloadMessageRequest req) throws Exception  {
                              
                    DownloadResponseType response = new DownloadResponseType();
                    DownloadResponseType.PayLoad payload = new DownloadResponseType.PayLoad();               
                    
                    javax.activation.DataHandler dataHandler = new javax.activation.DataHandler(new FileDataSource("c:\\temp\\maven-feather.png"));
                    payload.setMessagePayLoad(dataHandler);
                    response.setPayLoad(payload);
                    
                    return objectFactory.createDownloadMessageResponse(response);
          
               }
           I would like the response to include the file as an attachement similar to the following response:
          Content-Type: multipart/related; boundary=MIMEBoundary4A7AE55984E7438034;
                                   type="application/xop+xml"; start="<0.09BC7F4BE2E4D3EF1B@apache.org>";
                                   start-info="text/xml; charset=utf-8"
          
          --MIMEBoundary4A7AE55984E7438034
          content-type: application/xop+xml; charset=utf-8; type="application/soap+xml;"
          content-transfer-encoding: binary
          content-id: <0.09BC7F4BE2E4D3EF1B@apache.org>
          
          <?xml version='1.0' encoding='utf-8'?>
          <soapenv:Envelope xmlns:soapenv="...."....>
            ........
                   <xop:Include href="cid:1.A91D6D2E3D7AC4D580@apache.org" 
                                  xmlns:xop="http://www.w3.org/2004/08/xop/include">
                   </xop:Include>
            ........
          
          </soapenv:Envelope>
          --MIMEBoundary4A7AE55984E7438034
          content-type: application/octet-stream
          content-transfer-encoding: binary
          content-id: <1.A91D6D2E3D7AC4D580@apache.org>
          
          Binary Data.....
          --MIMEBoundary4A7AE55984E7438034--
      I have tried to follow the documentation and the sample code in the spring-ws samples and for some reason the output i am getting is always this (i.e. the base64 data is not an attachement.

        
       HTTP/1.1 200 OK
          Server: Apache-Coyote/1.1
          Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
          SOAPAction: ""
          Content-Type: text/xml;charset=utf-8
          Content-Length: 4750
          Date: Tue, 03 Jul 2012 17:05:21 GMT
          
          <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:downloadMessageResponse xmlns:ns2="http://ws.mypackage.com"><ns2:payLoad><ns2:messagePayLoad>....iVBORw0KGgoAAAANSUhEUgAAAFoAAAAeCyAAAAAElFTkSuQmCC....</ns2:messagePayLoad></ns2:payLoad></ns2:downloadMessageResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
      As you can see, the payload is not an attachment. Here is how i have configured my application:

      web.xml
          <context-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>
                  /WEB-INF/app-config.xml
                  </param-value>
              </context-param>
              
              <listener>
                  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
              </listener>
          
              <servlet>
                  <servlet-name>webservice</servlet-name>
                  <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
                  <init-param>
                       <param-name>contextConfigLocation</param-name>
                       <param-value>/WEB-INF/ws-config.xml</param-value>
                  </init-param>
              </servlet>
      ws-config.xml
          <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:ws="http://www.springframework.org/schema/web-services"
               xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="http://www.springframework.org/schema/web-services 
               http://www.springframework.org/schema/web-services/web-services-2.0.xsd
                    http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
               
               <context:component-scan base-package="com.mypackage"/>
               
               <ws:annotation-driven/>
               
                    
               <ws:dynamic-wsdl id="serviceDefinition" portTypeName="myService"
                                locationUri="http://localhost:8080/springWsTest/webservice">
                    <ws:xsd location="/WEB-INF/schemas/downloadMessageRequest.xsd"/>
               </ws:dynamic-wsdl>
               
               <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                  <property name="contextPath" value="com.mypackage.ws"/>
                  <property name="mtomEnabled" value="true"/>
              </bean>     
          </beans>
      downloadMessageRequest.xsd schema file
         <?xml version="1.0" encoding="UTF-8"?>
          <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
          xmlns:m="http://ws.mypackage.com" 
          xmlns:xmime="http://www.w3.org/2005/05/xmlmime" elementFormDefault="qualified"
          targetNamespace="http://ws.mypackage.com" 
          attributeFormDefault="unqualified">     
          
               <xs:element name="downloadMessageRequest">
                    <xs:complexType/>
               </xs:element>
               
               <xs:element name="downloadMessageResponse" type="m:downloadResponseType" />
               
               <xs:complexType name="downloadResponseType">
                         <xs:sequence>
                              <xs:element name="requestName" type="xs:string"/>
                              <xs:element name="payLoad">
                                   <xs:complexType>
                                        <xs:sequence>
                                             <xs:element name="messagePayLoad" type="xs:base64Binary" xmime:expectedContentTypes="application/octet-stream"/>
                                        </xs:sequence>
                                   </xs:complexType>
                              </xs:element>
                         </xs:sequence>
                </xs:complexType>
                
                
                <xs:element name="localDTMRequest">
                    <xs:complexType/>
               </xs:element>
               
               <xs:element name="localDTMResponse">
                    <xs:complexType>          
                         <xs:sequence>
                              <xs:element name="localDTM" type="xs:dateTime"/>
                         </xs:sequence>               
                    </xs:complexType>
               </xs:element>
               
               
          </xs:schema>
      The file does get converted to base64binary. The JAXB clases are generated correctly. The Endpoint works but it is not including the file as an attachement. It is including it as part of the XML tag even though i have set mtomEnabled=true.

      What am i missing?

      ps. Why are the forum code tags not working??

      Edited by: ziggy on Jul 5, 2012 10:15 AM

      Edited by: ziggy on Jul 5, 2012 10:17 AM
        • 1. Re: Spring-WS with MTOM Hello World
          gimbal2
          You should use \
           tags.
          public static void main(String[] args){
          }
          EDIT: yup, works just fine.
          
          And this is not a Spring forum; you'll find that on the Spring website.
          
          http://forum.springsource.org/forum.php