To consume webservice in Portlet :
1) Create a separate java project and paste wsdl file src folder
2) Right click on wsdl file and select “web service” then “generate client” option
3) Select “Client type” as “Java Proxy”. Select “Web service runtime” as “Jax-rpc”
4) Set level of client generation to “Deploy Client” so that ServiceLocator and ProxyType will get generated automatically
5) Once client code is generated, set this java project in build path of portlet project to resolve the ws generated classes at compile time also make this project available in runtime. In rad upto 7.5 use “j2ee module dependency” for radv8.0 use “Deployment Assembly” option.
6) Use the Servicelocator or PortTypeProxy generated class to call webservice operations
Sample code to send and receive SOAP headers using JAX-RPS :
ServiceLocator serviceLocator=new ServiceLocator();
PortType oppService=serviceLocator.getRetailPort();
javax.xml.rpc.Stub oppServiceStub=(javax.xml.rpc.Stub)oppService;
//setting webservice endpoint
String retailEndPointURL=”<ws_url>”;
oppServiceStub._setProperty("javax.xml.rpc.service.endpoint.address",retailEndPointURL);
oppServiceStub._setProperty("javax.xml.rpc.service.endpoint.address",retailEndPointURL);
1 //Create the request and response hashmaps.
2 HashMap requestHeaders=new HashMap();
3 HashMap responseHeaders=new HashMap();
4
5 //Add "AtmUuid1" and "AtmUuid2" to the request hashmap.
6 requestHeaders.put(new QName("com.rotbank.security", "AtmUuid1"),
7 "<AtmUuid1 xmlns=\"><uuid>ROTB-0A01254385FCA09</uuid></AtmUuid1>");
8 requestHeaders.put(new QName("com.rotbank.security", "AtmUuid2"),
9 ((IBMSOAPFactory)SOAPFactory.newInstance()).createElementFromXMLString(
10 "x:AtmUuid2 xmlns:x=\"com.rotbank.security\"><x:uuid>ROTB-0A01254385FCA09
</x:uuid><x:AtmUuid2>"));
11
12 //Add "ServerUuid" to the response hashmap.
13 //If "responseHeaders" is empty, all the SOAP headers are
14 //extracted from the response message.
15 responseHeaders.put(new QName("com.rotbank.security","ServerUuid"), null);
16
17 //Set the properties on the Stub object.
18 stub.setProperty(com.ibm.websphere.webservices.Constants.REQUEST_SOAP_HEADERS.requestHeaders);
19 stub.setProperty(com.ibm.websphere.webservices.Constants.RESPONSE_SOAP_HEADERS.responseHeaders);
20
21 //Call the operationon the Stub.
22 stub.foo(parm2, parm2);
23
24 //Retrieve "ServerUuid" from the response hashmap.
25 SOAPElement serverUuid =
26 (SOAPElement) responseHeaders.get(new QName("com.rotbank.security","ServerUuid"));
27
28 //Note: "serverUuid" now equals a SOAPElement object that represents the
29 //following code:
30//"<y:ServerUuid xmlns:y=\"com.rotbank.security\"><:uuid>ROTB-0A03519322FSA01
</y:uuid></y:ServerUuid.");
On lines 2-3, new HashMaps are created that are used for the request and response SOAP headers.
On lines 6-10, the AtmUuid1 and AtmUuid2 headers elements are added to the request HashMap.
On line 15, the ServerUuid header element name, along with a null value, is added to the response HashMap.
On line 18, the request HashMap is set as a property on the Stub object. This causes the AtmUuid1 and AtmUuid2 headers to be added to each request message that is associated with an operation that is invoked on the Stub object.
On line 19, the response HashMap is set as a property on the Stub object. This causes the ServerUuid header to be extracted from each response message that is associated with an operation that is invoked on the Stub object.
On line 22, the Web service operation is invoked on the Stub object.
On lines 25-26, the ServerUuid header is retrieved from the response HashMap. The header was extracted from the response message and inserted into the HashMap by the Web services engine.
Some part of sample code is taken from link: http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/rwbs_impsoapheadexmp.html
No comments:
Post a Comment