Download Web Service/> based Calculator

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CS551 Lab 4: <Web Service/> based Calculator
Submission Deadline: Nov. 8 (Saturday)
Your Submission Package should include following
1. Calculator Lab files (compiled)
2. VS.NET Calculator package
3. Documentation
Web service Implementations
Web services comprise of set of standards to enable application interaction over Web.
The technology is a combined effort from industry (Microsoft, IBM) and W3C. Currently
there are three major Web services toolkits being used widely
a. .NET Web services: Developed by Microsoft and is an integral part of the
complete .NET framework. Integrated and easy use with Visual Studio .NET.
Services are hosted on IIS web servers.
b. Java Web services: Sun’s Web service implementation for the Java community.
Comes bundled in a complete Java Web services Development Pack (JWSDP Ver
3) including Tomcat web server.
c. Apache Axis: Initially developed by IBM and donated to the Apache group. One
of the earliest and stable Web service implementation. Runs on Apache Web
servers.
In this lab we will experiment with .NET and Java Web services.
Hosting Web service
For this lab we have hosted two Web services developed using .NET and Java toolkits.
We will just discuss the steps in hosting a Web services on these platforms, you need not
implement them. The description for hosting Web services is just for your reference and
later actually we will develop clients for both.
Hosting using .NET
.Net Web services are hosted on IIS 5.0/6.0 servers. To develop a Web service it is
mandatory to have access to an IIS web server (if you don’t have, try using Microsoft’s
ASP.Net Web Matrix (http://www.asp.net/webmatrix/) FREE (yes, there is not typo
here)
File -> New -> Project -> ASP.NET Web service
Location: http://localhost/CalculatorService
localhost: could be replaced by any remote Web server, given you have sufficient rights
to access the Web server.
Coding a Web service
[WebMethod]
public double add(double op1, double op2)
{
return (op1+op2);
}
The attribute [WebMethod] identifies the method as being Web method and
consequently invokable over Web.
Currently we have hosted the calculator Web service at following URL
Information: http://sice527.ddns.umkc.edu/CalcService/CalcService.asmx
WSDL Location: http://sice527.ddns.umkc.edu/CalcService/CalcService.asmx?WSDL
Point your Browser to the Information URL and you could invoke the Remote Web
methods. Test the various Web methods (add, subtract divide, multiply)
(NOTE: The IIS Server has been very unstable (its Microsoft !!) and quite often goes
down. So the browser gives a 404, in such case please send a email to the TA, Chintan
([email protected]))
Hosting using Java Web services
Java Web services are default integrated with Tomcat, however one can also use Apache
Web server to host a service (which seems to be more stable). The Java Web services
Developer pack is freely available from Sun
(http://java.sun.com/webservices/webservicespack.html)
In Java Web services, there are several configuration files and path settings (that is
obvious!!). Following are essentials tools and files for developing and hosting Java based
Web services
Interface file: We define the Web services (remote methods) to be invoked in this file
CalcSvcIF.java
package calcsvc;
import java.rmi.*;
public interface CalcSvcIF extends Remote {
public float add(float op1, float op2) throws RemoteException;
public float subtract(float op1, float op2) throws RemoteException;
..
}
Implementation File
CalcSvcImpl.java
public class CalcSvcImpl implements CalcSvcIF {
public double add(double op1, double op2){
return (op1+op2);
}
}
Ant Tool: This a tool extensively used in J2EE architecture for handling multiple
configuration files while compiling several files. You can consider it similar to make
utility in *nix platforms.
Build.xml: This is the file used by the Ant tool to get the compilation and path directives.
It contains options to selectively
compile-server, compile-client, generate-client, run-client etc
Build.properties: Some misc properties and path settings that are referred to by
build.xml.
config.xml: Defines the URL for WSDL file location. Each Web services has a
corresponding WSDL (Web service Definition Language) document.
jaxrpc-ri.xml: Defines the various end points for referencing a Web service.
Misc. utilities: These are some internal tools and utilities called by Ant for compiling and
deployment of Web services.
wscompile: The wscompile tool generates stubs, ties, serializers, and WSDL files
used in JAX-RPC clients and services. The tool reads as input a configuration file and
either a WSDL file or an RMI interface that defines the service.
wsdeploy: Reads a WAR file (something like Jar file) and the jaxrpc-ri.xml
file and then generates another WAR file that is ready for deployment
The Java Calculator Web service has been deployed on Tomcat running on Linux
Status and Port Information: http://sice527.ddns.umkc.edu:81/calcsvc/calcsvc
WSDL Location: http://sice527.ddns.umkc.edu:81/calcsvc/calcsvc?WSDL
(NOTE: we are using same IP and running two Web servers on different port numbers for
IIS (80) and Tomcat(81) )
Creating Calculator Client
Firstly create a new DistMenu Item as “Web service” Distribution
Add a new package “webservice” and define corresponding declaration in
CalcFactoryImpl.java
Copy the PlainCalculatorImpl.java to webservice package.
In the Lab4 download, unzip the jwsdp-3 package.
Path settings
In the file setpaths.bat modify the jwsdphome path to point to the location where you
unzipped the jwsdp package.
set jwsdphome = y:/cs551/lab4/jwsdp-3
In the jwsdp-3/jwsdp-shared/bin folder find setenv.bat
Modify them accordingly (depending upon your JDK etc)
set JAVA_HOME=c:\progra~1\j2sdk1.4.0
(NOTE : Ant tool doesn’t recognize spaces so you need to specify the path in old DOS
format 8.3, short names)
set ANT_HOME=y:\cs551\lab4\jwsdp-3\apache-ant
set JWSDP_HOME=y:\cs551\lab4\jwsdp-3
Classpaths
As already mentioned, Ant doesn’t support long names, so doesn’t allow spaces to come
in the file names. Hence set classpath as follows
set classpath = .;C:\Progra~1\Java\j2re1.4.0\lib\ext\QTJava.zip (in FH-SCE Labs)
After setting this classpath
Run the setpaths.bat (provided in lab4 directory)
Configuration
In the folder etc you will find the several config files. Important one is config.xml. This
is the file where you point to the corresponding WSDL file and change the package name.
Edit config.xml
<wsdl
location="http://sice527.ddns.umkc.edu:81/calcsvc/calcsvc?WSDL"
packageName="webservice" />
COPY the etc folder inside the “webservice” package
Also Copy the build.properties and build.xml files under “webservice” folder.
Edit the build.properties to make the jwsdphome point the location where you unzipped
jwsdp
Edit build.xml to change following variables
<property name="endpoint"
value="http://sice527.ddns.umkc.edu:81/calcsvc/calcsvc"/>
<property name="server.port.url"
value="http://sice527.ddns.umkc.edu:81/calcsvc/calcsvc"/>
Generate Stubs
Now we will generate Client –stubs corresponding to the Web service. In the webservice
folder (where you have the build.xml and etc folder, run following command
ant generate-client
This would call wscompile which compiles the WSDL file and generates corresponding
stubs. You can find those stubs created in new webservice directory (created inside
webservice directory)
Coding a Web service Client
Now we modify the webservice.PlainCalculator.java to call the Client stubs
Define a new Stub class
CalcSvcIF_Stub stub;
We invoke the Web service methods via this stub object
In the newConnection() we instantiate the stub
stub = (CalcSvcIF_Stub)(new Calc_Impl().getCalcSvcIFPort());
Notice the names of the classes are generated depending upon the port definitions in
WSDL file (for more information http://www.w3.org/TR/wsdl) These port names are
actually specified in jaxrpc-ri.xml when we hosted the service
stub._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY,"http://
sice527.ddns.umkc.edu:81/calcsvc/calcsvc");
Here we specify the endpoint address that the stubs uses to access the service, in our case
its http://sice527.ddns.umkc.edu:81/calcsvc/calcsvc
Invoking Web Method: Just call the method name through the stub
double d = stub.add(a,b) ;
We compile the client: ant compile-client
This would generate the webservice.PlainCalculatorImpl class file and place it in a
webservice package (NOTE: You can always modify the source and destination directory
for compilation from the build.xml)
That’s It!
Creating .NET Calculator Client
Create a New Windows Application using whatever language of your choice (VB.NET,
C# or VC++) using Visual Studio.NET
Create the Basic Calculator Application (addition, subtraction etc, should not take more
than 15mins  )
(NOTE : If you working in SCE-FH labs, then Create the Web application in
C:\documents and settings\YOURID, otherwise in Y: or Q: drives it would throw security
exception when accessing the service)
Creating Client Stubs
In .NET creating client stubs is just couple of clicks.
Right Click on References in the Solution Explorer of Visual Studio.NET -> Add Web
Reference…
fig : GUI and the Solution Explorer
Add Reference to following URL:
http://sice527.ddns.umkc.edu/CalcService/CalcService.asmx?WSDL (which is hosted
on .NET /IIS) -> Add Reference
Include the Stub files in the project (assuming the code is in C# .NET)
using WindowsApplication1.edu.umkc.ddns.sice527;
where, WindowsApplication1 would be the names of the project.
Declare a Stub Variable and instantiate in the constructor
Calculator c;
public Form1(){
calc = new Calculator();
...
Invoking the Service
Visual Studio.NET provides an intuitive and easy way of invoking the Web Method.
Cool Stuff (For Nerds, not required for Submission)
.NET provides an easy way to work with Web services by hiding several complexities.
Java on other hand puts more burden on the developer to explicitly configure and execute
a Web service. The trade off is “bugs” (always entailed with Microsoft :) .
Lets try other way round. :
Invoking a Service hosted on Java Web service hosted on Tomcat (port 81)
though .NET
Add one more Web Reference : http://sice527.ddns.umkc.edu:81/calcsvc/calcsvc?WSDL
Include the stub files and create a Calc stub object
Try to invoke a method, although it would compile perfectly but would generate a
runtime exception
The request failed with HTTP status 405: Method not allowed.
The reason being VS.NET hides a lot of underlying details!! Check 2 things
a. Check the WSDL being fetched in VS.NET. Open the Calc.wsdl and you will
notice something strange :
http://sice527.ddns.umkc.edu:80/calcsvc/calcsvc
VS.NET automatically modified the port number to 80 even though we had
specified it something else. The reason being Microsoft Implementation assumes
that all Web services would (should be ?) hosted on port 80.
b. Open the correspond “Reference.cs” which is created in Web Reference folder
under your project. Change the port number back to 81 and then you would be
able to compile successfully.
Do Cool Stuff using Web services
a. Go to www.allesta.com , here you can give the WSDL url and invoke the methods
run time.
b. Google: you could invoke the Google Search in your applications. Check this
http://www.google.com/apis/
c. Xmethods.net: Extremely cool place to find some weirdest of Web services (you
could submit some service here !!)
a. Send SMS to different countries (including India !)
b. Find out online status of an MSN Id, (see who has ‘Blocked’ you)
d. www.SalCentral.com : Google for Web services
e. Access some real UDDIs
a. Microsoft: http://uddi.microsoft.com/
b. IBM (test): http://www-3.ibm.com/software/solutions/webservices/uddi/
c. SAP : https://websmp102.sap-ag.de/~form/uddi_discover/prod