大型MIME附件&肥皂

时间:2013-06-03 13:17:27

标签: soap glassfish-3 attachment spring-ws jaxb2

我有简单的Web服务服务器,它接收带有MIME附件的SOAP,将其存储到HashMap并使用此mime附件发回消息:

如果我发送带有小附件的请求(< 80MB),一切正常,但当我发送带有> 80 MB附件的Request时,DataHandler对象为空。因此它不会从Request中解析而不会存储到HashMap中。如果我启用了mimepull则无关紧要,如果是,则将大型附件存储到硬盘驱动器上的临时文件夹中。

启用MimePull:

-Dsaaj.use.mimepull=true
-Djava.io.tmpdir=/path/to/tmpdir

扩大的GlassFish线程池:

"Max Thread Pool Size" from 5 to 32
"Min Thread Pool Size" from 2 to 16

ImageRequest.java

package sample.mtom.schema;
@XmlRootElement
@XmlType(propOrder = {"fileName", "data"})
public class ImageRequest {

    protected String fileName;
    protected DataHandler data;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    @XmlMimeType("*/*")
    public DataHandler getData() {
        return data;
    }

    public void setData(DataHandler data) {
        this.data = data;
    }
}

ImageResponse.java

package sample.mtom.schema;
@XmlRootElement
@XmlType(propOrder = {"fileName", "data"})
public class ImageResponse {
    // the same as ImageRequest
}

package-info.java

@XmlSchema(namespace = "http://www.example.org/Image",
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package sample.mtom.schema;

import javax.xml.bind.annotation.XmlSchema;

StubRepository.java

package sample.mtom.service;
public class StubRepository implements Repository {

    private Map<String, DataHandler> files = new HashMap();

    public DataHandler readFile(String name) throws IOException {
        return files.get(name);
    }

    public void storeFile(String name, DataHandler file) throws IOException {
        files.put(name, file);
    }
}

ImageRepositoryEndpoint.java

package sample.mtom.ws;
@Endpoint
public class ImageRepositoryEndpoint {

    private static Repository repository;
    static {
        repository = new StubRepository();
    }

    @PayloadRoot(localPart = "imageRequest", namespace = "http://www.example.org/Image")
    @ResponsePayload
    public ImageResponse handleRequest(@RequestPayload ImageRequest request) throws IOException {
        repository.storeFile(request.getFileName(), request.getData());

        ImageResponse response = new ImageResponse();
        response.setFileName(request.getFileName());
        response.setData(repository.readFile(request.getFileName()));
        return response;
    }
}

schema.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
            targetNamespace="http://www.example.org/Image"
            elementFormDefault="qualified">

    <xsd:element name="imageRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="fileName" type="xsd:string"/>
                <xsd:element name="data" type="xsd:base64Binary" xmime:expectedContentTypes="*/*"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="imageResponse">
        <!-- the same as imageRequest -->
    </xsd:element>
</xsd:schema>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <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>

弹簧-WS-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:sws="http://www.springframework.org/schema/web-services"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
                           http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="sample.mtom.ws"/>

    <bean id="defaultMethodEndpointAdapter"
      class="org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter">
        <property name="methodArgumentResolvers" ref="marshallingPayloadMethodProcessor"/>
        <property name="methodReturnValueHandlers" ref="marshallingPayloadMethodProcessor"/>
    </bean>

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

    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="packagesToScan" value="sample.mtom.schema"/>
        <property name="mtomEnabled" value="true"/>
    </bean>

    <sws:dynamic-wsdl id="image"
                      portTypeName="ImageResource"
                      locationUri="/">
        <sws:xsd location="/WEB-INF/schema.xsd"/>
    </sws:dynamic-wsdl>
</beans>

0 个答案:

没有答案