I added some tests showing how to use the Dispatch API with the STSClient:
http://svn.apache.org/viewvc/cxf/trunk/services/sts/systests/basic/src/test/java/org/apache/cxf/systest/sts/transport/TransportBindingTest.java?view=markup
> So in the above code we wont to go away with the
> sts.setWsdlLocation("http://localhost:9999/SecurityTokenService/UT?wsdl")
An alternative as in one of the tests above is to do:
String location = "https://localhost:" + test.getStsPort() +
"/SecurityTokenService/Transport";
stsClient.setLocation(location);
stsClient.setPolicy("classpath:/org/apache/cxf/systest/sts/issuer/sts-transport-policy.xml");
Colm.
On Thu, Feb 13, 2014 at 2:44 PM, [email protected] <[email protected]>wrote:
> Hi Stepan,
>
> Today we tried to set all the addressing properties and were getting the
> response from the STS client. Now we are getting Body not encrypted and not
> signed . I will be debugging that issue.
>
> I have few questions in the implementation. We are going with the dispatch
> API and for the STS client , I could not find an example how we can set it
> .
> Please check my code and let us know how we can achieve it with Dispatch
> API. We are using the sample wsdl given in CXF sample .
>
> package com.cxf.sample;
> import static
> org.apache.cxf.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES;
>
> import java.io.FileNotFoundException;
> import java.io.IOException;
> import java.util.HashMap;
> import java.util.Map;
>
> import javax.xml.namespace.QName;
> import javax.xml.soap.MessageFactory;
> import javax.xml.soap.SOAPBody;
> import javax.xml.soap.SOAPConstants;
> import javax.xml.soap.SOAPElement;
> import javax.xml.soap.SOAPEnvelope;
> import javax.xml.soap.SOAPException;
> import javax.xml.soap.SOAPMessage;
> import javax.xml.soap.SOAPPart;
> import javax.xml.ws.Dispatch;
> import javax.xml.ws.soap.SOAPBinding;
>
> import org.apache.cxf.endpoint.Client;
> import org.apache.cxf.jaxws.DispatchImpl;
> 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.MetadataType;
> import org.apache.cxf.ws.addressing.ObjectFactory;
> import org.apache.cxf.ws.addressing.ReferenceParametersType;
> import org.apache.cxf.ws.addressing.RelatesToType;
> import org.apache.cxf.ws.addressing.impl.AddressingPropertiesImpl;
> import org.apache.cxf.ws.addressing.impl.MAPAggregatorImpl;
> import org.apache.cxf.ws.addressing.soap.MAPCodec;
> import org.apache.cxf.ws.policy.PolicyConstants;
> import org.apache.cxf.ws.security.trust.STSClient;
> import org.apache.neethi.Policy;
>
>
> public class TestCXFSTS {
> private static final ObjectFactory WSA_OBJECT_FACTORY = new
> ObjectFactory();
>
> /**
> * @param args
> * @throws SOAPException
> * @throws IOException
> */
> public static void main(String[] args) throws IOException,
> SOAPException {
> testService1();
> }
>
> private static void testService1() throws FileNotFoundException,
> IOException, SOAPException {
> Dispatch<SOAPMessage> dispatch = CXFUtils.getDispatch(
> SOAPBinding.SOAP11HTTP_BINDING,
> "
> http://127.0.0.1:9989/SoapContext/SoapPort");
> Client client = ((DispatchImpl<SOAPMessage>)
> dispatch).getClient();
>
>
> //engage addressing interceptors
> MAPCodec mapCodec = new MAPCodec();
> MAPAggregatorImpl mapAggregator = new MAPAggregatorImpl();
> client.getOutInterceptors().add(mapCodec);
> client.getOutInterceptors().add(mapAggregator);
> client.getInInterceptors().add(mapCodec);
> client.getInInterceptors().add(mapAggregator);
>
> Policy wsaPolicy = PolicyHelper.parsePolicy(client,
> "c://wsa-policy.xml");
> Policy policy = PolicyHelper.parsePolicy(client,
> "c://wsa-policy2.xml");
> Policy policy2 = PolicyHelper.parsePolicy(client,
> "c://wsa-policy3.xml");
> wsaPolicy.merge(policy);
> wsaPolicy.merge(policy2);
>
> dispatch.getRequestContext().put(CLIENT_ADDRESSING_PROPERTIES,
> createMaps());
>
> dispatch.getRequestContext().put(PolicyConstants.POLICY_OVERRIDE,
> wsaPolicy);
> Map<String,Object> stsmap = setSTSMaps();
> client.getRequestContext().putAll(stsmap);
> STSClient sts = new STSClient(client.getBus());
> sts.setServiceQName(new
> QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512/
> ","SecurityTokenService"));
> sts.setEndpointQName(new
> QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512/","UT_Port"));
>
> *sts.setWsdlLocation("http://localhost:9999/SecurityTokenService/UT?wsdl
> ");*
> sts.setEnableAppliesTo(false);
> client.getRequestContext().put("ws-security.sts.client",
> sts );
> Map<String, Object> stsprop = new HashMap<String,
> Object>() ;
> stsprop.put("ws-security.username","alice");
>
>
> stsprop.put("ws-security.callback-handler","com.cxf.sample.ClientCallbackHandler");
>
>
> stsprop.put("ws-security.encryption.properties","clientKeystore.properties");
> stsprop.put("ws-security.encryption.username","mystskey");
>
> stsprop.put("ws-security.sts.token.username","myclientkey");
>
>
> stsprop.put("ws-security.sts.token.properties","clientKeystore.properties");
> stsprop.put("ws-security.sts.token.usecert","false");
> sts.setUseCertificateForConfirmationKeyInfo(true);
> sts.getProperties().putAll(stsprop);
> MessageFactory mf =
> MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
> SOAPMessage request = mf.createMessage();
> SOAPPart part = request.getSOAPPart();
>
> // Obtain the SOAPEnvelope and header and body elements.
> SOAPEnvelope env = part.getEnvelope();
> SOAPBody body = env.getBody();
>
> // Construct the message payload.
> SOAPElement operation = body.addChildElement("greetMe",
> "tns",
> "http://apache.org/hello_world_soap_http/types");
> SOAPElement value =
> operation.addChildElement("requestType",
> "tns","http://apache.org/hello_world_soap_http/types");
> value.addTextNode("Renu");
> request.saveChanges();
> SOAPMessage response = dispatch.invoke(request);
> System.out.println(response);
>
>
> }
>
> private static Map<String,Object> setSTSMaps(){
>
> Map<String,Object> stsmap = new HashMap<String, Object>();
> stsmap.put("ws-security.signature.properties",
> "clientKeystore.properties");
> stsmap.put("ws-security.signature.username",
> "myclientkey");
> stsmap.put("ws-security.callback-handler",
> "com.cxf.sample.ClientCallbackHandler");
> stsmap.put("ws-security.encryption.properties",
> "clientKeystore.properties");
> stsmap.put("ws-security.encryption.username",
> "myservicekey");
> return stsmap;
>
> }
>
>
> private static AddressingProperties createMaps() {
>
> // get Message Addressing Properties instance
> AddressingProperties maps = new AddressingPropertiesImpl();
> // set MessageID property
> AttributedURIType messageID = WSA_OBJECT_FACTORY
> .createAttributedURIType();
> AttributedURIType action =
> WSA_OBJECT_FACTORY.createAttributedURIType();
> messageID.setValue("urn:uuid:" +
> System.currentTimeMillis());
> AttributedURIType to =
> WSA_OBJECT_FACTORY.createAttributedURIType();
> to.setValue("http://www.w3.org/2005/08/addressing");
> EndpointReferenceType toRefType =
> WSA_OBJECT_FACTORY.createEndpointReferenceType();
> toRefType.setAddress(to);
> EndpointReferenceType fault =
> WSA_OBJECT_FACTORY.createEndpointReferenceType();
> AttributedURIType faultTo =
> WSA_OBJECT_FACTORY.createAttributedURIType();
> faultTo.setValue("http://www.w3.org/2005/08/addressing");
> fault.setAddress(faultTo);
> EndpointReferenceType reply =
> WSA_OBJECT_FACTORY.createEndpointReferenceType();
> AttributedURIType replyTo =
> WSA_OBJECT_FACTORY.createAttributedURIType();
> replyTo.setValue("http://www.w3.org/2005/08/addressing");
> reply.setAddress(replyTo);
> EndpointReferenceType relates =
> WSA_OBJECT_FACTORY.createEndpointReferenceType();
> AttributedURIType relatesTo =
> WSA_OBJECT_FACTORY.createAttributedURIType();
> relatesTo.setValue("http://www.w3.org/2005/08/addressing
> ");
> relates.setAddress(relatesTo);
> action.setValue("http://www.w3.org/2005/08/addressing");
> maps.setTo(toRefType);
> maps.setTo(to);
> maps.setAction(action);
> maps.setMessageID(messageID);
> maps.setFaultTo(fault);
> RelatesToType rel = WSA_OBJECT_FACTORY.createRelatesToType();
> rel.setRelationshipType("http://www.w3.org/2005/08/addressing
> ");
> rel.setValue("http://www.w3.org/2005/08/addressing");
> maps.setRelatesTo(rel );
> maps.setReplyTo(reply);
> ReferenceParametersType value =
> WSA_OBJECT_FACTORY.createReferenceParametersType();
> value.getAny().add("http://www.w3.org/2005/08/addressing
> ");
> MetadataType value1 =
> WSA_OBJECT_FACTORY.createMetadataType();
> value1.getAny().add("http://www.w3.org/2005/08/addressing
> ");
> return maps;
> }
>
> }
>
> So in the above code we wont to go away with the
> sts.setWsdlLocation("http://localhost:9999/SecurityTokenService/UT?wsdl");
> .
> I tried to use the
> dispatch.getRequestContext().put(PolicyConstants.POLICY_OVERRIDE,
> wsaPolicy); but it didnt worked.
>
> Your help is appreciated. Thanks
>
>
>
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Issue-with-STS-using-Dispatch-API-tp5739874p5739953.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>
--
Colm O hEigeartaigh
Talend Community Coder
http://coders.talend.com