Java Code Examples for org.apache.cxf.interceptor.InterceptorChain#add()

The following examples show how to use org.apache.cxf.interceptor.InterceptorChain#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PolicyInterceptorsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientPolicyOutInterceptor() {
    PolicyOutInterceptor interceptor = new PolicyOutInterceptor();

    doTestBasics(interceptor, true, true);

    control.reset();
    setupMessage(true, true, true, true, true, true);
    EffectivePolicy effectivePolicy = control.createMock(EffectivePolicy.class);
    EasyMock.expect(pe.getEffectiveClientRequestPolicy(ei, boi, conduit, message))
        .andReturn(effectivePolicy);
    List<Interceptor<? extends Message>> li = createMockInterceptorList();
    EasyMock.expect(effectivePolicy.getInterceptors())
        .andReturn(li);
    InterceptorChain ic = control.createMock(InterceptorChain.class);
    EasyMock.expect(message.getInterceptorChain()).andReturn(ic);
    ic.add(li.get(0));
    EasyMock.expectLastCall();
    Collection<Assertion> assertions =
        CastUtils.cast(Collections.EMPTY_LIST, Assertion.class);
    EasyMock.expect(effectivePolicy.getChosenAlternative()).andReturn(assertions);
    control.replay();
    interceptor.handleMessage(message);
    control.verify();
}
 
Example 2
Source File: PolicyInterceptorsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientPolicyInInterceptor() {
    PolicyInInterceptor interceptor = new PolicyInInterceptor();

    doTestBasics(interceptor, true, false);

    control.reset();
    setupMessage(true, true, true, true, true, true);
    EffectivePolicy effectivePolicy = control.createMock(EffectivePolicy.class);
    EasyMock.expect(pe.getEffectiveClientResponsePolicy(ei, boi, message)).andReturn(effectivePolicy);
    EasyMock.expect(effectivePolicy.getPolicy()).andReturn(new Policy()).times(2);
    Interceptor<? extends Message> i = control.createMock(Interceptor.class);
    List<Interceptor<? extends Message>> lst = new ArrayList<>();
    lst.add(i);
    EasyMock.expect(effectivePolicy.getInterceptors()).andReturn(lst);
    InterceptorChain ic = control.createMock(InterceptorChain.class);
    EasyMock.expect(message.getInterceptorChain()).andReturn(ic).anyTimes();
    ic.add(i);
    EasyMock.expectLastCall();
    message.put(EasyMock.eq(AssertionInfoMap.class), EasyMock.isA(AssertionInfoMap.class));
    EasyMock.expectLastCall();
    ic.add(PolicyVerificationInInterceptor.INSTANCE);
    control.replay();
    interceptor.handleMessage(message);
    control.verify();
}
 
Example 3
Source File: PolicyInterceptorsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientPolicyInFaultInterceptor() {
    ClientPolicyInFaultInterceptor interceptor = new ClientPolicyInFaultInterceptor();

    doTestBasics(interceptor, true, false);

    control.reset();
    setupMessage(true, true, false, false, true, true);
    EndpointPolicy endpointPolicy = control.createMock(EndpointPolicy.class);
    EasyMock.expect(pe.getClientEndpointPolicy(ei, conduit, message)).andReturn(endpointPolicy);
    List<Interceptor<? extends Message>> li = createMockInterceptorList();
    EasyMock.expect(endpointPolicy.getFaultInterceptors(message))
        .andReturn(li);
    InterceptorChain ic = control.createMock(InterceptorChain.class);
    EasyMock.expect(message.getInterceptorChain()).andReturn(ic);
    ic.add(li.get(0));
    EasyMock.expectLastCall();
    Collection<Assertion> assertions =
        CastUtils.cast(Collections.EMPTY_LIST, Assertion.class);
    EasyMock.expect(endpointPolicy.getFaultVocabulary(message)).andReturn(assertions);
    control.replay();
    interceptor.handleMessage(message);
    control.verify();
}
 
Example 4
Source File: PolicyInterceptorsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testServerPolicyInInterceptor() {
    PolicyInInterceptor interceptor = new PolicyInInterceptor();

    doTestBasics(interceptor, false, false);

    control.reset();
    setupMessage(false, false, false, false, true, true);
    EndpointPolicy endpointPolicy = control.createMock(EndpointPolicyImpl.class);
    EasyMock.expect(pe.getServerEndpointPolicy(ei, destination, message)).andReturn(endpointPolicy);
    List<Interceptor<? extends Message>> li = createMockInterceptorList();
    EasyMock.expect(endpointPolicy.getInterceptors(message))
        .andReturn(li);
    InterceptorChain ic = control.createMock(InterceptorChain.class);
    EasyMock.expect(message.getInterceptorChain()).andReturn(ic);
    ic.add(li.get(0));
    EasyMock.expectLastCall();
    Collection<Assertion> assertions =
        CastUtils.cast(Collections.EMPTY_LIST, Assertion.class);
    EasyMock.expect(endpointPolicy.getVocabulary(message)).andReturn(assertions);
    control.replay();
    interceptor.handleMessage(message);
    control.verify();
}
 
Example 5
Source File: PolicyInterceptorsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testServerPolicyOutInterceptor() {
    PolicyOutInterceptor interceptor = new PolicyOutInterceptor();

    doTestBasics(interceptor, false, true);

    control.reset();
    setupMessage(false, false, true, true, true, true);
    EffectivePolicy effectivePolicy = control.createMock(EffectivePolicy.class);
    EasyMock.expect(pe.getEffectiveServerResponsePolicy(ei, boi, destination, null, message))
        .andReturn(effectivePolicy);
    List<Interceptor<? extends Message>> li = createMockInterceptorList();
    EasyMock.expect(effectivePolicy.getInterceptors())
        .andReturn(li);
    InterceptorChain ic = control.createMock(InterceptorChain.class);
    EasyMock.expect(message.getInterceptorChain()).andReturn(ic);
    ic.add(li.get(0));
    EasyMock.expectLastCall();
    Collection<Assertion> assertions =
        CastUtils.cast(Collections.EMPTY_LIST, Assertion.class);
    EasyMock.expect(effectivePolicy.getChosenAlternative()).andReturn(assertions);
    control.replay();
    interceptor.handleMessage(message);
    control.verify();
}
 
Example 6
Source File: AbstractEndpointSelectionInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void handleMessage(Message message) {
    Exchange ex = message.getExchange();
    Set<Endpoint> endpoints = CastUtils.cast((Set<?>)ex.get(MultipleEndpointObserver.ENDPOINTS));

    Endpoint ep = selectEndpoint(message, endpoints);

    if (ep == null) {
        return;
    }

    ex.put(Endpoint.class, ep);
    ex.put(Binding.class, ep.getBinding());
    ex.put(Service.class, ep.getService());

    InterceptorChain chain = message.getInterceptorChain();
    chain.add(ep.getInInterceptors());
    chain.add(ep.getBinding().getInInterceptors());
    chain.add(ep.getService().getInInterceptors());

    chain.setFaultObserver(ep.getOutFaultObserver());
}
 
Example 7
Source File: ChainInitiationObserver.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void addToChain(InterceptorChain chain, Message m) {
    Collection<InterceptorProvider> providers
        = CastUtils.cast((Collection<?>)m.get(Message.INTERCEPTOR_PROVIDERS));
    if (providers != null) {
        for (InterceptorProvider p : providers) {
            chain.add(p.getInInterceptors());
        }
    }
    Collection<Interceptor<? extends Message>> is
        = CastUtils.cast((Collection<?>)m.get(Message.IN_INTERCEPTORS));
    if (is != null) {
        chain.add(is);
    }
    if (m.getDestination() instanceof InterceptorProvider) {
        chain.add(((InterceptorProvider)m.getDestination()).getInInterceptors());
    }
}
 
Example 8
Source File: ClientImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void modifyChain(InterceptorChain chain, Message ctx, boolean in) {
    if (ctx == null) {
        return;
    }
    Collection<InterceptorProvider> providers
        = CastUtils.cast((Collection<?>)ctx.get(Message.INTERCEPTOR_PROVIDERS));
    if (providers != null) {
        for (InterceptorProvider p : providers) {
            if (in) {
                chain.add(p.getInInterceptors());
            } else {
                chain.add(p.getOutInterceptors());
            }
        }
    }
    String key = in ? Message.IN_INTERCEPTORS : Message.OUT_INTERCEPTORS;
    Collection<Interceptor<? extends Message>> is
        = CastUtils.cast((Collection<?>)ctx.get(key));
    if (is != null) {
        chain.add(is);
    }
}
 
Example 9
Source File: PolicyInterceptorsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void coachPolicyOverride(boolean in, boolean fault) {
    Assertion assertion = control.createMock(Assertion.class);
    EasyMock.expect(assertion.getName()).andReturn(ASSERTION_QNAME);
    Collection<Assertion> assertions =
        new ArrayList<>();
    assertions.add(assertion);

    Policy policyOverride = control.createMock(Policy.class);
    EasyMock.expect(message.getContextualProperty(PolicyConstants.POLICY_OVERRIDE))
        .andReturn(policyOverride);
    AlternativeSelector selector = control.createMock(AlternativeSelector.class);
    EasyMock.expect(selector.selectAlternative(policyOverride, pe, null, null, message)).andReturn(assertions);
    EasyMock.expect(pe.getAlternativeSelector()).andReturn(selector);
    EasyMock.expect(pe.getBus()).andReturn(bus).anyTimes();
    PolicyInterceptorProviderRegistry reg = control
        .createMock(PolicyInterceptorProviderRegistry.class);
    EasyMock.expect(bus.getExtension(PolicyInterceptorProviderRegistry.class)).andReturn(reg);

    List<Interceptor<? extends Message>> li = createMockInterceptorList();
    if (in && fault) {
        EasyMock.expect(reg.getInFaultInterceptorsForAssertion(ASSERTION_QNAME)).andReturn(li);
    } else if (!in && fault) {
        EasyMock.expect(reg.getOutFaultInterceptorsForAssertion(ASSERTION_QNAME)).andReturn(li);
    } else if (in && !fault) {
        EasyMock.expect(reg.getInInterceptorsForAssertion(ASSERTION_QNAME)).andReturn(li);
    } else if (!in && !fault) {
        EasyMock.expect(reg.getOutInterceptorsForAssertion(ASSERTION_QNAME)).andReturn(li);
    }
    InterceptorChain ic = control.createMock(InterceptorChain.class);
    EasyMock.expect(message.getInterceptorChain()).andReturn(ic).anyTimes();
    ic.add(li.get(0));
    EasyMock.expectLastCall();
}
 
Example 10
Source File: ResponseTimeMessageOutInterceptorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientOneWayMessageOut() {
    //need to increase the counter and is a client
    setupCounterRepository(true, true);
    setupExchangeForMessage();
    setupOperationForMessage();
    EasyMock.expect(message.getExchange()).andReturn(exchange).anyTimes();
    EasyMock.expect(message.get(Message.PARTIAL_RESPONSE_MESSAGE)).andReturn(Boolean.FALSE).anyTimes();
    EasyMock.expect(message.get(Message.REQUESTOR_ROLE)).andReturn(Boolean.TRUE).anyTimes();
    EasyMock.expect(exchange.getOutMessage()).andReturn(message).anyTimes();
    EasyMock.expect(exchange.get(FaultMode.class)).andReturn(null);
    EasyMock.expect(exchange.get(Exception.class)).andReturn(null);
    EasyMock.expect(exchange.isOneWay()).andReturn(true).anyTimes();
    MessageHandlingTimeRecorder mhtr = EasyMock.createMock(MessageHandlingTimeRecorder.class);
    EasyMock.expect(exchange.get(MessageHandlingTimeRecorder.class)).andReturn(mhtr).anyTimes();
    EasyMock.expect(exchange.get("org.apache.cxf.management.counter.enabled")).andReturn(null);

    InterceptorChain chain = EasyMock.createMock(InterceptorChain.class);
    EasyMock.expect(message.getInterceptorChain()).andReturn(chain);
    chain.add(EasyMock.isA(ResponseTimeMessageOutInterceptor.EndingInterceptor.class));
    EasyMock.expectLastCall();
    EasyMock.replay(exchange);
    EasyMock.replay(message);

    rtmoi.handleMessage(message);
    rtmoi.getEndingInterceptor().handleMessage(message);

    EasyMock.verify(message);
    EasyMock.verify(bus);
    EasyMock.verify(exchange);
}
 
Example 11
Source File: JAXRSBeanValidationInInterceptor.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(ContainerRequestContext context) throws IOException {
    InterceptorChain chain = PhaseInterceptorChain.getCurrentMessage().getInterceptorChain();
    chain.add(this);
}
 
Example 12
Source File: PolicyInterceptorsTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testServerPolicyOutFaultInterceptor() throws NoSuchMethodException {
    Method m = AbstractPolicyInterceptor.class.getDeclaredMethod("getBindingFaultInfo",
        new Class[] {Message.class, Exception.class, BindingOperationInfo.class});

    ServerPolicyOutFaultInterceptor interceptor =
        EasyMock.createMockBuilder(ServerPolicyOutFaultInterceptor.class)
            .addMockedMethod(m).createMock(control);

    doTestBasics(interceptor, false, true);

    control.reset();
    setupMessage(false, false, true, true, true, true);
    Exception ex = control.createMock(Exception.class);
    EasyMock.expect(exchange.get(Exception.class)).andReturn(ex);
    EasyMock.expect(interceptor.getBindingFaultInfo(message, ex, boi)).andReturn(null);
    control.replay();
    interceptor.handleMessage(message);
    control.verify();

    control.reset();
    setupMessage(false, false, true, true, true, true);
    // Exception ex = control.createMock(Exception.class);
    EasyMock.expect(exchange.get(Exception.class)).andReturn(ex);
    BindingFaultInfo bfi = control.createMock(BindingFaultInfo.class);
    EasyMock.expect(interceptor.getBindingFaultInfo(message, ex, boi)).andReturn(bfi);
    EffectivePolicy effectivePolicy = control.createMock(EffectivePolicyImpl.class);
    EasyMock.expect(pe.getEffectiveServerFaultPolicy(ei, boi, bfi, destination, message))
        .andReturn(effectivePolicy);
    List<Interceptor<? extends Message>> li = createMockInterceptorList();
    EasyMock.expect(effectivePolicy.getInterceptors())
        .andReturn(li);
    InterceptorChain ic = control.createMock(InterceptorChain.class);
    EasyMock.expect(message.getInterceptorChain()).andReturn(ic);
    ic.add(li.get(0));
    EasyMock.expectLastCall();
    Collection<Assertion> assertions =
        CastUtils.cast(Collections.EMPTY_LIST, Assertion.class);
    EasyMock.expect(effectivePolicy.getChosenAlternative()).andReturn(assertions);
    control.replay();
    interceptor.handleMessage(message);
    control.verify();
}
 
Example 13
Source File: ColocMessageObserver.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void onMessage(Message m) {
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        if (LOG.isLoggable(Level.FINER)) {
            LOG.finer("Processing Message at collocated endpoint.  Request message: " + m);
        }
        Exchange ex = new ExchangeImpl();
        setExchangeProperties(ex, m);

        Message inMsg = endpoint.getBinding().createMessage();
        MessageImpl.copyContent(m, inMsg);

        //Copy Request Context to Server inBound Message
        //TODO a Context Filter Strategy required.
        inMsg.putAll(m);

        inMsg.put(COLOCATED, Boolean.TRUE);
        inMsg.put(Message.REQUESTOR_ROLE, Boolean.FALSE);
        inMsg.put(Message.INBOUND_MESSAGE, Boolean.TRUE);
        BindingOperationInfo boi = ex.getBindingOperationInfo();
        OperationInfo oi = boi != null ? boi.getOperationInfo() : null;
        if (oi != null) {
            inMsg.put(MessageInfo.class, oi.getInput());
        }
        ex.setInMessage(inMsg);
        inMsg.setExchange(ex);

        if (LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Build inbound interceptor chain.");
        }

        //Add all interceptors between USER_LOGICAL and INVOKE.
        SortedSet<Phase> phases = new TreeSet<>(bus.getExtension(PhaseManager.class).getInPhases());
        ColocUtil.setPhases(phases, Phase.USER_LOGICAL, Phase.INVOKE);
        InterceptorChain chain = ColocUtil.getInInterceptorChain(ex, phases);
        chain.add(addColocInterceptors());
        inMsg.setInterceptorChain(chain);

        //Convert the coloc object type if necessary
        BindingOperationInfo bop = m.getExchange().getBindingOperationInfo();
        OperationInfo soi = bop != null ? bop.getOperationInfo() : null;
        if (soi != null && oi != null) {
            if (ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(oi, Source.class)) {
                // converting source -> pojo
                ColocUtil.convertSourceToObject(inMsg);
            } else if (ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(soi, Source.class)) {
                // converting pojo -> source
                ColocUtil.convertObjectToSource(inMsg);
            }
        }
        chain.doIntercept(inMsg);
        if (soi != null && oi != null) {
            if (ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && ex.getOutMessage() != null) {
                // converting pojo -> source
                ColocUtil.convertObjectToSource(ex.getOutMessage());
            } else if (ColocUtil.isAssignableOperationInfo(oi, Source.class)
                && !ColocUtil.isAssignableOperationInfo(soi, Source.class)
                && ex.getOutMessage() != null) {
                // converting pojo -> source
                ColocUtil.convertSourceToObject(ex.getOutMessage());
            }
        }
        //Set Server OutBound Message onto InBound Exchange.
        setOutBoundMessage(ex, m.getExchange());
    } finally {
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
        if (origLoader != null) {
            origLoader.reset();
        }
    }
}