Added weather client

This commit is contained in:
Arjen Poutsma
2014-02-18 12:53:28 +01:00
parent c2dbcf7ae1
commit e6de854a4e
7 changed files with 326 additions and 0 deletions

31
.gitignore vendored Normal file
View File

@@ -0,0 +1,31 @@
# Operating System Files
*.DS_Store
Thumbs.db
# Build Files #
bin
target
build/
.gradle
# Eclipse Project Files #
.classpath
.project
.settings
# IntelliJ IDEA Files #
*.iml
*.ipr
*.iws
*.idea
# Test files
*-uploaded
dependency-reduced-pom.xml
README.html

16
LICENSE.code.txt Normal file
View File

@@ -0,0 +1,16 @@
All code in this repository is:
=======================================================================
Copyright (c) 2013 GoPivotal, Inc. All Rights Reserved
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.

1
LICENSE.writing.txt Normal file
View File

@@ -0,0 +1 @@
Except where otherwise noted, this work is licensed under http://creativecommons.org/licenses/by-nd/3.0/

63
complete/build.gradle Normal file
View File

@@ -0,0 +1,63 @@
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: "hello.wsdl") {
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))
jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7"
}
task runClient(dependsOn: 'classes', type:JavaExec) {
main = "hello.WeatherClient"
classpath = sourceSets.main.runtimeClasspath
}

81
complete/pom.xml Normal file
View File

@@ -0,0 +1,81 @@
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-consuming-web-service</artifactId>
<version>0.1.0</version>
<properties>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>hello.wsdl</generatePackage>
<forceRegenerate>true</forceRegenerate>
<schemas>
<schema>
<url>http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl</url>
</schema>
</schemas>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>hello.WeatherClient</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone</url>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,91 @@
/*
* 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 hello;
import java.text.SimpleDateFormat;
import hello.wsdl.Forecast;
import hello.wsdl.ForecastReturn;
import hello.wsdl.GetCityForecastByZIP;
import hello.wsdl.GetCityForecastByZIPResponse;
import hello.wsdl.Temp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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 AnnotationConfigApplicationContext(WeatherConfiguration.class);
WeatherClient client = context.getBean(WeatherClient.class);
String zipCode = "94304";
if (args.length > 0) {
zipCode = args[0];
}
GetCityForecastByZIPResponse response = client.getCityForecastByZip(zipCode);
client.printResponse(response);
}
}

View File

@@ -0,0 +1,43 @@
/*
* 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 hello;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class WeatherConfiguration {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("hello.wsdl");
return marshaller;
}
@Bean
public WeatherClient weatherClient() {
WeatherClient client = new WeatherClient();
client.setDefaultUri("http://wsf.cdyne.com/WeatherWS/Weather.asmx");
Jaxb2Marshaller marshaller = marshaller();
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}