diff --git a/README.md b/README.md index 79ad6b5..95c2690 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ additional instructions. - [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 +- [weather](./weather) - shows how to connect to a public SOAP service ## Running the Server diff --git a/weather/README.md b/weather/README.md new file mode 100644 index 0000000..de3f977 --- /dev/null +++ b/weather/README.md @@ -0,0 +1,19 @@ +# Weather Forecast Sample + +This sample shows how to use Spring Web Services to connect to the [CDYNE Weather Service]. +It uses JAXB to generate classes from the service's WSDL, and the `WebServiceTemplate` to +send the request to and receive the response from the service. + +You can run the client by using the following command from within this directory: + + ```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 +[CDYNE Weather Service]: http://wiki.cdyne.com/index.php/CDYNE_Weather \ No newline at end of file diff --git a/weather/build.gradle b/weather/build.gradle new file mode 100644 index 0000000..2b51fe2 --- /dev/null +++ b/weather/build.gradle @@ -0,0 +1,65 @@ +configurations { + jaxb +} + +ext.springVersion = '3.2.4.RELEASE' +ext.springWsVersion = '2.1.4.RELEASE' + +apply plugin: 'java' +apply plugin: 'eclipse' +apply plugin: 'idea' + +repositories { + maven { url 'http://repo.spring.io/libs-release' } +} + +task genJaxb { + ext.sourcesDir = "${buildDir}/generated-sources/jaxb" + ext.classesDir = "${buildDir}/classes/jaxb" + ext.schema = "http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl" + + 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.weather") { + 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") + } + } + } + } +} + +dependencies { + compile("org.springframework.ws:spring-ws-core:$springWsVersion") + compile(files(genJaxb.classesDir).builtBy(genJaxb)) + + runtime("log4j:log4j:1.2.16") + + jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7" +} + +task runClient(dependsOn: 'classes', type:JavaExec) { + main = "org.springframework.ws.samples.weather.WeatherClient" + classpath = sourceSets.main.runtimeClasspath +} diff --git a/weather/src/main/java/org/springframework/ws/samples/weather/WeatherClient.java b/weather/src/main/java/org/springframework/ws/samples/weather/WeatherClient.java new file mode 100644 index 0000000..f4ccc19 --- /dev/null +++ b/weather/src/main/java/org/springframework/ws/samples/weather/WeatherClient.java @@ -0,0 +1,86 @@ +/* + * Copyright 2014 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.weather; + +import java.text.SimpleDateFormat; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.ws.client.core.support.WebServiceGatewaySupport; +import org.springframework.ws.soap.client.core.SoapActionCallback; + +public class WeatherClient extends WebServiceGatewaySupport { + + public GetCityForecastByZIPResponse getCityForecastByZip(String zipCode) { + GetCityForecastByZIP request = new GetCityForecastByZIP(); + request.setZIP(zipCode); + + System.out.println(); + System.out.println("Requesting forecast for " + zipCode); + + GetCityForecastByZIPResponse response = + (GetCityForecastByZIPResponse) getWebServiceTemplate() + .marshalSendAndReceive(request, new SoapActionCallback( + "http://ws.cdyne.com/WeatherWS/GetCityForecastByZIP")); + + return response; + } + + public void printResponse(GetCityForecastByZIPResponse response) { + ForecastReturn forecastReturn = response.getGetCityForecastByZIPResult(); + + + if (forecastReturn.isSuccess()) { + System.out.println(); + System.out.println("Forecast for " + forecastReturn.getCity() + ", " + + forecastReturn.getState()); + + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + for (Forecast forecast : forecastReturn.getForecastResult().getForecast()) { + System.out.print(format + .format(forecast.getDate().toGregorianCalendar().getTime())); + System.out.print(" "); + System.out.print(forecast.getDesciption()); + System.out.print(" "); + Temp temperature = forecast.getTemperatures(); + System.out.print(temperature.getMorningLow() + "\u00b0-" + + temperature.getDaytimeHigh() + "\u00b0 "); + System.out.println(); + } + } else { + System.out.println("No forecast received"); + } + + } + + public static void main(String[] args) { + ApplicationContext context = + new ClassPathXmlApplicationContext("applicationContext.xml", + WeatherClient.class); + WeatherClient client = context.getBean("weatherClient", WeatherClient.class); + + String zipCode = "94304"; + if (args.length > 0) { + zipCode = args[0]; + } + + GetCityForecastByZIPResponse response = client.getCityForecastByZip(zipCode); + client.printResponse(response); + + } + +} diff --git a/weather/src/main/resources/log4j.properties b/weather/src/main/resources/log4j.properties new file mode 100644 index 0000000..0f73a75 --- /dev/null +++ b/weather/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/weather/src/main/resources/org/springframework/ws/samples/weather/applicationContext.xml b/weather/src/main/resources/org/springframework/ws/samples/weather/applicationContext.xml new file mode 100644 index 0000000..b733295 --- /dev/null +++ b/weather/src/main/resources/org/springframework/ws/samples/weather/applicationContext.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + +