This example uses classes formjava.net, javax.xml and com.sun.xml.ws packages. You have to import these classes to be able to use them in your code. The following code imports the classes from these packages that are used in this example.
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.api.message.Headers;
import com.sun.xml.ws.developer.WSBindingProvider;
You also have to import the automatically generated JAX-WS portable artifacts. It is common to call these classes stubs.
import stubs.InstanceInfoBean;
import stubs.InstanceInfoBeanList;
import stubs.OperationException_Exception;
import stubs.PapiWebService;
import stubs.PapiWebService_Service;
import stubs.StringListBean;
To invoke PAPI Web Service you have to create a Service object. The constructor of a service object receives a URL object that contains the URL of the WSDL. In this example the URL of the WSDL is passed as an argument to the main method.
public class PapiWsJaxWsExample {
private static final String SECURITY_NAMESPACE =
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
public static void main(String[] args) {
try {
String endPoint = args[0];
QName qName = new QName("http://bea.com/albpm/PapiWebService", "PapiWebService");
Service service = PapiWebService_Service.create(new URL(endPoint), qName);
To be able to invoke operations over PAPI Web Service you have to obtain a PapiWebService object. PapiWebService exposes all the operations that you can invoke remotely over PAPI Web Service.
PapiWebService papiWebServicePort = service.getPort(PapiWebService.class);
If there is a problem accessing the WSDL endpoint the URL constructor throws a MalformedURLException, so you need to call it inside a try-catch block.
//... Configure Authentication
//... Operate with PAPI Web Service
} catch (MalformedURLException e) {
System.out.println("Could not connect to the web service endpoint");
e.printStackTrace();
}
}
}
Before invoking any operation over the web service the client has to authenticate itself. This example shows you how to use JAX-WS with Username Token Profile and HTTP Basic authentication. Usually you choose one of these two mechanisms because PAPI Web Service only uses the second one in case the first authentication mechanism fails. For more information on how authentications mechanisms work in PAPI Web Service, see PAPI Web Service Security Authentication.
The code for this authentication mechanism is divided into two methods, one for each mechanism. These methods should be invoked before invoking any operation over the web service.
addUsernameTokenProfile(papiWebServicePort);
addHttpBasicAuthentication(papiWebServicePort);
The method addUsernameTokenProfile() configures Username Token Profile authentication. Some of the operations performed in this method throw aSOAPException, so you need to call them inside a try-catch block.
This example adds the header programmatically but you can also configure Username Token Profile using Web Services Interoperability Technologies (WSIT) from Metro Web Services stack. For information about WSIT, seehttp://wsit.dev.java.
private static void addUsernameTokenProfile(PapiWebService papiWebServicePort)
throws SOAPException {
try {
SOAPFactory soapFactory = SOAPFactory.newInstance();
QName securityQName = new QName(SECURITY_NAMESPACE, "Security");
SOAPElement security = soapFactory.createElement(securityQName);
QName tokenQName = new QName(SECURITY_NAMESPACE, "UsernameToken");
SOAPElement token = soapFactory.createElement(tokenQName);
QName userQName = new QName(SECURITY_NAMESPACE, "Username");
SOAPElement username = soapFactory.createElement(userQName);
username.addTextNode("test");
QName passwordQName = new QName(SECURITY_NAMESPACE, "Password");
SOAPElement password = soapFactory.createElement(passwordQName);
password.addTextNode("test");
token.addChildElement(username);
token.addChildElement(password);
security.addChildElement(token);
Header header = Headers.create(security);
((WSBindingProvider) papiWebServicePort).setOutboundHeaders(header);
} catch (SOAPException e) {
System.out.println("Could not configure Username Token Profile authentication");
e.printStackTrace();
}
}
The method addHttpBasicAuthentication() configures HTTP Basic authentication by obtaining the request context and adding it the username and password properties.
private static void addHttpBasicAuthentication(PapiWebService papiWebServicePort) {
Map<String, Object> request =
((BindingProvider) papiWebServicePort).getRequestContext();
request.put(BindingProvider.USERNAME_PROPERTY, "test");
request.put(BindingProvider.PASSWORD_PROPERTY, "test");
}
Once PAPI Web Service successfully authenticates the client, the client is ready to perform operations over the web service.
The following code retrieves a list of available processes by invoking the method processesGetIds() over a PapiWebService object. It then iterates over them using those ids to obtain the instances for each process by invoking the method processGetInstances() over the PapiWebService object.If there is a problem performing any of the requested operations, this method throws an OperationException, so you need to invoke it inside a try-catch block. Finally it iterates over those instances invoking the method getId() and prints its result.
try {
StringListBean processIds = papiWebServicePort.processesGetIds(true);
for (String processId : processIds.getStrings()) {
System.out.println("\nProcess: " + processId);
InstanceInfoBeanList instances =
papiWebServicePort.processGetInstances(processId);
for (InstanceInfoBean instance : instances.getInstances()) {
System.out.println("-> " + instance.getId());
}
}
} catch (OperationException_Exception e) {
System.out.println("Could not perform the requested operation");
e.printStackTrace();
}
The following class contains all the steps described in this section.
package example;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.api.message.Headers;
import com.sun.xml.ws.developer.WSBindingProvider;
import stubs.InstanceInfoBean;
import stubs.InstanceInfoBeanList;
import stubs.OperationException_Exception;
import stubs.PapiWebService;
import stubs.PapiWebService_Service;
import stubs.StringListBean;
public class PapiWsJaxWsExample {
private static final String SECURITY_NAMESPACE =
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
public static void main(String[] args) {
try {
/////////////////// Initialize the web service client ///////////////////
String endPoint = args[0];
QName qName = new QName("http://bea.com/albpm/PapiWebService", "PapiWebService");
Service service = PapiWebService_Service.create(new URL(endPoint), qName);
PapiWebService papiWebServicePort = service.getPort(PapiWebService.class);
/////////////////// Configure authentication ///////////////////
addUsernameTokenProfile(papiWebServicePort);
addHttpBasicAuthentication(papiWebServicePort);
/////////////////// Operate with PAPI Web Service ///////////////////
try {
StringListBean processIds = papiWebServicePort.processesGetIds(true);
for (String processId : processIds.getStrings()) {
System.out.println("\nProcess: " + processId);
InstanceInfoBeanList instances = papiWebServicePort.processGetInstances(processId);
for (InstanceInfoBean instance : instances.getInstances()) {
System.out.println("-> " + instance.getId());
}
}
} catch (OperationException_Exception e) {
System.out.println("Could not perform the requested operation");
e.printStackTrace();
}
} catch (MalformedURLException e) {
System.out.println("Could not connect to the web service endpoint");
e.printStackTrace();
}
}
private static void addHttpBasicAuthentication(PapiWebService papiWebServicePort) {
Map<String, Object> request =
((BindingProvider) papiWebServicePort).getRequestContext();
request.put(BindingProvider.USERNAME_PROPERTY, "test");
request.put(BindingProvider.PASSWORD_PROPERTY, "test");
}
private static void addUsernameTokenProfile(PapiWebService papiWebServicePort) {
try {
SOAPFactory soapFactory = SOAPFactory.newInstance();
QName securityQName = new QName(SECURITY_NAMESPACE, "Security");
SOAPElement security = soapFactory.createElement(securityQName);
QName tokenQName = new QName(SECURITY_NAMESPACE, "UsernameToken");
SOAPElement token = soapFactory.createElement(tokenQName);
QName userQName = new QName(SECURITY_NAMESPACE, "Username");
SOAPElement username = soapFactory.createElement(userQName);
username.addTextNode("test");
QName passwordQName = new QName(SECURITY_NAMESPACE, "Password");
SOAPElement password = soapFactory.createElement(passwordQName);
password.addTextNode("test");
token.addChildElement(username);
token.addChildElement(password);
security.addChildElement(token);
Header header = Headers.create(security);
((WSBindingProvider) papiWebServicePort).setOutboundHeaders(header);
} catch (SOAPException e) {
System.out.println("Could not configure Username Token Profile authentication");
e.printStackTrace();
}
}
}