You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Vassalli Alessandro <A....@almaviva.it> on 2015/03/12 18:27:44 UTC

WS-MEX with Dynamic and runtime WS Policy took from external file.

Hey Guys,
I am quite new to the Cxf world.
I created a cxf service where the wsdl has no policy. I successfully added the policies dinamically through an external file called utpolicy.xml and the DynamicPolicyInInterceptor class.

However if I use the ws-mex to get the wsdl, returned by http://localhost:8080/ServerCXF2/services/ProvaWSSPortoSoap/mex the wsdl file it seems to be always the original file without the policies I have dynamically added through the DynamicPolicyOutInterceptor.

I wonder if it is wrong to expect the wsdl file to now contain the policies or it is the expected behaviour.
If it is the expected behavior indeed, how do I update dynamically the ws-mex at runtime in order to get an wsdl file that now contains the additions policies?


Any help is greatly appreciated..



Thanks,
Best Regards,
alessandro

PS.
Environment: Java 8 + Cfx 2.7.15

public class DynamicPolicyInInterceptor extends
                               AbstractPhaseInterceptor<SoapMessage> {

                public DynamicPolicyInInterceptor() {
                               super(Phase.RECEIVE);
                               getBefore().add(PolicyInInterceptor.class.getName());
                }

                @Override
                public void handleMessage(SoapMessage msg) throws Fault {
                               Policy wsaPolicy = PolicyHelper.parsePolicy(msg, DynamicPolicyFeature.WSDL_NEW_FILE_XML);
                               msg.put(PolicyConstants.POLICY_OVERRIDE, wsaPolicy);
                               System.out.println("inmsg  property ******** " + msg.getContextualProperty(PolicyConstants.POLICY_OVERRIDE));
                               System.out.println("inmsg  property ******** " + msg.get(PolicyConstants.POLICY_OVERRIDE));

                }

public class PolicyHelper {
                private PolicyHelper() {
                }

                public static Policy parsePolicy(SoapMessage msg, String policyPath) {
                               try {
                                               // 1. Load policy as DOM
                                               Element policyElement = loadXMLFileDocument(policyPath).getDocumentElement();
                                               // 2. Parse policy
                                               org.apache.cxf.ws.policy.PolicyBuilder builder = msg.getExchange()
                                                                              .getBus()
                                                                              .getExtension(org.apache.cxf.ws.policy.PolicyBuilder.class);

                                               return builder.getPolicy(policyElement);
                               } catch (Exception e) {
                                               throw new RuntimeException("Cannot parse policy: " + e.getMessage(), e);
                                }
                }

utpolicy.xml:
<?xml version="1.0" encoding="UTF-8"?>
<wsp:Policy xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                <sp:SupportingTokens xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
                    <wsp:Policy>
                        <sp:UsernameToken sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702/IncludeToken/AlwaysToRecipient">
                            <wsp:Policy>
                                <sp:WssUsernameToken11/>
                                                                                                                             <sp:HashPassword/>
                            </wsp:Policy>
                        </sp:UsernameToken>
                    </wsp:Policy>
                </sp:SupportingTokens>
</wsp:Policy>

WSDL:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:messages="http://prototype.common.oia.transportation.almaviva.it/wsprototype/messaggi" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702" xmlns:sp13="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200802" xmlns:tns="http://prototype.common.oia.transportation.almaviva.it/wsprototype/servizi" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://prototype.common.oia.transportation.almaviva.it/wsprototype/servizi">
  <wsdl:types>
    <xsd:schema elementFormDefault="qualified" targetNamespace="http://prototype.common.oia.transportation.almaviva.it/wsprototype/servizi">
                                               <xsd:import namespace="http://prototype.common.oia.transportation.almaviva.it/wsprototype/messaggi" schemaLocation="MessaggiPrototype_V1.0.xsd"/>
                               </xsd:schema>
  </wsdl:types>
  <wsdl:message name="SubscribeSoapIn">
    <wsdl:part element="messages:SubscribeFilter" name="parameter">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="SubscribeSoapOut">
    <wsdl:part element="messages:SubscribeResponse" name="parameter">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="SubscribeSoapFault3">
    <wsdl:part element="messages:SubscribeFault3" name="parameter">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="SubscribeSoapFault2">
    <wsdl:part element="messages:SubscribeFault2" name="parameter">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="SubscribeSoapFault1">
    <wsdl:part element="messages:SubscribeFault1" name="parameter">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="ChannelManagerPort">
    <wsdl:operation name="SubscribeOperation">
      <wsdl:input message="tns:SubscribeSoapIn">
    </wsdl:input>
      <wsdl:output message="tns:SubscribeSoapOut">
    </wsdl:output>
      <wsdl:fault message="tns:SubscribeSoapFault2" name="SubscribeFault2">
    </wsdl:fault>
      <wsdl:fault message="tns:SubscribeSoapFault3" name="SubscribeFault3">
    </wsdl:fault>
      <wsdl:fault message="tns:SubscribeSoapFault1" name="SubscribeFault1">
    </wsdl:fault>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ChannelManagerBinding" type="tns:ChannelManagerPort">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="SubscribeOperation">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input>
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal"/>
      </wsdl:output>
      <wsdl:fault name="SubscribeFault2">
        <soap:fault name="SubscribeFault2" use="literal"/>
      </wsdl:fault>
      <wsdl:fault name="SubscribeFault3">
        <soap:fault name="SubscribeFault3" use="literal"/>
      </wsdl:fault>
      <wsdl:fault name="SubscribeFault1">
        <soap:fault name="SubscribeFault1" use="literal"/>
      </wsdl:fault>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ChannelManager">
    <wsdl:port binding="tns:ChannelManagerBinding" name="ChannelManager">
      <soap:address location="http://localhost:8080/wsprototype/services/ChannelManager"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>