Example: Using WS-Addressing with P6 EPPM Web Services from Java

This example sets the messageId, Action, ReplyTo, and RelatesTo properties and illustrates the use of WS-Addressing when using P6 EPPM Web Services to print out all of the EPS in the database.

This example assumes that the P6 EPPM Web Services Server has been configured to use UsernameToken Profile for authentication

package com.oracle.pgbu.integration.ws;

import java.util.Date;

import javax.xml.soap.SOAPMessage;

import oracle.security.crypto.util.Utils;

import oracle.security.xmlsec.util.Base64;

import oracle.security.xmlsec.util.XMLUtils;

import oracle.security.xmlsec.wss.WSSecurity;

import oracle.security.xmlsec.wss.WSUCreated;

import oracle.security.xmlsec.wss.WSUExpires;

import oracle.security.xmlsec.wss.WSUTimestamp;

import oracle.security.xmlsec.wss.soap.WSSOAPEnvelope;

import oracle.security.xmlsec.wss.username.UsernameToken;

import oracle.security.xmlsec.wss.util.WSSTokenUtils;

import oracle.security.xmlsec.wss.util.WSSUtils;

import org.apache.cxf.binding.soap.SoapFault;

import org.apache.cxf.binding.soap.SoapMessage;

import org.apache.cxf.binding.soap.SoapVersion;

import org.apache.cxf.interceptor.Fault;

import org.apache.cxf.phase.AbstractPhaseInterceptor;

import org.apache.cxf.phase.Phase;

import org.w3c.dom.Element;

/**

*

* @author adavidson

*

*/

public class DemoOutInterceptor

extends AbstractPhaseInterceptor<SoapMessage>

{

//~ Static fields/initializers -----------------------------------------------------------------

private static final String TIMESTAMP_ID_PREFIX = "Timestamp-";

private static final String SCHEMA_DATE_TIME = "http://www.w3.org/2001/XMLSchema/dateTime";

private String username = null;

private String password = null;

//~ Instance fields ----------------------------------------------------------------------------

//~ Constructors -------------------------------------------------------------------------------

public DemoOutInterceptor(String username, String password)

{

super(Phase.POST_MARSHAL);

this.username = username;

this.password = password;

}

//~ Methods ------------------------------------------------------------------------------------

public void handleMessage(SoapMessage message)

throws Fault

{

SoapVersion version = message.getVersion();

try

{

SOAPMessage soapMessage = message.getContent(SOAPMessage.class);

WSSOAPEnvelope wsEnvelope = new WSSOAPEnvelope(soapMessage.getSOAPPart().getEnvelope());

// Create the Oracle WSSecurity element so we can add security information to SOAP header

WSSecurity sec = WSSecurity.newInstance(wsEnvelope.getOwnerDocument());

sec.setAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", "mustUnderstand", "1");

wsEnvelope.addSecurity(sec);

// Remember information on the authentication elements so we can encrypt and sign them later

String authTokenId = null;

// Add the UsernameToken information, including Nonce token and Created time

// Also, store the WsuId so we can sign with it later, if encryption is enabled

authTokenId = XMLUtils.randomName();

addUsernameToken(sec, authTokenId);

// Add Timestamp information to the header

addTimestamp(sec, wsEnvelope);

}

catch (Exception ex)

{

throw new SoapFault("Error while creating security credentials.", ex, version.getSender());

}

}

private Element addUsernameToken(WSSecurity sec, String wsuId)

{

// Create the basic UsernameToken information with the specified username and password

UsernameToken unToken = WSSTokenUtils.createUsernameToken(wsuId, username, null, null, password.toCharArray());

// A timestamp that the server checks to see if this message has taken too long to reach the server

unToken.setCreatedDate(new Date());

// A token to help prevent replay attacks

// If a second message with the same Nonce data is sent, it would be rejected by the server

unToken.setNonce(Base64.fromBase64(XMLUtils.randomName()));

sec.addUsernameToken(unToken);

return unToken.getElement();

}

private String addTimestamp(WSSecurity sec, WSSOAPEnvelope wsEnvelope)

{

WSUTimestamp timestamp = new WSUTimestamp(wsEnvelope.getOwnerDocument());

sec.setTimestamp(timestamp);

WSUCreated created = new WSUCreated(wsEnvelope.getOwnerDocument(), SCHEMA_DATE_TIME);

created.setValue(new Date());

WSUExpires expires = new WSUExpires(wsEnvelope.getOwnerDocument(), SCHEMA_DATE_TIME);

expires.setValue(Utils.minutesFrom(new Date(), 30));

timestamp.setCreated(created);

timestamp.setExpires(expires);

String rawTimestampId = TIMESTAMP_ID_PREFIX + XMLUtils.randomName();

WSSUtils.addWsuIdToElement(rawTimestampId, timestamp.getElement());

return rawTimestampId;

}

}

package com.oracle.pgbu.integration.ws;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import javax.xml.ws.BindingProvider;

import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;

import org.apache.cxf.endpoint.Client;

import org.apache.cxf.frontend.ClientProxy;

import org.apache.cxf.interceptor.LoggingOutInterceptor;

import org.apache.cxf.ws.addressing.AddressingBuilder;

import org.apache.cxf.ws.addressing.AddressingProperties;

import org.apache.cxf.ws.addressing.AttributedURIType;

import org.apache.cxf.ws.addressing.EndpointReferenceType;

import org.apache.cxf.ws.addressing.JAXWSAConstants;

import org.apache.cxf.ws.addressing.MAPAggregator;

import org.apache.cxf.ws.addressing.ObjectFactory;

import org.apache.cxf.ws.addressing.soap.MAPCodec;

import com.primavera.ws.p6.eps.EPS;

import com.primavera.ws.p6.eps.EPSFieldType;

import com.primavera.ws.p6.eps.EPSPortType;

import com.primavera.ws.p6.eps.EPSService;

public class AddressingDemo {

/**

* @param args

*/

public static void main(String[] args) throws Exception {

String url = "http://localhost:7001/p6ws/services/EPSService?wsdl";

URL wsdlURL = new URL(url);

EPSService service = new EPSService(wsdlURL);

EPSPortType servicePort = service.getEPSPort();

Client client = ClientProxy.getClient(servicePort);

MAPAggregator aggregator = new MAPAggregator();

aggregator.setAllowDuplicates(true);

MAPCodec codec = new MAPCodec();

client.getEndpoint().getOutInterceptors().add(new LoggingOutInterceptor());

client.getEndpoint().getOutInterceptors().add(new SAAJOutInterceptor());

client.getEndpoint().getOutInterceptors().add(new DemoOutInterceptor("admin", "admin"));

client.getEndpoint().getOutInterceptors().add(aggregator);

client.getEndpoint().getOutInterceptors().add(codec);

ObjectFactory wsaObjectFactory = new ObjectFactory();

AddressingBuilder builder = AddressingBuilder.getAddressingBuilder();

AddressingProperties maps = builder.newAddressingProperties();

// set MessageID property

AttributedURIType messageID = wsaObjectFactory.createAttributedURIType();

messageID.setValue("urn:uuid:" + System.currentTimeMillis());

maps.setMessageID(messageID);

// set Action property

AttributedURIType soapAction = wsaObjectFactory.createAttributedURIType();

soapAction.setValue("ReadEPS");

maps.setAction(soapAction);

/*

* Uncomment the following block of code to send the web service response

* to another server. You will need to set this up yourself.

*/

/*

AttributedURIType replyTo = new AttributedURIType();

replyTo.setValue("http://localhost:8080/SoapContext/SoapPort");

EndpointReferenceType replyToRef = new EndpointReferenceType();

replyToRef.setAddress(replyTo);

maps.setReplyTo(replyToRef);

*/

// associate MAPs with request context

Map<String, Object> requestContext = ((BindingProvider) servicePort).getRequestContext();

requestContext.put(JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES, maps);

List<EPSFieldType> epsFields = new ArrayList<EPSFieldType>();

epsFields.add(EPSFieldType.OBJECT_ID);

epsFields.add(EPSFieldType.ID);

epsFields.add(EPSFieldType.NAME);

// Read all EPS in the database. If you've redirected the response to another

// server (by specifying the ReplyTo WS Adressing header), the following

// call will not return any results. The results will be sent to the

// server specified in the ReplyTo field.

List<EPS> ePSs = servicePort.readEPS(epsFields, null, null);

if (ePSs != null) {

for (EPS eps : ePSs) {

System.out.println(eps.getName());

}

}

}

}



Last Published Friday, February 9, 2024