Dynamic Dispatch Client
To dynamically invoke web services, you can create a Dispatch client and configure:
-
Security properties depending on the security policy to be used
-
Keystore requirements
The following is an example code:
System.setProperty("oracle.security.jps.config", "path_to_jps-config.xml");
QName qname = new QName(svcns, svcname);
Service service = Service.create(new URL(endpointURL + WSDL_URL_SUFFIX), qname);
QName portname = new QName(svcns, portname);
SecurityPolicyFeature[] features =
new SecurityPolicyFeature[] { new SecurityPolicyFeature("policy:" + policy) };
Dispatch<SOAPMessage> dispatcher =
service.createDispatch(portname, SOAPMessage.class, Service.Mode.MESSAGE, features);
Map<String, Object> rc = dispatcher.getRequestContext();
if (!policy.contains("saml"))
rc.put(SecurityConstants.ClientConstants.WSS_CSF_KEY, "csf-key");
else
rc.put(BindingProvider.USERNAME_PROPERTY, username);
//only these two don't need keystore
if ("oracle/wss_http_token_over_ssl_client_policy".equals(policy) ||
"oracle/wss_username_token_over_ssl_client_policy".equals(policy)) {
} else {
//location of the keystore file
rc.put(ClientConstants.WSSEC_KEYSTORE_LOCATION, jksFPath);
}
if (policy.contains("saml")) {
//client side private key, keystore sign alias; without this, invoking service at client side fails
rc.put(ClientConstants.WSSEC_SIG_KEY_ALIAS, signKey);
//client side private key; without this, the user cannot be authenticated
rc.put(ClientConstants.WSSEC_ENC_KEY_ALIAS, encKey);
}
}
// Obtain a preconfigured SAAJ MessageFactory
MessageFactory factory = ((SOAPBinding)dispatcher.getBinding()).getMessageFactory();
// Create SOAPMessage Request
SOAPMessage req = factory.createMessage();
// Request Body
SOAPBody body = req.getSOAPBody();
// Construct request
DocumentBuilder docBuilder = getDocumentBuilder();
ByteArrayInputStream is = new ByteArrayInputStream(requestStr.getBytes());
try {
Document doc = docBuilder.parse(is);
body.addDocument(doc);
} catch (Exception e) {
throw new RuntimeException("Cannot parse " + requestStr, e);
}
//invoke the service
SOAPMessage res = dispatcher.invoke(req);
//get response
if (res != null) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
res.writeTo(os);
}