7ba4913a3af1d88bdc773e12a195c427.ppt
- Количество слайдов: 56
Enterprise Java Web Services Examples HTTP Tunnel/Monitor $ corej 2 ee tools tunnel –Dlocalport=8001 –Dhost=localhost –Dport=7001 v 031015 Web Services Examples 1
How Can We Communicate Within J 2 EE? • RMI Enterprise Java • HTTP – Easy – Standard • When Client/Server are both Java – Standard API • TCP/IP • Primarily Text – Easy • Java Centric • Sockets and Servlets – Proprietary Implementations • RMI/IIOP added basic interoperability, but no transactions or security • CORBA • Mail – Header – Body – Attachments • XML – Standard API • Java, C++, Many More – data-oriented – Interoperable – Difficult v 031015 Web Services Examples 2
Web Services Enterprise Java • 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 v 031015 Web Services Examples 3
ws. Demo. App Enterprise Java $ find. -type f | grep -v CVS. /bin/antfiles/ws. Demo. App. xml. /build. xml. /config/Name. xml. /config/say. Hello. To. Me. xml. /META-INF/application. xml. /soap. Demo. WEB/build. xml. /soap. Demo. WEB/WEB-INF/classes/corej 2 ee/examples/soap/Generic. Servlet. java. /soap. Demo. WEB/WEB-INF/classes/corej 2 ee/examples/soap/SOAPMsg. Servlet. java. /soap. Demo. WEB/WEB-INF/classes/corej 2 ee/examples/soap/SOAPRpc. Server. Impl. java. /soap. Demo. WEB/WEB-INF/classes/corej 2 ee/examples/soap/SOAPServlet. java. /soap. Demo. WEB/WEB-INF/classes/corej 2 ee/examples/soap/XMLServlet. java. /soap. Demo. WEB/WEB-INF/config/soap. Demo. WEB-soap-msg-dd. xml. /soap. Demo. WEB/WEB-INF/config/soap. Demo. WEB-soap-rpc-dd. xml. /soap. Demo. WEB/WEB-INF/web. xml. /ws. Demo. Util/build. xml. /ws. Demo. Util/corej 2 ee/examples/jaxm/client/JAXMRpc. Client. java. /ws. Demo. Util/corej 2 ee/examples/soap/client/SOAPHttp. Client. java. /ws. Demo. Util/corej 2 ee/examples/soap/client/SOAPRpc. Client. java. /ws. Demo. Util/corej 2 ee/examples/soap/client/XMLHttp. Client. java. /ws. Demo. Util/META-INF/MANIFEST. MF v 031015 Web Services Examples 4
Some Server Examples Enterprise Java • Generic. Servlet – 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 • SOAPMsg. Servlet – A Servlet that hooks into the Apache SOAP Message Router • SOAPRpc. Server. Impl – A generic class that hooks into Apache SOAP RPC Router v 031015 Web Services Examples 5
Some Client Examples Enterprise Java • XMLHttp. Client – Sends and receives and XML Document to an URL using HTTP • SOAPHttp. Client – Sends and receives SOAP Messages using Apache SOAP • SOAPRpc. Client – Invokes SOAP Services using Apache SOAP • JAXMRpc. Client – Invokes SOAP Services using JAXM v 031015 Web Services Examples 6
Review: Deploy a Servlet (web. xml) Enterprise Java <!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-name>Generic. Servlet</servlet-name> <servlet-class>corej 2 ee. examples. soap. Generic. Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Generic. Servlet</servlet-name> <url-pattern>/request. Dump</url-pattern> </servlet-mapping> v 031015 Web Services Examples 7
Review: Generic Servlet Enterprise Java public class Generic. Servlet extends Http. Servlet { v 031015 public void do. Post(Http. Servlet. Request request, Http. Servlet. Response response) throws IOException { print. Headers(request, response); print. Document(request, response); response. set. Content. Type("text/xml"); //required } protected int print. Document(Http. Servlet. Request request, Http. Servlet. Response response) throws IOException { Input. Stream is = request. get. Input. Stream(); Input. Stream. Reader reader = new Input. Stream. Reader(is); char buffer[] = new char[1024]; int len=0; while ((len=reader. read(buffer, 0, buffer. length)) >= 0) { get. Out(). print(new String(buffer, 0, len)); } return 0; } Web Services Examples 8
XMLHttp. Client Enterprise Java public Input. Stream xml. Request. Reply(Input. Stream doc) throws IOException { connection_ = get. Http. Connection(url_); //perform a POST connection_. set. Request. Method("POST"); //setup to send an XML document connection_. set. Request. Property( "Content-Type", "text/xml; charset=utf-8"); connection_. set. Do. Output(true); //setup to accept a response connection_. set. Do. Input(true); connection_. set. Request. Property("accept", "text/xml"); connection_. set. Request. Property("connection", "close"); v 031015 Web Services Examples 9
XMLHttp. Client (cont. ) Enterprise Java //send the XML document to the server Output. Stream os = connection_. get. Output. Stream(); Print. Writer printer = new Print. Writer(os); Input. Stream. Reader reader = new Input. Stream. Reader(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_. get. Content. Length() > 0) { return connection_. get. Input. Stream(); } else { return null; } v 031015 } Web Services Examples 10
Invoke the Generic. Servlet Enterprise Java $ corej 2 ee ws. Demo. App request. Dump java -Durl=http: //localhost: 7001/soap. Demo/request. Dump -classpath ${XERCES_HOME}xerces. Impl. jar; ${WL_HOME}serverlibweblogic. jar; ${COREJ 2 EE_HOME}libws. Demo. jar; ${COREJ 2 EE_HOME}libcorej 2 ee. jar corej 2 ee. examples. soap. client. XMLHttp. Client ${COREJ 2 EE_HOME}/config/Name. xml' opening connection to: http: //localhost: 7001/soap. Demo/request. Dump reply= Name. xml <? xml version='1. 0' encoding='UTF-8'? > <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> v 031015 Web Services Examples 11
Exchanging Messages Request POST /soap. Demo/request. Dump HTTP/1. 1 Content-Type: text/xml; charset=utf-8 accept: text/xml connection: close User-Agent: Java 1. 3. 1_02 Host: localhost: 7001 Content-length: 132 Enterprise Java Reply HTTP/1. 1 200 OK Date: Wed, 16 Oct 2002 04: 02: 43 GMT Server: Web. Logic Server 7. 0 Thu Jun 20 11: 47: 11 PDT 2002 190955 Content-Length: 0 Content-Type: text/xml Connection: Close <? xml version="1. 0"? > <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> v 031015 Web Services Examples 12
XMLHttp. Client -> Generic. Servlet Output Enterprise Java ----- BEGIN: (POST) Generic Servlet Invoked -----Content-Type : text/xml; charset=utf-8 accept : text/xml connection : close User-Agent : Java 1. 3. 1_02 Host : localhost: 7001 Content-length : 132 - - - begin: text document - - <? xml version="1. 0"? > <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> - - - end: text document - - ----- END: (POST) Generic Servlet Invoked -----v 031015 Web Services Examples 13
Inserting XML Knowledge into Servlet (XMLServlet) Enterprise Java protected int print. Document(Http. Servlet. Request request, Http. Servlet. Response response) throws IOException { try { if (request. get. Content. Length() == 0) { return 0; } Input. Stream is = request. get. Input. Stream(); Document. Builder parser = dbf_. new. Document. Builder(); Document doc = parser. parse(is); Output. Format format = new Output. Format("XML", null, true); XMLSerializer serializer = new XMLSerializer(get. Out(), format); serializer. serialize(doc); } catch (… return 0; } response. set. Content. Type("text/xml"); //required response. get. Writer(). print("<reply>hello</reply>"); v 031015 Web Services Examples 14
Invoke the XMLServlet Enterprise Java $ corej 2 ee ws. Demo. App xml. Dump java -Durl=http: //localhost: 7001/soap. Demo/xml. Dump -classpath ${XERCES_HOME}xerces. Impl. jar; ${WL_HOME}serverlibweblogic. jar; ${COREJ 2 EE_HOME}libws. Demo. jar; ${COREJ 2 EE_HOME}libcorej 2 ee. jar corej 2 ee. examples. soap. client. XMLHttp. Client ${COREJ 2 EE_HOME}/config/Name. xml' opening connection to: http: //localhost: 7001/soap. Demo/xml. Dump reply=<? xml version="1. 0"? > <reply>hello</reply> Name. xml <? xml version='1. 0' encoding='UTF-8'? > <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> v 031015 Web Services Examples 15
Exchanging Messages Enterprise Java Request POST /soap. Demo/xml. Dump HTTP/1. 1 Content-Type: text/xml; charset=utf-8 accept: text/xml connection: close User-Agent: Java 1. 3. 1_02 Host: localhost: 7001 Content-length: 132 Reply HTTP/1. 1 200 OK Date: Wed, 16 Oct 2002 06: 22: 03 GMT Server: Web. Logic Server 7. 0 Thu Jun 20 11: 47: 11 PDT 2002 190955 Content-Length: 20 Content-Type: text/xml Connection: Close <? xml version="1. 0"? > <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> <reply>hello</reply> v 031015 Web Services Examples 16
Enterprise Java XMLHttp. Client -> XMLServlet Output ----- BEGIN: (POST) XML Servlet Invoked -----Content-Type : text/xml; charset=utf-8 accept : text/xml connection : close User-Agent : Java 1. 3. 1_02 Host : localhost: 7001 Content-length : 132 - - - begin: xml parsed document - - <? xml version="1. 0"? > <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> - - - end: xml parsed document - - ----- END: (POST) XML Servlet Invoked -----v 031015 Web Services Examples 17
Build SOAP Messages Enterprise Java • SOAP Message – SOAP Envelope • SOAP Header (optional) • SOAP Body v 031015 Web Services Examples 18
SOAPHttp. Client Enterprise Java public Buffered. Reader soap. Request. Reply(String to, Document doc) throws SOAPException { Element to. Element = doc. create. Element("to"); Text to. Text = doc. create. Text. Node(to); to. Element. append. Child(to. Text); Build Header Element header. Element = doc. create. Element. NS("urn: corej 2 ee-examples-soap", "header"); header. Element. append. Child(to. Element); Vector soap. Header. Entries = new Vector(); soap. Header. Entries. add(header. Element); Header soap. Header = new Header(); soap. Header. set. Header. Entries(soap. Header. Entries); Build Body v 031015 Vector soap. Body. Entries = new Vector(); soap. Body. Entries. add(doc. get. Document. Element()); Body soap. Body = new Body(); soap. Body. set. Body. Entries(soap. Body. Entries); Web Services Examples 19
SOAPHttp. Client (cont. ) Enterprise Java //build the envelope of the SOAP Request Envelope soap. Envelope = new Envelope(); soap. Envelope. set. Header(soap. Header); soap. Envelope. set. Body(soap. Body); Build Envelope //build the SOAP Message soap. Message = new Message(); log("sending SOAP request to: " + url_); soap. Message. send(url_, "urn: corej 2 ee-examples-soap", soap. Envelope); Build/Send Message, log("getting reply"); SOAPTransport soap. Transport = soap. Message. get. SOAPTransport(); Buffered. Reader breader = soap. Transport. receive(); return breader; Get Reply } v 031015 Web Services Examples 20
Invoke the XMLServlet using SOAPHttp. Client Enterprise Java $ corej 2 ee ws. Demo. App soap. Xml. Dump java -Durl=http: //localhost: 7001/soap. Demo/soap. Xml. Dump -classpath ${XERCES_HOME}xerces. Impl. jar; ${WL_HOME}serverlibweblogic. jar; ${COREJ 2 EE_HOME}libws. Demo. jar; ${COREJ 2 EE_HOME}libcorej 2 ee. jar corej 2 ee. examples. soap. client. SOAPHttp. Client ${COREJ 2 EE_HOME}/config/Name. xml' sending SOAP request to: http: //localhost: 7001/soap. Demo/soap. Xml. Dump getting reply <? xml version="1. 0"? > Name. xml <reply>hello</reply> <? xml version='1. 0' encoding='UTF-8'? > <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> v 031015 Web Services Examples 21
Exchanging Messages Request POST /soap. Demo/xml. Dump HTTP/1. 0 Host: localhost: 7001 Content-Type: text/xml; charset=utf-8 Content-Length: 462 SOAPAction: "urn: corej 2 ee-examples-soap" <? xml version='1. 0' encoding='UTF-8'? > <SOAP-ENV: Envelope xmlns: SOAPENV="http: //schemas. xmlsoap. org/soap/enve lope/" xmlns: xsi="http: //www. w 3. org/2001/XMLSc hema-instance" xmlns: xsd="http: //www. w 3. org/2001/XMLSc hema"> <SOAP-ENV: Header> <header><to>drseuss</to></header> </SOAP-ENV: Header> v 031015 Enterprise Java <SOAP-ENV: Body> <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> </SOAP-ENV: Body> </SOAP-ENV: Envelope> Reply HTTP/1. 0 200 OK Date: Wed, 16 Oct 2002 06: 46: 53 GMT Server: Web. Logic Server 7. 0 Thu Jun 20 11: 47: 11 PDT 2002 190955 Content-Length: 20 Content-Type: text/xml Connection: Close <reply>hello</reply> Web Services Examples 22
Enterprise Java SOAPHttp. Client ->XMLServlet Output ----- BEGIN: (POST) XML Servlet Invoked -----Host : localhost: 7001 Content-Type : text/xml; charset=utf-8 Content-Length : 462 SOAPAction : "urn: corej 2 ee-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. w 3. org/2001/XMLSchema" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance"> <SOAP-ENV: Header> <header> <to>drseuss</to> </header> </SOAP-ENV: Header> <SOAP-ENV: Body> <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> </SOAP-ENV: Body> </SOAP-ENV: Envelope> - - - end: xml parsed document - - ----- END: (POST) XML Servlet Invoked -----v 031015 Web Services Examples 23
Add SOAP Smarts to Servlet SOAPServlet Enterprise Java protected int print. Request(Http. Servlet. Request request, Http. Servlet. Response response) throws IOException { try { if (request. get. Content. Length() == 0) { return 0; } Input. Stream is = request. get. Input. Stream(); Document. Builder parser = XMLParser. Utils. get. XMLDoc. Builder(); Document doc = parser. parse(is); Envelope soap. Envelope = Envelope. unmarshall(doc. get. Document. Element()); Header soap. Header = soap. Envelope. get. Header(); Body soap. Body = soap. Envelope. get. Body(); v 031015 Writer writer = new Output. Stream. Writer(get. Out()); get. Out(). println("- soap headers -"); print(writer, soap. Header. get. Header. Entries()); writer. write("n"); writer. flush(); get. Out(). println("- soap body -"); print(writer, soap. Body. get. Body. Entries()); Web Services Examples writer. flush(); } catch (. . . 24
Invoke the SOAPServlet using SOAPHttp. Client Enterprise Java $ corej 2 ee ws. Demo. App soap. Dump java -Durl=http: //localhost: 7001/soap. Demo/soap. Dump -classpath ${XERCES_HOME}xerces. Impl. jar; ${WL_HOME}serverlibweblogic. jar; ${COREJ 2 EE_HOME}libws. Demo. jar; ${COREJ 2 EE_HOME}libcorej 2 ee. jar corej 2 ee. examples. soap. client. SOAPHttp. Client ${COREJ 2 EE_HOME}/config/Name. xml' sending SOAP request to: http: //localhost: 7001/soap. Demo/soap. Dump getting reply <? xml version="1. 0"? > Name. xml <reply>hello</reply> <? xml version='1. 0' encoding='UTF-8'? > <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name> v 031015 Web Services Examples 25
Enterprise Java SOAPHttp. Client ->SOAPServlet Output ----- BEGIN: (POST) SOAP Servlet Invoked -----Host : localhost: 7001 Content-Type : text/xml; charset=utf-8 Content-Length : 462 SOAPAction : "urn: corej 2 ee-examples-soap" - - - begin: soap request - - - soap headers <header><to>drseuss</to></header> - soap body <name xmlns="urn: corej 2 ee-examples-ws"> <first. Name>cat</first. Name> <last. Name>inhat</last. Name> </name>- - - end: xml parsed document - - ----- END: (POST) SOAP Servlet Invoked -----v 031015 Web Services Examples 26
Add SOAP Messaging (web. xml) Enterprise Java <servlet> <servlet-name>Message. Router</servlet-name> <display-name>Apache-SOAP Message Router</display-name> <servlet-class>org. apache. soap. server. http. Message. Router. Servlet </servlet-class> <init-param> <param-name>fault. Listener</param-name> <param-value>org. apache. soap. server. DOMFault. Listener</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Message. Router</servlet-name> <url-pattern>/servlet/messagerouter</url-pattern> </servlet-mapping> v 031015 Web Services Examples 27
Add SOAP Messaging (soap. Demo. WEB-soap-msg-dd. xml) Enterprise Java <isd: service xmlns: isd="http: //xml. apache. org/xml-soap/deployment" id="urn: corej 2 ee-examples-soap-msg" type="message"> <isd: provider type="java" scope="Application" methods="say. Hello. To. Me"> <isd: java class="corej 2 ee. examples. soap. SOAPMsg. Servlet" static="false"/> </isd: provider> <isd: fault. Listener>org. apache. soap. server. DOMFault. Listener</isd: fault. Listener> </isd: service> $ corej 2 ee ws. Demo. App soapdeploy $ java -classpath “${COREJ 2 EE_ROOT}softwarexerces-2_2_0xerces. Impl. jar; ${WL_HOME}serverlibweblogic. jar; ${COREJ 2 EE_ROOT}softwaresoap-2_3_1libsoap. jar org. apache. soap. server. Service. Manager. Client http: //localhost: 8001/soap. Demo/servlet/rpcrouter deploy ${COREJ 2 EE_HOME}/config/soap. Demo-WEB-soap-msg-dd. xml v 031015 Web Services Examples 28
SOAPMsg. Servlet Enterprise Java public void say. Hello. To. Me(Envelope envelope, SOAPContext request, SOAPContext response) throws SOAPException { get. Out(). println("- - - SOAPMsg. Servlet: say. Hello. To. Me Called - - -"); Print. Writer writer = new Print. Writer(get. Out()); Body soap. Body = envelope. get. Body(); Iterator itr=soap. Body. get. Body. Entries(). iterator(); Element root. Element = (Element)itr. next(); String first. Name = DOMUtil. get. First. Value(root. Element, "first. Name"); String last. Name = DOMUtil. get. First. Value(root. Element, "last. Name"); String reply = "hello " + first. Name + " " + last. Name + " from SOAPMsg. Servlet"; get. Out(). println(reply); v 031015 Web Services Examples 29
SOAPMsg. Servlet (cont. ) Enterprise Java Response soap. Response = Response. extract. From. Envelope( envelope, SOAPMapping. Registry. get. Base. Registry(""), request); soap. Response. set. Return. Value(new Parameter("reply", String. class, reply, null)); envelope = soap. Response. build. Envelope(); try { String. Writer buffer = new String. Writer(); envelope. marshall(buffer, SOAPMapping. Registry. get. Base. Registry(""), response); response. set. Root. Part(buffer. to. String(), "text/xml"); } catch (Exception ex) { throw new SOAPException(Constants. FAULT_CODE_SERVER, "Error writing response: " + ex); } } v 031015 Web Services Examples 30
SOAPRpc. Client Enterprise Java public String say. Hello. To. Me(String first. Name, String last. Name) throws SOAPException, Exception { Call soap. Rpc. Call = new Call(); soap. Rpc. Call. set. Encoding. Style. URI(Constants. NS_URI_SOAP_ENC); soap. Rpc. Call. set. Target. Object. URI(urn_); soap. Rpc. Call. set. Method. Name(method. Name_); //setup parameters to the method Vector params = new Vector(); params. add. Element(new Parameter("first. Name", String. class, first. Name, null)); //encoding. Style params. add. Element(new Parameter("last. Name", String. class, last. Name, null)); //encoding. Style soap. Rpc. Call. set. Params(params); v 031015 Web Services Examples 31
SOAPRpc. Client (cont. ) Enterprise Java log("calling: " + soap. Rpc. Call); Response soap. Rpc. Response = soap. Rpc. Call. invoke(url_, ""); //soap action URI //check for error if (soap. Rpc. Response. generated. Fault()) { Fault soap. Fault = soap. Rpc. Response. get. Fault(); throw new Exception("Error saying hello: " + " fault code=" + soap. Fault. get. Fault. Code() + ", fault=" + soap. Fault. get. Fault. String()); } else { Parameter soap. Rpc. Return. Value = soap. Rpc. Response. get. Return. Value(); return (String)soap. Rpc. Return. Value. get. Value(); } } v 031015 Web Services Examples 32
Invoke the SOAPMsg. Servlet using SOAPRpc. Client Enterprise Java $ corej 2 ee ws. Demo. App msg. Call java -Durl=http: //localhost: 7001/soap. Demo/servlet/messagerouter -Durn=urn: corej 2 ee-examples-soap-msg -Dmethod. Name=say. Hello. To. Me -classpath ${XERCES_HOME}xerces. Impl. jar; ${WL_HOME}serverlibweblogic. jar; ${COREJ 2 EE_HOME}libws. Demo. jar; ${COREJ 2 EE_HOME}libcorej 2 ee. jar corej 2 ee. examples. soap. client. SOAPRpc. Client cat inhat calling: [Header=null] [method. Name=say. Hello. To. Me] [target. Object. URI=urn: corej 2 ee-ex amples-soap-msg] [encoding. Style. URI=http: //schemas. xmlsoap. org/soap/encoding/] [S OAPContext=[Parts={}]] [Params={[[name=first. Name] [type=class java. lang. String] [value=cat] [encoding. Style. URI=null]], [[name=last. Name] [type=class java. lang. Str ing] [value=inhat] [encoding. Style. URI=null]]}] --client. say. Hello. To. Me(cat, inhat) returned: hello cat inhat from SOAPMsg. Servlet v 031015 Web Services Examples 33
Exchanging Messages Request POST /soap. Demo/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: SOAPENV="http: //schemas. xmlsoap. org/soap/enve lope/" xmlns: xsi="http: //www. w 3. org/2001/XMLSc hema-instance" xmlns: xsd="http: //www. w 3. org/2001/XMLSc hema"> v 031015 Enterprise Java <SOAP-ENV: Body> <ns 1: say. Hello. To. Me xmlns: ns 1="urn: corej 2 eeexamples-soap-msg" SOAPENV: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/"> <first. Name xsi: type="xsd: string">cat</first. Name> <last. Name xsi: type="xsd: string">inhat</last. Name> </ns 1: say. Hello. To. Me> </SOAP-ENV: Body> </SOAP-ENV: Envelope> Web Services Examples 34
Exchanging Messages Reply HTTP/1. 0 200 OK Date: Wed, 16 Oct 2002 07: 22: 09 GMT Server: Web. Logic Server 7. 0 Thu Jun 20 11: 47: 11 PDT 2002 190955 Content-Length: 569 Content-Type: text/xml Set-Cookie: JSESSIONID=9 t. Th. HJhuf. PWYBmce 9 ug. K 8 C a. MGhtx 76 LWt 1 Iz. Zp. EKn. G 5 P 9 cv. ZIw 2 r!32486876; path=/ Cache-control: no-cache="set-cookie" Connection: Close Enterprise Java <SOAP-ENV: Envelope xmlns: SOAPENV="http: //schemas. xmlsoap. org/soap/enve lope/" xmlns: xsi="http: //www. w 3. org/2001/XMLSc hema-instance" xmlns: xsd="http: //www. w 3. org/2001/XMLSc hema"> <SOAP-ENV: Body> <ns 1: say. Hello. To. Me. Response xmlns: ns 1="urn: corej 2 ee-examples-soapmsg" SOAPENV: encoding. Style="http: //schemas. xmlsoap. org/soap/encoding/"> <reply xsi: type="xsd: string">hello cat inhat from SOAPMsg. Servlet</reply> <last. Name <? xml version='1. 0' encoding='UTF-8'? > xsi: type="xsd: string">inhat</last. Name> </ns 1: say. Hello. To. Me. Response> </SOAP-ENV: Body> </SOAP-ENV: Envelope> v 031015 Web Services Examples 35
Add SOAP RPC (soap. Demo. WEB-soap-rpc-dd. xml) Enterprise Java <isd: service xmlns: isd="http: //xml. apache. org/xml-soap/deployment" id="urn: corej 2 ee-examples-soap-rpc"> <isd: provider type="java" scope="Application" methods="say. Hello. To. Me"> <isd: java class="corej 2 ee. examples. soap. SOAPRpc. Server. Impl" static="false"/> </isd: provider> <isd: fault. Listener>org. apache. soap. server. DOMFault. Listener</isd: fault. Listener> </isd: service> java -classpath “${COREJ 2 EE_ROOT}softwarexerces-2_2_0xerces. Impl. jar; ${WL_HOME}serverlibweblogic. jar; ${COREJ 2 EE_ROOT}softwaresoap-2_3_1libsoap. jar org. apache. soap. server. Service. Manager. Client http: //localhost: 8001/soap. Demo/servlet/rpcrouter deploy ${COREJ 2 EE_HOME}/config/soap. Demo-WEB-soap-rpc-dd. xml v 031015 Web Services Examples 36
Add SOAP RPC (web. xml) Enterprise Java <servlet> <servlet-name>Rpc. Router</servlet-name> <display-name>Apache-SOAP RPC Router</display-name> <servlet-class>org. apache. soap. server. http. RPCRouter. Servlet </servlet-class> <init-param> <param-name>fault. Listener</param-name> <param-value>org. apache. soap. server. DOMFault. Listener</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Rpc. Router</servlet-name> <url-pattern>/servlet/rpcrouter</url-pattern> </servlet-mapping> v 031015 Web Services Examples 37
SOAPRpc. Server. Impl Enterprise Java package corej 2 ee. examples. soap; public class SOAPRpc. Server. Impl { public String say. Hello. To. Me(String first. Name, String last. Name) { String response = "hello " + first. Name + " " + last. Name + " from SOAPRpc. Server. Impl"; System. out. println("- - - SOAPRpc. Server. Impl: say. Hello. To. Me Called - - -"); System. out. println(response); return response; } } v 031015 Web Services Examples 38
Invoking SOAPRpc. Server. Impl with SOAPRpc. Client Enterprise Java $ corej 2 ee ws. Demo. App rpc. Call java -Durl=http: //localhost: 7001/soap. Demo/servlet/rpcrouter -Durn=urn: corej 2 ee-examples-soap-rpc -Dmethod. Name=say. Hello. To. Me' -classpath ${COREJ 2 EE_ROOT}softwarexerces-2_2_0xerces. Impl. jar; ${WL_HOME}serverlibweblogic. jar; ${COREJ 2 EE_ROOT}softwaresoap-2_3_1libsoap. jar; ${COREJ 2 EE_HOME}libws. Demo. jar corej 2 ee. examples. soap. client. SOAPRpc. Client cat inhat calling: [Header=null] [method. Name=say. Hello. To. Me] [target. Object. URI=urn: corej 2 ee-ex amples-soap-rpc] [encoding. Style. URI=http: //schemas. xmlsoap. org/soap/encoding/] [S OAPContext=[Parts={}]] [Params={[[name=first. Name] [type=class java. lang. String] [value=cat] [encoding. Style. URI=null]], [[name=last. Name] [type=class java. lang. Str ing] [value=inhat] [encoding. Style. URI=null]]}] --client. say. Hello. To. Me(cat, inhat) returned: hello cat inhat from SOAPRpc. Server. Impl v 031015 Web Services Examples 39
Adding a JAXM Client (JAXMRpc. Client) Enterprise Java public JAXMRpc. Client(String url) throws SOAPException { url_ = url; connection. Factory_ = SOAPConnection. Factory. new. Instance(); connection_ = connection. Factory_. create. Connection(); message. Factory_ = Message. Factory. new. Instance(); } public String say. Hello. To. Me(String first. Name, String last. Name) throws SOAPException { SOAPMessage msg = message. Factory_. create. Message(); SOAPPart part = msg. get. SOAPPart(); SOAPEnvelope envelope = part. get. Envelope(); envelope. add. Namespace. Declaration( "xsi", "http: //www. w 3. org/2001/XMLSchema-instance"); envelope. add. Namespace. Declaration( "xsd", "http: //www. w 3. org/2001/XMLSchema"); v 031015 Web Services Examples 40
Adding a JAXM Client (JAXMRpc. Client) v 031015 Enterprise Java //create the body part of the message SOAPBody body = envelope. get. Body(); Name encoding. Style = envelope. create. Name("encoding. Stype", "soap-env", null); Name method. Name = envelope. create. Name(method. Name_, "ns 1", urn_); SOAPElement method. Element= body. add. Child. Element(method. Name); method. Element. set. Encoding. Style( "http: //schemas. xmlsoap. org/soap/encoding/"); Name fname. Name = envelope. create. Name("first. Name"); SOAPElement fname. Element= method. Element. add. Child. Element(fname. Name); fname. Element. add. Text. Node(first. Name); fname. Element. add. Attribute( envelope. create. Name("xsi: type"), "xsd: string"); Name lname. Name = envelope. create. Name("last. Name"); SOAPElement lname. Element= method. Element. add. Child. Element(lname. Name); lname. Element. add. Text. Node(last. Name); lname. Element. add. Attribute( envelope. create. Name("xsi: type"), "xsd: string"); Web Services Examples 41
Adding a JAXM Client (JAXMRpc. Client) Enterprise Java URLEndpoint endpoint = new URLEndpoint(url_); log("sending JAXM request to: " + endpoint); SOAPMessage reply = connection_. call(msg, endpoint); return get. Result(reply. get. SOAPPart(). get. Envelope(). get. Body()); v 031015 } String get. Result(SOAPElement element) { String value = element. get. Value(); if (value != null && value. trim(). length() > 0) { return value; } for (Iterator itr=element. get. Child. Elements(); itr. has. Next(); ){ Object object = itr. next(); if (object instanceof SOAPElement) { return get. Result((SOAPElement)object); } } return null; } Web Services Examples 42
Invoke the SOAPRpc. Server. Impl use the JAXMCRpc. Client $ corej 2 ee ws. Demo. App jaxm. Call java. exe -Djavax. xml. soap. SOAPConnection. Factory=com. sun. xml. messaging. saaj. client. p 2 p. Ht tp. SOAPConnection. Factory -Durl=http: //localhost: 7001/soap. Demo/servlet/rpcrouter -Durn=urn: corej 2 ee-examples-soap-rpc -Dmethod. Name=say. Hello. To. Me -classpath ${WSDP_HOME}commonlibsaaj-api. jar; ${WSDP_HOME}commonlibsaaj-ri. jar; ${WSDP_HOME}commonlibmail. jar; ${WSDP_HOME}commonlibjaxp-api. jar; ${WSDP_HOME}commonlibactivation. jar; ${WSDP_HOME}commonlibcommons-logging. jar; ${WSDP_HOME}commonlibdom 4 j. jar; ${WSDP_HOME}commonlibjaxm-api. jar; ${WSDP_HOME}commonlibjaxm-runtime. jar; ${WSDP_HOME}commonendorsedsax. jar; ${COREJ 2 EE_HOME}libws. Demo. jar; ${COREJ 2 EE_HOME}libcorej 2 ee. jar corej 2 ee. examples. jaxm. client. JAXMRpc. Client cat inhat sending JAXM request to: http: //localhost: 7001/soap. Demo/servlet/rpcrouter Warning: Error occurred using JAXP to load a SAXParser. Will use Aelfred instead --client. say. Hello. To. Me(cat, inhat) returned: hello cat inhat from SOAPRpc. Server. Impl v 031015 Web Services Examples Enterprise Java 43
Axis Example Enterprise Java • Services – Hello. Web. Service • Dynamically Deployed (. jws) Web Service – Calculator. Impl • Statically Deployed (WSDL) Service • Clients – Hello. Web. Client • Uses dynamic interface – Calculator. Client • Uses WSDL Stubs v 031015 Web Services Examples 44
Axis. Demo. App Source Enterprise Java • Create EAR and register axis. Demo. WEB as a member axis. Demo. App/META-INF/application. xml • Create dynamically deployed implementations axis. Demo. App/axis. Demo. WEB/Hello. Web. Service. jws • Create an interface to generate WSDL and an implementation axis. Demo. App/axis. Demo. Util/corej 2 ee/examples/ws/calculator/Calculator. java axis. Demo. App/axis. Demo. Util/corej 2 ee/examples/ws/calculator/impl/Calculator. Impl. java • Create server-side skeletons from WSDL axis. Demo. App/axis. Demo. Util/corej 2 ee/examples/ws/calculator/axisimpl/Calculator. java axis. Demo. App/axis. Demo. Util/corej 2 ee/examples/ws/calculator/axisimpl/Calculator. Service. Locator. java axis. Demo. App/axis. Demo. Util/corej 2 ee/examples/ws/calculator/axisimpl/Calculator. Soap. Binding. Impl. java axis. Demo. App/axis. Demo. Util/corej 2 ee/examples/ws/calculator/axisimpl/Calculator. Soap. Binding. Stub. java axis. Demo. App/axis. Demo. Util/corej 2 ee/examples/ws/calculator/axisimpl/deploy. wsdd axis. Demo. App/axis. Demo. Util/corej 2 ee/examples/ws/calculator/axisimpl/undeploy. wsdd • Register any resources required by deployed services in deployment descriptor(s) axis. Demo. App/axis. Demo. WEB/WEB-INF/web. xml • Update axis-managed security files (not done) axis. Demo. App/axis. Demo. WEB/WEB-INF/perms. lst axis. Demo. App/axis. Demo. WEB/WEB-INF/users. lst • Create Ant run script axis. Demo. App/bin/antfiles/axis. Demo. App. xml v 031015 Web Services Examples 45
axis. Demo. App EAR Enterprise Java $ find axis. Demo. App -type f axis. Demo. App/axis. Demo. jar axis. Demo. App/axis. Demo. WEB/Hello. Web. Service. jws axis. Demo. App/axis. Demo. WEB/WEB-INF/jws. Classes/Hello. Web. Service. class axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/axis-ant. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/axis. Demo. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/commons-discovery. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/commons-logging. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/jaxrpc. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/log 4 j-1. 2. 4. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/saaj. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/tools. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/wsdl 4 j. jar axis. Demo. App/axis. Demo. WEB/WEB-INF/perms. lst axis. Demo. App/axis. Demo. WEB/WEB-INF/users. lst axis. Demo. App/axis. Demo. WEB/WEB-INF/web. xml axis. Demo. App/axis. Demo. WEB. war axis. Demo. App/META-INF/application. xml v 031015 Web Services Examples 46
axis. Demo. jar Enterprise Java $ jar tf axis. Demo. App/axis. Demo. WEB/WEB-INF/lib/axis. Demo. jar --- not used here, but would have been self authored when needed --META-INF/MANIFEST. MF --- autogenerated from WSDL --corej 2 ee/examples/ws/calculator/axisimpl/Calculator. class corej 2 ee/examples/ws/calculator/axisimpl/Calculator. Service. Locator. class corej 2 ee/examples/ws/calculator/axisimpl/Calculator. Soap. Binding. Stub. class --- autogenerated and then slightly modified to connect to Calculator. Impl --corej 2 ee/examples/ws/calculator/axisimpl/Calculator. Soap. Binding. Impl. class --- self authored : contain implementation --corej 2 ee/examples/ws/calculator/Calculator. class corej 2 ee/examples/ws/calculator/impl/Calculator. Impl. class v 031015 Web Services Examples 47
axis. Client. Lib Source Enterprise Java • Create a client for the dynamically deployed (no WSDL) service axis. Demo. Client. Lib/corej 2 ee/examples/ws/client/Hello. Web. Service. Client. java • Generate the stubs from the WSDL axis. Demo. Client. Lib/corej 2 ee/examples/ws/calculator/axisclient/Calculator. java axis. Demo. Client. Lib/corej 2 ee/examples/ws/calculator/axisclient/Calculator. Service. Locator. java axis. Demo. Client. Lib/corej 2 ee/examples/ws/calculator/axisclient/Calculator. Soap. Binding. Stub. java • Create a client for the WSDL service axis. Demo. Client. Lib/corej 2 ee/examples/ws/calculator/client/Calculator. Client. java • Create an Ant run script axis. Demo. Client. Lib/bin/antfiles/axis. Demo. Client. xml v 031015 Web Services Examples 48
Other axis. Demo. App files Enterprise Java • Generated by axis. Demo. App/build. xml deploy/wsdl/Calculator. wsdl • Generated/Copied by axis. Demo. App/build. xml deploy/bin/axis_wsdd/deploy_Calculator. wsdd deploy/bin/axis_wsdd/undeploy_Calculator. wsdd v 031015 Web Services Examples 49
Hello. Web. Service. jws Enterprise Java public class Hello. Web. Service { public String say. Hello() { return "Hello From Web Service"; } public String say. Hello. To. Me(String name) { return "Hello " + name; } } v 031015 Web Services Examples 50
Calculator/Calculator. Impl. java Enterprise Java package corej 2 ee. examples. ws. calculator; public interface Calculator { int add(int val 1, int val 2); int subtract(int val 1, int val 2); } package corej 2 ee. examples. ws. calculator. impl; import corej 2 ee. examples. ws. calculator. Calculator; public class Calculator. Impl implements Calculator { public int add(int val 1, int val 2) { return val 1 + val 2; } public int subtract(int val 1, int val 2) { return val 1 - val 2; } } v 031015 Web Services Examples 51
Hello. Web. Service. Client Enterprise Java package corej 2 ee. 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. Parameter. Mode; public class Hello. Web. Service. Client { static String hostport_ = System. get. Property("hostport", "localhost: 7001"); public static void main(String [] args) throws Exception { Options options = new Options(args); String name = "anonymous"; String endpoint = "http: //" + hostport_ + "/axis. Demo/Hello. Web. Service. jws"; v 031015 Web Services Examples 52
Hello. Web. Service. Client Enterprise Java // Make the call Service service = new Service(); Call call = (Call) service. create. Call(); //say. Hello call. set. Target. Endpoint. Address(new java. net. URL(endpoint)); call. set. Operation. Name( "say. Hello" ); call. set. Return. Type(XMLType. XSD_STRING); String ret = (String) call. invoke( new Object [] {}); System. out. println("say. Hello returned: " + ret); } v 031015 Web Services Examples 53
Calculator Client Enterprise Java package corej 2 ee. examples. ws. calculator. client; import corej 2 ee. examples. ws. calculator. axisclient. Calculator. Service; import corej 2 ee. examples. ws. calculator. axisclient. Calculator. Service. Locator; public class Calculator. Client { public static void main(String args[]) throws Exception { Calculator. Service. Locator calc. Service = new Calculator. Service. Locator(); Calculator calc = calc. Service. get. Calculator(); 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)); } } v 031015 Web Services Examples 54
Generate WSDL Elements Enterprise Java • Generate WSDL Stubs (axis. Demo. Util) $ ant wsdl _axiswsdl_: [echo] building WSDL file Calculator. wsdl • Generate WSDL Skeletons (axis. Demo. Util) $ ant wsdlimpl _axisimpl_: [echo] generating axisimpl for Calculator • Modify Binding. Impl. java (axis. Demo. Util) • Generate WSDL Stubs (axis. Demo. Client. Util) $ ant wsdlclient _axisclient_: [echo] generating wsdlclient for Calculator v 031015 Web Services Examples 55
Enterprise Java Deploy/Run Example (EAR Directory) $ ant stagedeploy _stagedeploy_: [java] executing deploy [java] Activate application axis. Demo. App. Stage on myserver $ ant wsdeploy _wsdeploy_: [echo] http: //localhost: 7001/axis. Demo/services/Admin. Service [java] [INFO] Admin. Client - -Processing file C: cygwinhomejcstaffprojco rej 2 eesrcaxis. Demo. App/. . /deploy/bin/axis_wsdd/deploy_Calculator. wsdd [java] [INFO] Admin. Client - -<Admin>Done processing</Admin> $ corej 2 ee axis. Demo. Client Hello. Web. Service. Client: say. Hello returned: Hello From Web Service Calculator. Client: 0+0=0. . . 2 -2=0 v 031015 Web Services Examples 56
7ba4913a3af1d88bdc773e12a195c427.ppt