Added mtom tutorial

This commit is contained in:
Arjen Poutsma
2007-06-06 02:25:46 +00:00
parent 665bbaaef2
commit 3b06729ec5
13 changed files with 497 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
<?xml version="1.0"?>
<project name="spring-ws-airline-sample-jax-ws-client" default="build" xmlns:artifact="urn:maven-artifact-ant">
<property name="bin.dir" value="bin"/>
<property name="src.dir" value="src"/>
<property name="gen.dir" value="gen"/>
<property name="lib.dir" value="lib"/>
<target name="init">
<mkdir dir="${bin.dir}"/>
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
<classpath>
<pathelement location="${basedir}/../../../maven-artifact-ant-2.0.4-dep.jar"/>
</classpath>
</typedef>
<artifact:remoteRepository id="java.net" url="https://maven-repository.dev.java.net/nonav/repository"
layout="legacy"/>
<artifact:remoteRepository id="spring-ext"
url="https://svn.sourceforge.net/svnroot/springframework/repos/repo-ext/"/>
<artifact:dependencies pathId="generate.classpath">
<remoteRepository refid="java.net"/>
<remoteRepository refid="spring-ext"/>
<dependency groupId="com.sun.xml.ws" artifactId="jaxws-tools" version="EA3"/>
<dependency groupId="com.sun.xml.bind" artifactId="jaxb-xjc" version="2.1-EA2"/>
<dependency groupId="com.sun.xml.stream.buffer" artifactId="streambuffer" version="0.3"/>
<dependency groupId="com.sun.xml.stream" artifactId="sjsxp" version="1.0"/>
<dependency groupId="org.jvnet.staxex" artifactId="stax-ex" version="1.0"/>
<dependency groupId="com.sun.xml.messaging.saaj" artifactId="saaj-impl" version="1.3"/>
</artifact:dependencies>
<artifact:dependencies pathId="compile.classpath">
<remoteRepository refid="java.net"/>
<remoteRepository refid="spring-ext"/>
<dependency groupId="javax.xml.ws" artifactId="jaxws-api" version="2.1-SNAPSHOT"/>
<dependency groupId="org.springframework" artifactId="spring-core" version="2.0.5"/>
</artifact:dependencies>
<artifact:dependencies pathId="runtime.classpath">
<remoteRepository refid="java.net"/>
<remoteRepository refid="spring-ext"/>
<dependency groupId="com.sun.xml.ws" artifactId="jaxws-rt" version="EA3"/>
<dependency groupId="com.sun.xml.bind" artifactId="jaxb-impl" version="2.1-EA2"/>
<dependency groupId="com.sun.xml.bind" artifactId="jaxb-xjc" version="2.1-EA2"/>
<dependency groupId="com.sun.xml.stream.buffer" artifactId="streambuffer" version="0.3"/>
<dependency groupId="com.sun.xml.stream" artifactId="sjsxp" version="1.0"/>
<dependency groupId="org.jvnet.staxex" artifactId="stax-ex" version="1.0"/>
<dependency groupId="com.sun.xml.messaging.saaj" artifactId="saaj-impl" version="1.3"/>
</artifact:dependencies>
</target>
<target name="generate" depends="init">
<taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport" classpathref="generate.classpath"/>
<wsimport wsdl="http://localhost:8080/mtom/mtom.wsdl" destdir="${bin.dir}"
package="org.springframework.ws.samples.mtom.client.jaxws">
<produces dir="${bin.dir}/org/springframework/ws/samples/mtom/client/jaxws" includes="**/*" />
</wsimport>
</target>
<target name="build" depends="generate">
<javac srcdir="${src.dir}" destdir="${bin.dir}" debug="true">
<classpath refid="compile.classpath"/>
<classpath location="${bin.dir}"/>
</javac>
</target>
<target name="clean">
<delete dir="${bin.dir}"/>
</target>
<target name="run" depends="build">
<java classname="org.springframework.ws.samples.mtom.client.jaxws.Main" fork="true" failonerror="true">
<arg path="spring-ws-logo.png"/>
<classpath refid="compile.classpath"/>
<classpath refid="runtime.classpath"/>
<classpath location="${bin.dir}"/>
</java>
</target></project>

View File

@@ -0,0 +1,11 @@
SPRING WEB SERVICES
This directory contains a Java client for the MTOM Sample that uses JAX-WS.
The client can be run from the provided ant file, by calling "ant run".
Note that the airline sample has to be running before invoking this target.
Client Sample table of contents
---------------------------------------------------
* src - The source files for the client
* build.xml - Ant build file with a 'build' and a 'run' target

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed 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.springframework.ws.samples.mtom.client.jaxws;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import javax.activation.DataHandler;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.SOAPBinding;
import javax.xml.ws.soap.SOAPFaultException;
import org.springframework.util.StopWatch;
/**
* Simple client that calls the <code>GetFlights</code> and <code>BookFlight</code> operations using JAX-WS.
*
* @author Arjen Poutsma
*/
public class Main {
public static void main(String[] args) throws IOException {
try {
ImageRepositoryService service = new ImageRepositoryService();
ImageRepository imageRepository = service.getImageRepositoryPort();
SOAPBinding binding = (SOAPBinding)((BindingProvider)imageRepository).getBinding ();
binding.setMTOMEnabled (true);
Image request = new Image();
File file = new File(args[0]);
request.setName(file.getName());
request.setImage(new DataHandler(file.toURL()));
StopWatch stopWatch = new StopWatch();
stopWatch.start("store");
imageRepository.storeImage(request);
stopWatch.stop();
stopWatch.start("load");
Image response = imageRepository.loadImage(file.getName());
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
}
catch (SOAPFaultException ex) {
System.err.format("SOAP Fault Code %1s%n", ex.getFault().getFaultCodeAsQName());
System.err.format("SOAP Fault String: %1s%n", ex.getFault().getFaultString());
}
}
}

87
samples/mtom/pom.xml Normal file
View File

@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>spring-ws-samples</artifactId>
<groupId>org.springframework.ws</groupId>
<version>1.0-rc2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mtom</artifactId>
<packaging>war</packaging>
<name>Spring WS MTOM Example</name>
<description>This sample shows how to use MTOM and JAXB2.</description>
<pluginRepositories>
<pluginRepository>
<id>java.net</id>
<name>Java.net Repository for Maven2</name>
<url>http://download.java.net/maven/1/</url>
<layout>legacy</layout>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<generatePackage>org.springframework.ws.samples.mtom.schema</generatePackage>
<schemaDirectory>src/main/webapp/WEB-INF</schemaDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Spring-WS dependencies -->
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core-tiger</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-oxm-tiger</artifactId>
</dependency>
<!-- JEE Mapping dependencies -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.3</version>
<scope>runtime</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
</dependency>
<dependency>
<groupId>easymock</groupId>
<artifactId>easymock</artifactId>
<version>1.2_Java1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

12
samples/mtom/readme.txt Normal file
View File

@@ -0,0 +1,12 @@
====================================
== Spring Web Service MTOM Sample ==
====================================
1. INTRODUCTION
This sample shows how to use marshal and unmarshal MTOM attachments using JAXB2.
2. INSTALLATION
Simply run "mvn package" and deploy the war file generated in target

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2007 the original author or authors.
*
* Licensed 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.springframework.ws.samples.mtom.service;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
/** @author Arjen Poutsma */
public interface ImageRepository {
void streamImage(String name, OutputStream os) throws IOException;
void storeImage(String name, InputStream is) throws IOException;
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2007 the original author or authors.
*
* Licensed 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.springframework.ws.samples.mtom.service;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.FileCopyUtils;
/** @author Arjen Poutsma */
public class StubImageRepository implements ImageRepository {
private static final Log logger = LogFactory.getLog(StubImageRepository.class);
private Map<String, byte[]> images = new HashMap<String, byte[]>();
public void streamImage(String name, OutputStream os) throws IOException {
if (images.containsKey(name)) {
logger.info("Streaming image " + name);
byte[] buf = images.get(name);
FileCopyUtils.copy(buf, os);
}
}
public void storeImage(String name, InputStream is) throws IOException {
byte[] buf = FileCopyUtils.copyToByteArray(is);
logger.info("Storing image " + name + " with size: " + buf.length);
images.put(name, buf);
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2007 the original author or authors.
*
* Licensed 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.springframework.ws.samples.mtom.ws;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.xml.bind.JAXBElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.Assert;
import org.springframework.ws.samples.mtom.schema.Image;
import org.springframework.ws.samples.mtom.schema.ObjectFactory;
import org.springframework.ws.samples.mtom.service.ImageRepository;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
/** @author Arjen Poutsma */
@Endpoint
public class ImageRepositoryEndpoint {
private ImageRepository imageRepository;
private ObjectFactory objectFactory;
public ImageRepositoryEndpoint(ImageRepository imageRepository) {
Assert.notNull(imageRepository, "'imageRepository' must not be null");
this.imageRepository = imageRepository;
this.objectFactory = new ObjectFactory();
}
@PayloadRoot(localPart = "StoreImageRequest", namespace = "http://www.springframework.org/spring-ws/samples/mtom")
public void store(JAXBElement<Image> requestElement) throws IOException {
Image request = requestElement.getValue();
imageRepository.storeImage(request.getName(), request.getImage().getInputStream());
}
@PayloadRoot(localPart = "LoadImageRequest", namespace = "http://www.springframework.org/spring-ws/samples/mtom")
public JAXBElement<Image> load(JAXBElement<String> requestElement) throws IOException {
String name = requestElement.getValue();
ByteArrayOutputStream os = new ByteArrayOutputStream();
imageRepository.streamImage(name, os);
DataHandler dataHandler = new DataHandler(os.toByteArray(), "application/octet-stream");
Image response = new Image();
response.setName(name);
response.setImage(dataHandler);
return objectFactory.createLoadImageResponse(response);
}
}

View File

@@ -0,0 +1,7 @@
log4j.rootLogger=WARN, stdout
log4j.logger.org.springframework.ws=DEBUG
log4j.logger.org.springframework.xml=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

View File

@@ -0,0 +1,19 @@
<?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="application/octet-stream"/>
</sequence>
</complexType>
</schema>

View File

@@ -0,0 +1,31 @@
<?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.MarshallingMethodEndpointAdapter">
<constructor-arg ref="marshaller"/>
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="org.springframework.ws.samples.mtom.schema"/>
</bean>
<bean id="mtom" class="org.springframework.ws.wsdl.wsdl11.DynamicWsdl11Definition">
<property name="builder">
<bean class="org.springframework.ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilder">
<property name="schema" value="/WEB-INF/schema.xsd"/>
<property name="portTypeName" value="ImageRepository"/>
<property name="locationUri" value="http://localhost:8080/mtom/"/>
</bean>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2007 the original author or authors.
~
~ Licensed 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.
-->
<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>