1 Homework Building a Web Servic

Document Sample
1 Homework Building a Web Servic Powered By Docstoc
					Homework: Building a Web Service Client for a Stock Quote Service

1. Objectives

   •   Become familiar with Web Services technologies;
   •   Use a combination of HTML, CSS, DOM, XML and Java Servlets;
   •   Provide a web interface to obtaining real-time stock quotes from an online stock
       quotes web service.

2. Background

2.1 Web Services

A Web service is any piece of software that makes itself available over the Internet and
uses a standardized XML messaging system. XML is used to encode all communications
to a Web service. Communication is performed via the Simple Object Access Protocol
(SOAP). A Web Service can have a public interface, defined in a common XML
grammar. The interface describes all the methods available to clients and specifies the
signature for each method. Currently, interface definition is accomplished via the Web
Service Description Language (WSDL).

2.2 Invesbot.com Stock Quotes Web Service

Invesbot.com is a web service of InveShare, providing real-time stock quotes and stock
indexes. Using the Invesbot.com Stock Quotes Web Service, web developers can obtain
real-time stock quotes and market indexes and embed them into their web sites. Access to
the Stock Quotes Web Service can be accomplished in any one of three ways:

   1. Using direct SOAP message communication with the Stock Quotes Web Service;
   2. Using Java code produced from the WSDL definition file, which describes all
      supported SOAP methods;
   3. Using URL-based REST request-response transactions.

For this exercise, only method 2 above will be acceptable. The WSDL of the
Invesbot.com service is available at the URL:

http://ws.invesbot.com/stockquotes.asmx?WSDL

and is also downloadable from the class website at the URL:

http://www-scf.usc.edu/~csci571/2006Spring/hw11/stockquotes.wsdl

The web service performs 4 operations:

/* Enter symbols, separated by space, Quotes delayed in 20 minutes. */
any GetQuotes(string symbols)




                                                                                       1
/* Enter one symbol, quote delayed in 20 minutes. */
any GetQuote(string symbol)

/* Get Dow, Nasdaq, S&P500 index. */
any GetMarketIndex()

/* Enter symbols, separated by space, Real-Time ECN quote. */
any GetECNQuotes(string symbols)

The operations are documented at:

http://ws.invesbot.com/stockquotes.asmx

3. Description of the Exercise

In this homework assignment you will build a Web Services client that queries the
Invesbot.com Stock Quotes Web Service and returns the 15-minute delayed stock quote
for any stock listed on one of the major stock exchanges, using the function GetQuote().

Web Service Client for Stock Market Quotes

You will install Apache Axis 1.1 in your Tomcat servlet-container and will use
WSDL2Java (included with Axis) to generate Java stubs, skeleton and data types from
the WSDL at stockquotes.wsdl. You will then implement a Java Servlet that uses the
GetQuote()operation to return the 15-minute delayed stock quote of a user-selected
stock. You should provide all of the following components:

   1. A top level Start Page containing the following components:

           1. A text box which will contain a user-selected stock symbol;
           2. A “Get Stock Quote” button;

   2. A Response Page displaying an HTML table containing a translation of all the
      components returned in the XML-based result.

For example, if the XML result returned by the web service is as follows:

<StockQuote>
  <Symbol>AAPL</Symbol>
  <Company>APPLE COMPUTER</Company>
  <Price><big><b>66.24</b></big></Price>
  <Time>12:59PM ET</Time>
  <Change>
    <img width="10" height="14" border="0"
    src="http://us.i1.yimg.com/us.yimg.com/i/us/fi/03rd/up_g.gif"
    alt="Up"> <b style="color:#008800;">0.56 (0.85%)</b>
  </Change>
  <PrevClose>65.68</PrevClose>
  <Open>65.79</Open>
  <Bid>66.24<small> x 400</small></Bid>



                                                                                       2
  <Ask>66.25<small> x 500</small></Ask>
  <YearTarget>90.33</YearTarget>
  <DayRange>65.50 - 66.60</DayRange>
  <YearRange>33.11 - 86.40</YearRange>
  <Volume>12,494,003</Volume>
  <AvgVol>32,644,400</AvgVol>
  <MarketCap>56.21B</MarketCap>
  <PE>35.73</PE>
  <EPS>1.85</EPS>
  <DivYield>N/A (N/A)</DivYield>
  <WebSite>
    <a href="http://www.apple.com">http://www.apple.com</a>
  </WebSite>
  <Business>
Apple Computer, Inc. engages in the design, manufacture, and marketing
of personal computers [.. other information omitted ..] The company is
headquartered in Cupertino, California.
</Business>
<sic/>
<sicname/>
</StockQuote>

The servlet should output the HTML table on the following page:




                                                                         3
The HTML that produced the table above is available at:

http://www-scf.usc.edu/~csci571/2006Spring/hw11/stockquote.html

Notice that HTML contained inside XML tags has been “embedded” in the table result
(see Price, Change and WebSite).


4. Implementation Hints

To do the homework, you must make use of Java Servlets. It is expected you will use
Jakarta-Tomcat. You may either use your own account with the server running on
csci571, csci571b or some other machine that is Internet accessible.

To implement the homework, you should use HTML to design the Top-level page. You
can also design one Java Servlet class, which is responsible for interfacing with the
GetQuote() Web Services operation and displaying its result in table format as outlined
above.


   1. Step 1: Create the Web Service stubs and interfaces using Axis WSDL2Java

         If you will be installing your servlets using the built-in Tomcat examples
         directories, you should execute this step from the webapps/examples/WEB-
         INF/classes      subdirectory, after having copied the WSDL file,
         stockquotes.wsdl, in the same directory. You should invoke WSDL2Java as
         follows:

         java -classpath ${CLASSPATH} org.apache.axis.wsdl.WSDL2Java -D
         stockquotes.wsdl

         After you execute WSDL2Java, the following set of files will be created in the
         subdirectory com/invesbot/ws:

         _GetMarketIndexResponse_GetMarketIndexResult.java
         _GetQuote.java
         _GetQuoteResponse_GetQuoteResult.java
         _GetECNQuotesResponse_GetECNQuotesResult.java
         _GetMarketIndexResponse.java
         _GetQuoteResponse.java
         _GetMarketIndex.java
         _GetECNQuotes.java
         _GetQuotesResponse_GetQuotesResult.java
         _GetECNQuotesResponse.java
         StockQuotes.java
         StockQuotesLocator.java
         StockQuotesSoap.java
         StockQuotesSoapStub.java



                                                                                          4
     StockQuotesSoap12Stub.java

2. Step 2: Compile Axis WSDL stub files

     You must move up to the webapps/examples/WEB-INF/classes subdirectory
     to compile all the stubs produced in Step 1 using javac. For example:

     javac com/invesbot/ws/StockQuotesSoap12Stub.java

     Corresponding .class files will be generated for each of the stubs and
     interfaces.

3. Step 3: Writing your Servlet to interface with the Web Service

     You     should     now      create    your    servlet    in    com/invesbot/ws,
     MyStockQuotes.java,      for example, which invokes methods from the produced
     interfaces and stubs. To help you out, we have created a Javadoc documentation
     database of all the stubs and interfaces from Step 2, available at:

     http://www-scf.usc.edu/~csci571/2006Spring/hw11/api/index.html

     The Java Servlet should do all of the following:

     a. Instantiate a service “locator” using StockQuotesLocator() ;
     b. Set the service “port” using getStockQuotesSoap();
     c. Invoke the web service operation, such as calling getQuote(stock) and
        retrieving the result.

     See the provided Javadocs for details on the parameters and return values of the
     above method.

4. Step 4: Use the Java Servlet to process the XML result

     All operations of the web service define the results in the WSDL as follows:

                  <s:element minOccurs="0" maxOccurs="1"
                    name="GetXXXXResult">
                    <s:complexType mixed="true">
                      <s:sequence>
                        <s:any/>
                      </s:sequence>
                    </s:complexType>
                  </s:element>

     By using a <sequence> of <any>, the operations can return results consisting
     of arbitrary XML fragments. You can call the get_any() method of the result
     object to extract the entire XML and convert it to an array of



                                                                                    5
     org.apache.axis.message.MessageElement.       For example, you could output
     the entire XML document that was returned by executing these lines:

     org.apache.axis.message.MessageElement[] msg = result.get_any();
     writer.println(msg[0]);

5. Step 5: Use Java Servlets to convert the XML into HTML

     You can then create one more servlet, MyStockQuotesImpl.java, called when
     clicking the button from the top-level HTML form below:




     The servlet will invoke the previously built servlet, retrieve the XML output
     and parse and “translate” the data; converting the XML data into HTML format,
     as described in section 3. You can use several techniques to do this: use JAXM,
     JAXP, or JDOM. One technique that we recommend is using JAXP and TraX
     (Transformation for XML) and an XSLT style sheet to produce the HTML
     output.

     Apache Xalan-Java is an XSLT processor for transforming XML documents
     into HTML, text, or other XML document types. Xalan-Java implements the
     javax.xml.transform interface in Java API for XML Processing (JAXP) 1.3, and
     includes the transformation API for XML (TrAX). Documentation and
     download instructions for obtaining Xalan-Java version 2.7 are available at:

     http://xml.apache.org/xalan-j/
     http://xml.apache.org/xalan-j/trax.html

     Important Note: The Java Servlets should handle exceptions such as
     MalformedURLException,              IOException,            Exception,
     javax.xml.rpc.ServiceException,         java.rmi.RemoteException   and
     possibly other exceptions, such as ParserConfigurationException,
     depending on your choice of XML Parser.



                                                                                  6
   6. Step 6: Deploy the Application

         See section “6, Deployment Structure” below for the set of steps to follow to
         deploy your servlet(s).

The above is just one way to implement your program. You can try your own method, as
long as it works and meets our requirement of using Web Services and SOAP.

A Reference implementation is available at:

http://csci571.usc.edu:57201/examples/servlets/Asgn11.html

Important Note: implementing this homework using the Representational State Transfer
(REST) web-based interface that uses XML and HTTP without the extra abstractions of
the Web Services SOAP protocol is not acceptable.

5. Prerequisites

This homework requires the use of all of the following components:

   1. A servlet-based web server, Tomcat 4.1.27. Instructions on how to load Tomcat
      4.1.27 can be found here: http://www-
      scf.usc.edu/~csci571/2006Spring/tomcatinstall.html. A tar version of Tomcat
      4.1.27 can be found here: http://www-scf.usc.edu/~csci571/download/jakarta-
      tomcat-4.1.27.tar.
   2. The Java Servlet library, which has functionality similar to Perl’s LWP library, to
      perform HTTP transactions using methods such as doGet() or doPost() from
      Java.
   3. A Java XML parser library. You may use the JDOM 1.0, an object model that
      uses XML parsers to build documents, available in the Download section of the
      class website. Additional information on JDOM is available at
      http://www.jdom.org/. You may also use JAXP, the Java API for XML Parsing,
      version 1.1, included in the Java JDK 1.4 (import javax.xml.parsers.*) and
      documented at: http://java.sun.com/xml/jaxp/dist/1.1/docs/api/. A good tutorial on
      JAXP is available at http://www-106.ibm.com/developerworks/xml/library/x-
      jaxp1.html.
   4. A SOAP engine and tools such as Apache Axis. A tar version of Apache Axis 1.1
      can be found here: http://www-scf.usc.edu/~csci571/download/axis-1_1.tar.
      Instructions on how to properly install and setup Axis are included in the
      documentation. Additional documentation and setup for this project can be found
      here: http://www-scf.usc.edu/~csci571/2006Spring/axisinstall.html.
   5. A WSDL to Java translator to create Java stubs. The WSDL2Java tool is included
      in Apache Axis 1.1.




                                                                                         7
6. Deployment Structure

To write your own Java Servlets program using Tomcat 4.1.27, you need to:

   1. Successfully install Tomcat 4.1.27 on your machine.
   2. Go to $CATALINA_HOME/webapps/examples subdirectory.
   3. Place the HTML, CSS and JavaScript (.js) files in the Tomcat servlets
      subdirectory.
   4. Place your Java Servlets class files (.class) in the /WEB_INF/classes/
      com/invesbot/ws subdirectory.
   5. Place the Axis libraries (axis.jar, commons-logging.jar, log4j-1.2.8.jar,
      wsdl4j.jar, commons-discovery.jar, jaxrpc.jar, saaj.jar) in the
      /WEB-INF/lib subdirectory. You may have to create this directory, if it does not
      already exist.
   6. Add appropriate sections to the WEB-INF/web.xml file, as in:

       <!-- Stock Quote Servlet -->
       <servlet>
           <servlet-name>MyStockQuotesServlet</servlet-name>
           <display-name>Stock Quotes Application</display-name>
           <servlet-class>com.invesbot.ws.MyStockQuotes</servlet-class>
       </servlet>
       <servlet-mapping>
           <servlet-name>MyStockQuotesServlet</servlet-name>
           <url-pattern>/StockQuotesAxis</url-pattern>
       </servlet-mapping>


   7. To avoid UTFDataFormatException during file IO operation, you have to use
      JDK 1.4 or later for Tomcat. In the .cshrc file under your home directory, add
      the entries:

       setenv JAVA_HOME /usr/j2se

       setenv PATH /usr/j2se/bin:${PATH}

   8. Before you issue a request to your Java Servlet file, you need to compile it. You
      might need a Java Servlet class to compile your Java code, so open the .cshrc file,
      and add “ /home/scf-22/csci571/servlet_classfiles/j2ee.jar ” to your
      CLASSPATH variable:

       setenv CLASSPATH /home/scf-
       22/csci571/servlet_classfiles/j2ee.jar:${CLASSPATH}

   9. Then run “source .cshrc”, and compile your 4 Java classes, as in:

       javac com/invesbot/ws/MyStockQuotes.java

       javac MyStockQuotesImpl.java



                                                                                        8
       Important Note: since the Axis stubs and interfaces are created as part of a Java
       “package”, you will have to include the following import at the beginning of your
       servlet above:

       package com.invesbot.ws;

   10. Restart your Tomcat server and access your servlets from your HTML code. The
      path your Servlet file is:

   http://server_name:port/examples/servlet/servlet-name,             as in:

   http://localhost:8080/examples/servlet/MyStockQuotesServlet

7. Material You Need to Submit

On your course homework page, your link for this homework should go to a page that
includes your HTML program (the Top-level page mentioned above). You should submit
all source code files including HTML (.html), Cascading Style Sheets (.CSS), JavaScript
(.js), Java Servlets (.java) and a README file electronically to the csci571 account so
that it can be graded and compared to all other students' code via the MOSS code
comparison tool.




                                                                                       9

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:4
posted:4/11/2010
language:English
pages:9