Search in sources :

Example 76 with BindingOperationInfo

use of org.apache.cxf.service.model.BindingOperationInfo 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)

Example 77 with BindingOperationInfo

use of org.apache.cxf.service.model.BindingOperationInfo in project cxf by apache.

the class PolicyVerificationInInterceptor method handle.

/**
 * Determines the effective policy, and checks if one of its alternatives
 * is supported.
 *
 * @param message
 * @throws PolicyException if none of the alternatives is supported
 */
protected void handle(Message message) {
    AssertionInfoMap aim = message.get(AssertionInfoMap.class);
    if (null == aim) {
        return;
    }
    Exchange exchange = message.getExchange();
    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;
    }
    Bus bus = exchange.getBus();
    PolicyEngine pe = bus.getExtension(PolicyEngine.class);
    if (null == pe) {
        return;
    }
    if (MessageUtils.isPartialResponse(message)) {
        LOG.fine("Not verifying policies on inbound partial response.");
        return;
    }
    getTransportAssertions(message);
    EffectivePolicy effectivePolicy = message.get(EffectivePolicy.class);
    if (effectivePolicy == null) {
        EndpointInfo ei = e.getEndpointInfo();
        if (MessageUtils.isRequestor(message)) {
            effectivePolicy = pe.getEffectiveClientResponsePolicy(ei, boi, message);
        } else {
            effectivePolicy = pe.getEffectiveServerRequestPolicy(ei, boi, message);
        }
    }
    try {
        List<List<Assertion>> usedAlternatives = aim.checkEffectivePolicy(effectivePolicy.getPolicy());
        if (usedAlternatives != null && !usedAlternatives.isEmpty() && message.getExchange() != null) {
            message.getExchange().put("ws-policy.validated.alternatives", usedAlternatives);
        }
    } catch (PolicyException ex) {
        LOG.log(Level.SEVERE, "Inbound policy verification failed: " + ex.getMessage());
        // exception to pass jaxws2.2 tests
        if (ex.getMessage().indexOf("Addressing") > -1) {
            throw new Fault("A required header representing a Message Addressing Property " + "is not present", LOG).setFaultCode(new QName("http://www.w3.org/2005/08/addressing", "MessageAddressingHeaderRequired"));
        }
        throw ex;
    }
    LOG.fine("Verified policies for inbound message.");
}
Also used : Bus(org.apache.cxf.Bus) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) QName(javax.xml.namespace.QName) Fault(org.apache.cxf.interceptor.Fault) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) List(java.util.List)

Example 78 with BindingOperationInfo

use of org.apache.cxf.service.model.BindingOperationInfo in project cxf by apache.

the class ServerPolicyOutFaultInterceptor method handle.

protected void handle(Message msg) {
    if (MessageUtils.isRequestor(msg)) {
        LOG.fine("Is a requestor.");
        return;
    }
    Exchange exchange = msg.getExchange();
    assert null != exchange;
    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();
    Bus bus = exchange.getBus();
    PolicyEngine pe = bus.getExtension(PolicyEngine.class);
    if (null == pe) {
        return;
    }
    Destination destination = exchange.getDestination();
    Exception ex = exchange.get(Exception.class);
    List<Interceptor<? extends Message>> faultInterceptors = 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, false, true, msg);
        PolicyUtils.logPolicy(LOG, Level.FINEST, "Using effective policy: ", effectivePolicy.getPolicy());
        faultInterceptors.addAll(effectivePolicy.getInterceptors());
        assertions.addAll(effectivePolicy.getChosenAlternative());
    } else {
        // 2. Process effective server policy
        BindingFaultInfo bfi = getBindingFaultInfo(msg, ex, boi);
        if (bfi == null && msg.get(FaultMode.class) != FaultMode.UNCHECKED_APPLICATION_FAULT && msg.get(FaultMode.class) != FaultMode.CHECKED_APPLICATION_FAULT) {
            return;
        }
        EffectivePolicy effectivePolicy = pe.getEffectiveServerFaultPolicy(ei, boi, bfi, destination, msg);
        if (effectivePolicy != null) {
            faultInterceptors.addAll(effectivePolicy.getInterceptors());
            assertions.addAll(effectivePolicy.getChosenAlternative());
        }
    }
    // add interceptors into message chain
    for (Interceptor<? extends Message> oi : faultInterceptors) {
        msg.getInterceptorChain().add(oi);
        LOG.log(Level.FINE, "Added interceptor of type {0}", oi.getClass().getSimpleName());
    }
    // insert assertions of the chosen alternative into the message
    if (null != assertions && !assertions.isEmpty()) {
        msg.put(AssertionInfoMap.class, new AssertionInfoMap(assertions));
    }
}
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) FaultMode(org.apache.cxf.message.FaultMode) Endpoint(org.apache.cxf.endpoint.Endpoint) Interceptor(org.apache.cxf.interceptor.Interceptor) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo)

Example 79 with BindingOperationInfo

use of org.apache.cxf.service.model.BindingOperationInfo in project cxf by apache.

the class PolicyEngineTest method testSetEffectiveClientResponsePolicy.

@Test
public void testSetEffectiveClientResponsePolicy() throws Exception {
    engine = new PolicyEngineImpl();
    EndpointInfo ei = createMockEndpointInfo();
    BindingOperationInfo boi = createMockBindingOperationInfo();
    EffectivePolicy epi = control.createMock(EffectivePolicy.class);
    control.replay();
    engine.setEffectiveClientResponsePolicy(ei, boi, epi);
    assertSame(epi, engine.getEffectiveClientResponsePolicy(ei, boi, msg));
    control.verify();
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Test(org.junit.Test)

Example 80 with BindingOperationInfo

use of org.apache.cxf.service.model.BindingOperationInfo in project cxf by apache.

the class PolicyEngineTest method testGetEffectiveServerRequestPolicyInfo.

@Test
public void testGetEffectiveServerRequestPolicyInfo() throws Exception {
    Method m = PolicyEngineImpl.class.getDeclaredMethod("createOutPolicyInfo", new Class[] {});
    engine = EasyMock.createMockBuilder(PolicyEngineImpl.class).addMockedMethod(m).createMock(control);
    engine.init();
    EndpointInfo ei = createMockEndpointInfo();
    BindingOperationInfo boi = createMockBindingOperationInfo();
    EffectivePolicyImpl epi = control.createMock(EffectivePolicyImpl.class);
    EasyMock.expect(engine.createOutPolicyInfo()).andReturn(epi);
    epi.initialise(ei, boi, engine, false, true, msg);
    EasyMock.expectLastCall();
    control.replay();
    assertSame(epi, engine.getEffectiveServerRequestPolicy(ei, boi, msg));
    assertSame(epi, engine.getEffectiveServerRequestPolicy(ei, boi, msg));
    control.verify();
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)214 QName (javax.xml.namespace.QName)82 BindingInfo (org.apache.cxf.service.model.BindingInfo)57 Test (org.junit.Test)55 Exchange (org.apache.cxf.message.Exchange)50 OperationInfo (org.apache.cxf.service.model.OperationInfo)47 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)42 Endpoint (org.apache.cxf.endpoint.Endpoint)41 Message (org.apache.cxf.message.Message)36 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)36 BindingMessageInfo (org.apache.cxf.service.model.BindingMessageInfo)32 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)31 Service (org.apache.cxf.service.Service)29 Fault (org.apache.cxf.interceptor.Fault)24 MessageInfo (org.apache.cxf.service.model.MessageInfo)24 MessageContentsList (org.apache.cxf.message.MessageContentsList)23 Method (java.lang.reflect.Method)22 SoapOperationInfo (org.apache.cxf.binding.soap.model.SoapOperationInfo)22 ArrayList (java.util.ArrayList)21 BindingFaultInfo (org.apache.cxf.service.model.BindingFaultInfo)16