Search in sources :

Example 21 with Assertion

use of org.apache.neethi.Assertion in project cxf by apache.

the class PolicyInInterceptor method handle.

protected void handle(Message msg) {
    Exchange exchange = msg.getExchange();
    Bus bus = exchange.getBus();
    Endpoint e = exchange.getEndpoint();
    if (null == e) {
        LOG.fine("No endpoint.");
        return;
    }
    EndpointInfo ei = e.getEndpointInfo();
    PolicyEngine pe = bus.getExtension(PolicyEngine.class);
    if (null == pe) {
        return;
    }
    List<Interceptor<? extends Message>> interceptors = new ArrayList<>();
    Collection<Assertion> assertions = new ArrayList<>();
    // 1. Check overridden policy
    Policy p = (Policy) msg.getContextualProperty(PolicyConstants.POLICY_OVERRIDE);
    if (p != null) {
        EndpointPolicyImpl endpi = new EndpointPolicyImpl(p);
        EffectivePolicyImpl effectivePolicy = new EffectivePolicyImpl();
        effectivePolicy.initialise(endpi, pe, true, msg);
        msg.put(EffectivePolicy.class, effectivePolicy);
        PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
        interceptors.addAll(effectivePolicy.getInterceptors());
        assertions.addAll(effectivePolicy.getChosenAlternative());
    } else if (MessageUtils.isRequestor(msg)) {
        // 2. Process client policy
        BindingOperationInfo boi = exchange.getBindingOperationInfo();
        if (boi == null) {
            Conduit conduit = exchange.getConduit(msg);
            EndpointPolicy ep = pe.getClientEndpointPolicy(ei, conduit, msg);
            if (ep != null) {
                interceptors.addAll(ep.getInterceptors(msg));
                assertions.addAll(ep.getVocabulary(msg));
            }
        } else {
            // We do not know the underlying message type yet - so we pre-emptively add interceptors
            // that can deal with any resposes or faults returned to this client endpoint.
            EffectivePolicy ep = pe.getEffectiveClientResponsePolicy(ei, boi, msg);
            if (ep != null) {
                interceptors.addAll(ep.getInterceptors());
                // insert assertions of endpoint's vocabulary into message
                if (ep.getPolicy() != null) {
                    msg.put(AssertionInfoMap.class, new AssertionInfoMap(ep.getPolicy()));
                    msg.getInterceptorChain().add(PolicyVerificationInInterceptor.INSTANCE);
                }
            }
        }
    } else {
        // 3. Process server policy
        Destination destination = exchange.getDestination();
        // We do not know the underlying message type yet - so we pre-emptively add interceptors
        // that can deal with any messages to this endpoint
        EndpointPolicy ep = pe.getServerEndpointPolicy(ei, destination, msg);
        if (ep != null) {
            interceptors.addAll(ep.getInterceptors(msg));
            assertions.addAll(ep.getVocabulary(msg));
        }
    }
    // add interceptors into message chain
    for (Interceptor<? extends Message> i : interceptors) {
        msg.getInterceptorChain().add(i);
    }
    // Insert assertions of endpoint's vocabulary into message
    if (!assertions.isEmpty()) {
        msg.put(AssertionInfoMap.class, new AssertionInfoMap(assertions));
        msg.getInterceptorChain().add(PolicyVerificationInInterceptor.INSTANCE);
    }
}
Also used : Policy(org.apache.neethi.Policy) Bus(org.apache.cxf.Bus) Destination(org.apache.cxf.transport.Destination) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) Assertion(org.apache.neethi.Assertion) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) Conduit(org.apache.cxf.transport.Conduit) Interceptor(org.apache.cxf.interceptor.Interceptor)

Example 22 with Assertion

use of org.apache.neethi.Assertion in project cxf by apache.

the class EffectivePolicyImplTest method testInitialiseFromEndpointPolicy.

@Test
public void testInitialiseFromEndpointPolicy() throws NoSuchMethodException {
    Method m = EffectivePolicyImpl.class.getDeclaredMethod("initialiseInterceptors", new Class[] { PolicyEngine.class, Message.class });
    EffectivePolicyImpl effectivePolicy = EasyMock.createMockBuilder(EffectivePolicyImpl.class).addMockedMethod(m).createMock(control);
    EndpointPolicyImpl endpointPolicy = control.createMock(EndpointPolicyImpl.class);
    Policy p = control.createMock(Policy.class);
    EasyMock.expect(endpointPolicy.getPolicy()).andReturn(p);
    Collection<Assertion> chosenAlternative = new ArrayList<>();
    EasyMock.expect(endpointPolicy.getChosenAlternative()).andReturn(chosenAlternative);
    PolicyEngineImpl pe = new PolicyEngineImpl();
    effectivePolicy.initialiseInterceptors(pe, false, msg);
    EasyMock.expectLastCall();
    control.replay();
    effectivePolicy.initialise(endpointPolicy, pe, false, null);
    control.verify();
}
Also used : Policy(org.apache.neethi.Policy) Assertion(org.apache.neethi.Assertion) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 23 with Assertion

use of org.apache.neethi.Assertion in project cxf by apache.

the class EffectivePolicyImplTest method testInitialiseInterceptors.

private void testInitialiseInterceptors(boolean useIn, boolean fault) {
    EffectivePolicyImpl epi = new EffectivePolicyImpl();
    List<Assertion> alternative = new ArrayList<>();
    epi.setChosenAlternative(alternative);
    PolicyEngineImpl engine = control.createMock(PolicyEngineImpl.class);
    PolicyInterceptorProviderRegistry reg = control.createMock(PolicyInterceptorProviderRegistry.class);
    setupPolicyInterceptorProviderRegistry(engine, reg);
    control.replay();
    epi.initialiseInterceptors(engine, useIn, fault, msg);
    assertEquals(0, epi.getInterceptors().size());
    control.verify();
    control.reset();
    setupPolicyInterceptorProviderRegistry(engine, reg);
    List<Interceptor<? extends Message>> il = new ArrayList<>();
    setupRegistryInterceptors(useIn, fault, reg, null, il);
    PolicyAssertion a = control.createMock(PolicyAssertion.class);
    alternative.add(a);
    control.replay();
    epi.initialiseInterceptors(engine, useIn, fault, msg);
    assertEquals(0, epi.getInterceptors().size());
    control.verify();
    control.reset();
    setupPolicyInterceptorProviderRegistry(engine, reg);
    QName qn = new QName("http://x.y.z", "a");
    EasyMock.expect(a.getName()).andReturn(qn);
    il = new ArrayList<>();
    setupRegistryInterceptors(useIn, fault, reg, qn, il);
    control.replay();
    epi.initialiseInterceptors(engine, useIn, fault, msg);
    assertEquals(0, epi.getInterceptors().size());
    control.verify();
    control.reset();
    setupPolicyInterceptorProviderRegistry(engine, reg);
    EasyMock.expect(a.getName()).andReturn(qn);
    Interceptor<Message> pi = control.createMock(Interceptor.class);
    il = new ArrayList<>();
    il.add(pi);
    setupRegistryInterceptors(useIn, fault, reg, qn, il);
    control.replay();
    epi.initialiseInterceptors(engine, useIn, fault, msg);
    assertEquals(1, epi.getInterceptors().size());
    assertSame(pi, epi.getInterceptors().get(0));
    control.verify();
}
Also used : Message(org.apache.cxf.message.Message) QName(javax.xml.namespace.QName) Assertion(org.apache.neethi.Assertion) ArrayList(java.util.ArrayList) Interceptor(org.apache.cxf.interceptor.Interceptor)

Example 24 with Assertion

use of org.apache.neethi.Assertion in project cxf by apache.

the class IgnorablePolicyInterceptorProviderTest method createTestAssertions.

private AssertionInfoMap createTestAssertions() {
    AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(Collections.EMPTY_LIST, PolicyAssertion.class));
    Assertion a = new PrimitiveAssertion(ONEWAY_QNAME);
    Assertion b = new PrimitiveAssertion(DUPLEX_QNAME);
    AssertionInfo ai = new AssertionInfo(a);
    AssertionInfo bi = new AssertionInfo(b);
    aim.put(ONEWAY_QNAME, Collections.singleton(ai));
    aim.put(DUPLEX_QNAME, Collections.singleton(bi));
    return aim;
}
Also used : PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) PrimitiveAssertion(org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion) Assertion(org.apache.neethi.Assertion)

Example 25 with Assertion

use of org.apache.neethi.Assertion in project cxf by apache.

the class PolicyOutInterceptor method handle.

protected void handle(Message msg) {
    Exchange exchange = msg.getExchange();
    Bus bus = exchange.getBus();
    BindingOperationInfo boi = exchange.getBindingOperationInfo();
    if (null == boi) {
        LOG.fine("No binding operation info.");
        return;
    }
    Endpoint e = exchange.getEndpoint();
    if (null == e) {
        LOG.fine("No endpoint.");
        return;
    }
    EndpointInfo ei = e.getEndpointInfo();
    PolicyEngine pe = bus.getExtension(PolicyEngine.class);
    if (null == pe) {
        return;
    }
    Collection<Assertion> assertions = new ArrayList<>();
    // 1. Check overridden policy
    Policy p = (Policy) msg.getContextualProperty(PolicyConstants.POLICY_OVERRIDE);
    if (p != null) {
        EndpointPolicyImpl endpi = new EndpointPolicyImpl(p);
        EffectivePolicyImpl effectivePolicy = new EffectivePolicyImpl();
        effectivePolicy.initialise(endpi, pe, false, msg);
        msg.put(EffectivePolicy.class, effectivePolicy);
        PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
        addInterceptors(effectivePolicy.getInterceptors(), msg);
        assertions.addAll(effectivePolicy.getChosenAlternative());
    } else if (MessageUtils.isRequestor(msg)) {
        // 2. Process client policy
        Conduit conduit = exchange.getConduit(msg);
        // add the required interceptors
        EffectivePolicy effectivePolicy = pe.getEffectiveClientRequestPolicy(ei, boi, conduit, msg);
        msg.put(EffectivePolicy.class, effectivePolicy);
        if (effectivePolicy != null) {
            PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
            addInterceptors(effectivePolicy.getInterceptors(), msg);
            assertions.addAll(effectivePolicy.getChosenAlternative());
        }
    } else {
        // 3. Process server policy
        Destination destination = exchange.getDestination();
        List<List<Assertion>> incoming = CastUtils.cast((List<?>) exchange.get("ws-policy.validated.alternatives"));
        EffectivePolicy effectivePolicy = pe.getEffectiveServerResponsePolicy(ei, boi, destination, incoming, msg);
        msg.put(EffectivePolicy.class, effectivePolicy);
        if (effectivePolicy != null) {
            PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
            addInterceptors(effectivePolicy.getInterceptors(), msg);
            assertions.addAll(effectivePolicy.getChosenAlternative());
        }
    }
    // insert assertions of endpoint's fault vocabulary into message
    if (!assertions.isEmpty()) {
        if (LOG.isLoggable(Level.FINEST)) {
            StringBuilder buf = new StringBuilder();
            buf.append("Chosen alternative: ");
            String nl = SystemPropertyAction.getProperty("line.separator");
            buf.append(nl);
            for (Assertion a : assertions) {
                PolicyUtils.printPolicyComponent(a, buf, 1);
            }
            LOG.finest(buf.toString());
        }
        msg.put(AssertionInfoMap.class, new AssertionInfoMap(assertions));
        msg.getInterceptorChain().add(PolicyVerificationOutInterceptor.INSTANCE);
    }
}
Also used : Policy(org.apache.neethi.Policy) Bus(org.apache.cxf.Bus) Destination(org.apache.cxf.transport.Destination) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Assertion(org.apache.neethi.Assertion) ArrayList(java.util.ArrayList) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) Conduit(org.apache.cxf.transport.Conduit) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Assertion (org.apache.neethi.Assertion)64 Policy (org.apache.neethi.Policy)27 Message (org.apache.cxf.message.Message)25 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)23 QName (javax.xml.namespace.QName)21 Interceptor (org.apache.cxf.interceptor.Interceptor)19 PrimitiveAssertion (org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion)19 PolicyContainingAssertion (org.apache.neethi.PolicyContainingAssertion)9 Element (org.w3c.dom.Element)9 MessageImpl (org.apache.cxf.message.MessageImpl)7 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)7 All (org.apache.neethi.All)7 ExactlyOne (org.apache.neethi.ExactlyOne)7 Bus (org.apache.cxf.Bus)6 InterceptorChain (org.apache.cxf.interceptor.InterceptorChain)6 PolicyEngine (org.apache.cxf.ws.policy.PolicyEngine)6 PolicyContainingPrimitiveAssertion (org.apache.neethi.builders.PolicyContainingPrimitiveAssertion)6 XMLPrimitiveAssertionBuilder (org.apache.neethi.builders.xml.XMLPrimitiveAssertionBuilder)6 List (java.util.List)5