Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

This class represents an assertion without any attributes or child elements (in particular without a nested Policy element). The AnonymousResponses or NonAnonymousResponses assertions in the addressing metadata namespace http://www.w3.org/2007/01/addressing/metadataImage Removed are example of this type of assertions. The implementation of the equal and normalize methods in the class are trivial, and there should be no need to extend this class.

...

Implementing and Registering the AssertionBuilder Interface

Assertion Builder class for custom assertion should implement AssertionBuilder<T> interface. The interface type can be Element, XMLStreamReader or OMElement.
Interface contains two methods: build() and getKnownElements().
Implementation of build() method should construct Assertion from the incoming type. It can be PrimitiveAssertion (without attributes or child elements), NestedPrimitiveAssertion (without attributes but with nested policy element) and JaxbAssertion (assertion described by any XML schema).
getKnownElements() method must return QNames of assertion elements from which assertion can be built.

Implementing the build method of the AssertionBuilder interface is straightforward (in the case of JaxbAssertions you can extend the JaxbAssertionBuilder class, which provides an appropriate JAXB context and some other useful methods).

...

Code Block
java
java
package mycompany.com.interceptors;
import org.apache.cxf.ws.policy.AssertionInfoMap;

class MyPolicyAwareInterceptor {
   static final QName assertionType = new QName("http://mycompany.com}", 
       "MyType"});

   public void handleMessage(Message message) {

      // get AssertionInfoMap
       org.apache.cxf.ws.policy.AssertionInfoMap aim = 
           message.get(org.apache.cxf.ws.policy.AssertionInfoMap.class);
       Collection<AssertionInfo ais> = aim.get(assertionType );

       // extract Assertion information
       for (AssertionInfo ai : ais) {
           org.apache.neethi.Assertion a = ai.getAssertion();
           MyAssertionType ma = (MyAssertionType)a;
          // digest ....
       }

       // process message ...
            // express support

       for (AssertionInfo ai : ais) {
           ai.setAsserted(...);
       }          }
}

Sometimes, it may be more convenient to spead the above functionality accross several interceptors, possibly according to chain (in, in fault, out, outfault). In any case, you need to also provide a PolicyInterceptorProvider, and declare a corresponding bean. Either implement one from scratch or use the PolicyInterceptorProviderImpl in the api package and customise it as follows (assuming that one and the same interceptor is used for all paths). The main task of policy interceptor provider is to say which interceptors must be activated for specified policy assertion:

Code Block
xml
xml

<bean name="MyPolicyAwareInterceptor" "
      class="mycompany.com.interceptors.MyPolicyAwareInterceptor"/>

<bean class="org.apache.cxf.ws.policy.PolicyInterceptorProviderImpl">
        <constructor-arg>
            <!-- the list of assertion types supported by this 
                 PolicyInterceptorProvider -->
            <list>
                <bean class="javax.xml.namespace.QName">
                    <constructor-arg value="http://mycompany.com}"/>
                    <constructor-arg value="MyType"/>
                </bean>
            </list>
        </constructor-arg>
        <property name="inInterceptors">
            <list>
                <ref bean="MyPolicyAwareInterceptor"/>
            </list>
        </property>
        <property name="inFaultInterceptors">
            <list>
                <ref bean="MyPolicyAwareInterceptor"/>
            </list>
        </property>
        <property name="outInterceptors">
            <list>
                <ref bean="MyPolicyAwareInterceptor"/>
            </list>
        </property>
        <property name="outFaultInterceptors">
            <list>
                <ref bean="MyPolicyAwareInterceptor"/>
            </list>
        </property>
    </bean>

All beans of type PolicyInterceptorProvider are automatically registered with the framework's PolicyInterceptorProviderRegistry.

It is also possible to implement policy interceptor provider programmatically from scratch. It's constructor gives assertions QNames as argument of super constructor and adds corresponded interceptors using getters:

Code Block

public class MyInterceptorProvider extends AbstractPolicyInterceptorProvider {
    private static final long serialVersionUID = -5248428637449096540L;
    private static final MyInInterceptor IN_INTERCEPTOR = new MyInInterceptor();
    private static final MyOutInterceptor OUT_INTERCEPTOR = new MyOutInterceptor();
    private static final MyOutFaultInterceptor OUT_FAULT_INTERCEPTOR = new MyOutFaultInterceptor();
    
    private static final Collection<QName> ASSERTION_TYPES;
    static {
        ASSERTION_TYPES = new ArrayList<QName>();
        ASSERTION_TYPES.add(new QName("www.mycompany.org", "myassertion"));
    }

    public MyInterceptorProvider() {
        super(ASSERTION_TYPES);
        getInInterceptors().add(IN_INTERCEPTOR);        
        getOutInterceptors().add(OUT_INTERCEPTOR);        
        getOutFaultInterceptors().add(OUT_FAULT_INTERCEPTOR);        
    }
}

Since version 2.5.2, Assertion builder and policy interceptor provider can be registered using CXF bus extension mechanism: just create a file META-INF/cxf/bus-extensions.txt containing the following:

Code Block

org.company.MyInterceptorProvider::true
org.company.MyAssertionBuilder::true

Boolean value at the end specifies lazy loading strategy.
CXF automatically recognizes the assertion builder and policy interceptor provider and store them into registries: AssertionBuilderRegistry and PolicyInterceptorProviderRegistry.
Since CXF 2.6.0 it is possible to register multiple interceptor providers for single assertion.

Implementing a Policy-Aware Conduit/Destination

...

Code Block
java
java
class MyPolicyAwareConduit {
    static final QName assertionType = new QName("http://mycompany.com}", 
        "MyType"});
    ...

    void init() {
        PolicyEngine engine = bus.getExtenation(PolicyEngine.class);
        if (null != engine && engine.isEnabled()) {
        EffectiveEndpointPolicy ep = engine.getEndpointPolicy(endpoint, this);
        Collection<Assertion> as = ep.getChosenAlternative();
        for (Assertion a : as) {
            if (assertType.equals(a.getName()) {
                // do something with it ...
            }
       }
       ... 
    }
}

...

Code Block
java
java
class MyPolicyAwareConduit implements Assertor {
    static final QName MYTYPE = new QName("http://mycompany.com}", 
        "MyType"});

    public boolean canAssert(QName name) {
        return MTYPE.equals(name);
    }

    public void assertMessage(Mesage message) {
        AssertionInfoMap = message.get(AssertioninfoMap.class);
        ...
    }
}