Constructing the InSys Catalog Web Service XML

You will now start constructing the InSys Web service. You will need to create the following three files:

  • The CGI script that redirects Web method calls to a package
  • The package file
  • The client to test the service

The CGI script is given next. Name the file productinfo.cgi and store it in the cgi-bin folder of your Apache installation.

The CGI script, as you can see, simply redirects all calls to the package ProductInfo. Next, you need to create the package, which will handle all queries from the client. The package will support the following methods:

  • getProductInfo(). This method is exposed to the client and is called by all Web service clients. It accepts two parameters: the name of the product on which information is required and the kind of information that is requested.
  • getProductPrice(). The getProductInfo() method calls this method internally when the information requested is the price of a product.
  • getProductData(). The getProductInfo( method calls this method when the information requested is details about a particular product.
  • getProductID(). This method returns the ID of a product.
  • getProductVendor(). This method returns the name of the vendor of the product.
  • getCompanyURL(). This method returns the product manufacturer's URL.

The code chooses its data from a file databaseschema.conf, so the database file must be in the same folder as the script. First, the text file that contains the data is opened. If the file is not opened, the script exits.

In the preceding script, the file is opened first. If it's successful, its contents are read into a variable and the file is closed. The getProductInfo() method accepts two parameters. We will look at the SOAP message that the client sends when asking for the price of a product, such as Handspring Treo. The bold portions of the following code give the input values from the client application.

When the getProductInfo() method receives this data, the method checks the value of the second parameter. The values that are sent are accessible through a perl-define array

Because the first value in the array contains the name of the script, it is shifted out. The first value in the array is the product name as passed by the client and the second value is the information that is required.

$second=$_[0];

The product name is stored in a variable, and the second value is checked against possible values to determine the method to be called. For example, if the value is price, then the method getProductPrice() is called.

Next, you will look at the getProductPrice() method. Each of these methods creates a search pattern depending on the information required, and a pattern search is made in the text database file. Perl is the best language to use for pattern matching because it has a lot of built-in commands and syntaxes specifically for this task, which is normally not available in other programming languages.

Before you continue, you need to look at the data source text file to learn how the data is stored in it and what pattern you can use to search for a particular piece of information. The following is a segment of the data in a flat text file. Save the file as

Handspring Treo models are favorites of reviewers for theirexpansion slots. By plugging various modules (sold separately) into this unit's"Springboard," you can turn it into a cell phone, digital camera, or MP3 player, among other applications. The Prism has a color screen in addition to 8MB of memory on the Palm OS. Experts say you'll get more battery life out of this color PDA than a comparable Pocket PC color unit—over 12 hours. Keep in mind, however, that monochrome models like the CLIE, above, can run for weeks without a recharge."

We have reproduced only the data for Product1. As is evident from the content of the preceding file, the pattern to search when you want to look up the price is company.product1.price. The method getProductPrice() should search for this pattern. However, it cannot be specific to a product. Therefore, you need to use the variable $second, which stores the product name. The search pattern then becomes company. $second. price. Here is the code for searching for the pattern:

All other methods use a similar pattern search and return appropriate values. Save the package file as ProductInfo.pm. Following is the complete code for the package:

Now that the Web service is ready, we will turn our attention to creating a client for this Web service.

The client application consists of two parts:

  • Taking input from the user by using an HTML form
  • Processing and displaying the results retrieved from the client

The script decides which part of the form to display depending on the variable flag that is used. When the input form is to be displayed, the default value of the variable is 0. However, when the form is submitted, a hidden field sets the value to 1 and the script retrieves the data from the server and displays it.

When you click the Submit button, the input form posts two values: the product and the information required on the product. Here is the code in the script that does that:

If you notice, the Select control has the name first and the set of radio buttons second. These are the names for the values that are sent to the script after the form has been posted. Here is the code that extracts the name-value pairs:

These values are passed to the Web service's getProductInfo() method. The code for doing this is shown next:

The returned values are then displayed within a table. Save the client application as Webservice.pl. Here is the complete code for the client application:

Ensure that all three files you created are in the <Apache Web Server_Home> cgi-bin folder. Try the application by connecting to the client using http://localhost/cgi-bin/webservice.pl. The client interface before the Web service is invoked is shown in Figure.

The Web Service Client

The Web Service Client

The reply from the Web service is shown in Figure.

Result of Querying the Web Service

Result of Querying the Web Service


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

XML Topics