454 WebSphere Studio Application Developer Version 5 Programming Guide The ItsoServer is used as the server. Figure 13-9 Testing options for the Web service Publication In the Web Service Publication page you can publish the Web service to a UDDI registry. For now, leave the boxes unchecked and click Finish . Finish The Web service is deployed in the Web project and the Web project is deployed to a WebSphere test server: An existing server is updated (WebSphere v5.0 Test Environment). The enterprise application is added to the configuration. The sample JSP test client is launched to test the Web service. Chapter 13. Developing Web services 455 The Web Service Explorer is launched in an external Web browser to publish the Web service (only if you select publishing in the last step of the wizard). If you have problems creating the Web service, consult the online documentation and the d:\workspace\.metadata\.log file. Generated files Before we test the Web service, let’s look at the generated files (Figure 13-10). Figure 13-10 J2EE Navigator view after the generation of the Web service The Web service is installed in the ItsoProGuideWebServ project and the proxy and sample test client are in the ItsoProGuideWebServClient project. Client project JavaBean Proxy Test sample Admin application SOAP runtime ISD file XSD files WSDL files Server project descriptor deployment Starting point Helper 456 WebSphere Studio Application Developer Version 5 Programming Guide Files generated in the server Web project According to the settings made during the run of the wizard, the following files in the ItsoProGuideWebServ project have been created: The Web service deployment descriptor Banking.isd in Web Content/WEB-INF/isd/java/itso/webserv/model provides information about the services that should be made available to clients. This information is added to the dds.xml file, the SOAP deployment descriptor, and used by the SOAP run-time. The Web Content/WEB-INF/lib folder is extended with three library files that are part of the SOAP run-time code: – soapcfg.jar – webservice-runtime.jar – xsd.bean.runtime.jar The Web Content folder contains the admin folder where SOAP administration can be performed (select the index.html and Run on Server ). In the Web Content folder, a new folder wsdl/itso/webserv/model contains the WSDL files and an XSD file: – Banking.wsdl—service interface – BankingBinding.wsdl—service binding – BankingJava.wsdl—service binding for Java (for WSIF) – BankingService.wsdl—service implementation – Banking.xsd—which defines how the TransRecord array object is mapped to XML The wsdl/itso/bank/model folder contains the XSD files of the return types Account and TransRecord. Finally, the Web Content folder itself contains the files: – dds.xml, the deployment descriptor for SOAP, a concatenation of all the ISD files (only one for now) – soap.xml, the Apache SOAP server configuration file Files generated in the client Web project If the creation of a client-side proxy is selected, Application Developer generates two classes in the new ItsoProGuideWebServClient project: itso.bank.model package contains the Account and TransRecord objects, mapped from the XML representation into a Java class. Tip: The Banking.xsd file will show errors if you are not connected to the Internet because the import statements cannot be resolved. You can ignore the errors, the WSDL files are not used at runtime. Chapter 13. Developing Web services 457 proxy.soap.BankingProxy is the proxy class that contains the methods withdraw, deposit, and getAccount. These methods can now be locally invoked by a client (see “Implementing a real client application” on page 465). The test client is generated into the Web Content/sample/Banking folder. Client Account class The generated client-side Account class (Figure 13-11) provides the same accessor methods as the original Account class that we imported into the Web project. Figure 13-11 Generated account class (extract) package itso.webserv.model; import ; public class Account extends AnyType { public Account() { addElement("type", java.lang.String.class); addElement("balance", java.math.BigDecimal.class); addElement("id", java.lang.String.class); } public String getType() { return (String)this.basicGet("type", 0); } public void setType(String type) { this.basicSet("type", 0, type); } public BigDecimal getBalance() { return (BigDecimal)this.basicGet("balance", 0); } public void setBalance(BigDecimal balance) { this.basicSet("balance", 0, balance); } public String getId() { return (String)this.basicGet("id", 0); } public void setId(String id) { this.basicSet("id", 0, id); } } 458 WebSphere Studio Application Developer Version 5 Programming Guide Proxy class Figure 13-12 shows the getAccount method of the proxy class as an example of the processing in the proxy. Figure 13-12 Generated proxy class (extract) public class BankingProxy { private Call call; private URL url = null; private String stringURL = "http://localhost:9080/ItsoProGuideWebServ/servlet/rpcrouter"; private java.lang.reflect.Method setTcpNoDelayMethod; public synchronized itso.webserv.model.Account getAccount (java.lang.String accountId) throws Exception { String targetObjectURI = "http://tempuri.org/itso.webserv.model.Banking"; String SOAPActionURI = ""; if(getURL() == null) { throw new SOAPException(Constants.FAULT_CODE_CLIENT, "A URL must be specified via BankingProxy.setEndPoint(URL)."); } call.setMethodName("getAccount"); call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); call.setTargetObjectURI(targetObjectURI); Vector params = new Vector(); Parameter accountIdParam = new Parameter("accountId", java.lang.String.class, accountId, Constants.NS_URI_SOAP_ENC); params.addElement(accountIdParam); call.setParams(params); Response resp = call.invoke(getURL(), SOAPActionURI); <==== Web Service Call //Check the response. if (resp.generatedFault()) { Fault fault = resp.getFault(); call.setFullTargetObjectURI(targetObjectURI); throw new SOAPException(fault.getFaultCode(), fault.getFaultString()); } else { Parameter refValue = resp.getReturnValue(); return ((itso.webserv.model.Account)refValue.getValue()); } } Chapter 13. Developing Web services 459 A Call object is filled with the method name, encoding style, target URI, and parameters. The Web service is invoked and the result examined. A good result is converted into the required return type. Testing the Web service There are two ways to test the generated proxy code:. Using the generated sample test application, which is composed of a set of four JSPs Using the universal test client with the proxy class Using the sample test application By selecting Test the generated proxy in the Web services wizard (Figure 13-9 on page 454), the sample test client is started in a browser pane with this URL: http://local :9080/ItsoProGuideWebServClient/sample/Banking/TestClient.jsp Figure 13-13 shows the sample test client in the internal browser. You can also type the URL into an external browser. Select a method (for example getAccount, enter the parameter, click Invoke , and the result is displayed. Figure 13-13 Sample test client 460 WebSphere Studio Application Developer Version 5 Programming Guide Note that the deposit, withdraw and transfer methods have no result, but you can check if they worked by using the getAccount method and verifying the balance amount. The sample test client was created in the new ItsoProGuideWebServClient Web project called. If the browser does not launch automatically, follow these steps: Select the TestClient.jsp in sample/Banking then select Run on Server from the context menu. In the Select Server dialog, select Use an existing server and select the ItsoServer. Also select Set server as project default, then click Finish . Using the universal test client To test a Web service with the universal test client, select the proxy class (proxy.soap.BankingProxy) and Launch Universal Test Client (context). An instance of the proxy class is instantiated and you can run its methods (Figure 13-14). Figure 13-14 Web service testing with universal test client Click Work with Object to add the result Account object to the object references. Expand the Account object to run its getter methods. Chapter 13. Developing Web services 461 Creating a Web service client Application Developer has wizards to create a client that accesses an existing Web service. We now explain how to quickly create a Java proxy, given a WSDL file, in order to access a Web service from a Web application. We use the WSDL file generated by the Web service wizard in “Creating a Web service from a JavaBean” on page 446. To simulate the activities of a real client, we copy the WSDL files from the server Web project to the client Web project: Copy the wsdl folder and all its subfolders: From: ItsoProGuideWebServ/Web Content To: ItsoProGuideWebServClient/Web Content Select the wsdl folder and Copy (context), then select the target Web Content folder and Paste (context). For this exercise, imagine that the server Web project is on another server somewhere in the Internet. All you have available is the description of the Web service in the form of the WSDL files. From the WSDL files, we generate a client proxy and a test sample and verify that the Web service works. Both the client and the service actually run on the same machine. Run the Web Service Client wizard Expand ItsoProGuideWebServClient/Web Content/wsdl, select the BankingService.wsdl file and New -> Other -> Web Services -> Web Service Client . Click Next to start the Web service client wizard. We go through all the pages of the wizard. Click Next on each page to get to the next dialog. In the first page of the wizard, select the Test the generated proxy check box, as shown in Figure 13-15. This causes the wizard to create sample JSP files that we will use later to verify that the proxy works properly. 462 WebSphere Studio Application Developer Version 5 Programming Guide Figure 13-15 Web Service Client wizard In the Web Services WSDL File Selection page, the BankingService.wsdl file that you selected earlier is entered in the WSDL file name or URL text box, as shown in Figure 13-16. Here you could enter the URL to a WSDL file on the Internet, or click Browse to select a different file that exists in your workspace. Figure 13-16 Select a WSDL file The panels that follow are the same as when we created the Web service. We will only make a few changes to create different classes and JSPs (Figure 13-17): For the proxy we change the name to proxy.soap.ClientBankingProxy. For the sample test application we use sample/ClientBanking as output folder. Chapter 13. Developing Web services 463 Figure 13-17 Client proxy and sample application generation Click Finish to generate the Java proxy and sample JSPs. After the files are generated, an internal Web browser opens the TestClient.jsp, as explained in “Testing the Web service” on page 459. Note that you have now two proxy classes and two folders with a sample test application. The code of both is identical. Application Developer provides an easy way to create a client Java application for a Web service. The Web Service Client wizard generates the Java proxy and, optionally, a sample Web application that gets you started quickly in using Web services in your Web applications. . 454 WebSphere Studio Application Developer Version 5 Programming Guide The ItsoServer is used as the server. Figure 13-9 Testing. files Server project descriptor deployment Starting point Helper 456 WebSphere Studio Application Developer Version 5 Programming Guide Files generated in the server Web project According to the. setId(String id) { this.basicSet("id", 0, id); } } 458 WebSphere Studio Application Developer Version 5 Programming Guide Proxy class Figure 13-12 shows the getAccount method of the