advjhtp1_29 soap.ppt

download advjhtp1_29  soap.ppt

of 20

Transcript of advjhtp1_29 soap.ppt

  • 7/27/2019 advjhtp1_29 soap.ppt

    1/20

    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

  • 7/27/2019 advjhtp1_29 soap.ppt

    2/20

    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

  • 7/27/2019 advjhtp1_29 soap.ppt

    3/20

    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 aRemote Procedure Call(RPC)

    Request to another machine to run a task

  • 7/27/2019 advjhtp1_29 soap.ppt

    4/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.1 ClassSimpleService.

    Line 4

    Lines 6-12

    1 // Fig. 29.1: SimpleService.java2 // Implementation for the requested method on the server34 public class SimpleService {56 public String getWelcome( String message ) throws Exception7 {

    8 String text =9 "Welcome to SOAP!\nHere is your message: " + message;1011 return text; // response12 }13 }

    Class SimpleService

    resides on a server

    Method returns a String when

    invoked; this method is made

    available to remote clients

  • 7/27/2019 advjhtp1_29 soap.ppt

    5/20

    2002 Prentice Hall. All rights reserved.

    29.2 Simple Object Access Protocol

    (SOAP) (cont.)

    Fig. 29.2 SOAP package administration tool.

  • 7/27/2019 advjhtp1_29 soap.ppt

    6/20

    2002 Prentice Hall. All rights reserved.

    29.2 Simple Object Access Protocol

    (SOAP) (cont.)

    Fig. 29.3 Description of deployed service.

  • 7/27/2019 advjhtp1_29 soap.ppt

    7/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.4 Clientmaking a SOAP

    request (part 1).

    Lines 10-11

    Line 13

    Line 17

    Lines 27-28

    Lines 31-33

    1 // Fig. 29.4 : GetMessage.java2 // Program that makes a SOAP RPC34 // import Java packages5 import java.io.*;6 import java.net.*;7 import java.util.*;

    89 // import third-party packages10 import org.apache.soap.*;11 import org.apache.soap.rpc.*;1213 public class GetMessage {1415 // main method16 public static voidmain( String args[] ) {17 String encodingStyleURI = Constants.NS_URI_SOAP_ENC;18 String message;1920 if ( args.length != 0 )21 message = args[ 0 ];22 else23 message = "Thanks!";24

    25 // attempt SOAP remote procedure call26 try {27 URL url = new URL(28 "http://localhost:8080/soap/servlet/rpcrouter" );2930 // build call31 Call remoteMethod = new Call();32 remoteMethod.setTargetObjectURI(33 "urn:xml-simple-message" );

    34

    Client for the RPC

    SOAP-implementation APIs

    Specify encodingstyle used for

    SOAP message

    Specify URL of server

    to which the client sends

    the SOAP message

    Instantiate Call

    object and set its URI

  • 7/27/2019 advjhtp1_29 soap.ppt

    8/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.4 Clientmaking a SOAPrequest (part 2).

    Lines 40-44

    Line 48

    Lines 51-57

    Lines 60-63

    35 // set name of remote method to be invoked36 remoteMethod.setMethodName( "getWelcome" );37 remoteMethod.setEncodingStyleURI( encodingStyleURI );3839 // set parameters for remote method40 Vector parameters = new Vector();41

    42 parameters.addElement( new Parameter( "message",43 String.class, message, null ) );44 remoteMethod.setParams( parameters );45 Response response;4647 // invoke remote method48 response = remoteMethod.invoke( url, "" );4950 // get response51 if ( response.generatedFault() ) {52 Fault fault = response.getFault();5354 System.err.println( "CALL FAILED:\nFault Code = "55 + fault.getFaultCode()+ "\nFault String = "56 + fault.getFaultString() );57 }58

    59 else {60 Parameter result = response.getReturnValue();6162 // display result of call63 System.out.println( result.getValue() );64 }65 }66

    Build parameters usedto invoke method

    getWelcome of classSimpleService

    Invoke methodgetWelcome and

    store returned valuein Response object

    Display message iferror occurred

    Display remote

    methods returned value

    if no problems occurred

  • 7/27/2019 advjhtp1_29 soap.ppt

    9/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.4 Clientmaking a SOAPrequest (part 3).

    67 // catch malformed URL exception68 catch ( MalformedURLException malformedURLException ) {69 malformedURLException.printStackTrace();70 System.exit( 1 );71 }7273 // catch SOAPException

    74 catch ( SOAPException soapException ) {75 System.err.println( "Error message: " +76 soapException.getMessage() );77 System.exit( 1 );78 }79 }80 }

    java GetMessageWelcome to SOAP!Here is your message: Thanks!

    java GetMessage "my messageWelcome to SOAP!

    Here is your message: my message

  • 7/27/2019 advjhtp1_29 soap.ppt

    10/20 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

  • 7/27/2019 advjhtp1_29 soap.ppt

    11/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.5 SOAPimplementation ofclassWeather-Service (part 1).

    Line 11

    1 // Fig. 29.5: WeatherService.java2 // WeatherService provides a method to retrieve weather3 // information from the National Weather Service.4 package com.deitel.advjhtp1.soap.weather;56 // Java core packages7 import java.io.*;

    8 import java.net.URL;9 import java.util.*;1011 publicclass WeatherService {1213 private Vector weatherInformation; // WeatherBean objects1415 // get weather information from NWS16 privatevoidupdateWeatherConditions()17 {18 try {19 System.out.println( "Update weather information..." );2021 // National Weather Service Travelers Forecast page22 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 contents26 BufferedReader in = new BufferedReader(27 new InputStreamReader( url.openStream() ) );2829 // helps determine starting point of data on Web page30 String separator = "TAV12";3132 // locate separator string in Web page33 while ( !in.readLine().startsWith( separator ) )

    34 ; // do nothing35

    ClassWeatherService provides

    method getWeatherInformationthat classWeatherServiceClient

    calls through SOAP RPC

  • 7/27/2019 advjhtp1_29 soap.ppt

    12/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.5 SOAPimplementation ofclassWeather-Service (part 2).

    Lines 66-71

    36 // strings representing headers on Travelers Forecast37 // Web page for daytime and nighttime weather38 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 = "";4445 // locate header that begins weather information46 do {47 inputLine = in.readLine();48 }while ( !inputLine.equals( dayHeader ) &&49 !inputLine.equals( nightHeader ) );5051 weatherInformation = new Vector(); // create Vector5253 // create WeatherBeans containing weather data and54 // store in weatherInformation Vector55 inputLine = in.readLine(); // get first city's data5657 // The portion of inputLine containing relevant data58 // is 28 characters long. If the line length is not at59 // least 28 characters long, then done processing data.

    60 while ( inputLine.length() >28 ) {6162 // Prepare strings for WeatherBean for each city.63 // First 16 characters are city name. Next, six64 // characters are weather description. Next six65 // 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

    Travelers ForecastWeb

    page toVector

  • 7/27/2019 advjhtp1_29 soap.ppt

    13/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.5 SOAPimplementation ofclassWeather-Service (part 3).

    Lines 95-100

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

    7778 System.out.println( "Weather information updated." );79 }8081 // process failure to connect to National Weather Service82 catch( java.net.ConnectException connectException ) {83 connectException.printStackTrace();84 System.exit( 1 );85 }8687 // process other exceptions88 catch( Exception exception ) {89 exception.printStackTrace();90 System.exit( 1 );91 }92 }93

    94 // implementation for WeatherService interface method95 public Vector getWeatherInformation()96 {97 updateWeatherConditions();9899 return weatherInformation;100 }101 }

    Method getWeatherInformation

    is made available to remote clients

  • 7/27/2019 advjhtp1_29 soap.ppt

    14/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.6 SOAPimplementation ofclassWeather-ServiceClient

    (part 1).

    Lines 31-32

    1 // Fig. 29.6: WeatherServiceClient.java2 // WeatherServiceClient accesses the WeatherService remote3 // object via SOAP to retrieve weather information.4 package com.deitel.advjhtp1.soap.weather;56 // Java core packages7 import java.util.*;

    8 import java.net.*;910 // Java extension packages11 import javax.swing.*;1213 // third-party packages14 import org.apache.soap.*;15 import org.apache.soap.rpc.*;1617 // Deitel packages18 import com.deitel.advjhtp1.rmi.weather.*;1920 publicclass WeatherServiceClient extends JFrame {2122 // WeatherServiceClient constructor23 public WeatherServiceClient( String server )24 {

    25 super( "SOAP WeatherService Client" );2627 // connect to server and get weather information28 try {2930 // URL of remote SOAP object31 URL url = new URL( "http://" + server + ":8080/soap/"32 + "servlet/rpcrouter" );33

    Set SOAP services URL

  • 7/27/2019 advjhtp1_29 soap.ppt

    15/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.6 SOAPimplementation ofclassWeather-ServiceClient

    (part 2).

    Lines 35-37

    Line 46

    Lines 49-55

    Lines 58-65

    34 // build SOAP RPC call35 Call remoteMethod = new Call();36 remoteMethod.setTargetObjectURI(37 "urn:xml-weather-service" );3839 // set name of remote method to be invoked40 remoteMethod.setMethodName(

    41 "getWeatherInformation" );42 remoteMethod.setEncodingStyleURI(43 Constants.NS_URI_SOAP_ENC );4445 // invoke remote method46 Response response = remoteMethod.invoke( url, "" );4748 // get response49 if ( response.generatedFault() ) {

    50 Fault fault = response.getFault();5152 System.err.println( "CALL FAILED:\nFault Code = "53 + fault.getFaultCode() + "\nFault String = "54 + fault.getFaultString() );55 }5657 else {

    58 Parameter result = response.getReturnValue();5960 Vector weatherStrings = ( Vector )61 result.getValue();6263 // get weather information from result object64 List weatherInformation = createBeans(65 weatherStrings );66

    Instantiate Call

    object and set its URI

    Invoke remote method

    getWeatherInfomationand store returned value in

    Response object

    Display message iferror occurred

    Display remote

    methods returned

    value as List if noproblems occurred

  • 7/27/2019 advjhtp1_29 soap.ppt

    16/20

    2002 Prentice Hall.

    All rights reserved.

    Outline

    Fig. 29.6 SOAPimplementation ofclassWeather-ServiceClient

    (part 3).

    Line 95

    67 // create WeatherListModel for weather information68 ListModel weatherListModel =69 new WeatherListModel( weatherInformation );7071 // create JList, set its CellRenderer and add to72 // layout73 JList weatherJList = new JList( weatherListModel );

    74 weatherJList.setCellRenderer( new75 WeatherCellRenderer() );76 getContentPane().add( new77 JScrollPane( weatherJList ) );78 }7980 } // end try8182 // handle bad URL

    83 catch ( MalformedURLException malformedURLException ) {84 malformedURLException.printStackTrace();85 }8687 // handle SOAP exception88 catch ( SOAPException soapException ) {89 soapException.printStackTrace();90 }9192 } // end WeatherServiceClient constructor9394 // create List of WeatherBeans from Vector of Strings95 public List createBeans( Vector weatherStrings )96 {97 List list = new ArrayList();

    CovertVector ofStringsto List ofWeatherBeans

  • 7/27/2019 advjhtp1_29 soap.ppt

    17/20

    2002 Prentice Hall.All rights reserved.

    Outline

    Fig. 29.6 SOAPimplementation ofclassWeather-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 }

    105106 return list;107 }108109 // execute WeatherServiceClient110 publicstaticvoidmain( String args[] )111 {112 WeatherServiceClient client = null;113

    114 // if no server IP address or host name specified,115 // use "localhost"; otherwise use specified host116 if ( args.length == 0 )117 client = new WeatherServiceClient( "localhost" );118 else119 client = new WeatherServiceClient( args[ 0 ] );120121 // configure and display application window122 client.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );123 client.pack();124 client.setResizable( false );125 client.setVisible( true );126 }127 }

  • 7/27/2019 advjhtp1_29 soap.ppt

    18/20 2002 Prentice Hall. All rights reserved.

    29.3 SOAP Weather Service (cont.)

    Fig. 29.7 Apache SOAP Admin page.

  • 7/27/2019 advjhtp1_29 soap.ppt

    19/20 2002 Prentice Hall. All rights reserved.

    29.3 SOAP Weather Service (cont.)

    Fig. 29.8 Apache SOAPService Deployment Descriptor Template.

  • 7/27/2019 advjhtp1_29 soap.ppt

    20/20 2002 P ti H ll All i ht d

    29.3 SOAP Weather Service (cont.)

    Fig. 29.9 SOAP WeatherService Client.