Search in sources :

Example 16 with Policy

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

the class Wsdl11AttachmentPolicyProviderTest method testResolveExternal.

@Test
public void testResolveExternal() {
    // service has one extension of type PolicyReference, reference is external
    Policy p = app.getElementPolicy(services[17]);
    verifyAssertionsOnly(p, 2);
    // referenced document does not contain policy with the required if
    try {
        app.getElementPolicy(endpoints[17]);
        fail("Expected PolicyException not thrown.");
    } catch (PolicyException ex) {
    // expected
    }
    // referenced document cannot be found
    try {
        app.getElementPolicy(endpoints[17].getBinding());
        fail("Expected PolicyException not thrown.");
    } catch (PolicyException ex) {
    // expected
    }
}
Also used : Policy(org.apache.neethi.Policy) PolicyException(org.apache.cxf.ws.policy.PolicyException) Test(org.junit.Test)

Example 17 with Policy

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

the class Wsdl11AttachmentPolicyProviderTest method testEffectiveMessagePolicies.

@Test
public void testEffectiveMessagePolicies() {
    Policy ep;
    // binding operation message has no extensions
    // operation message has no extensions
    // message has no extensions
    ep = app.getEffectivePolicy(getBindingMessageInfo(endpoints[0], true), null);
    assertTrue(ep == null || ep.isEmpty());
    // binding operation message has one extension of type Policy
    // operation message has no extensions
    // message has no extensions
    ep = app.getEffectivePolicy(getBindingMessageInfo(endpoints[12], true), null);
    assertTrue(!ep.isEmpty());
    verifyAssertionsOnly(ep, 1);
    // binding operation message has no extensions
    // operation message has one extension of type Policy
    // message has no extensions
    ep = app.getEffectivePolicy(getBindingMessageInfo(endpoints[13], true), null);
    assertTrue(!ep.isEmpty());
    verifyAssertionsOnly(ep, 1);
    // binding operation message has no extensions
    // operation message has no extensions
    // message has one extension of type Policy
    ep = app.getEffectivePolicy(getBindingMessageInfo(endpoints[14], true), null);
    assertTrue(!ep.isEmpty());
    verifyAssertionsOnly(ep, 1);
    // binding operation message has one extension of type Policy
    // operation message has one extension of type Policy
    // message has one extension of type Policy
    ep = app.getEffectivePolicy(getBindingMessageInfo(endpoints[15], true), null);
    assertTrue(!ep.isEmpty());
    verifyAssertionsOnly(ep, 3);
}
Also used : Policy(org.apache.neethi.Policy) Test(org.junit.Test)

Example 18 with Policy

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

the class MinimalMaximalAlternativeSelectorTest method testChooseMaxAlternative.

@Test
public void testChooseMaxAlternative() {
    Message m = new MessageImpl();
    AlternativeSelector selector = new MaximalAlternativeSelector();
    PolicyEngine engine = control.createMock(PolicyEngine.class);
    Assertor assertor = control.createMock(Assertor.class);
    Policy policy = new Policy();
    ExactlyOne ea = new ExactlyOne();
    All all = new All();
    PolicyAssertion a1 = new TestAssertion();
    all.addAssertion(a1);
    ea.addPolicyComponent(all);
    Collection<PolicyAssertion> maxAlternative = CastUtils.cast(all.getPolicyComponents(), PolicyAssertion.class);
    all = new All();
    ea.addPolicyComponent(all);
    Collection<PolicyAssertion> minAlternative = CastUtils.cast(all.getPolicyComponents(), PolicyAssertion.class);
    policy.addPolicyComponent(ea);
    EasyMock.expect(engine.supportsAlternative(maxAlternative, assertor, m)).andReturn(true);
    EasyMock.expect(engine.supportsAlternative(minAlternative, assertor, m)).andReturn(true);
    control.replay();
    Collection<Assertion> choice = selector.selectAlternative(policy, engine, assertor, null, m);
    assertEquals(1, choice.size());
    assertSame(a1, choice.iterator().next());
    control.verify();
}
Also used : Policy(org.apache.neethi.Policy) All(org.apache.neethi.All) PolicyAssertion(org.apache.cxf.ws.policy.PolicyAssertion) Message(org.apache.cxf.message.Message) Assertion(org.apache.neethi.Assertion) TestAssertion(org.apache.cxf.ws.policy.TestAssertion) PolicyAssertion(org.apache.cxf.ws.policy.PolicyAssertion) PolicyEngine(org.apache.cxf.ws.policy.PolicyEngine) ExactlyOne(org.apache.neethi.ExactlyOne) AlternativeSelector(org.apache.cxf.ws.policy.AlternativeSelector) Assertor(org.apache.cxf.ws.policy.Assertor) TestAssertion(org.apache.cxf.ws.policy.TestAssertion) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 19 with Policy

use of org.apache.neethi.Policy 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<Interceptor<? extends Message>>();
    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 20 with Policy

use of org.apache.neethi.Policy 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 (null != assertions && !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

Policy (org.apache.neethi.Policy)122 Test (org.junit.Test)47 Assertion (org.apache.neethi.Assertion)27 QName (javax.xml.namespace.QName)23 ArrayList (java.util.ArrayList)21 All (org.apache.neethi.All)18 ExactlyOne (org.apache.neethi.ExactlyOne)18 Message (org.apache.cxf.message.Message)15 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)14 Element (org.w3c.dom.Element)13 Bus (org.apache.cxf.Bus)12 PrimitiveAssertion (org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion)12 List (java.util.List)9 Interceptor (org.apache.cxf.interceptor.Interceptor)9 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)9 HashMap (java.util.HashMap)7 OMElement (org.apache.axiom.om.OMElement)7 MessageImpl (org.apache.cxf.message.MessageImpl)7 ReferenceResolver (org.apache.cxf.ws.policy.attachment.reference.ReferenceResolver)6 InputStream (java.io.InputStream)5