Title: Automated Tools for Web Services JAVA Version
1Automated Tools for Web Services (JAVA Version)
- IWSJ Fall 2006, Lecture 10
- Adam Poswolsky
- 14 November 2006
2Why Tools?
- Why not do everything by hand?
- Not masochistic.
- Why JAVA
- Tools also available for C, C, etc.
3Quick Recap of Buzzwords
- SOAP
- XML-based communication protocol
- We are using on top of HTTP
- Not following REST architecture principal.
- WSDL
- Web Service Description Language
- Contract of Services provided.
- UDDI
- Service Registry
- Return WSDL documents for Services.
4Web Services Platform
UDDI Registry
Find
Register
WSDL
Client
Server
SOAP
5Apache Axis
- Axis is a SOAP engine
- Axis stands for Apache EXtensible Interaction
System - Facilitate creation of clients and servers using
SOAP message framework. - Extensive support for WSDL.
- Automatically create skeleton from WSDL.
- Or create WSDL based on JAVA code.
- And More!
- See Axis User guide
- http//ws.apache.org/axis/java/user-guide.html
6Stylus Studio
- One of many possible frontends.
- Nice GUI utilities for
- XML Editing
- Create Schemas, Stylesheets, etc.
- Webservices
- But it needs underlying SOAP tools.
- You still need Apache Axis!
- http//www.stylusstudio.com
7What will we do today? (1/2)
- Install Axis (if not on lab machines).
- Use tools to create a simple web service.
- Create Server
- Create WSDL
- Create Stub for Client
- Create Skeleton for Server
- Should we have created the WSDL first?
- We will need to plug web service into Tomcat.
8What will we do today? (2/2)
- Service discovery and registration using UDDI
- UDDI4j is a Java class library providing an API
to interact with a registry. - Stylus Studio has a nice GUI interface to UDDI.
9Simple Client (DKK to USD)
Execution C\axis\adamgtjava DKKtoUSD You should
give me ???DKK for just one USD.
- import org.apache.axis.client.
- import javax.xml.namespace.QName
- public class DKKtoUSD
- public static void main(String args)
- try
- Service service new Service()
- Call call (Call) service.createCall()
- call.setTargetEndpointAddress (new
java.net.URL -
("http//localhost8080/axis/Exchange.jws")) - call.setOperationName (new
QName("http//DefaultNamespage", -
"convert")) - Float ret (Float) call.invoke (new
Object"DKK", "USD") - System.out.println("You should give me "
ret - "DKK for just one
USD.") - catch (Exception e) System.out.println(e.t
oString()) -
10Simple Example Details (1/2)
- call.setOperationName (new QName("http//DefaultNa
mespage", -
"convert")) - Namespace is used to distinguish between
different convert functions. - It should be globally unique!
- Does not need to be real
- i.e. you may not be able to access it via web
browser. - Could have used urnposwolsky-giveMeMoney
11Simple Example Details (2/2)
- IDEAL REQUEST
- ltSOAP-ENVEnvelope ltSOAP-ENVBodygt ltn
s1convert xmlnsns1"http//DefaultNamespace"gt
ltfromSymbol xsitype"xsdstring"gtDKKlt/f
romSymbolgt lttoSymbol
xsitype"xsdstring"gtUSDlt/toSymbolgt lt/ns1
convertgt lt/SOAP-ENVBodygtlt/SOAP-ENVEnvelope
gt
IDEAL RESPONSE ltsoapenvEnvelope
gtltsoapenvBodygtltns1convertResponse
soapenvencodingStyle"http//schemas.xmlsoap.org/
soap/encoding/" xmlnsns1"http//DefaultNamespace
"gtltconvertReturn xsitype"xsdfloat"gt?????lt/conv
ertReturngtlt/ns1convertResponsegtlt/soapenvBodygtlt
/soapenvEnvelopegt
12Monitoring Connections (TCPMon)
- Axis includes a tool called TCPMon
- Inspect SOAP messages to and from services.
- Resend messages
- Change messages
- .
- Execution
- java org.apache.axis.utils.tcpmon
- Alternatives
- Axis also has a SOAP monitor service which is
disabled by default (for security concerns).
13java org.apache.axis.utils.tcpmon
- Choose some Listen Port
- E.g. 9999
- Change client to connect to Listen port instead
of Tomcat Port (8080)
14Easy Deployment of Web Services
15Easiest Possible Deployment
- For extremely simple Java programs,
- i.e. public class Calculator public int add(int
i1, int i2) return i1 i2 - Just copy source (Calculator.java) to
lttomcat-rootgtaxis/Calculator.jws - Access service by
- http//localhost8080/axis/Calculator.jws
- Get automatic generated WSDL at
- http//localhost8080/axis/Calculator.jws?wsdl
16Difficult (but more thorough) version of
Deployment
17Isnt Easy enough?
- Limitations
- Cannot use packages.
- Must have access to the source.
- And it is compiled at run-time.
- No customization,
- But the axis team is working on allowing you to
embed this metadata into your source-code.
18Axis Web Service Deployment Descriptor (WSDD)
- WSDD describes what to make available to Axis
engine. - ltdeployment xmlns"http//xml.apache.org/axis/wsdd
/" - xmlnsjava"http//xml.apache.org/axis/wsdd/pr
oviders/java"gt - ltservice name"AdamCalcService"
provider"javaRPC"gt - ltparameter name"className"
valueAdamCalcServiceClass/gt - ltparameter name"allowedMethods" value""/gt
- lt/servicegt
- lt/deploymentgt
- Saved as deploy.wsdd
19Deployment
- Copy AdamCalcServiceClass.class to
lttomcatgt\webapps\axis\WEB-INF\classes - Run java org.apache.axis.client.AdminClient
deploy.wsdd - Now I can access at
- http//localhost8080/axis/services/AdamCalcServic
e
20Advanced WSDD Features (1/2)
- When should Axis make new objects of the
aforementioned class. - They call this scoping
- Request scope
- Create a new object for every request (default).
- Application scope
- Singleton object for all requests.
- Session scope
- New object for each session-enabled client.
21Advanced WSDD Features (2/2)
- Different Service Styles
- RPC (from our example) uses SOAP RPC conventions
and SOAP encoding. - Document and Wrapped do not use SOAP encoding for
data (just specify XML Schema) but binds JAVA
representations to the XML. - In most cases you won't need to worry about
document or wrapped services - Message will give access to raw XML.
- You have to turn it manually into JAVA object.
- But you can verify with an XML Schema.
22Java types to SOAP/XML types
Axis will only send objects for which there is a
registered Axis serializer. Axis includes the
ability to serialize/deserialize, without writing
any code, arbitrary Java classes which follow the
standard JavaBean pattern of get/set accessors.
All you need to do is tell Axis which Java
classes map to which XML Schema types.
Exceptions become SOAP faults.
23Advanced Calculator Server
..gtjava AdamCalcServiceClass 128 to binary is
10000000 100 to decimal is 4
- public class AdamCalcServiceClass
- public static int binary2Int(String numBinary)
-
- int length numBinary.length()
- int least_sig_bit
- if (length 0) return 0
- if (numBinary.charAt(length-1) '1')
least_sig_bit1 - else
least_sig_bit0 - String restNum numBinary.substring(0,
length-1) - return least_sig_bit (2
binary2Int(restNum)) -
- public static String int2Binary(int inputNum)
-
- int least_sig_bit inputNum 2
- int quotient inputNum / 2
- if (quotient0) return (Integer.toString(lea
st_sig_bit)) - return (int2Binary(quotient)
least_sig_bit) -
24Axis WSDL Support
- When you deploy a service, you can retrieve WSDL
by appending ?WSDL to end of URL. - Sample with AdamCalcService
- WSDL2Java tool
- Build java stubs/skeletons for services with WSDL
descriptions. - Business Contract approach would be to start with
WSDL. - Java2WSDL tool
- Build WSDL from Java classes.
- What we will do today
- Use Java2WSDL to create WSDL based on Server
written in Java. Create client using WSDL2Java.
25Java2WSDL
- Generate the WSDL description based on the server
interface - java org.apache.axis.wsdl.Java2WSDL -o
adamcalc.wsdl -l http//localhost8080/axis/serv
ices/AdamCalcService -n urnposwolskyFoo
AdamCalcServiceClass
26Output adamcalc.wsdl (1/4)
- ltwsdltypesgt
- ltschema targetNamespace"urnposwolskyfoo"
xmlns"http//www.w3.org/2001/XMLSchema"gt - ltimport namespace"http//schemas.xmlsoap.org/s
oap/encoding/"/gt - ltcomplexType name"ArrayOf_soapenc_string"gt
- ltcomplexContentgt
- ltrestriction base"soapencArray"gt
- ltattribute ref"soapencarrayType"
wsdlarrayType"soapencstring"/gt - lt/restrictiongt
- lt/complexContentgt
- lt/complexTypegt
- lt/schemagt
- lt/wsdltypesgt
27Output adamcalc.wsdl (2/4)
- ltwsdlmessage name"binary2IntResponse"gt
- ltwsdlpart name"binary2IntReturn"
type"xsdint"/gt - lt/wsdlmessagegt
-
- ltwsdlmessage name"binary2IntRequest"gt
- ltwsdlpart name"in0" type"soapencstring"/
gt - lt/wsdlmessagegt
- ltwsdlmessage name"int2BinaryRequest"gt
- ltwsdlpart name"in0" type"xsdint"/gt
- lt/wsdlmessagegt
- ltwsdlmessage name"int2BinaryResponse"gt
- ltwsdlpart name"int2BinaryReturn"
type"soapencstring"/gt - lt/wsdlmessagegt
- ltwsdlmessage name"mainRequest"gt
-
- lt/wsdlmessagegt
28Output adamcalc.wsdl (3/4)
- ltwsdlportType name"AdamCalcServiceClass"gt
-
- ltwsdloperation name"binary2Int"
parameterOrder"in0"gt - ltwsdlinput message"implbinary2IntReque
st" name"binary2IntRequest"/gt - ltwsdloutput message"implbinary2IntResp
onse" name"binary2IntResponse"/gt - lt/wsdloperationgt
- ltwsdloperation name"int2Binary"
parameterOrder"in0"gt - ltwsdlinput message"implint2BinaryReque
st" name"int2BinaryRequest"/gt - ltwsdloutput message"implint2BinaryResp
onse" name"int2BinaryResponse"/gt - lt/wsdloperationgt
- lt/wsdlportTypegt
29Output adamcalc.wsdl (4/4)
- ltwsdlbinding name"AdamCalcServiceSoapBinding
type"implAdamCalcServiceClass"gt -
- ltwsdloperation name"binary2Int"gt
- ltwsdlsoapoperation soapAction""/gt
- ltwsdlinput name"binary2IntRequest"gt
- ltwsdlsoapbody encodingStyle"http//s
chemas.xmlsoap.org/soap/encoding/"
namespace"urnposwolskyfoo" use"encoded"/gt - lt/wsdlinputgt
- .
- lt/wsdloperationgt
- lt/wsdlbindinggt
- ltwsdlservice name"AdamCalcServiceClassService
"gt - ltwsdlport binding"implAdamCalcServiceSoap
Binding" name"AdamCalcService"gt - ltwsdlsoapaddress location"http//localh
ost8080/axis/services/AdamCalcService"/gt - lt/wsdlportgt
- lt/wsdlservicegt
30WSDL2Java (client stubs)
- Automated Tool to create Java client stub based
on WSDL - java org.apache.axis.wsdl.WSDL2Java
adamcalc.wsdl - What is created?
- AdamCalcServiceClass.java
- Java Interface (fun. declaration of binary2Int,
int2Binary) - AdamCalcServiceClassService.java
- Service Interface (e.g. getAdamCalcServiceAddress)
- AdamCalcServiceClassServiceLocator.java
- Implementation of Service Interface (Automatic)
- AdamCalcServiceSoapBindingStub.java
- Your client will use this to make requests to
Service. - Details available at
- http//ws.apache.org/axis/java/user-guide.html
31What is the purpose of a Stub?
- A Stub contains the code which turns the method
invocations into SOAP calls. - It stands in as a proxy (another term for the
same idea) for the remote service, letting you
call it exactly as if it were a local object.
32Example Client
- import poswolskyfoo.
- public class adamClient
- public static void main(String args)
- try
- AdamCalcServiceClassService service
- new AdamCalcServiceClassServiceLocator()
- AdamCalcServiceClass calc
- service.getAdamCalcService()
- // Convert input binary string to an
integer. - int result calc.binary2Int(args0)
- System.out.println(args0 " is "
result) - catch (Exception e) e.printStackTrace()
-
33Server Creation from WSDL
- We also have tool support to create a server from
the WSDL. - java org.apache.axis.wsdl.WSDL2Java --server-side
--skeletonDeploy true adamcalc.wsdl - Skeleton Description
- The skeleton class is the class that sits between
the Axis engine and the actual service
implementation. Its name here would be - AdamCalcServiceSoapBindingSkeleton.java
- Just need to fill out
- AdamCalcServiceSoapBindingImpl.java
- Hides SOAP details!
- Tool also creates a deploy.wsdd
34Installation of Apache Axis
- At least how I got it to work
35Installing Apache Axis (1/2)
- http//ws.apache.org/axis/java/install.html
- Download axis
- http//www.apache.org/dyn/closer.cgi/ws/axis/1_4
- Download XML Parser (Xerces)
- http//xml.apache.org/dist/xerces-j/
- Download JavaBeans Activation Framwork (JAF)
- http//java.sun.com/products/javabeans/jaf/
- Download JavaMail
- http//java.sun.com/products/javamail/
- Optionally download XML Security
- http//xml.apache.org/security/
36Installing Apache Axis (2/2)
- Copy ltaxisgt/webapps/axis to lttomcatgt/webapps/axis
- ltXgt stands for installation path of X..
- Copy ltaxisgt/lib/ and all jar files from
additional packages to both - lttomcatgt/common/lib
- lttomcatgt\webapps\axis\WEB-INF\lib
- This may be overkill, but this is how I got it to
work. - Restart Tomcat and try to go to
http//localhost8080/axis/ - And verify by clicking Validation
37Running Apache Examples
- My Classpath includes
- ...\axis.jar
- ...\axis-ant.jar
- ...\commons-discovery-0.2.jar
- ...\commons-logging-1.0.4.jar
- ...\jaxrpc.jar
- ...\mail.jar
- ...\resolver.jar
- ...\saaj.jar
- ...\tools.jar
- ...\wsdl4j-1.5.1.jar
- ...\xercesImpl.jar
- ...\xml-apis.jar
- ...\xmlsec-1.2.1.jar
- ...\activation.jar
- ltaxisgt
- For sample code
38Axis Administration
- http//localhost8080/axis
- Axis Admin client
- Java org.apache.axis.client.AdminClient
- Deploy Services
- Remove Services
- List Services
- Etc
39Service Registry
40Java way (UDDI4j)
- UDDI4J is a Java class library that provides an
API that is can be used to interact with a UDDI
registry. This class library generates and parses
messages sent to and received from a UDDI server - http//www.uddi4j.org/
41Examples with Stylus Studio
- Demonstration to search for Services.
- Demonstration to use Services
- Via SOAP messages.
- Other demonstrations
42Please Evaluate Us
- One Vote can make a difference!
43They like meThey like me not
- ITU Course Evaluation
- From Monday 13 Nov. until Monday 27 Nov.
- On your own or lab computer!
- You should have received an email
- Dedicated rooms at ITU
- From 9-12 use Room 4A58
- From 16-18 use Room 2A52
- Why bother?
- Help us/administration ensure you receive a
quality education. - What are we doing right? What are we doing
wrong? - Things can change based on your feedback.
- We can use your feedback to make the rest of the
course better.
44Your Work
45Mini Project 2
- Mini Project 2 is due tomorrow!
- November 15th, 1200
- Questions/Comments/Concerns?
46Exercises
- http//www.itu.dk/courses/IWSJ/E2006/exercises/10
.html