Code for web service client to invoke OC4J web service with WSS Username Token policy
- Put UsernameToken.xml in the same package of the web service client class.
- Please find the code below for the coding details:
protected Location locationWebSPort = null;
protected static volatile LocationWebSManager me;
protected String endpoint = null;
protected String userName = null;
protected String password = null;
protected LocationWebSManager(){
}
public static LocationWebSManager getInstance(){
if (me == null){
synchronized(LocationWebSManager.class){
me = new LocationWebSManager();
}
}
return me;
}
public Location getWebService() throws Exception{
if (locationWebSPort == null){
synchronized(LocationWebSManager.class){
ClientPolicyFeature cpf = new ClientPolicyFeature();
InputStream asStream = LocationWebSManager.class.getResourceAsStream("UsernameToken.xml");
cpf.setEffectivePolicy(new InputStreamPolicySource(asStream));
URL url = new URL(getEndpoint());
QName qName = new QName("http://location.ws.xxx.xx/","Location");
Location_Service service = new Location_Service(url, qName);
Location port = service.getLocationSoapHttpPort(new WebServiceFeature[]{cpf});
Map requestContext = ((BindingProvider)port).getRequestContext();
setPortCredentialProviderList(requestContext);
locationWebSPort = port;
}
}
return locationWebSPort;
}
protected void setPortCredentialProviderList(Map requestContext) throws Exception {
List credList = new ArrayList();
credList.add(getUNTCredentialProvider(getUserName(), getPassword()));
requestContext.put(WSSecurityContext.CREDENTIAL_PROVIDER_LIST, credList);
}
留言
張貼留言