Spring-WS webservice with MTOM attachment - Hello world test
Asked Answered
M

1

10

I am trying to create a simple Spring webservice which when called returns a file attachment 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="<[email protected]>";
                         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: <[email protected]>

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="...."....>
  ........
         <xop:Include href="cid:[email protected]" 
                        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: <[email protected]>

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?

Maui answered 3/7, 2012 at 17:24 Comment(2)
Have you found the solution to this? I'm struggling with the very same issue.Odelle
Still on this issue? Haven't sorted it out yet, but I created a new question about it, with all the info I could gather. I'd appreciate your help, or maybe you could make good use of it too. Thanks. #11565399Odelle
M
13

Finally managed to get it to work. The configuration is more or less the same as what i had in my original post. I had to update the configuration file. Here is how my configuration file looks like now.

<?xml version="1.0" encoding="UTF-8"?>
<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" />

    <ws:annotation-driven />

    <ws:dynamic-wsdl id="serviceDefinition"
        portTypeName="Msm" locationUri="http://localhost:8080/MyWebService/webservice">
        <ws:xsd location="/WEB-INF/schemas/schema.xsd" />
    </ws:dynamic-wsdl>

    <bean id="messageReceiver"
        class="org.springframework.ws.soap.server.SoapMessageDispatcher">
        <property name="endpointAdapters">
            <list>
                <ref bean="defaultMethodEndpointAdapter" />
            </list>
        </property>
    </bean>

    <bean id="defaultMethodEndpointAdapter"
        class="org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter">
        <property name="methodArgumentResolvers">
            <list>
                <!-- Be careful here! You might need to add more processors if you do 
                    more than webservices! -->
                <ref bean="marshallingPayloadMethodProcessor" />
            </list>
        </property>
        <property name="methodReturnValueHandlers">
            <list>
                <ref bean="marshallingPayloadMethodProcessor" />
            </list>
        </property>
    </bean>

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="com.mypackage.ws" />
        <property name="mtomEnabled" value="true" />
    </bean>

    <bean id="marshallingPayloadMethodProcessor"
        class="org.springframework.ws.server.endpoint.adapter.method.MarshallingPayloadMethodProcessor">
        <constructor-arg ref="marshaller" />
        <constructor-arg ref="marshaller" />
    </bean>
</beans>

The changes i added were based on what i read on this article - blog.hpxn.net/2012/06/

Edit - An example

Here is an example (based on the spring samples) that returns the attachment in MTOM format. I just tried it and the response is shown below:

   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: Multipart/Related; start-info="text/xml"; type="application/xop+xml"; boundary="----=_Part_0_17910623.1342789122256"
Transfer-Encoding: chunked
Date: Fri, 20 Jul 2012 12:58:42 GMT

------=_Part_0_17910623.1342789122256
Content-Type: application/xop+xml; charset=utf-8; type="text/xml"

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:LoadImageResponse xmlns:ns2="http://www.springframework.org/spring-ws/samples/mtom"><ns2:name>?</ns2:name><ns2:image><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:f1c3ef65-a519-4bba-9b92-9acffc0c14f7%40www.springframework.org"/></ns2:image></ns2:LoadImageResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
------=_Part_0_17910623.1342789122256
Content-Type: image/png
Content-ID: <[email protected]>
Content-Transfer-Encoding: binary

‰PNG

Note that i have not configured any Axiom factories. The necessary files are listed below:

schema.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.springframework.org/spring-ws/samples/mtom"
        xmlns:tns="http://www.springframework.org/spring-ws/samples/mtom"
        xmlns:xmime="http://www.w3.org/2005/05/xmlmime" elementFormDefault="qualified">

    <element name="StoreImageRequest" type="tns:Image"/>

    <element name="LoadImageRequest" type="string"/>

    <element name="LoadImageResponse" type="tns:Image"/>

    <complexType name="Image">
        <sequence>
            <element name="name" type="string"/>
            <element name="image" type="base64Binary" xmime:expectedContentTypes="image/png"/>
        </sequence>
    </complexType>

</schema>

spring-ws-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd">

    <bean id="imageRepository" class="org.springframework.ws.samples.mtom.service.StubImageRepository"/>

    <bean class="org.springframework.ws.samples.mtom.ws.ImageRepositoryEndpoint">
        <constructor-arg ref="imageRepository"/>
    </bean>

    <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping"/>

    <bean class="org.springframework.ws.server.endpoint.adapter.GenericMarshallingMethodEndpointAdapter">
        <constructor-arg ref="marshaller"/>
    </bean>

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="org.springframework.ws.samples.mtom.schema"/>
        <property name="mtomEnabled" value="true"/>
    </bean>

    <bean id="mtom" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
        <property name="schema" ref="schema"/>
        <property name="portTypeName" value="ImageRepository"/>
        <property name="locationUri" value="http://localhost:8080/mtom-server/"/>
    </bean>

    <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
        <property name="xsd" value="/WEB-INF/schema.xsd"/>
    </bean>

</beans>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>MyCompany HR Holiday Service</display-name>

    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

To get the response returned as a SAAJ attachment rather than an MTOM attachment, i have to manually configure the SAAj factories as described in this thread How do I add an attachment to a response payload in Spring-WS?

Maui answered 20/7, 2012 at 9:5 Comment(8)
I tried and it indeed works, except that it uses SAAJ, which does not support MTOM and handles all the attachment data in memory, leading to crashes on big attachments. Axiom solves those, but lacks integration with Spring (no XOP support) and inlines the attachments, leading to the same memory issues. That's the scenario AFAIK, but I'd love to know that I'm wrong at something. Could you handle big attachments (like over 200mb) coming and going without memory usage skyrocketing? Anyway, thanks for the working solution.Odelle
I managed to get both SAAJ and MTOM working. I have not tried large files yet as the files i am working on are quite small. Let me have a look and ill update the post to show both the SAAJ and MTOM implementations.Maui
If i remember correctly, to use SAAJ you have to configure the Message factory. Do you have any message factories configured? That could be the reason why it is using SAAJ in your example. The above configuration does not configure any SAAJ factories hence MTOM is used by default.Maui
I always assumed that SAAJ would be the default, as it is an official API, anyway. In my environment SAAJ is used if none is defined, as my logs show: 2012-07-20 09:05:35,219 INFO [org.springframework.ws.soap.saaj.SaajSoapMessageFactory] : Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol 2012-07-20 09:05:35,220 DEBUG [org.springframework.ws.soap.saaj.SaajSoapMessageFactory] : Using MessageFactory class [weblogic.xml.saaj.MessageFactoryImpl]. We use Weblogic, maybe other app servers do it differently.Odelle
You are probably right. I am using Jboss. See my second answer below showing the MTOM (xop) response. Hope that helps.Maui
Your logs are showing that it is using a weblogic specific implementation of the saaj message factory (weblogic.xml.saaj.MessageFactoryImpl). This is probably what is forcing the SAAJ type responses. You need to find out how it is being included in your classpath. It could be your config or weblogic includes it by default. I dont know much about weblogic so cant be of much help im afraid. :0Maui
It is part of weblogic classpath. I suppose it is easy to replace with another implementation, but that won't change lack of disk caching support. According to an old post JBoss uses its own implementation of Axiom, so this might be the difference. After all, the root seems to be when Spring's AxiomSoapMessage.isXopPackage calls (Apache) Axiom and it gives an unavoidable exception that disables XOP. I'll see if I can get another implementation of Axiom, maybe JBoss', if pluggable.Odelle
Thank you ziggy, your answer has been very precious for me to solve this issue. Just one note: I had to specify the messageReceiver as the messageReceiverBeanName init parameter of my MessageDispatcherServlet in order to make Spring WS actually use this configuration and enable proper MTOM support. Anyway, I opened an improvement request on Spring WS JIRA to simplify (or at least document...) this use case, which doesn't sound so uncommon to me... It's at jira.spring.io/browse/SWS-905Eth

© 2022 - 2024 — McMap. All rights reserved.