From 3e7e422ba889aaebf15f6515dc49cee110f5cda0 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Mon, 28 Apr 2014 15:37:22 +0200 Subject: [PATCH] Reworked mtom sample to code config --- mtom/build.gradle | 5 ++ mtom/client/spring-ws/build.gradle | 5 +- .../ws/samples/mtom/client/sws/Driver.java | 4 +- .../mtom/client/sws/MtomClientConfig.java | 49 +++++++++++ .../mtom/client/sws/SaajMtomClient.java | 3 +- .../mtom/client/sws/applicationContext.xml | 28 ------- mtom/server/build.gradle | 6 +- .../mtom/config/MtomServerConfiguration.java | 84 +++++++++++++++++++ .../mtom/service/StubImageRepository.java | 2 + .../{webapp/WEB-INF => resources}/schema.xsd | 0 .../main/webapp/WEB-INF/spring-ws-servlet.xml | 37 -------- mtom/server/src/main/webapp/WEB-INF/web.xml | 29 ++----- 12 files changed, 158 insertions(+), 94 deletions(-) create mode 100644 mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/MtomClientConfig.java delete mode 100644 mtom/client/spring-ws/src/main/resources/org/springframework/ws/samples/mtom/client/sws/applicationContext.xml create mode 100644 mtom/server/src/main/java/org/springframework/ws/samples/mtom/config/MtomServerConfiguration.java rename mtom/server/src/main/{webapp/WEB-INF => resources}/schema.xsd (100%) delete mode 100644 mtom/server/src/main/webapp/WEB-INF/spring-ws-servlet.xml diff --git a/mtom/build.gradle b/mtom/build.gradle index 5736641..25ce793 100644 --- a/mtom/build.gradle +++ b/mtom/build.gradle @@ -1,3 +1,7 @@ +ext.springWsVersion = '2.2.0.BUILD-SNAPSHOT' +ext.springVersion = '4.0.3.RELEASE' + + subprojects { apply plugin: 'java' apply plugin: 'eclipse' @@ -5,6 +9,7 @@ subprojects { repositories { maven { url 'http://repo.spring.io/libs-release' } + maven { url 'http://repo.spring.io/libs-snapshot' } } dependencies { diff --git a/mtom/client/spring-ws/build.gradle b/mtom/client/spring-ws/build.gradle index 8e8dd9b..6947de7 100644 --- a/mtom/client/spring-ws/build.gradle +++ b/mtom/client/spring-ws/build.gradle @@ -2,12 +2,10 @@ configurations { jaxb } -ext.springWsVersion = '2.1.4.RELEASE' - task genJaxb { ext.sourcesDir = "${buildDir}/generated-sources/jaxb" ext.classesDir = "${buildDir}/classes/jaxb" - ext.schema = "${projectDir}/../../server/src/main/webapp/WEB-INF/schema.xsd" + ext.schema = "${projectDir}/../../server/src/main/resources/schema.xsd" inputs.files schema outputs.dir classesDir @@ -43,6 +41,7 @@ task genJaxb { dependencies { compile("org.springframework.ws:spring-ws-core:$springWsVersion") + compile("org.springframework:spring-context:$springVersion") compile("org.apache.ws.commons.axiom:axiom-api:1.2.14") compile(files(genJaxb.classesDir).builtBy(genJaxb)) diff --git a/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/Driver.java b/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/Driver.java index 2f7c556..8bea663 100644 --- a/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/Driver.java +++ b/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/Driver.java @@ -17,7 +17,7 @@ package org.springframework.ws.samples.mtom.client.sws; import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Driver { @@ -27,7 +27,7 @@ public class Driver { fileName = args[0]; } ApplicationContext applicationContext = - new ClassPathXmlApplicationContext("applicationContext.xml", Driver.class); + new AnnotationConfigApplicationContext(MtomClientConfig.class); SaajMtomClient saajClient = applicationContext.getBean("saajClient", SaajMtomClient.class); saajClient.doIt(fileName); diff --git a/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/MtomClientConfig.java b/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/MtomClientConfig.java new file mode 100644 index 0000000..6e18eac --- /dev/null +++ b/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/MtomClientConfig.java @@ -0,0 +1,49 @@ +package org.springframework.ws.samples.mtom.client.sws; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; +import org.springframework.ws.soap.axiom.AxiomSoapMessageFactory; +import org.springframework.ws.soap.saaj.SaajSoapMessageFactory; + +/** + * @author Arjen Poutsma + */ +@Configuration +public class MtomClientConfig { + + @Bean + public SaajMtomClient saajClient() { + SaajMtomClient client = new SaajMtomClient(saajSoapMessageFactory()); + client.setDefaultUri("http://localhost:8080/mtom-server/services"); + client.setMarshaller(marshaller()); + client.setUnmarshaller(marshaller()); + return client; + } + + @Bean + public SaajSoapMessageFactory saajSoapMessageFactory() { + return new SaajSoapMessageFactory(); + } + + @Bean + public AxiomMtomClient axiomClient() { + AxiomMtomClient client = new AxiomMtomClient(axiomSoapMessageFactory()); + client.setDefaultUri("http://localhost:8080/mtom-server/services"); + return client; + } + + @Bean + public AxiomSoapMessageFactory axiomSoapMessageFactory() { + return new AxiomSoapMessageFactory(); + } + + @Bean + public Jaxb2Marshaller marshaller() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setContextPath("org.springframework.ws.samples.mtom.client.sws"); + marshaller.setMtomEnabled(true); + return marshaller; + } + +} diff --git a/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/SaajMtomClient.java b/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/SaajMtomClient.java index de78e43..b891979 100644 --- a/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/SaajMtomClient.java +++ b/mtom/client/spring-ws/src/main/java/org/springframework/ws/samples/mtom/client/sws/SaajMtomClient.java @@ -27,7 +27,7 @@ import org.springframework.ws.soap.client.SoapFaultClientException; import org.springframework.ws.soap.saaj.SaajSoapMessageFactory; /** - * Simple client that demonstartes MTOM by invoking StoreImage and LoadImage using a + * Simple client that demonstrates MTOM by invoking {@code StoreImage} and {@code LoadImage} using a * WebServiceTemplate and SAAJ. * * @author Tareq Abed Rabbo @@ -66,6 +66,7 @@ public class SaajMtomClient extends WebServiceGatewaySupport { stopWatch.stop(); } + @SuppressWarnings("unchecked") private void load(String path) { JAXBElement loadImageRequest = objectFactory.createLoadImageRequest(StringUtils.getFilename(path)); diff --git a/mtom/client/spring-ws/src/main/resources/org/springframework/ws/samples/mtom/client/sws/applicationContext.xml b/mtom/client/spring-ws/src/main/resources/org/springframework/ws/samples/mtom/client/sws/applicationContext.xml deleted file mode 100644 index 52d6395..0000000 --- a/mtom/client/spring-ws/src/main/resources/org/springframework/ws/samples/mtom/client/sws/applicationContext.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/mtom/server/build.gradle b/mtom/server/build.gradle index b932ee3..1912428 100644 --- a/mtom/server/build.gradle +++ b/mtom/server/build.gradle @@ -12,8 +12,6 @@ buildscript { } } -ext.springVersion = '3.2.4.RELEASE' -ext.springWsVersion = '2.1.4.RELEASE' ext.tomcatVersion = '7.0.42' @@ -64,10 +62,12 @@ tomcatRun { dependencies { compile("org.springframework.ws:spring-ws-core:$springWsVersion") + compile("org.springframework:spring-context:$springVersion") compile(files(genJaxb.classesDir).builtBy(genJaxb)) runtime("log4j:log4j:1.2.16") - + runtime("wsdl4j:wsdl4j:1.6.1") + jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7" tomcat("org.apache.tomcat.embed:tomcat-embed-core:$tomcatVersion", diff --git a/mtom/server/src/main/java/org/springframework/ws/samples/mtom/config/MtomServerConfiguration.java b/mtom/server/src/main/java/org/springframework/ws/samples/mtom/config/MtomServerConfiguration.java new file mode 100644 index 0000000..307d942 --- /dev/null +++ b/mtom/server/src/main/java/org/springframework/ws/samples/mtom/config/MtomServerConfiguration.java @@ -0,0 +1,84 @@ +package org.springframework.ws.samples.mtom.config; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; +import org.springframework.ws.config.annotation.EnableWs; +import org.springframework.ws.config.annotation.WsConfigurationSupport; +import org.springframework.ws.samples.mtom.service.ImageRepository; +import org.springframework.ws.samples.mtom.service.StubImageRepository; +import org.springframework.ws.samples.mtom.ws.ImageRepositoryEndpoint; +import org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter; +import org.springframework.ws.server.endpoint.adapter.method.MarshallingPayloadMethodProcessor; +import org.springframework.ws.server.endpoint.adapter.method.MethodArgumentResolver; +import org.springframework.ws.server.endpoint.adapter.method.MethodReturnValueHandler; +import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition; +import org.springframework.xml.xsd.SimpleXsdSchema; + +/** + * @author Arjen Poutsma + */ +@EnableWs +@Configuration +public class MtomServerConfiguration extends WsConfigurationSupport { + + @Bean + public ImageRepository imageRepository() { + return new StubImageRepository(); + } + + @Bean + public ImageRepositoryEndpoint imageRepositoryEndpoint() { + return new ImageRepositoryEndpoint(imageRepository()); + } + + @Bean + @Override + public DefaultMethodEndpointAdapter defaultMethodEndpointAdapter() { + List argumentResolvers = + new ArrayList(); + argumentResolvers.add(methodProcessor()); + + List returnValueHandlers = + new ArrayList(); + returnValueHandlers.add(methodProcessor()); + + DefaultMethodEndpointAdapter adapter = new DefaultMethodEndpointAdapter(); + adapter.setMethodArgumentResolvers(argumentResolvers); + adapter.setMethodReturnValueHandlers(returnValueHandlers); + + return adapter; + } + + @Bean + public MarshallingPayloadMethodProcessor methodProcessor() { + return new MarshallingPayloadMethodProcessor(marshaller()); + } + + @Bean + public Jaxb2Marshaller marshaller() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setContextPath("org.springframework.ws.samples.mtom.schema"); + marshaller.setMtomEnabled(true); + return marshaller; + } + + @Bean + public DefaultWsdl11Definition mtom() { + DefaultWsdl11Definition definition = new DefaultWsdl11Definition(); + definition.setSchema(schema()); + definition.setPortTypeName("ImageRepository"); + definition.setLocationUri("http://localhost:8080/mtom-server/"); + return definition; + } + + @Bean + public SimpleXsdSchema schema() { + return new SimpleXsdSchema(new ClassPathResource("/schema.xsd")); + } + +} diff --git a/mtom/server/src/main/java/org/springframework/ws/samples/mtom/service/StubImageRepository.java b/mtom/server/src/main/java/org/springframework/ws/samples/mtom/service/StubImageRepository.java index 163a228..abdd25f 100644 --- a/mtom/server/src/main/java/org/springframework/ws/samples/mtom/service/StubImageRepository.java +++ b/mtom/server/src/main/java/org/springframework/ws/samples/mtom/service/StubImageRepository.java @@ -31,11 +31,13 @@ public class StubImageRepository implements ImageRepository { private Map images = new HashMap(); + @Override public Image readImage(String name) throws IOException { logger.info("Loading image " + name); return images.get(name); } + @Override public void storeImage(String name, Image image) throws IOException { logger.info("Storing image " + name + " [" + image.getWidth(null) + "x" + image.getHeight(null) + "]"); images.put(name, image); diff --git a/mtom/server/src/main/webapp/WEB-INF/schema.xsd b/mtom/server/src/main/resources/schema.xsd similarity index 100% rename from mtom/server/src/main/webapp/WEB-INF/schema.xsd rename to mtom/server/src/main/resources/schema.xsd diff --git a/mtom/server/src/main/webapp/WEB-INF/spring-ws-servlet.xml b/mtom/server/src/main/webapp/WEB-INF/spring-ws-servlet.xml deleted file mode 100644 index 58e0614..0000000 --- a/mtom/server/src/main/webapp/WEB-INF/spring-ws-servlet.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/mtom/server/src/main/webapp/WEB-INF/web.xml b/mtom/server/src/main/webapp/WEB-INF/web.xml index 68288e8..b843902 100644 --- a/mtom/server/src/main/webapp/WEB-INF/web.xml +++ b/mtom/server/src/main/webapp/WEB-INF/web.xml @@ -1,29 +1,18 @@ - - - - + MyCompany HR Holiday Service spring-ws org.springframework.ws.transport.http.MessageDispatcherServlet + + contextClass + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + contextConfigLocation + org.springframework.ws.samples.mtom.config.MtomServerConfiguration +