Note: This example assumes that the P6 Web Services Server has been configured to use UsernameToken Profile for authentication.
Step one: Create an ActivityPortType:
import java.net.URL;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.BindingProvider;
import com.primavera.ws.p6.activity.Activity;
import com.primavera.ws.p6.activity.ActivityFieldType;
import com.primavera.ws.p6.activity.ActivityPortType;
import com.primavera.ws.p6.activity.ActivityService;
//...
URL wsdlURL = new URL("https://localhost:8443/p6ws/services/ActivityService?wsdl");
ActivityService service = new ActivityService(wsdlURL);
ActivityPortType port = service.getActivityPort();
Step two: Implement the client password callback Handler. For example:
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class ClientPasswordCallback implements CallbackHandler {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
// set the password
pc.setPassword("admin");
<...>
Step three: Set the UsernameToken.
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
// ...
//Retrieve the client object from the port
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(port);
org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
Map<String, Object> outProps = new HashMap<String, Object>();
outProps.put(WSHandlerConstants.ACTION, "UsernameToken Timestamp");
outProps.put(WSHandlerConstants.USER, "admin");
outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
//Set the call client call back.This will set the password
outProps.put(WSHandlerConstants.USERNAME_TOKEN,ClientPasswordCallback.class.getName());
//Set the properties on the interceptor
WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
cxfEndpoint.getOutInterceptors().add(wssOut);
Step four: Use P6 Web Services. For example, the following code reads an Activity
// Reads an Activity from Web Service
String filter = "ObjectId = 176913";
List<ActivityFieldType> fieldList = new ArrayList<ActivityFieldType>();
fieldList.add(ActivityFieldType.fromValue("Id"));
fieldList.add(ActivityFieldType.fromValue("RemainingDuration"));
List<Activity> actList = port.readActivities(fieldList, filter);
System.out.println("Activity ObjectId Is " + actList.get(0).getObjectId());
System.out.println("Activity RemainingDuration is " + actList.get(0).getRemainingDuration().getValue());
The following Java example invokes the Login operation of the Authentication Web Services over the Secure Sockets Layer.
import com.primavera.ws.p6.authentication.AuthenticationService;
import com.primavera.ws.p6.authentication.AuthenticationServicePortType;
import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
//...
System.setProperty("javax.net.ssl.trustStore","C:/keystore_certs/server.keystore");
URL wsdlURL = new URL("https://localhost:8443/p6ws/services/ActivityService?wsdl");
ActivityService service = new ActivityService(wsdlURL);
ActivityPortType port = service.getActivityPort();
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(port);
HTTPConduit httpConduit = (HTTPConduit)client.getConduit();
TLSClientParameters tlsParams = new TLSClientParameters();
tlsParams.setSecureSocketProtocol("SSL");
httpConduit.setTlsClientParameters(tlsParams);List<Activity> actList = port.readActivities(fieldList, filter);