Consuming P6 Web Services over HTTPS (SSL) From Java

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);



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值