WebServiceBClient.java Sample

This topic inludes the source code for the WebServiceBClient.java Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/ProxyClient/WSSE/token/

Sample Source Code


01 package WSSE.token;
02 
03 import java.util.ArrayList;
04 import java.util.List;
05 import javax.xml.namespace.QName;
06 import javax.xml.rpc.handler.HandlerInfo;
07 import javax.xml.rpc.handler.HandlerRegistry;
08 import weblogic.jws.proxies.*;
09 //import weblogic.jws.proxies.WebServiceB;
10 //import weblogic.jws.proxies.WebServiceB_Impl;
11 //import weblogic.jws.proxies.WebServiceBSoap;
12 import weblogic.webservice.context.WebServiceContext;
13 import weblogic.webservice.context.WebServiceSession;
14 import weblogic.webservice.core.handler.WSSEClientHandler;
15 import weblogic.xml.security.UserInfo;
16 import weblogic.xml.security.wsse.Security;
17 import weblogic.xml.security.wsse.SecurityElementFactory; 
18 
19 public class WebServiceBClient 
20 
21     
22     public static void main(String[] args){
23     
24         try{
25             /*
26              * Instantiate the main proxy class. The proxy class has the same name as the
27              * web service, with "_Impl" appended.
28              */
29             WebServiceB myservice = new WebServiceB_Impl("http://localhost:7001/WebServices/security/wsse/usertoken/webServiceB/WebServiceB.wsdl");
30 
31             WebServiceContext context = myservice.context();
32             WebServiceSession session = context.getSession();
33 
34             /**
35              * Registers a handler for the SOAP message traffic.
36              */
37             HandlerRegistry registry = myservice.getHandlerRegistry();
38             List list = new ArrayList();
39             list.add(new HandlerInfo(WSSEClientHandler.class, null, null));
40             registry.setHandlerChain(new QName("hello"), list);
41 
42             /**
43              * Set the username and password token for SOAP message sent from the client, through
44              * the proxy, to the web service.
45              */
46             UserInfo ui = new UserInfo("weblogic""weblogic");
47             session.setAttribute(WSSEClientHandler.REQUEST_USERINFO, ui);
48             
49             /**
50              * Adds the username / password token to the SOAP header.
51              */
52             SecurityElementFactory factory = SecurityElementFactory.getDefaultFactory();
53             Security security = factory.createSecurity(null);
54             security.addToken(ui);       
55             session.setAttribute(WSSEClientHandler.REQUEST_SECURITY, security);
56     
57             /*
58              * Get the protocol-specific proxy class.
59              */
60             WebServiceBSoap msg=myservice.getWebServiceBSoap();
61 
62             /**
63              * Invoke the web service method hello()
64              */            
65             String result=msg.hello();
66             
67             System.out.println();
68             System.out.println("Web Service Response:");            
69             System.out.println(result);
70         }
71         catch(Exception e){
72             e.printStackTrace();
73         }
74     }   
75     
76