From 070f72cb33783645b748d2e8efc95688509d5029 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 20 Nov 2013 16:14:38 +0100 Subject: [PATCH] Added stockquote sample --- README.md | 3 +- echo/README.md | 2 +- stockquote/README.md | 31 ++++++++ stockquote/build.gradle | 19 +++++ stockquote/client/jax-ws/.gitignore | 6 ++ stockquote/client/jax-ws/build.gradle | 38 ++++++++++ .../samples/stockquote/client/jaxws/Main.java | 57 +++++++++++++++ stockquote/client/spring-ws/.gitignore | 6 ++ stockquote/client/spring-ws/build.gradle | 11 +++ .../stockquote/client/sws/StockClient.java | 61 ++++++++++++++++ .../src/main/resources/log4j.properties | 7 ++ .../client/sws/applicationContext.xml | 13 ++++ .../stockquote/client/sws/quotesRequest.xml | 5 ++ stockquote/server/.gitignore | 6 ++ stockquote/server/build.gradle | 60 +++++++++++++++ .../ws/samples/stockquote/Driver.java | 34 +++++++++ .../samples/stockquote/ws/StockService.java | 68 +++++++++++++++++ .../src/main/resources/log4j.properties | 7 ++ .../samples/stockquote/applicationContext.xml | 26 +++++++ .../stockquote/ws/applicationContext-ws.xml | 40 ++++++++++ .../ws/samples/stockquote/ws/stockquote.wsdl | 73 +++++++++++++++++++ stockquote/settings.gradle | 2 + 22 files changed, 573 insertions(+), 2 deletions(-) create mode 100644 stockquote/README.md create mode 100644 stockquote/build.gradle create mode 100644 stockquote/client/jax-ws/.gitignore create mode 100644 stockquote/client/jax-ws/build.gradle create mode 100644 stockquote/client/jax-ws/src/main/java/org/springframework/ws/samples/stockquote/client/jaxws/Main.java create mode 100644 stockquote/client/spring-ws/.gitignore create mode 100644 stockquote/client/spring-ws/build.gradle create mode 100644 stockquote/client/spring-ws/src/main/java/org/springframework/ws/samples/stockquote/client/sws/StockClient.java create mode 100644 stockquote/client/spring-ws/src/main/resources/log4j.properties create mode 100644 stockquote/client/spring-ws/src/main/resources/org/springframework/ws/samples/stockquote/client/sws/applicationContext.xml create mode 100644 stockquote/client/spring-ws/src/main/resources/org/springframework/ws/samples/stockquote/client/sws/quotesRequest.xml create mode 100644 stockquote/server/.gitignore create mode 100644 stockquote/server/build.gradle create mode 100644 stockquote/server/src/main/java/org/springframework/ws/samples/stockquote/Driver.java create mode 100644 stockquote/server/src/main/java/org/springframework/ws/samples/stockquote/ws/StockService.java create mode 100644 stockquote/server/src/main/resources/log4j.properties create mode 100644 stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/applicationContext.xml create mode 100644 stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/ws/applicationContext-ws.xml create mode 100644 stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/ws/stockquote.wsdl create mode 100644 stockquote/settings.gradle diff --git a/README.md b/README.md index 31d92cd..23d6e2a 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,13 @@ additional instructions. - [echo](./echo) - a simple sample that shows a bare-bones Echo service - [mtom](./mtom) - shows how to use MTOM and JAXB2 marshalling +- [stockquote](./stockquote) - shows how to use WS-Addressing and the Java 6 HTTP Server - [tutorial](./tutorial) - contains the code from the Spring-WS tutorial ## Running the Server Most of the sample apps can be built and run using the following commands from -within the sample's folder. +within the ``server`` folder. ```sh $ ./gradlew tomcatRun diff --git a/echo/README.md b/echo/README.md index 4f60173..d866539 100644 --- a/echo/README.md +++ b/echo/README.md @@ -1,4 +1,4 @@ -# Spring Web Service Tutorial +# Echo Sample This sample shows a bare-bones echoing service. Incoming messages are handled via DOM, and a simple 'business logic' service is used to obtain the result. diff --git a/stockquote/README.md b/stockquote/README.md new file mode 100644 index 0000000..000fee5 --- /dev/null +++ b/stockquote/README.md @@ -0,0 +1,31 @@ +# Stock Quote Sample + +This sample shows a Stock Quote service. Incoming messages are routed via +WS-Addressing, and SOAP message content is handled using JAXB2. Additionally, +this sample uses the HTTP server built into Java 6. + +## Running the Server + +The server can be built and run using the following command from +within the ``server`` folder. + + ```sh + $ ./gradlew runServer + ``` + +## Running the Client(s) + +There is a separate ``client`` directory containing clients that connect to the +server. You can run these clients by using the following command from within +each of client subdirectories: + + ```sh + $ gradle runClient + ``` + +## License + +[Spring Web Services] is released under version 2.0 of the [Apache License]. + +[Spring Web Services]: http://projects.spring.io/spring-ws +[Apache License]: http://www.apache.org/licenses/LICENSE-2.0 \ No newline at end of file diff --git a/stockquote/build.gradle b/stockquote/build.gradle new file mode 100644 index 0000000..5736641 --- /dev/null +++ b/stockquote/build.gradle @@ -0,0 +1,19 @@ +subprojects { + apply plugin: 'java' + apply plugin: 'eclipse' + apply plugin: 'idea' + + repositories { + maven { url 'http://repo.spring.io/libs-release' } + } + + dependencies { + testCompile("junit:junit:4.10") + testCompile("org.easymock:easymock:3.1") + } + +} + +task wrapper(type: Wrapper) { + gradleVersion = '1.8' +} diff --git a/stockquote/client/jax-ws/.gitignore b/stockquote/client/jax-ws/.gitignore new file mode 100644 index 0000000..2739940 --- /dev/null +++ b/stockquote/client/jax-ws/.gitignore @@ -0,0 +1,6 @@ +target +*.iml +.classpath +.project +.settings + diff --git a/stockquote/client/jax-ws/build.gradle b/stockquote/client/jax-ws/build.gradle new file mode 100644 index 0000000..f89b89c --- /dev/null +++ b/stockquote/client/jax-ws/build.gradle @@ -0,0 +1,38 @@ +configurations { + wsimport +} + +ext.springWsVersion = '2.1.4.RELEASE' + +task wsImport { + ext.outputDir = "${buildDir}/classes/wsimport" + ext.wsdl = "${projectDir}/../../server//src/main/resources/org/springframework/ws/samples/stockquote/ws/stockquote.wsdl" + + inputs.files wsdl + outputs.dir outputDir + + doLast() { + project.ant { + taskdef name: "wsimport", classname: "com.sun.tools.ws.ant.WsImport", + classpath: configurations.wsimport.asPath + mkdir(dir: outputDir) + + wsimport(destdir: outputDir, wsdl: wsdl, + package: "org.springframework.ws.samples.stockquote.client.jaxws") { + produces(dir: outputDir, includes: "**/*.class") + } + } + } +} + +dependencies { + compile("org.springframework.ws:spring-ws-core:$springWsVersion") + compile(files(wsImport.outputDir).builtBy(wsImport)) + runtime("log4j:log4j:1.2.16") + wsimport "com.sun.xml.ws:jaxws-tools:2.1.7" +} + +task runClient(dependsOn: 'classes', type:JavaExec) { + main = "org.springframework.ws.samples.stockquote.client.jaxws.Main" + classpath = sourceSets.main.runtimeClasspath +} \ No newline at end of file diff --git a/stockquote/client/jax-ws/src/main/java/org/springframework/ws/samples/stockquote/client/jaxws/Main.java b/stockquote/client/jax-ws/src/main/java/org/springframework/ws/samples/stockquote/client/jaxws/Main.java new file mode 100644 index 0000000..8931d2f --- /dev/null +++ b/stockquote/client/jax-ws/src/main/java/org/springframework/ws/samples/stockquote/client/jaxws/Main.java @@ -0,0 +1,57 @@ +/* + * 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.stockquote.client.jaxws; + +import java.net.URL; +import javax.xml.namespace.QName; + +/** + * Simple client that calls the StockQuote operations using JAX-WS. + * + * @author Arjen Poutsma + */ +public class Main { + + public static void main(String[] args) throws Exception { + StockService service; + if (args.length == 0) { + service = new StockService(); + } + else { + QName serviceName = new QName("http://www.springframework.org/spring-ws/samples/stockquote", "StockSoap11"); + service = new StockService(new URL(args[0]), serviceName); + } + Stock stock = service.getStockSoap11(); + StockQuoteRequest request = new StockQuoteRequest(); + request.getSymbol().add("FABRIKAM"); + request.getSymbol().add("CONTOSO"); + + System.out.format("Requesting quotes for %s%n", request.getSymbol()); + + StockQuoteResponse response = stock.stockQuote(request); + + System.out.format("Got %d results%n", response.getStockQuote().size()); + for (StockQuote quote : response.getStockQuote()) { + System.out.println(); + System.out.println("Symbol: " + quote.getSymbol()); + System.out.println("\tName:\t\t\t" + quote.getName()); + System.out.println("\tLast Price:\t\t" + quote.getLast()); + System.out.println("\tPrevious Change:\t" + quote.getChange() + "%"); + } + } + +} diff --git a/stockquote/client/spring-ws/.gitignore b/stockquote/client/spring-ws/.gitignore new file mode 100644 index 0000000..2739940 --- /dev/null +++ b/stockquote/client/spring-ws/.gitignore @@ -0,0 +1,6 @@ +target +*.iml +.classpath +.project +.settings + diff --git a/stockquote/client/spring-ws/build.gradle b/stockquote/client/spring-ws/build.gradle new file mode 100644 index 0000000..d41d980 --- /dev/null +++ b/stockquote/client/spring-ws/build.gradle @@ -0,0 +1,11 @@ +ext.springWsVersion = '2.1.4.RELEASE' + +dependencies { + compile("org.springframework.ws:spring-ws-core:$springWsVersion") + runtime("log4j:log4j:1.2.16") +} + +task runClient(dependsOn: 'classes', type:JavaExec) { + main = "org.springframework.ws.samples.stockquote.client.sws.StockClient" + classpath = sourceSets.main.runtimeClasspath +} \ No newline at end of file diff --git a/stockquote/client/spring-ws/src/main/java/org/springframework/ws/samples/stockquote/client/sws/StockClient.java b/stockquote/client/spring-ws/src/main/java/org/springframework/ws/samples/stockquote/client/sws/StockClient.java new file mode 100644 index 0000000..1f05c2b --- /dev/null +++ b/stockquote/client/spring-ws/src/main/java/org/springframework/ws/samples/stockquote/client/sws/StockClient.java @@ -0,0 +1,61 @@ +/* + * 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.stockquote.client.sws; + +import java.io.IOException; +import java.net.URI; +import javax.xml.transform.Source; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.core.io.Resource; +import org.springframework.ws.client.core.support.WebServiceGatewaySupport; +import org.springframework.ws.soap.addressing.client.ActionCallback; +import org.springframework.xml.transform.ResourceSource; +import org.springframework.xml.transform.StringResult; + +public class StockClient extends WebServiceGatewaySupport { + + private Resource request; + + private URI action; + + public void setRequest(Resource request) { + this.request = request; + } + + public void setAction(URI action) { + this.action = action; + } + + public void quotes() throws IOException { + Source requestSource = new ResourceSource(request); + StringResult result = new StringResult(); + getWebServiceTemplate().sendSourceAndReceiveToResult(requestSource, new ActionCallback(action), result); + System.out.println(); + System.out.println(result); + System.out.println(); + } + + public static void main(String[] args) throws IOException { + ApplicationContext applicationContext = + new ClassPathXmlApplicationContext("applicationContext.xml", StockClient.class); + StockClient stockClient = (StockClient) applicationContext.getBean("stockClient"); + stockClient.quotes(); + } + +} diff --git a/stockquote/client/spring-ws/src/main/resources/log4j.properties b/stockquote/client/spring-ws/src/main/resources/log4j.properties new file mode 100644 index 0000000..0f73a75 --- /dev/null +++ b/stockquote/client/spring-ws/src/main/resources/log4j.properties @@ -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 \ No newline at end of file diff --git a/stockquote/client/spring-ws/src/main/resources/org/springframework/ws/samples/stockquote/client/sws/applicationContext.xml b/stockquote/client/spring-ws/src/main/resources/org/springframework/ws/samples/stockquote/client/sws/applicationContext.xml new file mode 100644 index 0000000..83cbb81 --- /dev/null +++ b/stockquote/client/spring-ws/src/main/resources/org/springframework/ws/samples/stockquote/client/sws/applicationContext.xml @@ -0,0 +1,13 @@ + + + + + + + + + + diff --git a/stockquote/client/spring-ws/src/main/resources/org/springframework/ws/samples/stockquote/client/sws/quotesRequest.xml b/stockquote/client/spring-ws/src/main/resources/org/springframework/ws/samples/stockquote/client/sws/quotesRequest.xml new file mode 100644 index 0000000..b40a2dc --- /dev/null +++ b/stockquote/client/spring-ws/src/main/resources/org/springframework/ws/samples/stockquote/client/sws/quotesRequest.xml @@ -0,0 +1,5 @@ + + + FABRIKAM + CONTOSO + diff --git a/stockquote/server/.gitignore b/stockquote/server/.gitignore new file mode 100644 index 0000000..2739940 --- /dev/null +++ b/stockquote/server/.gitignore @@ -0,0 +1,6 @@ +target +*.iml +.classpath +.project +.settings + diff --git a/stockquote/server/build.gradle b/stockquote/server/build.gradle new file mode 100644 index 0000000..31b6013 --- /dev/null +++ b/stockquote/server/build.gradle @@ -0,0 +1,60 @@ +configurations { + jaxb +} + +ext.springVersion = '3.2.4.RELEASE' +ext.springWsVersion = '2.1.4.RELEASE' + +task genJaxb { + ext.sourcesDir = "${buildDir}/generated-sources/jaxb" + ext.classesDir = "${buildDir}/classes/jaxb" + ext.schema = "${projectDir}/src/main/resources/org/springframework/ws/samples/stockquote/ws/stockquote.wsdl" + + inputs.files schema + outputs.dir classesDir + + doLast() { + project.ant { + taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask", + classpath: configurations.jaxb.asPath + mkdir(dir: sourcesDir) + mkdir(dir: classesDir) + + xjc(destdir: sourcesDir, schema: schema, + package: "org.springframework.ws.samples.stockquote.schema") { + arg(value: "-wsdl") + produces(dir: sourcesDir, includes: "**/*.java") + } + + javac(destdir: classesDir, source: 1.6, target: 1.6, debug: true, + debugLevel: "lines,vars,source", + classpath: configurations.jaxb.asPath) { + src(path: sourcesDir) + include(name: "**/*.java") + include(name: "*.java") + } + + copy(todir: classesDir) { + fileset(dir: sourcesDir, erroronmissingdir: false) { + exclude(name: "**/*.java") + } + } + } + } +} + +task runServer(dependsOn: 'classes', type:JavaExec) { + main = "org.springframework.ws.samples.stockquote.Driver" + standardInput = System.in + classpath = sourceSets.main.runtimeClasspath +} + +dependencies { + compile("org.springframework.ws:spring-ws-core:$springWsVersion") + compile("org.springframework.ws:spring-ws-support:$springWsVersion") + compile(files(genJaxb.classesDir).builtBy(genJaxb)) + + runtime("log4j:log4j:1.2.16") + + jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7" +} \ No newline at end of file diff --git a/stockquote/server/src/main/java/org/springframework/ws/samples/stockquote/Driver.java b/stockquote/server/src/main/java/org/springframework/ws/samples/stockquote/Driver.java new file mode 100644 index 0000000..75bac39 --- /dev/null +++ b/stockquote/server/src/main/java/org/springframework/ws/samples/stockquote/Driver.java @@ -0,0 +1,34 @@ +/* + * Copyright 2008 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.stockquote; + +import java.io.IOException; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class Driver { + + public static void main(String[] args) throws IOException { + ClassPathXmlApplicationContext applicationContext = + new ClassPathXmlApplicationContext("applicationContext.xml", Driver.class); + System.out.println(); + System.out.println("Press [Enter] to shut down..."); + System.in.read(); + applicationContext.close(); + } + +} diff --git a/stockquote/server/src/main/java/org/springframework/ws/samples/stockquote/ws/StockService.java b/stockquote/server/src/main/java/org/springframework/ws/samples/stockquote/ws/StockService.java new file mode 100644 index 0000000..64eed48 --- /dev/null +++ b/stockquote/server/src/main/java/org/springframework/ws/samples/stockquote/ws/StockService.java @@ -0,0 +1,68 @@ +/* + * Copyright 2008 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.stockquote.ws; + +import java.util.GregorianCalendar; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.springframework.ws.samples.stockquote.schema.StockQuote; +import org.springframework.ws.samples.stockquote.schema.StockQuoteRequest; +import org.springframework.ws.samples.stockquote.schema.StockQuoteResponse; +import org.springframework.ws.server.endpoint.annotation.Endpoint; +import org.springframework.ws.soap.addressing.server.annotation.Action; +import org.springframework.ws.soap.addressing.server.annotation.Address; + +@Endpoint +@Address("http://localhost:8080/StockService") +// optional +public class StockService { + + private DatatypeFactory datatypeFactory; + + public StockService() throws DatatypeConfigurationException { + datatypeFactory = DatatypeFactory.newInstance(); + } + + @Action(value = "http://www.springframework.org/spring-ws/samples/stockquote/StockService/GetQuote", + output = "http://www.springframework.org/spring-ws/samples/stockquote/StockService/Quotes") + public StockQuoteResponse getStockQuotes(StockQuoteRequest request) { + StockQuoteResponse response = new StockQuoteResponse(); + + XMLGregorianCalendar now = datatypeFactory.newXMLGregorianCalendar(new GregorianCalendar()); + for (String symbol : request.getSymbol()) { + StockQuote quote = new StockQuote(); + quote.setSymbol(symbol); + quote.setDate(now); + if ("FABRIKAM".equals(symbol)) { + quote.setName("Fabrikam, Inc."); + quote.setLast(120.00); + quote.setChange(5.5); + } + else { + quote.setName("Contoso Corp."); + quote.setLast(50.07); + quote.setChange(1.15); + } + response.getStockQuote().add(quote); + } + + return response; + } + +} diff --git a/stockquote/server/src/main/resources/log4j.properties b/stockquote/server/src/main/resources/log4j.properties new file mode 100644 index 0000000..0f73a75 --- /dev/null +++ b/stockquote/server/src/main/resources/log4j.properties @@ -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 \ No newline at end of file diff --git a/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/applicationContext.xml b/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/applicationContext.xml new file mode 100644 index 0000000..8f53e13 --- /dev/null +++ b/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/applicationContext.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/ws/applicationContext-ws.xml b/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/ws/applicationContext-ws.xml new file mode 100644 index 0000000..ca23b94 --- /dev/null +++ b/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/ws/applicationContext-ws.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/ws/stockquote.wsdl b/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/ws/stockquote.wsdl new file mode 100644 index 0000000..76a212d --- /dev/null +++ b/stockquote/server/src/main/resources/org/springframework/ws/samples/stockquote/ws/stockquote.wsdl @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/stockquote/settings.gradle b/stockquote/settings.gradle new file mode 100644 index 0000000..b3428de --- /dev/null +++ b/stockquote/settings.gradle @@ -0,0 +1,2 @@ + +include "server", "client:jax-ws", "client:spring-ws"