2002 Prentice Hall. All rights reserved.
Chapter 29: Introduction to Web Services and SOAP
Outline
29.1 Introduction
29.2 Simple Object Access Protocol (SOAP) 29.3 SOAP Weather Service
2002 Prentice Hall. All rights reserved.
29.1 Introduction
• SOAP
– Simple Object Access Protocol – Promote interoperability
• Communication among different software systems.
– Provides XML communication in many Web services
• Web Services
– Exposes public interfaces usable by Web applications
2002 Prentice Hall. All rights reserved.
29.2 Simple Object Access Protocol (SOAP)
• SOAP
– HTTP-XML-based protocol
– Enables application to communicate over Internet – Uses XML documents called messages
• SOAP message contains an envelope
– Describes message’s content and intended recipient
– Ability to make a Remote Procedure Call (RPC)
• Request to another machine to run a task
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.1 Class SimpleService.
Line 4 Lines 6-12
1 // Fig. 29.1: SimpleService.java
2 // Implementation for the requested method on the server 3
4 public class SimpleService { 5
6 public String getWelcome( String message ) throws Exception 7 {
8 String text =
9 "Welcome to SOAP!\nHere is your message: " + message;
10
11 return text; // response 12 }
13 }
Class SimpleService resides on a server
Method returns a String when invoked; this method is made
available to remote clients
2002 Prentice Hall. All rights reserved.
29.2 Simple Object Access Protocol (SOAP) (cont.)
Fig. 29.2 SOAP package administration tool.
2002 Prentice Hall. All rights reserved.
29.2 Simple Object Access Protocol (SOAP) (cont.)
Fig. 29.3 Description of deployed service.
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.4 Client making a SOAP request (part 1).
Lines 10-11 Line 13 Line 17 Lines 27-28 Lines 31-33
1 // Fig. 29.4 : GetMessage.java 2 // Program that makes a SOAP RPC 3
4 // import Java packages 5 import java.io.*;
6 import java.net.*;
7 import java.util.*;
8
9 // import third-party packages 10 import org.apache.soap.*;
11 import org.apache.soap.rpc.*;
12
13 public class GetMessage { 14
15 // main method
16 public static void main( String args[] ) {
17 String encodingStyleURI = Constants.NS_URI_SOAP_ENC;
18 String message;
19
20 if ( args.length != 0 ) 21 message = args[ 0 ];
22 else
23 message = "Thanks!";
24
25 // attempt SOAP remote procedure call 26 try {
27 URL url = new URL(
28 "http://localhost:8080/soap/servlet/rpcrouter" );
29
30 // build call
31 Call remoteMethod = new Call();
32 remoteMethod.setTargetObjectURI(
33 "urn:xml-simple-message" );
34
Client for the RPC
SOAP-implementation APIs
Specify encoding style used for SOAP message
Specify URL of server to which the client sends
the SOAP message
Instantiate Call object and set its URI
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.4 Client making a SOAP request (part 2).
Lines 40-44 Line 48 Lines 51-57 Lines 60-63
35 // set name of remote method to be invoked 36 remoteMethod.setMethodName( "getWelcome" );
37 remoteMethod.setEncodingStyleURI( encodingStyleURI );
38
39 // set parameters for remote method 40 Vector parameters = new Vector();
41
42 parameters.addElement( new Parameter( "message", 43 String.class, message, null ) );
44 remoteMethod.setParams( parameters );
45 Response response;
46
47 // invoke remote method
48 response = remoteMethod.invoke( url, "" );
49
50 // get response
51 if ( response.generatedFault() ) { 52 Fault fault = response.getFault();
53
54 System.err.println( "CALL FAILED:\nFault Code = "
55 + fault.getFaultCode()+ "\nFault String = "
56 + fault.getFaultString() );
57 } 58
59 else {
60 Parameter result = response.getReturnValue();
61
62 // display result of call
63 System.out.println( result.getValue() );
64 } 65 } 66
Build parameters used to invoke method getWelcome of class
SimpleService Invoke method getWelcome and store returned value in Response object
Display message if error occurred
Display remote method’s returned value
if no problems occurred
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.4 Client making a SOAP request (part 3).
67 // catch malformed URL exception
68 catch ( MalformedURLException malformedURLException ) { 69 malformedURLException.printStackTrace();
70 System.exit( 1 );
71 } 72
73 // catch SOAPException
74 catch ( SOAPException soapException ) { 75 System.err.println( "Error message: " + 76 soapException.getMessage() );
77 System.exit( 1 );
78 } 79 } 80 }
java GetMessage Welcome to SOAP!
Here is your message: Thanks!
java GetMessage "my message”
Welcome to SOAP!
Here is your message: my message
2002 Prentice Hall. All rights reserved.
29.3 SOAP Weather Service
• SOAP Weather Service
– Web service
– Modification of RMI-based Weather Service (Chapter 13) – Use SOAP RPC instead of Java RMI
• Send information from server to client
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.5 SOAP implementation of class Weather- Service (part 1).
Line 11
1 // Fig. 29.5: WeatherService.java
2 // WeatherService provides a method to retrieve weather 3 // information from the National Weather Service.
4 package com.deitel.advjhtp1.soap.weather;
5
6 // Java core packages 7 import java.io.*;
8 import java.net.URL;
9 import java.util.*;
10
11 public class WeatherService {
12
13 private Vector weatherInformation; // WeatherBean objects 14
15 // get weather information from NWS 16 private void updateWeatherConditions() 17 {
18 try {
19 System.out.println( "Update weather information..." );
20
21 // National Weather Service Travelers Forecast page 22 URL url = new URL(
23 "http://iwin.nws.noaa.gov/iwin/us/traveler.html" );
24
25 // set up text input stream to read Web page contents 26 BufferedReader in = new BufferedReader(
27 new InputStreamReader( url.openStream() ) );
28
29 // helps determine starting point of data on Web page 30 String separator = "TAV12";
31
32 // locate separator string in Web page
33 while ( !in.readLine().startsWith( separator ) ) 34 ; // do nothing
35
Class WeatherService provides method getWeatherInformation that class WeatherServiceClient
calls through SOAP RPC
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.5 SOAP implementation of class Weather- Service (part 2).
Lines 66-71
36 // strings representing headers on Travelers Forecast 37 // Web page for daytime and nighttime weather
38 String dayHeader =
39 "CITY WEA HI/LO WEA HI/LO";
40 String nightHeader =
41 "CITY WEA LO/HI WEA LO/HI";
42
43 String inputLine = "";
44
45 // locate header that begins weather information 46 do {
47 inputLine = in.readLine();
48 } while ( !inputLine.equals( dayHeader ) &&
49 !inputLine.equals( nightHeader ) );
50
51 weatherInformation = new Vector(); // create Vector 52
53 // create WeatherBeans containing weather data and 54 // store in weatherInformation Vector
55 inputLine = in.readLine(); // get first city's data 56
57 // The portion of inputLine containing relevant data 58 // is 28 characters long. If the line length is not at 59 // least 28 characters long, then done processing data.
60 while ( inputLine.length() > 28 ) { 61
62 // Prepare strings for WeatherBean for each city.
63 // First 16 characters are city name. Next, six 64 // characters are weather description. Next six 65 // characters are HI/LO or LO/HI temperature.
66 weatherInformation.add(
67 inputLine.substring( 0, 16 ) );
68 weatherInformation.add(
69 inputLine.substring( 16, 22 ) );
Add Strings parsed from Traveler’s Forecast Web
page to Vector
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.5 SOAP implementation of class Weather- Service (part 3).
Lines 95-100
70 weatherInformation.add(
71 inputLine.substring( 23, 29 ) );
72
73 inputLine = in.readLine(); // get next city's data 74 }
75
76 in.close(); // close connection to NWS Web server 77
78 System.out.println( "Weather information updated." );
79 } 80
81 // process failure to connect to National Weather Service 82 catch( java.net.ConnectException connectException ) { 83 connectException.printStackTrace();
84 System.exit( 1 );
85 } 86
87 // process other exceptions 88 catch( Exception exception ) { 89 exception.printStackTrace();
90 System.exit( 1 );
91 } 92 } 93
94 // implementation for WeatherService interface method 95 public Vector getWeatherInformation()
96 {
97 updateWeatherConditions();
98
99 return weatherInformation;
100 } 101 }
Method getWeatherInformation is made available to remote clients
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.6 SOAP implementation of class Weather- ServiceClient (part 1).
Lines 31-32
1 // Fig. 29.6: WeatherServiceClient.java
2 // WeatherServiceClient accesses the WeatherService remote 3 // object via SOAP to retrieve weather information.
4 package com.deitel.advjhtp1.soap.weather;
5
6 // Java core packages 7 import java.util.*;
8 import java.net.*;
9
10 // Java extension packages 11 import javax.swing.*;
12
13 // third-party packages 14 import org.apache.soap.*;
15 import org.apache.soap.rpc.*;
16
17 // Deitel packages
18 import com.deitel.advjhtp1.rmi.weather.*;
19
20 public class WeatherServiceClient extends JFrame { 21
22 // WeatherServiceClient constructor
23 public WeatherServiceClient( String server ) 24 {
25 super( "SOAP WeatherService Client" );
26
27 // connect to server and get weather information 28 try {
29
30 // URL of remote SOAP object
31 URL url = new URL( "http://" + server + ":8080/soap/"
32 + "servlet/rpcrouter" );
33
Set SOAP service’s URL
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.6 SOAP implementation of class Weather- ServiceClient (part 2).
Lines 35-37 Line 46 Lines 49-55
Lines 58-65
34 // build SOAP RPC call
35 Call remoteMethod = new Call();
36 remoteMethod.setTargetObjectURI(
37 "urn:xml-weather-service" );
38
39 // set name of remote method to be invoked 40 remoteMethod.setMethodName(
41 "getWeatherInformation" );
42 remoteMethod.setEncodingStyleURI(
43 Constants.NS_URI_SOAP_ENC );
44
45 // invoke remote method
46 Response response = remoteMethod.invoke( url, "" );
47
48 // get response
49 if ( response.generatedFault() ) { 50 Fault fault = response.getFault();
51
52 System.err.println( "CALL FAILED:\nFault Code = "
53 + fault.getFaultCode() + "\nFault String = "
54 + fault.getFaultString() );
55 } 56
57 else {
58 Parameter result = response.getReturnValue();
59
60 Vector weatherStrings = ( Vector ) 61 result.getValue();
62
63 // get weather information from result object 64 List weatherInformation = createBeans(
65 weatherStrings );
66
Instantiate Call object and set its URI
Invoke remote method getWeatherInfomation
and store returned value in Response object
Display message if error occurred
Display remote method’s returned value as List if no
problems occurred
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.6 SOAP implementation of class Weather- ServiceClient (part 3).
Line 95
67 // create WeatherListModel for weather information 68 ListModel weatherListModel =
69 new WeatherListModel( weatherInformation );
70
71 // create JList, set its CellRenderer and add to 72 // layout
73 JList weatherJList = new JList( weatherListModel );
74 weatherJList.setCellRenderer( new 75 WeatherCellRenderer() );
76 getContentPane().add( new
77 JScrollPane( weatherJList ) );
78 } 79
80 } // end try 81
82 // handle bad URL
83 catch ( MalformedURLException malformedURLException ) { 84 malformedURLException.printStackTrace();
85 } 86
87 // handle SOAP exception
88 catch ( SOAPException soapException ) { 89 soapException.printStackTrace();
90 } 91
92 } // end WeatherServiceClient constructor 93
94 // create List of WeatherBeans from Vector of Strings 95 public List createBeans( Vector weatherStrings )
96 {
97 List list = new ArrayList(); Covert Vector of Strings to List of WeatherBeans
2002 Prentice Hall.
All rights reserved.
Outline
Fig. 29.6 SOAP implementation of class Weather- ServiceClient (part 4).
98 for ( int i = 0; ( weatherStrings.size() - 1 ) > i;
99 i += 3 ) {
100 list.add( new WeatherBean(
101 ( String ) weatherStrings.elementAt( i ), 102 ( String ) weatherStrings.elementAt( i + 1 ), 103 ( String ) weatherStrings.elementAt( i + 2 ) ) );
104 } 105
106 return list;
107 } 108
109 // execute WeatherServiceClient
110 public static void main( String args[] ) 111 {
112 WeatherServiceClient client = null;
113
114 // if no server IP address or host name specified, 115 // use "localhost"; otherwise use specified host 116 if ( args.length == 0 )
117 client = new WeatherServiceClient( "localhost" );
118 else
119 client = new WeatherServiceClient( args[ 0 ] );
120
121 // configure and display application window
122 client.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
123 client.pack();
124 client.setResizable( false );
125 client.setVisible( true );
126 } 127 }
2002 Prentice Hall. All rights reserved.
29.3 SOAP Weather Service (cont.)
Fig. 29.7 Apache SOAP Admin page.
2002 Prentice Hall. All rights reserved.
29.3 SOAP Weather Service (cont.)
Fig. 29.8 Apache SOAP Service Deployment Descriptor Template.
2002 Prentice Hall. All rights reserved.
29.3 SOAP Weather Service (cont.)
Fig. 29.9 SOAP WeatherService Client.