You are on page 1of 20

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 messages 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.

1 2 3 4 5 6 7 8 9 10 11 12 13

// Fig. 29.1: SimpleService.java // Implementation for the requested method on the server public class SimpleService { public String getWelcome( String message ) throws Exception { String text = "Welcome to SOAP!\nHere is your message: " + message;

Outline
Class SimpleService Fig. 29.1 Class resides on a server SimpleService.
Line 4

return text;
} }

// response

Lines 6-12 when Method returns a String 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

// Fig. 29.4 : GetMessage.java // Program that makes a SOAP RPC // import Java packages import java.io.*; import java.net.*; import java.util.*; // import third-party packages import org.apache.soap.*; import org.apache.soap.rpc.*; public class GetMessage { // main method public static void main( String args[] ) { String encodingStyleURI = Constants.NS_URI_SOAP_ENC; String message; if ( args.length != 0 ) message = args[ 0 ]; else message = "Thanks!"; // attempt SOAP remote procedure call try { URL url = new URL( "http://localhost:8080/soap/servlet/rpcrouter" ); // build call Call remoteMethod = new Call(); remoteMethod.setTargetObjectURI( "urn:xml-simple-message" );

Outline
Fig. 29.4 Client making a SOAP SOAP-implementation APIs request (part 1).

Lines 10-11 Client for the RPC Line 13


Line 17 Specify encoding style used for Lines 27-28 SOAP message Lines 31-33 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.

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

// set name of remote method to be invoked remoteMethod.setMethodName( "getWelcome" ); remoteMethod.setEncodingStyleURI( encodingStyleURI ); // set parameters for remote method Vector parameters = new Vector(); parameters.addElement( new Parameter( "message", String.class, message, null ) ); remoteMethod.setParams( parameters ); Response response; // invoke remote method response = remoteMethod.invoke( url, "" ); // get response if ( response.generatedFault() ) { Fault fault = response.getFault(); System.err.println( "CALL FAILED:\nFault Code = " + fault.getFaultCode()+ "\nFault String = " + fault.getFaultString() ); } else { Parameter result = response.getReturnValue(); // display result of call System.out.println( result.getValue() ); } }

Outline
Fig. 29.4 Client making a SOAP request (part 2). Build parameters used to invoke method Lines 40-44 getWelcome of class SimpleService Line 48 Invoke method Lines 51-57 getWelcome and store returned value Lines 60-63 in Response object Display message if error occurred

Display remote methods returned value if no problems occurred

2002 Prentice Hall.


All rights reserved.

67 68 69 70 71 72 73 74 75 76 77 78 79 80

// catch malformed URL exception catch ( MalformedURLException malformedURLException ) { malformedURLException.printStackTrace(); System.exit( 1 ); } // catch SOAPException catch ( SOAPException soapException ) { System.err.println( "Error message: " + soapException.getMessage() ); System.exit( 1 ); } } }

Outline
Fig. 29.4 Client making a SOAP request (part 3).

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

// Fig. 29.5: WeatherService.java // WeatherService provides a method to retrieve weather // information from the National Weather Service. package com.deitel.advjhtp1.soap.weather; // Java core packages import java.io.*; import java.net.URL; import java.util.*;

Outline

public class WeatherService {


private Vector weatherInformation;

Fig. 29.5 SOAP implementation Class WeatherService provides of class Weathermethod getWeatherInformation Service (part 1). that class WeatherServiceClient calls through SOAP RPC Line 11

// WeatherBean objects

// get weather information from NWS private void updateWeatherConditions() { try { System.out.println( "Update weather information..." ); // National Weather Service Travelers Forecast page URL url = new URL( "http://iwin.nws.noaa.gov/iwin/us/traveler.html" ); // set up text input stream to read Web page contents BufferedReader in = new BufferedReader( new InputStreamReader( url.openStream() ) ); // helps determine starting point of data on Web page String separator = "TAV12"; // locate separator string in Web page while ( !in.readLine().startsWith( separator ) ) ; // do nothing

2002 Prentice Hall.


All rights reserved.

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

// strings representing headers on Travelers Forecast // Web page for daytime and nighttime weather String dayHeader = "CITY WEA HI/LO WEA HI/LO"; String nightHeader = "CITY WEA LO/HI WEA LO/HI"; String inputLine = ""; // locate header that begins weather information do { inputLine = in.readLine(); } while ( !inputLine.equals( dayHeader ) && !inputLine.equals( nightHeader ) ); weatherInformation = new Vector(); // create Vector // create WeatherBeans containing weather data and // store in weatherInformation Vector inputLine = in.readLine(); // get first city's data // The portion of inputLine containing relevant data // is 28 characters long. If the line length is not at // least 28 characters long, then done processing data. while ( inputLine.length() > 28 ) {

Outline
Fig. 29.5 SOAP implementation of class WeatherService (part 2).

Lines 66-71

// Prepare strings for WeatherBean for each city. // First 16 characters are city name. Next, six // characters are weather description. Next six // characters are HI/LO or LO/HI temperature. weatherInformation.add( inputLine.substring( 0, 16 ) ); weatherInformation.add( inputLine.substring( 16, 22 ) );

Add Strings parsed from Travelers Forecast Web page to Vector


2002 Prentice Hall.
All rights reserved.

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

weatherInformation.add( inputLine.substring( 23, 29 ) ); inputLine = in.readLine(); } in.close(); // close connection to NWS Web server // get next city's data

Outline
Fig. 29.5 SOAP implementation of class WeatherService (part 3).

System.out.println( "Weather information updated." ); } // process failure to connect to National Weather Service catch( java.net.ConnectException connectException ) { connectException.printStackTrace(); System.exit( 1 ); } // process other exceptions catch( Exception exception ) { exception.printStackTrace(); System.exit( 1 ); } } // implementation for WeatherService interface method public Vector getWeatherInformation() { updateWeatherConditions(); Method return weatherInformation; } }

Lines 95-100

getWeatherInformation is made available to remote clients

2002 Prentice Hall.


All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

// Fig. 29.6: WeatherServiceClient.java // WeatherServiceClient accesses the WeatherService remote // object via SOAP to retrieve weather information. package com.deitel.advjhtp1.soap.weather; // Java core packages import java.util.*; import java.net.*; // Java extension packages import javax.swing.*; // third-party packages import org.apache.soap.*; import org.apache.soap.rpc.*; // Deitel packages import com.deitel.advjhtp1.rmi.weather.*; public class WeatherServiceClient extends JFrame { // WeatherServiceClient constructor public WeatherServiceClient( String server ) { super( "SOAP WeatherService Client" );

Outline
Fig. 29.6 SOAP implementation of class WeatherServiceClient (part 1). Lines 31-32

// connect to server and get weather information try {


// URL of remote SOAP object URL url = new URL( "http://" + server + ":8080/soap/" + "servlet/rpcrouter" );

Set SOAP services URL

2002 Prentice Hall.


All rights reserved.

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

// build SOAP RPC call Call remoteMethod = new Call(); remoteMethod.setTargetObjectURI( "urn:xml-weather-service" ); // set name of remote method to be invoked remoteMethod.setMethodName( "getWeatherInformation" ); remoteMethod.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC ); // invoke remote method Response response = remoteMethod.invoke( url, "" ); // get response if ( response.generatedFault() ) { Fault fault = response.getFault();

Outline
Instantiate Call object and set its URI Fig. 29.6 SOAP implementation of Invoke remote method class WeathergetWeatherInfomation ServiceClient and store returned (part 2). value in Response object Lines 35-37
Line 46 Lines 49-55

System.err.println( "CALL FAILED:\nFault Code = " + fault.getFaultCode() + "\nFault String = " + fault.getFaultString() );
} else { Parameter result = response.getReturnValue();

Display message if Lines 58-65 error occurred

Vector weatherStrings = ( Vector ) result.getValue();


// get weather information from result object List weatherInformation = createBeans( weatherStrings );

Display remote methods returned value as List if no problems occurred


2002 Prentice Hall.
All rights reserved.

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

// create WeatherListModel for weather information ListModel weatherListModel = new WeatherListModel( weatherInformation ); // create JList, set its CellRenderer and add to // layout JList weatherJList = new JList( weatherListModel ); weatherJList.setCellRenderer( new WeatherCellRenderer() ); getContentPane().add( new JScrollPane( weatherJList ) ); } } // end try // handle bad URL catch ( MalformedURLException malformedURLException ) { malformedURLException.printStackTrace(); } // handle SOAP exception catch ( SOAPException soapException ) { soapException.printStackTrace(); } } // end WeatherServiceClient constructor // create List of WeatherBeans from Vector of Strings public List createBeans( Vector weatherStrings ) { List list = new ArrayList();

Outline
Fig. 29.6 SOAP implementation of class WeatherServiceClient (part 3). Line 95

Covert Vector of Strings to List of WeatherBeans


2002 Prentice Hall.
All rights reserved.

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

for ( int i = 0; i += 3 ) { list.add( new ( String ) ( String ) ( String ) } return list; }

( weatherStrings.size() - 1 ) > i;

Outline
Fig. 29.6 SOAP implementation of class WeatherServiceClient (part 4).

WeatherBean( weatherStrings.elementAt( i ), weatherStrings.elementAt( i + 1 ), weatherStrings.elementAt( i + 2 ) ) );

// execute WeatherServiceClient public static void main( String args[] ) { WeatherServiceClient client = null; // if no server IP address or host name specified, // use "localhost"; otherwise use specified host if ( args.length == 0 ) client = new WeatherServiceClient( "localhost" ); else client = new WeatherServiceClient( args[ 0 ] ); // configure and display application window client.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); client.pack(); client.setResizable( false ); client.setVisible( true ); } }

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.

2002 Prentice Hall. All rights reserved.

You might also like