Level: Introductory Han Kam (kamhw@sg.ibm.com), ASEAN WebSphere Technical Enablement Team, IBM Singapore
26 Jun 2002 This is Part 1 of a two-part series that demonstrates how to develop a Web services client to a third-party Web services provider, Google. Part 1 shows how a J2EE developer using WebSphere Studio Application Developer can quickly and easily develop a J2EE-compliant Web application that consumes the Google Web APIs as a Web service.
Introduction
These days, there is much discussion concerning Web services and how they can help, both technically and economically, to lower costs and increase value for businesses. There has also been much discussion and vendor-controlled demonstrations touting tool platforms that help to reduce development time and provide more productivity in creating and consuming Web services.
GoogleTM, a popular online Web search engine, has recently released the beta version of the Google Web APIs (see the report by
CNET News.com
[Olsen, 2002]). The Google Web APIs let software developers query Google's online search engine directly from their own computer programs. By using the Simple Object Access Protocol (SOAP) and Web Services Definition Language (WSDL) specifications that are part of the Web services standards, Google lets software developers choose their favorite development environment (JavaTM, Perl, Visual Studio .NET, etc.) on which applications can be written to consume their online search engine functions as a Web service.
In this article, we will develop a Web services client to Google, a real-world Web services provider. In walking through this tutorial, you will see how IBM ® WebSphere® Studio Application Developer makes for an excellent Web services development environment.
This article demonstrates how a J2EE developer using WebSphere Studio Application Developer's powerful wizards can quickly and easily develop a J2EE-compliant Web application that consumes the Google Web APIs as a Web service.
Part 1 of this two-part series covers:
- The type of Web services that are provided by the Google Web APIs.
- How to set up the development environment in WebSphere Studio Application Developer.
- How to use the Web Services Client Wizard in WebSphere Studio Application Developer to generate Java proxy and data classes for accessing and consuming the Web services provided by Google.
- How to test the generated Java proxy and data classes using the WebSphere Test Environment and the Universal Test Client.
Prerequisites
Hardware prerequisites
Minimum configuration:
- 500Mhz P-III processor
- 256Mb RAM
- Reliable network connection to the World Wide Web
Note that the hardware configuration used for this article is an IBM Thinkpad T20 with a 750Mhz P-III processor and 512Mb of RAM.
Software
-
Operating system:
Windows® 98, Windows Me, Windows NT® 4.0 with SP 6a or Windows 2000
-
IDE:
WebSphere Studio Application Developer, Version 4.03 (Versions 4.01 and 4.02 can be used but some screens may differ slightly from the screen shots provided in this article)
Note that the software configuration used for this article is WebSphere Studio Application Developer, Version 4.03 running on Windows 2000 Professional (SP 2).
Assumptions
-
There is a direct network connection to reach the Google server; you do not need to use proxies (HTTP or SOCKS) on the World Wide Web from your machine. If this is not possible, however, you are still able to proceed but the important note in the section,
Using the Web Services Client Wizard
, will apply to you.
- It is assumed that WebSphere Studio Application Developer is already installed and the operating system is stable (that is, it is assumed that there are no outstanding OS level setup and configuration issues).
- It is assumed that the reader is fairly familiar with WebSphere Studio Application Developer as a development tool.
Understanding the Google Web API
First, go to the Google Web site and download their Web API developer kit at the
Google Web APIs Web site
. This is a three-step process:
- Download the developer kit.
- Create a Google account.
- Receive a unique license key through e-mail (note that you are asked to provide your e-mail address during step 2 above) that authorizes your use of Google's Web APIs.
The developer kit
The developer kit comes in the form of a ZIP file called
googleapi.zip
. Unzip this file to any drive (for example,
C:\
); a sub-folder called
googleapi
will be created. The full contents of the developer kit are listed in the
README.txt
file in the
googleapi
folder (for example,
C:\googleapi
).
The two required files for this exercise are
APIs_Reference.html
and
GoogleSearch.wsdl
:
-
The
APIs_Reference.html
file contains documentation on what kind of methods (requests) and their parameters can be invoked via SOAP. The target audience of this file is developers.
-
The
GoogleSearch.wsdl
file is a WSDL file that describes technical information needed to access and consume the Web services provided by Google. The purpose of this file is for the Web services tool wizards in WebSphere Studio Application Developer to generate appropriate Java data and proxy classes that are necessary to access and consume the Web services.
Important!
It is the reader's responsibility to read and accept the terms and conditions governed by the usage of the Google Web APIs.
Setting up WebSphere Studio Application Developer
1. Start WebSphere Studio Application Developer
After WebSphere Studio Application Developer is started, ensure that you are in the J2EE perspective. Check that the title bar reads
J2EE - Application Developer
as shown in Figure 1 below.
Figure 1.
If you are not in the J2EE perspective, then open the J2EE perspective by clicking on the Open Perspective icon ( )
located in the left perspective bar, and select J2EE. See Figure 2.
Figure 2.
2. Create a new enterprise application project called GoogleSearchProject
Right-click anywhere in the J2EE view of the J2EE perspective. From the drop-down menu, select
New => Enterprise Application Project
. See Figure 3 below.
Figure 3.
In the
Enterprise Application Project Creation wizard
, enter
GoogleSearchProject
in the Enterprise application project name field. Deselect the
Application client project name
and
EJB project name
check boxes. Enter
GoogleSearch
in the Web project name field. See Figure 4 below.
Figure 4.
Click
Finish
.
3. Create WebSphere v4 Test Environment for the GoogleSearchProject
Open the Server perspective by again clicking on the
Open Perspective
icon, and this time selecting
Server
. See Figure 5.
Figure 5.
Right-click anywhere in the Navigator view of the Server perspective. From the drop-down menu, select
New => Server Instance and Configuration
. See Figure 6 below.
Figure 6.
In the Create New Server Instance and Configuration wizard, enter
WebSphere v4 Test Env
in the Server name field. Enter
Servers
in the Folder field. Expand
WebSphere Servers
, and select
WebSphere v4.0 Test Environment
. See Figure 7.
Figure 7.
Click
Finish
.
Click
Yes
when the Create Server Project message dialog opens. See Figure 8 below.
Figure 8.
In the Server Configuration view, expand
Server Configurations
, and highlight
WebSphere v4 Test Env
. See Figure 9.
Figure 9.
Right-click on
WebSphere v4 Test Env
, and select
Add Project => GoogleSearchProject
. See Figure 10 below.
Figure 10.
This associates
GoogleSearchProject
to the newly created WebSphere Test Environment.
4. Import the WSDL file
In the Navigator view of the Server perspective, expand the
GoogleSearch
project and then expand the
webApplication
folder. See Figure 11.
Figure 11.
Highlight the
webApplication
folder, and then select
File => Import
from the menu
In the Import - Select wizard, select
File System
, and click
Next
.
In the Import - File system wizard, click
Browse
and select the directory where the
GoogleSearch.wsdl
file is located (for example,
C:\googleapi
).
Note:
You must explicitly select the
GoogleSearch.wsdl
file by checking the check box next to the file, as shown in Figure 12 below.
Figure 12.
Click
Finish
.
Using the Web Services Client Wizard
Important!
If you do not have a direct connection to Google's Web services server, you will still be able to complete this section successfully. However, please pay particular attention to the sections marked "Note" in the remainder of this article.
1. Open the Web perspective
Open the Web perspective by again clicking on the
Open Perspective
icon, and this time selecting
Web
. See Figure 13 below.
Figure 13.
In the Navigator view of the Web perspective, expand the
GoogleSearch
project, and then expand the
webApplication
folder. See Figure 14 below.
Figure 14.
2. Using the Web Services Client Wizard
Highlight
GoogleSearch.wsdl
, and select
File => New => Other
.
In the
New - Select wizard
, select
Web Services
in the left pane, and then select
Web Service client
in the right pane. Click
Next
:
Figure 15.
In the
Web Service Client-Web Services wizard
, ensure that
GoogleSearch
is entered in the Web project field, and select the following check boxes:
- Generate a proxy
- Launch the Universal Test Client
- Overwrite files without warning
- Create folders when necessary
Figure 16.
Click
Finish
. Depending on the speed of your processor and how much free RAM is on your machine, the time taken to complete the tasks after clicking
Finish
will vary.
Testing using the Universal Test Client
Important!
If you do not have a direct connection to Google's Web services server, you will NOT be able to complete this section since the Web Services Test Client will not be able to open a socket to Google's Web services server. If this is the case, then you should proceed to the next section,
Understanding generated SOAP and data classes
.
1. What you are supposed to see
When you have finished with the Web Service Client wizard, the Server perspective will now be in focus. You will noticed that the WebSphere v4 Test Env instance has been started and that a Web browser opens within the Server perspective. This Web browser is open to the Universal Test Client Web application that is running on the WebSphere v4 Test Env server.
2. Testing Google Web APIs using the Universal Test Client
The Universal Test Client has two main panes (that is, the left and right panes). In the left pane, expand the
GoogleSearchProxy
object instance under
Object References
. Click on the
doGoogleSearch
method in the GoogleSearchProxy object:
Figure 17.
In the right pane, you will see the details of the
doGoogleSearch
method with a list of parameters to be filled in before we can invoke the method. Refer to the
APIs_Reference.html
file from Google's developer kit -- in section 2.1, under "Search Parameters," you will find a detailed explanation of what the appropriate values are for each of the required parameters.
Under Parameters (see Figure 18 below), replace the value
ABCXYZ
in the first parameter field with the unique key that Google sent you through e-mail when you created the Google account (see
Understanding the Google Web API
section above).
You may use the same values for the other parameters as shown in the screen shot below. In summary, the choice of parameters below reflects how we want to perform a search on Google's search engine:
-
We want the search to be based on the keyword,
websphere
.
- We want the top 10 results.
- We want to enable the filter feature.
- We want NULL for restriction (that is, no restriction).
- We want to enable a safe search.
- We want to search only in the English language.
Figure 18.
Click
Finish
. The
doGoogleSearch
method in the
GoogleSearchProxy
object now opens a socket to access and consume one of the Web services function calls provided by Google Web APIs. Depending on the speed of your network connection, the time taken for a response will vary.
You should see an instance of the
GoogleSearchResultContentType
object returned. Click on the
Work with Object
button. See Figure 19.
Figure 19.
The instance of
GoogleSearchResultContentType
will be available in the Object References tree in the left pane. Expand
GoogleSearchResultContentType
and you will see the methods that are available. Click on the
getEstimatedTotalResultsCount
method; the right pane now displays the method for us to invoke. Click the
Invoke
button. The results of the invocation of the
getEstimatedTotalResultsCount
method is displayed at the bottom of the right pane. We can conclude that the Google search engine returned an estimated 223,000 number of hits from its database. See Figure 20 below.
Figure 20.
To get more information on the top 10 results, we will need to have each of the elements returned. Click on the
getResultElements
method; the right pane now displays the method for us to invoke. Click the
Invoke
button. Next, click on the
Work with Object
button to get the reference to the array of elements that are returned. The array of
ResultElementContentType
will be available in the left pane.
Figure 21.
Working with the array of ResultElementContentType in the left pane, click on the Inspect Fields function and you will see references to each of the ten ResultElementContentType instances. To work with the first ResultElementContentType instance, click on the Work with Object icon ( ).
Figure 22.
Now, click on the
Work with Object
button. See Figure 23.
Figure 23.
Working with the first
ResultElementContentType
instance in the left pane, we can invoke the relevant
getXXX
methods to get information about the best hit on the search engine. In the figure below, the
getSnippet
method is invoked and it returns a snippet of the page found with the keyword,
websphere
.
You may wish to invoke other methods on this first instance of an array of 10
<ResultElementContentType>
or invoke the same methods on the remaining nine instances.
Figure 24.
When the Web Services wizard, together with the Universal Test Client, has created the appropriate classes (proxy and data) to access and consume the Web services provided by Google Web APIs, shut down the WebSphere Application Server instance, WebSphere v4 Test Env, by clicking the icon in the Servers view of the Server perspective, as shown in Figure 25.
Figure 25.
Understanding generated SOAP and data classes
The Universal Test Client (see the previous section) was able to access and consume the Web services provided by Google because it used the Java SOAP proxy class and the appropriate Java data classes generated by the Web Services Client Wizard.
The Java SOAP proxy class
The
proxy.soap.GoogleSearchProxy
class contains methods that correspond to the three callable functions in Google Web APIs, as documented in section 1 of
APIs_Reference.html
.
|
Callable functions (Google)
|
Corresponding methods in GoogleSearchProxy
| | Search | doGoogleSearch(String, String, int, int, boolean, String, Boolean, String, String, String) | | Cache | doGetCachedPage(String, String) | | Spelling | doSpellingSuggestion(String, String) |
Therefore, each method's arguments will correspond to the parameters required by each of the three callable functions. The detailed description of these functions are well-documented in
APIs_Reference.html
.
While the cache and spelling requests return a String datatype, the search request returns a complex type. With reference to sections 3.1 and 3.2 of the
APIs_Reference.html
, the search function will return a search response containing summary information of the search performed as well as an array of result elements.
Thankfully, the Web Services Client wizard also generated Java data classes to represent the appropriate responses from the search request.
The Java data classes
The two data classes used are
mappings.GoogleSearchResultContentType
and
mappings.ResultElementContentType
.
With reference to sections 3.1 and 3.2 of the
APIs_Reference.html
, all information returned from the responses are encapsulated as getter methods in each data class. This allows Java developers to easily obtain and manipulate information returned from the consumed Web services of Google.
Conclusion
At this point, you should have a better understanding of:
- The type of Web services that are provided by the Google Web APIs.
- How to set up the development environment in WebSphere Studio Application Developer.
- How to use the Web Services Client Wizard in WebSphere Studio Application Developer to generate Java proxy and data classes for accessing and consuming the Web services provided by Google.
- How to test the generated Java proxy and data classes using the WebSphere Test Environment and the Universal Test Client.
In Part 2 of this series, we will be developing a J2EE Web application that will access and consume the Web services provided by Google. More powerful J2EE-related features will be introduced.
Furthermore, this J2EE Web application will be based on the Model-View-Control software design architecture. In this article, we generated the Model components using the Web Services Client wizard of WebSphere Studio Application Developer. In Part 2, we will develop and test the Control and View components.
Top of page
Resources
-
Stefanie Olsen,
Developers dig in to Google's toolbox
, CNET News.com, April 2002.
-
Ueli Wahli et al,
Web Services Wizardry with WebSphere Studio Application Developer
, IBM Redbooks, April 2002.
-
Ueli Wahli et al,
Self-Study Guide: WebSphere Studio Application Developer and Web Services
, IBM Redbooks, February 2002.
-
Reiner Kraft,
How IBM WebSphere Studio Application Developer Compares with Microsoft® Visual Studio .NET -- Part 1: Conceptual Differences
, IBM WebSphere Developer Technical Journal, WebSphere Developer Domain, February 2002.
-
Reiner Kraft,
How IBM WebSphere Studio Application Developer Compares with Microsoft Visual Studio .NET -- Part 2: Implementation Differences
, IBM WebSphere Developer Technical Journal, WebSphere Developer Domain, April 2002.
About the author  | |  | Han Wen Kam is a member of the ASEAN WebSphere Technical Enablement Team based in Singapore. His product focus is on WebSphere Application Server and the WebSphere Studio family of development tools. Han works closely with IBM Software Business Partners and conducts the appropriate product training. He has also, from time to time, worked in a WebSphere technical consultative role to direct IBM customers. Han can be contacted at kamhw@sg.ibm.com
|
Rate this page
|