enterprise java v031015web services examples1 http tunnel/monitor $ corej2ee tools tunnel...

56
v031015 Web Services Examples 1 Enterprise Java Web Services Examples HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 – Dhost=localhost –Dport=7001

Upload: anabel-hampton

Post on 12-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 1

EnterpriseJava

Web Services Examples

HTTP Tunnel/Monitor

$ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

Page 2: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 2

EnterpriseJavaHow Can We

Communicate Within J2EE?• RMI

– Easy• When Client/Server are both

Java

– Standard API• Java Centric

– Proprietary Implementations• RMI/IIOP added basic

interoperability, but no transactions or security

• CORBA– Standard API

• Java, C++, Many More

– Interoperable– Difficult

• HTTP– Standard

• TCP/IP

• Primarily Text

– Easy• Sockets and Servlets

• Mail– Header

– Body

– Attachments

• XML– data-oriented

Page 3: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 3

EnterpriseJava

Web Services

• A standard way to request a service and get a reply between two or more independent parties

• Combines – XML

• descriptive• no client/server implementation coupling

– different languages and programming models

• Simple Object Access Protocol (SOAP) schema added to permit messaging

– HTTP/HTTPS• standard protocol that is accepted through firwalls

– Servlets• standard deployment model for code

Page 4: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 4

EnterpriseJava

wsDemoApp$ find . -type f | grep -v CVS./bin/antfiles/wsDemoApp.xml./build.xml./config/Name.xml./config/sayHelloToMe.xml./META-INF/application.xml./soapDemoWEB/build.xml./soapDemoWEB/WEB-INF/classes/corej2ee/examples/soap/GenericServlet.java./soapDemoWEB/WEB-INF/classes/corej2ee/examples/soap/SOAPMsgServlet.java./soapDemoWEB/WEB-INF/classes/corej2ee/examples/soap/SOAPRpcServerImpl.java./soapDemoWEB/WEB-INF/classes/corej2ee/examples/soap/SOAPServlet.java./soapDemoWEB/WEB-INF/classes/corej2ee/examples/soap/XMLServlet.java./soapDemoWEB/WEB-INF/config/soapDemoWEB-soap-msg-dd.xml./soapDemoWEB/WEB-INF/config/soapDemoWEB-soap-rpc-dd.xml./soapDemoWEB/WEB-INF/web.xml./wsDemoUtil/build.xml./wsDemoUtil/corej2ee/examples/jaxm/client/JAXMRpcClient.java./wsDemoUtil/corej2ee/examples/soap/client/SOAPHttpClient.java./wsDemoUtil/corej2ee/examples/soap/client/SOAPRpcClient.java./wsDemoUtil/corej2ee/examples/soap/client/XMLHttpClient.java./wsDemoUtil/META-INF/MANIFEST.MF

Page 5: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 5

EnterpriseJava

Some Server Examples

• GenericServlet– A review/example of deploying a Servlet that accepts data

• XMLServlet– A Servlet that accepts and returns XML using DOM Parsing

• SOAPServlet– A Servlet the accepts and returns SOAP XML using Apache

SOAP

• SOAPMsgServlet– A Servlet that hooks into the Apache SOAP Message Router

• SOAPRpcServerImpl– A generic class that hooks into Apache SOAP RPC Router

Page 6: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 6

EnterpriseJava

Some Client Examples

• XMLHttpClient– Sends and receives and XML Document to an URL using HTTP

• SOAPHttpClient– Sends and receives SOAP Messages using Apache SOAP

• SOAPRpcClient– Invokes SOAP Services using Apache SOAP

• JAXMRpcClient– Invokes SOAP Services using JAXM

Page 7: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 7

EnterpriseJavaReview: Deploy a Servlet

(web.xml)

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>

<servlet> <servlet-name>GenericServlet</servlet-name> <servlet-class>corej2ee.examples.soap.GenericServlet</servlet-class> </servlet>

<servlet-mapping> <servlet-name>GenericServlet</servlet-name> <url-pattern>/requestDump</url-pattern> </servlet-mapping>

Page 8: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 8

EnterpriseJava

Review: Generic Servletpublic class GenericServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { printHeaders(request, response); printDocument(request, response); response.setContentType("text/xml"); //required}protected int printDocument(HttpServletRequest request, HttpServletResponse response) throws IOException { InputStream is = request.getInputStream(); InputStreamReader reader = new InputStreamReader(is); char buffer[] = new char[1024]; int len=0; while ((len=reader.read(buffer, 0, buffer.length)) >= 0) { getOut().print(new String(buffer,0,len)); } return 0;}

Page 9: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 9

EnterpriseJava

XMLHttpClientpublic InputStream xmlRequestReply(InputStream doc) throws IOException { connection_ = getHttpConnection(url_);

//perform a POST connection_.setRequestMethod("POST");

//setup to send an XML document connection_.setRequestProperty( "Content-Type","text/xml; charset=utf-8"); connection_.setDoOutput(true);

//setup to accept a response connection_.setDoInput(true); connection_.setRequestProperty("accept","text/xml"); connection_.setRequestProperty("connection","close");

Page 10: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 10

EnterpriseJava

XMLHttpClient (cont.)

//send the XML document to the server OutputStream os = connection_.getOutputStream(); PrintWriter printer = new PrintWriter(os); InputStreamReader reader = new InputStreamReader(doc); char buffer[] = new char[1024]; int len=0; while ((len=reader.read(buffer, 0, buffer.length)) >= 0) { printer.write(buffer,0,len); } printer.close();

//read the XML document replied if (connection_.getContentLength() > 0) { return connection_.getInputStream(); } else { return null; }}

Page 11: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 11

EnterpriseJava

Invoke the GenericServlet$ corej2ee wsDemoApp requestDump

java -Durl=http://localhost:7001/soapDemo/requestDump -classpath \${XERCES_HOME}\xercesImpl.jar;${WL_HOME}\server\lib\weblogic.jar;\${COREJ2EE_HOME}\lib\wsDemo.jar;${COREJ2EE_HOME}\lib\corej2ee.jar \corej2ee.examples.soap.client.XMLHttpClient${COREJ2EE_HOME}/config/Name.xml'

opening connection to:http://localhost:7001/soapDemo/requestDumpreply=

Name.xml<?xml version='1.0' encoding='UTF-8'?><name xmlns="urn:corej2ee-examples-ws"> <firstName>cat</firstName> <lastName>inhat</lastName></name>

Page 12: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 12

EnterpriseJava

Exchanging Messages

Request

POST /soapDemo/requestDump HTTP/1.1

Content-Type: text/xml; charset=utf-8

accept: text/xml

connection: close

User-Agent: Java1.3.1_02

Host: localhost:7001

Content-length: 132

<?xml version="1.0"?>

<name xmlns="urn:corej2ee-examples-ws">

<firstName>cat</firstName>

<lastName>inhat</lastName>

</name>

Reply

HTTP/1.1 200 OK

Date: Wed, 16 Oct 2002 04:02:43 GMT

Server: WebLogic WebLogic Server 7.0 Thu Jun 20 11:47:11 PDT 2002 190955

Content-Length: 0

Content-Type: text/xml

Connection: Close

Page 13: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 13

EnterpriseJavaXMLHttpClient -> GenericServlet

Output----- BEGIN: (POST) Generic Servlet Invoked ------Content-Type : text/xml; charset=utf-8accept : text/xmlconnection : closeUser-Agent : Java1.3.1_02Host : localhost:7001Content-length : 132- - - begin: text document - - -<?xml version="1.0"?><name xmlns="urn:corej2ee-examples-ws"> <firstName>cat</firstName> <lastName>inhat</lastName></name>- - - end: text document - - ------ END: (POST) Generic Servlet Invoked ------

Page 14: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 14

EnterpriseJavaInserting XML Knowledge

into Servlet (XMLServlet)protected int printDocument(HttpServletRequest request, HttpServletResponse response) throws IOException { try { if (request.getContentLength() == 0) { return 0; } InputStream is = request.getInputStream(); DocumentBuilder parser = dbf_.newDocumentBuilder(); Document doc = parser.parse(is); OutputFormat format = new OutputFormat("XML",null,true); XMLSerializer serializer = new XMLSerializer(getOut(), format); serializer.serialize(doc); } catch (… return 0;}

response.setContentType("text/xml"); //requiredresponse.getWriter().print("<reply>hello</reply>");

Page 15: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 15

EnterpriseJava

Invoke the XMLServlet$ corej2ee wsDemoApp xmlDump

java -Durl=http://localhost:7001/soapDemo/xmlDump -classpath \${XERCES_HOME}\xercesImpl.jar;${WL_HOME}\server\lib\weblogic.jar;\${COREJ2EE_HOME}\lib\wsDemo.jar;${COREJ2EE_HOME}\lib\corej2ee.jar \corej2ee.examples.soap.client.XMLHttpClient${COREJ2EE_HOME}/config/Name.xml'

opening connection to:http://localhost:7001/soapDemo/xmlDumpreply=<?xml version="1.0"?><reply>hello</reply>

Name.xml<?xml version='1.0' encoding='UTF-8'?><name xmlns="urn:corej2ee-examples-ws"> <firstName>cat</firstName> <lastName>inhat</lastName></name>

Page 16: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 16

EnterpriseJava

Exchanging Messages

Request

POST /soapDemo/xmlDump HTTP/1.1

Content-Type: text/xml; charset=utf-8

accept: text/xml

connection: close

User-Agent: Java1.3.1_02

Host: localhost:7001

Content-length: 132

<?xml version="1.0"?>

<name xmlns="urn:corej2ee-examples-ws">

<firstName>cat</firstName>

<lastName>inhat</lastName>

</name>

Reply

HTTP/1.1 200 OK

Date: Wed, 16 Oct 2002 06:22:03 GMT

Server: WebLogic WebLogic Server 7.0 Thu Jun 20 11:47:11 PDT 2002 190955

Content-Length: 20

Content-Type: text/xml

Connection: Close

<reply>hello</reply>

Page 17: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 17

EnterpriseJava

XMLHttpClient -> XMLServlet Output

----- BEGIN: (POST) XML Servlet Invoked ------Content-Type : text/xml; charset=utf-8accept : text/xmlconnection : closeUser-Agent : Java1.3.1_02Host : localhost:7001Content-length : 132- - - begin: xml parsed document - - -<?xml version="1.0"?><name xmlns="urn:corej2ee-examples-ws"> <firstName>cat</firstName> <lastName>inhat</lastName></name>- - - end: xml parsed document - - ------ END: (POST) XML Servlet Invoked ------

Page 18: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 18

EnterpriseJava

Build SOAP Messages

• SOAP Message– SOAP Envelope

• SOAP Header (optional)

• SOAP Body

Page 19: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 19

EnterpriseJava

SOAPHttpClientpublic BufferedReader soapRequestReply(String to, Document doc) throws SOAPException { Element toElement = doc.createElement("to"); Text toText = doc.createTextNode(to); toElement.appendChild(toText);

Element headerElement = doc.createElementNS("urn:corej2ee-examples-soap", "header"); headerElement.appendChild(toElement);

Vector soapHeaderEntries = new Vector(); soapHeaderEntries.add(headerElement); Header soapHeader = new Header(); soapHeader.setHeaderEntries(soapHeaderEntries);

Vector soapBodyEntries = new Vector(); soapBodyEntries.add(doc.getDocumentElement()); Body soapBody = new Body(); soapBody.setBodyEntries(soapBodyEntries);

BuildHeader

BuildBody

Page 20: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 20

EnterpriseJava

SOAPHttpClient (cont.) //build the envelope of the SOAP Request Envelope soapEnvelope = new Envelope(); soapEnvelope.setHeader(soapHeader); soapEnvelope.setBody(soapBody);

//build the SOAP Message Message soapMessage = new Message(); log("sending SOAP request to:" + url_); soapMessage.send(url_, "urn:corej2ee-examples-soap", soapEnvelope);

log("getting reply"); SOAPTransport soapTransport = soapMessage.getSOAPTransport(); BufferedReader breader = soapTransport.receive(); return breader;}

BuildEnvelope

Build/SendMessage,

GetReply

Page 21: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 21

EnterpriseJavaInvoke the XMLServlet

using SOAPHttpClient$ corej2ee wsDemoApp soapXmlDump

java -Durl=http://localhost:7001/soapDemo/soapXmlDump -classpath \${XERCES_HOME}\xercesImpl.jar;${WL_HOME}\server\lib\weblogic.jar;\${COREJ2EE_HOME}\lib\wsDemo.jar;${COREJ2EE_HOME}\lib\corej2ee.jar \corej2ee.examples.soap.client.SOAPHttpClient${COREJ2EE_HOME}/config/Name.xml'

sending SOAP request to:http://localhost:7001/soapDemo/soapXmlDumpgetting reply<?xml version="1.0"?><reply>hello</reply> Name.xml

<?xml version='1.0' encoding='UTF-8'?><name xmlns="urn:corej2ee-examples-ws"> <firstName>cat</firstName> <lastName>inhat</lastName></name>

Page 22: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 22

EnterpriseJava

Exchanging Messages

Request

POST /soapDemo/xmlDump HTTP/1.0

Host: localhost:7001

Content-Type: text/xml; charset=utf-8

Content-Length: 462

SOAPAction: "urn:corej2ee-examples-soap"

<?xml version='1.0' encoding='UTF-8'?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<SOAP-ENV:Header>

<header><to>drseuss</to></header>

</SOAP-ENV:Header>

<SOAP-ENV:Body>

<name xmlns="urn:corej2ee-examples-ws">

<firstName>cat</firstName>

<lastName>inhat</lastName>

</name>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

Reply

HTTP/1.0 200 OK

Date: Wed, 16 Oct 2002 06:46:53 GMT

Server: WebLogic WebLogic Server 7.0 Thu Jun 20 11:47:11 PDT 2002 190955

Content-Length: 20

Content-Type: text/xml

Connection: Close

<reply>hello</reply>

Page 23: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 23

EnterpriseJava

SOAPHttpClient ->XMLServlet Output----- BEGIN: (POST) XML Servlet Invoked ------Host : localhost:7001Content-Type : text/xml; charset=utf-8Content-Length : 462SOAPAction : "urn:corej2ee-examples-soap"- - - begin: xml parsed document - - -<?xml version="1.0"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <SOAP-ENV:Header> <header> <to>drseuss</to> </header> </SOAP-ENV:Header> <SOAP-ENV:Body> <name xmlns="urn:corej2ee-examples-ws"> <firstName>cat</firstName> <lastName>inhat</lastName> </name> </SOAP-ENV:Body></SOAP-ENV:Envelope>- - - end: xml parsed document - - ------ END: (POST) XML Servlet Invoked ------

Page 24: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 24

EnterpriseJavaAdd SOAP Smarts to Servlet

SOAPServletprotected int printRequest(HttpServletRequest request, HttpServletResponse response) throws IOException { try { if (request.getContentLength() == 0) { return 0; } InputStream is = request.getInputStream(); DocumentBuilder parser = XMLParserUtils.getXMLDocBuilder(); Document doc = parser.parse(is);

Envelope soapEnvelope = Envelope.unmarshall(doc.getDocumentElement()); Header soapHeader = soapEnvelope.getHeader(); Body soapBody = soapEnvelope.getBody();

Writer writer = new OutputStreamWriter(getOut()); getOut().println("- soap headers -"); print(writer, soapHeader.getHeaderEntries()); writer.write("\n"); writer.flush(); getOut().println("- soap body -"); print(writer, soapBody.getBodyEntries()); writer.flush(); } catch (...

Page 25: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 25

EnterpriseJavaInvoke the SOAPServlet

using SOAPHttpClient$ corej2ee wsDemoApp soapDump

java -Durl=http://localhost:7001/soapDemo/soapDump -classpath \${XERCES_HOME}\xercesImpl.jar;${WL_HOME}\server\lib\weblogic.jar;\${COREJ2EE_HOME}\lib\wsDemo.jar;${COREJ2EE_HOME}\lib\corej2ee.jar \corej2ee.examples.soap.client.SOAPHttpClient${COREJ2EE_HOME}/config/Name.xml'

sending SOAP request to:http://localhost:7001/soapDemo/soapDumpgetting reply<?xml version="1.0"?><reply>hello</reply> Name.xml

<?xml version='1.0' encoding='UTF-8'?><name xmlns="urn:corej2ee-examples-ws"> <firstName>cat</firstName> <lastName>inhat</lastName></name>

Page 26: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 26

EnterpriseJava

SOAPHttpClient ->SOAPServlet Output

----- BEGIN: (POST) SOAP Servlet Invoked ------Host : localhost:7001Content-Type : text/xml; charset=utf-8Content-Length : 462SOAPAction : "urn:corej2ee-examples-soap"- - - begin: soap request - - -- soap headers -<header><to>drseuss</to></header>- soap body -<name xmlns="urn:corej2ee-examples-ws"> <firstName>cat</firstName> <lastName>inhat</lastName></name>- - - end: xml parsed document - - ------ END: (POST) SOAP Servlet Invoked ------

Page 27: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 27

EnterpriseJavaAdd SOAP Messaging

(web.xml)<servlet> <servlet-name>MessageRouter</servlet-name> <display-name>Apache-SOAP Message Router</display-name> <servlet-class>org.apache.soap.server.http.MessageRouterServlet </servlet-class> <init-param> <param-name>faultListener</param-name> <param-value>org.apache.soap.server.DOMFaultListener</param-value> </init-param></servlet>

<servlet-mapping> <servlet-name>MessageRouter</servlet-name> <url-pattern>/servlet/messagerouter</url-pattern></servlet-mapping>

Page 28: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 28

EnterpriseJavaAdd SOAP Messaging

(soapDemoWEB-soap-msg-dd.xml)<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn:corej2ee-examples-soap-msg" type="message"> <isd:provider type="java" scope="Application" methods="sayHelloToMe"> <isd:java class="corej2ee.examples.soap.SOAPMsgServlet" static="false"/> </isd:provider> <isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener></isd:service>

$ corej2ee wsDemoApp soapdeploy$ java -classpath “${COREJ2EE_ROOT}\software\xerces-2_2_0\xercesImpl.jar;\

${WL_HOME}\server\lib\weblogic.jar;\${COREJ2EE_ROOT}\software\soap-2_3_1\lib\soap.jar \org.apache.soap.server.ServiceManagerClienthttp://localhost:8001/soapDemo/servlet/rpcrouterdeploy${COREJ2EE_HOME}/config/soapDemo-WEB-soap-msg-dd.xml

Page 29: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 29

EnterpriseJava

SOAPMsgServlet

public void sayHelloToMe(Envelope envelope, SOAPContext request, SOAPContext response) throws SOAPException {

getOut().println("- - - SOAPMsgServlet:sayHelloToMe Called - - -"); PrintWriter writer = new PrintWriter(getOut());

Body soapBody = envelope.getBody(); Iterator itr=soapBody.getBodyEntries().iterator(); Element rootElement = (Element)itr.next(); String firstName = DOMUtil.getFirstValue(rootElement, "firstName"); String lastName = DOMUtil.getFirstValue(rootElement, "lastName");

String reply = "hello " + firstName + " " + lastName + " from SOAPMsgServlet"; getOut().println(reply);

Page 30: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 30

EnterpriseJava

SOAPMsgServlet (cont.)

Response soapResponse = Response.extractFromEnvelope( envelope, SOAPMappingRegistry.getBaseRegistry(""), request); soapResponse.setReturnValue(new Parameter("reply", String.class, reply, null)); envelope = soapResponse.buildEnvelope(); try { StringWriter buffer = new StringWriter(); envelope.marshall(buffer, SOAPMappingRegistry.getBaseRegistry(""), response); response.setRootPart(buffer.toString(),"text/xml"); } catch (Exception ex) { throw new SOAPException(Constants.FAULT_CODE_SERVER, "Error writing response:" + ex); }}

Page 31: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 31

EnterpriseJava

SOAPRpcClientpublic String sayHelloToMe(String firstName, String lastName) throws SOAPException, Exception {

Call soapRpcCall = new Call(); soapRpcCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); soapRpcCall.setTargetObjectURI(urn_); soapRpcCall.setMethodName(methodName_);

//setup parameters to the method Vector params = new Vector(); params.addElement(new Parameter("firstName", String.class, firstName, null)); //encodingStyle params.addElement(new Parameter("lastName", String.class, lastName, null)); //encodingStyle soapRpcCall.setParams(params);

Page 32: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 32

EnterpriseJava

SOAPRpcClient (cont.)log("calling:" + soapRpcCall); Response soapRpcResponse = soapRpcCall.invoke(url_, "");//soap action URI //check for error if (soapRpcResponse.generatedFault()) { Fault soapFault = soapRpcResponse.getFault(); throw new Exception("Error saying hello:" + " fault code=" + soapFault.getFaultCode() + ", fault=" + soapFault.getFaultString()); } else { Parameter soapRpcReturnValue = soapRpcResponse.getReturnValue(); return (String)soapRpcReturnValue.getValue(); }}

Page 33: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 33

EnterpriseJavaInvoke the SOAPMsgServlet

using SOAPRpcClient$ corej2ee wsDemoApp msgCall

java -Durl=http://localhost:7001/soapDemo/servlet/messagerouter \-Durn=urn:corej2ee-examples-soap-msg \-DmethodName=sayHelloToMe -classpath \${XERCES_HOME}\xercesImpl.jar;${WL_HOME}\server\lib\weblogic.jar;\${COREJ2EE_HOME}\lib\wsDemo.jar;${COREJ2EE_HOME}\lib\corej2ee.jar \corej2ee.examples.soap.client.SOAPRpcClient cat inhat

calling:[Header=null] [methodName=sayHelloToMe] [targetObjectURI=urn:corej2ee-examples-soap-msg] [encodingStyleURI=http://schemas.xmlsoap.org/soap/encoding/] [SOAPContext=[Parts={}]] [Params={[[name=firstName] [type=class java.lang.String][value=cat] [encodingStyleURI=null]], [[name=lastName] [type=class java.lang.String] [value=inhat] [encodingStyleURI=null]]}]---client.sayHelloToMe(cat, inhat) returned:hello cat inhat from SOAPMsgServlet

Page 34: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 34

EnterpriseJava

Exchanging Messages

Request

POST /soapDemo/servlet/messagerouter HTTP/1.0

Host: localhost:7001

Content-Type: text/xml; charset=utf-8

Content-Length: 527

SOAPAction: ""

<?xml version='1.0' encoding='UTF-8'?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<SOAP-ENV:Body>

<ns1:sayHelloToMe xmlns:ns1="urn:corej2ee-examples-soap-msg" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<firstName xsi:type="xsd:string">cat</firstName>

<lastName xsi:type="xsd:string">inhat</lastName>

</ns1:sayHelloToMe>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

Page 35: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 35

EnterpriseJava

Exchanging Messages

Reply

HTTP/1.0 200 OK

Date: Wed, 16 Oct 2002 07:22:09 GMT

Server: WebLogic WebLogic Server 7.0 Thu Jun 20 11:47:11 PDT 2002 190955

Content-Length: 569

Content-Type: text/xml

Set-Cookie: JSESSIONID=9tThHJhufPWYBmce9ugK8CaMGhtx76LWt1IzZpEKnG5P9cvZIw2r!-32486876; path=/

Cache-control: no-cache="set-cookie"

Connection: Close

<?xml version='1.0' encoding='UTF-8'?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<SOAP-ENV:Body>

<ns1:sayHelloToMeResponse xmlns:ns1="urn:corej2ee-examples-soap-msg" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<reply xsi:type="xsd:string">hello cat inhat from SOAPMsgServlet</reply>

<lastName xsi:type="xsd:string">inhat</lastName>

</ns1:sayHelloToMeResponse>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

Page 36: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 36

EnterpriseJavaAdd SOAP RPC

(soapDemoWEB-soap-rpc-dd.xml)<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn:corej2ee-examples-soap-rpc"> <isd:provider type="java" scope="Application" methods="sayHelloToMe"> <isd:java class="corej2ee.examples.soap.SOAPRpcServerImpl" static="false"/> </isd:provider> <isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener></isd:service>

java -classpath “${COREJ2EE_ROOT}\software\xerces-2_2_0\xercesImpl.jar;\${WL_HOME}\server\lib\weblogic.jar;\${COREJ2EE_ROOT}\software\soap-2_3_1\lib\soap.jar \org.apache.soap.server.ServiceManagerClienthttp://localhost:8001/soapDemo/servlet/rpcrouterdeploy${COREJ2EE_HOME}/config/soapDemo-WEB-soap-rpc-dd.xml

Page 37: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 37

EnterpriseJavaAdd SOAP RPC

(web.xml)<servlet> <servlet-name>RpcRouter</servlet-name> <display-name>Apache-SOAP RPC Router</display-name> <servlet-class>org.apache.soap.server.http.RPCRouterServlet </servlet-class> <init-param> <param-name>faultListener</param-name> <param-value>org.apache.soap.server.DOMFaultListener</param-value> </init-param></servlet>

<servlet-mapping> <servlet-name>RpcRouter</servlet-name> <url-pattern>/servlet/rpcrouter</url-pattern></servlet-mapping>

Page 38: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 38

EnterpriseJava

SOAPRpcServerImpl

package corej2ee.examples.soap;

public class SOAPRpcServerImpl { public String sayHelloToMe(String firstName, String lastName) { String response = "hello " + firstName + " " + lastName + " from SOAPRpcServerImpl"; System.out.println("- - - SOAPRpcServerImpl: sayHelloToMe Called - - -"); System.out.println(response); return response; }}

Page 39: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 39

EnterpriseJavaInvoking SOAPRpcServerImpl

with SOAPRpcClient$ corej2ee wsDemoApp rpcCall

java -Durl=http://localhost:7001/soapDemo/servlet/rpcrouter \-Durn=urn:corej2ee-examples-soap-rpc -DmethodName=sayHelloToMe'-classpath ${COREJ2EE_ROOT}\software\xerces-2_2_0\xercesImpl.jar;\${WL_HOME}\server\lib\weblogic.jar;\${COREJ2EE_ROOT}\software\soap-2_3_1\lib\soap.jar;\${COREJ2EE_HOME}\lib\wsDemo.jarcorej2ee.examples.soap.client.SOAPRpcClient cat inhat

calling:[Header=null] [methodName=sayHelloToMe] [targetObjectURI=urn:corej2ee-examples-soap-rpc] [encodingStyleURI=http://schemas.xmlsoap.org/soap/encoding/] [SOAPContext=[Parts={}]] [Params={[[name=firstName] [type=class java.lang.String][value=cat] [encodingStyleURI=null]], [[name=lastName] [type=class java.lang.String] [value=inhat] [encodingStyleURI=null]]}]---client.sayHelloToMe(cat, inhat) returned:hello cat inhat from SOAPRpcServerImpl

Page 40: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 40

EnterpriseJavaAdding a JAXM Client

(JAXMRpcClient)public JAXMRpcClient(String url) throws SOAPException {

url_ = url; connectionFactory_ = SOAPConnectionFactory.newInstance(); connection_ = connectionFactory_.createConnection(); messageFactory_ = MessageFactory.newInstance();}

public String sayHelloToMe(String firstName, String lastName) throws SOAPException {

SOAPMessage msg = messageFactory_.createMessage(); SOAPPart part = msg.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); envelope.addNamespaceDeclaration( "xsi", "http://www.w3.org/2001/XMLSchema-instance"); envelope.addNamespaceDeclaration( "xsd", "http://www.w3.org/2001/XMLSchema");

Page 41: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 41

EnterpriseJavaAdding a JAXM Client

(JAXMRpcClient)//create the body part of the message SOAPBody body = envelope.getBody(); Name encodingStyle = envelope.createName("encodingStype", "soap-env", null); Name methodName = envelope.createName(methodName_,"ns1",urn_); SOAPElement methodElement= body.addChildElement(methodName); methodElement.setEncodingStyle( "http://schemas.xmlsoap.org/soap/encoding/"); Name fnameName = envelope.createName("firstName"); SOAPElement fnameElement= methodElement.addChildElement(fnameName); fnameElement.addTextNode(firstName); fnameElement.addAttribute( envelope.createName("xsi:type"), "xsd:string"); Name lnameName = envelope.createName("lastName"); SOAPElement lnameElement= methodElement.addChildElement(lnameName); lnameElement.addTextNode(lastName); lnameElement.addAttribute( envelope.createName("xsi:type"), "xsd:string");

Page 42: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 42

EnterpriseJavaAdding a JAXM Client

(JAXMRpcClient)URLEndpoint endpoint = new URLEndpoint(url_); log("sending JAXM request to:" + endpoint);

SOAPMessage reply = connection_.call(msg, endpoint); return getResult(reply.getSOAPPart().getEnvelope().getBody());}String getResult(SOAPElement element) { String value = element.getValue(); if (value != null && value.trim().length() > 0) { return value; } for (Iterator itr=element.getChildElements(); itr.hasNext();){ Object object = itr.next(); if (object instanceof SOAPElement) { return getResult((SOAPElement)object); } } return null;}

Page 43: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 43

EnterpriseJavaInvoke the SOAPRpcServerImpl

use the JAXMCRpcClient $ corej2ee wsDemoApp jaxmCalljava.exe -Djavax.xml.soap.SOAPConnectionFactory=com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnectionFactory \

-Durl=http://localhost:7001/soapDemo/servlet/rpcrouter \-Durn=urn:corej2ee-examples-soap-rpc \-DmethodName=sayHelloToMe \-classpath ${WSDP_HOME}\common\lib\saaj-api.jar;\${WSDP_HOME}\common\lib\saaj-ri.jar;\${WSDP_HOME}\common\lib\mail.jar;\${WSDP_HOME}\common\lib\jaxp-api.jar;\${WSDP_HOME}\common\lib\activation.jar;\${WSDP_HOME}\common\lib\commons-logging.jar;\${WSDP_HOME}\common\lib\dom4j.jar;\${WSDP_HOME}\common\lib\jaxm-api.jar;\${WSDP_HOME}\common\lib\jaxm-runtime.jar;\${WSDP_HOME}\common\endorsed\sax.jar;\${COREJ2EE_HOME}\lib\wsDemo.jar;${COREJ2EE_HOME}\lib\corej2ee.jarcorej2ee.examples.jaxm.client.JAXMRpcClient cat inhat

sending JAXM request to:http://localhost:7001/soapDemo/servlet/rpcrouterWarning: Error occurred using JAXP to load a SAXParser. Will use Aelfred instead---client.sayHelloToMe(cat, inhat) returned:hello cat inhat from SOAPRpcServerImpl

Page 44: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 44

EnterpriseJava

Axis Example

• Services– HelloWebService

• Dynamically Deployed (.jws) Web Service

– CalculatorImpl• Statically Deployed (WSDL) Service

• Clients– HelloWebClient

• Uses dynamic interface

– CalculatorClient• Uses WSDL Stubs

Page 45: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 45

EnterpriseJava

AxisDemoApp Source• Create EAR and register axisDemoWEB as a memberaxisDemoApp/META-INF/application.xml• Create dynamically deployed implementationsaxisDemoApp/axisDemoWEB/HelloWebService.jws• Create an interface to generate WSDL and an implementationaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/Calculator.javaaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/impl/CalculatorImpl.java•Create server-side skeletons from WSDLaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/axisimpl/Calculator.javaaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/axisimpl/CalculatorService.javaaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/axisimpl/CalculatorServiceLocator.javaaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/axisimpl/CalculatorSoapBindingImpl.javaaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/axisimpl/CalculatorSoapBindingStub.javaaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/axisimpl/deploy.wsddaxisDemoApp/axisDemoUtil/corej2ee/examples/ws/calculator/axisimpl/undeploy.wsdd• Register any resources required by deployed services in deployment descriptor(s)axisDemoApp/axisDemoWEB/WEB-INF/web.xml• Update axis-managed security files (not done)axisDemoApp/axisDemoWEB/WEB-INF/perms.lstaxisDemoApp/axisDemoWEB/WEB-INF/users.lst• Create Ant run scriptaxisDemoApp/bin/antfiles/axisDemoApp.xml

Page 46: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 46

EnterpriseJava

axisDemoApp EAR$ find axisDemoApp -type faxisDemoApp/axisDemo.jaraxisDemoApp/axisDemoWEB/HelloWebService.jwsaxisDemoApp/axisDemoWEB/WEB-INF/jwsClasses/HelloWebService.classaxisDemoApp/axisDemoWEB/WEB-INF/lib/axis-ant.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/axis.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/axisDemo.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/commons-discovery.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/commons-logging.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/jaxrpc.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/log4j-1.2.4.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/saaj.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/tools.jaraxisDemoApp/axisDemoWEB/WEB-INF/lib/wsdl4j.jaraxisDemoApp/axisDemoWEB/WEB-INF/perms.lstaxisDemoApp/axisDemoWEB/WEB-INF/users.lstaxisDemoApp/axisDemoWEB/WEB-INF/web.xmlaxisDemoApp/axisDemoWEB.waraxisDemoApp/META-INF/application.xml

Page 47: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 47

EnterpriseJava

axisDemo.jar$ jar tf axisDemoApp/axisDemoWEB/WEB-INF/lib/axisDemo.jar

--- not used here, but would have been self authored when needed ---META-INF/MANIFEST.MF

--- autogenerated from WSDL ---corej2ee/examples/ws/calculator/axisimpl/Calculator.classcorej2ee/examples/ws/calculator/axisimpl/CalculatorService.classcorej2ee/examples/ws/calculator/axisimpl/CalculatorServiceLocator.classcorej2ee/examples/ws/calculator/axisimpl/CalculatorSoapBindingStub.class

--- autogenerated and then slightly modified to connect to CalculatorImpl ---corej2ee/examples/ws/calculator/axisimpl/CalculatorSoapBindingImpl.class

--- self authored : contain implementation ---corej2ee/examples/ws/calculator/Calculator.classcorej2ee/examples/ws/calculator/impl/CalculatorImpl.class

Page 48: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 48

EnterpriseJava

axisClientLib Source

• Create a client for the dynamically deployed (no WSDL) serviceaxisDemoClientLib/corej2ee/examples/ws/client/HelloWebServiceClient.java

• Generate the stubs from the WSDLaxisDemoClientLib/corej2ee/examples/ws/calculator/axisclient/Calculator.javaaxisDemoClientLib/corej2ee/examples/ws/calculator/axisclient/CalculatorService.javaaxisDemoClientLib/corej2ee/examples/ws/calculator/axisclient/CalculatorServiceLocator.javaaxisDemoClientLib/corej2ee/examples/ws/calculator/axisclient/CalculatorSoapBindingStub.java • Create a client for the WSDL serviceaxisDemoClientLib/corej2ee/examples/ws/calculator/client/CalculatorClient.java

• Create an Ant run scriptaxisDemoClientLib/bin/antfiles/axisDemoClient.xml

Page 49: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 49

EnterpriseJava

Other axisDemoApp files

• Generated by axisDemoApp/build.xmldeploy/wsdl/Calculator.wsdl

• Generated/Copied by axisDemoApp/build.xmldeploy/bin/axis_wsdd/deploy_Calculator.wsdd deploy/bin/axis_wsdd/undeploy_Calculator.wsdd

Page 50: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 50

EnterpriseJava

HelloWebService.jws

public class HelloWebService { public String sayHello() { return "Hello From Web Service"; }

public String sayHelloToMe(String name) { return "Hello " + name; }}

Page 51: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 51

EnterpriseJava

Calculator/CalculatorImpl.javapackage corej2ee.examples.ws.calculator;

public interface Calculator { int add(int val1, int val2); int subtract(int val1, int val2);}

package corej2ee.examples.ws.calculator.impl;import corej2ee.examples.ws.calculator.Calculator;

public class CalculatorImpl implements Calculator { public int add(int val1, int val2) { return val1 + val2; }

public int subtract(int val1, int val2) { return val1 - val2; }}

Page 52: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 52

EnterpriseJava

HelloWebServiceClient

package corej2ee.examples.ws.client;

import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.encoding.XMLType;import org.apache.axis.utils.Options;import javax.xml.rpc.ParameterMode;

public class HelloWebServiceClient { static String hostport_ = System.getProperty("hostport","localhost:7001"); public static void main(String [] args) throws Exception { Options options = new Options(args);

String name = "anonymous"; String endpoint = "http://" + hostport_ + "/axisDemo/HelloWebService.jws";

Page 53: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 53

EnterpriseJava

HelloWebServiceClient

// Make the call Service service = new Service(); Call call = (Call) service.createCall();

//sayHello call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName( "sayHello" ); call.setReturnType(XMLType.XSD_STRING); String ret = (String) call.invoke( new Object [] {}); System.out.println("sayHello returned: " + ret);}

Page 54: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 54

EnterpriseJava

Calculator Clientpackage corej2ee.examples.ws.calculator.client;

import corej2ee.examples.ws.calculator.axisclient.Calculator;import corej2ee.examples.ws.calculator.axisclient.CalculatorService;import corej2ee.examples.ws.calculator.axisclient.CalculatorServiceLocator;

public class CalculatorClient { public static void main(String args[]) throws Exception { CalculatorServiceLocator calcService = new CalculatorServiceLocator(); Calculator calc = calcService.getCalculator();

for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { System.out.println("" + i + "+" + j + "=" + calc.add(i,j)); System.out.println("" + i + "-" + j + "=" + calc.subtract(i,j)); } } }}

Page 55: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 55

EnterpriseJava

Generate WSDL Elements• Generate WSDL Stubs (axisDemoUtil)$ ant wsdl_axiswsdl_: [echo] building WSDL file Calculator.wsdl

• Generate WSDL Skeletons (axisDemoUtil)$ ant wsdlimpl

_axisimpl_: [echo] generating axisimpl for Calculator

• Modify BindingImpl.java (axisDemoUtil)• Generate WSDL Stubs (axisDemoClientUtil)$ ant wsdlclient_axisclient_: [echo] generating wsdlclient for Calculator

Page 56: Enterprise Java v031015Web Services Examples1 HTTP Tunnel/Monitor $ corej2ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001

v031015 Web Services Examples 56

EnterpriseJava

Deploy/Run Example (EAR Directory)$ ant stagedeploy_stagedeploy_: [java] executing deploy [java] Activate application axisDemoAppStage on myserver

$ ant wsdeploy_wsdeploy_: [echo] http://localhost:7001/axisDemo/services/AdminService [java] [INFO] AdminClient - -Processing file C:\cygwin\home\jcstaff\proj\corej2ee\src\axisDemoApp/../../deploy/bin/axis_wsdd/deploy_Calculator.wsdd [java] [INFO] AdminClient - -<Admin>Done processing</Admin>

$ corej2ee axisDemoClientHelloWebServiceClient:sayHello returned: Hello From Web Service

CalculatorClient:0+0=0...2-2=0