Search in sources :

Example 1 with Interceptor

use of org.apache.cxf.interceptor.Interceptor in project camel by apache.

the class RawMessageWSDLGetInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
    String query = (String) message.get(Message.QUERY_STRING);
    if (!"GET".equals(method) || StringUtils.isEmpty(query)) {
        return;
    }
    String baseUri = (String) message.get(Message.REQUEST_URL);
    String ctx = (String) message.get(Message.PATH_INFO);
    Map<String, String> map = UrlUtils.parseQueryString(query);
    if (isRecognizedQuery(map, baseUri, ctx, message.getExchange().getEndpoint().getEndpointInfo())) {
        Document doc = getDocument(message, baseUri, map, ctx);
        Endpoint e = message.getExchange().get(Endpoint.class);
        Message mout = new MessageImpl();
        mout.setExchange(message.getExchange());
        mout = e.getBinding().createMessage(mout);
        mout.setInterceptorChain(OutgoingChainInterceptor.getOutInterceptorChain(message.getExchange()));
        message.getExchange().setOutMessage(mout);
        mout.put(DOCUMENT_HOLDER, doc);
        Iterator<Interceptor<? extends Message>> iterator = mout.getInterceptorChain().iterator();
        while (iterator.hasNext()) {
            Interceptor<? extends Message> inInterceptor = iterator.next();
            if (inInterceptor instanceof AbstractPhaseInterceptor) {
                AbstractPhaseInterceptor<?> interceptor = (AbstractPhaseInterceptor<?>) inInterceptor;
                if (interceptor.getPhase().equals(Phase.PREPARE_SEND) || interceptor.getPhase().equals(Phase.PRE_STREAM)) {
                    // just make sure we keep the right interceptors
                    continue;
                }
            }
            mout.getInterceptorChain().remove(inInterceptor);
        }
        // notice this is being added after the purge above, don't swap the order!
        mout.getInterceptorChain().add(RawMessageWSDLGetOutInterceptor.INSTANCE);
        // skip the service executor and goto the end of the chain.
        message.getInterceptorChain().doInterceptStartingAt(message, OutgoingChainInterceptor.class.getName());
    }
}
Also used : Endpoint(org.apache.cxf.endpoint.Endpoint) Message(org.apache.cxf.message.Message) OutgoingChainInterceptor(org.apache.cxf.interceptor.OutgoingChainInterceptor) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) Document(org.w3c.dom.Document) MessageImpl(org.apache.cxf.message.MessageImpl) Interceptor(org.apache.cxf.interceptor.Interceptor) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) EndpointSelectionInterceptor(org.apache.cxf.binding.soap.interceptor.EndpointSelectionInterceptor) OutgoingChainInterceptor(org.apache.cxf.interceptor.OutgoingChainInterceptor)

Example 2 with Interceptor

use of org.apache.cxf.interceptor.Interceptor in project camel by apache.

the class CxfRsProducerClientFactoryBeanTest method testProducerInOutInterceptors.

@Test
public void testProducerInOutInterceptors() throws Exception {
    CxfRsEndpoint e = context.getEndpoint("cxfrs://bean://rsClientHttpInterceptors", CxfRsEndpoint.class);
    CxfRsProducer p = new CxfRsProducer(e);
    CxfRsProducer.ClientFactoryBeanCache cache = p.getClientFactoryBeanCache();
    JAXRSClientFactoryBean bean = cache.get("http://localhost:8080/CxfRsProducerClientFactoryBeanInterceptors/");
    List<Interceptor<?>> ins = bean.getInInterceptors();
    assertEquals(1, ins.size());
    assertTrue(ins.get(0) instanceof LoggingInInterceptor);
    List<Interceptor<?>> outs = bean.getOutInterceptors();
    assertEquals(1, outs.size());
    assertTrue(outs.get(0) instanceof LoggingOutInterceptor);
}
Also used : JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) LoggingOutInterceptor(org.apache.cxf.interceptor.LoggingOutInterceptor) LoggingInInterceptor(org.apache.cxf.interceptor.LoggingInInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) LoggingOutInterceptor(org.apache.cxf.interceptor.LoggingOutInterceptor) LoggingInInterceptor(org.apache.cxf.interceptor.LoggingInInterceptor) Test(org.junit.Test)

Example 3 with Interceptor

use of org.apache.cxf.interceptor.Interceptor in project camel by apache.

the class MessageLossSimulator method handleMessage.

public void handleMessage(Message message) throws Fault {
    Object maps = RMContextUtils.retrieveMAPs(message, false, true);
    // RMContextUtils.ensureExposedVersion(maps);
    String action = getAction(maps);
    if (RMContextUtils.isRMProtocolMessage(action)) {
        return;
    }
    appMessageCount++;
    // do not discard odd-numbered messages
    if (0 != (appMessageCount % 2)) {
        return;
    }
    // discard even-numbered message
    InterceptorChain chain = message.getInterceptorChain();
    ListIterator<Interceptor<? extends Message>> it = chain.getIterator();
    while (it.hasNext()) {
        PhaseInterceptor<?> pi = (PhaseInterceptor<?>) it.next();
        if (MessageSenderInterceptor.class.getName().equals(pi.getId())) {
            chain.remove(pi);
            LOG.debug("Removed MessageSenderInterceptor from interceptor chain.");
            break;
        }
    }
    message.setContent(OutputStream.class, new WrappedOutputStream(message));
    message.getInterceptorChain().add(new AbstractPhaseInterceptor<Message>(Phase.PREPARE_SEND_ENDING) {

        public void handleMessage(Message message) throws Fault {
            try {
                message.getContent(OutputStream.class).close();
            } catch (IOException e) {
                throw new Fault(e);
            }
        }
    });
}
Also used : Message(org.apache.cxf.message.Message) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) MessageSenderInterceptor(org.apache.cxf.interceptor.MessageSenderInterceptor) AbstractWrappedOutputStream(org.apache.cxf.io.AbstractWrappedOutputStream) Interceptor(org.apache.cxf.interceptor.Interceptor) MessageSenderInterceptor(org.apache.cxf.interceptor.MessageSenderInterceptor) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) PhaseInterceptor(org.apache.cxf.phase.PhaseInterceptor)

Example 4 with Interceptor

use of org.apache.cxf.interceptor.Interceptor in project tomee by apache.

the class CxfRsHttpListener method configureFactory.

private void configureFactory(final Collection<Object> givenAdditionalProviders, final ServiceConfiguration serviceConfiguration, final JAXRSServerFactoryBean factory, final WebBeansContext ctx) {
    if (!"true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cxf.rs.skip-provider-sorting", "false"))) {
        final Comparator<?> providerComparator = findProviderComparator(serviceConfiguration, ctx);
        if (providerComparator != null) {
            factory.setProviderComparator(providerComparator);
        }
    }
    CxfUtil.configureEndpoint(factory, serviceConfiguration, CXF_JAXRS_PREFIX);
    boolean enforceCxfBvalMapper = false;
    if (ctx == null || !ctx.getBeanManagerImpl().isInUse()) {
        // activate bval
        boolean bvalActive = Boolean.parseBoolean(SystemInstance.get().getProperty("openejb.cxf.rs.bval.active", serviceConfiguration.getProperties().getProperty(CXF_JAXRS_PREFIX + "bval.active", "true")));
        if (factory.getFeatures() == null && bvalActive) {
            factory.setFeatures(new ArrayList<Feature>());
        } else if (bvalActive) {
            // check we should activate it and user didn't configure it
            for (final Feature f : factory.getFeatures()) {
                if (BeanValidationFeature.class.isInstance(f)) {
                    bvalActive = false;
                    break;
                }
            }
            for (final Interceptor<?> i : factory.getInInterceptors()) {
                if (BeanValidationInInterceptor.class.isInstance(i)) {
                    bvalActive = false;
                    break;
                }
            }
            for (final Interceptor<?> i : factory.getOutInterceptors()) {
                if (BeanValidationOutInterceptor.class.isInstance(i)) {
                    bvalActive = false;
                    break;
                }
            }
        }
        if (bvalActive) {
            // bval doesn't need the actual instance so faking it to avoid to lookup the bean
            final BeanValidationProvider provider = new BeanValidationProvider();
            final BeanValidationInInterceptor in = new JAXRSBeanValidationInInterceptor() {

                @Override
                protected Object getServiceObject(final Message message) {
                    return CxfRsHttpListener.this.getServiceObject(message);
                }
            };
            in.setProvider(provider);
            in.setServiceObject(FAKE_SERVICE_OBJECT);
            factory.getInInterceptors().add(in);
            final BeanValidationOutInterceptor out = new JAXRSBeanValidationOutInterceptor() {

                @Override
                protected Object getServiceObject(final Message message) {
                    return CxfRsHttpListener.this.getServiceObject(message);
                }
            };
            out.setProvider(provider);
            out.setServiceObject(FAKE_SERVICE_OBJECT);
            factory.getOutInterceptors().add(out);
            // and add a mapper to get back a 400 like for bval
            enforceCxfBvalMapper = true;
        }
    }
    final Collection<ServiceInfo> services = serviceConfiguration.getAvailableServices();
    final String staticSubresourceResolution = serviceConfiguration.getProperties().getProperty(CXF_JAXRS_PREFIX + STATIC_SUB_RESOURCE_RESOLUTION_KEY);
    if (staticSubresourceResolution != null) {
        factory.setStaticSubresourceResolution("true".equalsIgnoreCase(staticSubresourceResolution));
    }
    // resource comparator
    final String resourceComparator = serviceConfiguration.getProperties().getProperty(RESOURCE_COMPARATOR_KEY);
    if (resourceComparator != null) {
        try {
            ResourceComparator instance = (ResourceComparator) ServiceInfos.resolve(services, resourceComparator);
            if (instance == null) {
                instance = (ResourceComparator) Thread.currentThread().getContextClassLoader().loadClass(resourceComparator).newInstance();
            }
            factory.setResourceComparator(instance);
        } catch (final Exception e) {
            LOGGER.error("Can't create the resource comparator " + resourceComparator, e);
        }
    }
    // static resources
    final String staticResources = serviceConfiguration.getProperties().getProperty(STATIC_RESOURCE_KEY);
    if (staticResources != null) {
        final String[] resources = staticResources.split(",");
        for (final String r : resources) {
            final String trimmed = r.trim();
            if (!trimmed.isEmpty()) {
                staticResourcesList.add(Pattern.compile(trimmed));
            }
        }
    }
    // providers
    Set<String> providersConfig = null;
    {
        final String provider = serviceConfiguration.getProperties().getProperty(PROVIDERS_KEY);
        if (provider != null) {
            providersConfig = new HashSet<>();
            for (final String p : Arrays.asList(provider.split(","))) {
                providersConfig.add(p.trim());
            }
        }
        {
            if (GLOBAL_PROVIDERS != null) {
                if (providersConfig == null) {
                    providersConfig = new HashSet<>();
                }
                providersConfig.addAll(Arrays.asList(GLOBAL_PROVIDERS.split(",")));
            }
        }
    }
    // another property to configure the scanning of providers but this one is consistent with current cxf config
    // the other one is more generic but need another file
    final String key = CXF_JAXRS_PREFIX + "skip-provider-scanning";
    final boolean ignoreAutoProviders = "true".equalsIgnoreCase(SystemInstance.get().getProperty(key, serviceConfiguration.getProperties().getProperty(key, "false")));
    final Collection<Object> additionalProviders = ignoreAutoProviders ? Collections.emptyList() : givenAdditionalProviders;
    List<Object> providers = null;
    if (providersConfig != null) {
        providers = ServiceInfos.resolve(services, providersConfig.toArray(new String[providersConfig.size()]), OpenEJBProviderFactory.INSTANCE);
        if (providers != null && additionalProviders != null && !additionalProviders.isEmpty()) {
            providers.addAll(providers(serviceConfiguration.getAvailableServices(), additionalProviders, ctx));
        }
    }
    if (providers == null) {
        providers = new ArrayList<>(4);
        if (additionalProviders != null && !additionalProviders.isEmpty()) {
            providers.addAll(providers(serviceConfiguration.getAvailableServices(), additionalProviders, ctx));
        }
    }
    if (!ignoreAutoProviders) {
        addMandatoryProviders(providers, serviceConfiguration);
        if (enforceCxfBvalMapper) {
            if (!shouldSkipProvider(CxfResponseValidationExceptionMapper.class.getName())) {
                providers.add(new CxfResponseValidationExceptionMapper());
            }
        }
    }
    SystemInstance.get().fireEvent(new ExtensionProviderRegistration(AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE), providers));
    if (!providers.isEmpty()) {
        factory.setProviders(providers);
    }
}
Also used : Message(org.apache.cxf.message.Message) JAXRSBeanValidationOutInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor) BeanValidationFeature(org.apache.cxf.validation.BeanValidationFeature) Feature(org.apache.cxf.feature.Feature) EJBRestServiceInfo(org.apache.openejb.server.rest.EJBRestServiceInfo) ServiceInfo(org.apache.openejb.assembler.classic.ServiceInfo) JAXRSBeanValidationOutInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) BeanValidationInInterceptor(org.apache.cxf.validation.BeanValidationInInterceptor) JAXRSBeanValidationInInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor) BeanValidationOutInterceptor(org.apache.cxf.validation.BeanValidationOutInterceptor) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) BeanValidationFeature(org.apache.cxf.validation.BeanValidationFeature) BeanValidationInInterceptor(org.apache.cxf.validation.BeanValidationInInterceptor) JAXRSBeanValidationInInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor) ResourceComparator(org.apache.cxf.jaxrs.ext.ResourceComparator) JAXRSBeanValidationInInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationInInterceptor) ResponseConstraintViolationException(org.apache.cxf.validation.ResponseConstraintViolationException) IOException(java.io.IOException) EndpointException(org.apache.cxf.endpoint.EndpointException) ServletException(javax.servlet.ServletException) BusException(org.apache.cxf.BusException) BeanValidationProvider(org.apache.cxf.validation.BeanValidationProvider) JAXRSBeanValidationOutInterceptor(org.apache.cxf.jaxrs.validation.JAXRSBeanValidationOutInterceptor) BeanValidationOutInterceptor(org.apache.cxf.validation.BeanValidationOutInterceptor) ExtensionProviderRegistration(org.apache.openejb.server.cxf.rs.event.ExtensionProviderRegistration)

Example 5 with Interceptor

use of org.apache.cxf.interceptor.Interceptor in project tomee by apache.

the class EjbInterceptor method copyDataBindingInterceptors.

private static void copyDataBindingInterceptors(PhaseInterceptorChain newChain, InterceptorChain oldChain) {
    for (Interceptor interceptor : oldChain) {
        if (interceptor instanceof AbstractInDatabindingInterceptor) {
            log.debug("Added data binding interceptor: " + interceptor);
            newChain.add(interceptor);
        }
    }
}
Also used : AbstractInDatabindingInterceptor(org.apache.cxf.interceptor.AbstractInDatabindingInterceptor) AbstractInDatabindingInterceptor(org.apache.cxf.interceptor.AbstractInDatabindingInterceptor) SOAPHandlerInterceptor(org.apache.cxf.jaxws.handler.soap.SOAPHandlerInterceptor) HolderInInterceptor(org.apache.cxf.jaxws.interceptors.HolderInInterceptor) OutgoingChainInterceptor(org.apache.cxf.interceptor.OutgoingChainInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) LogicalHandlerInInterceptor(org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor) MustUnderstandInterceptor(org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor) WrapperClassInInterceptor(org.apache.cxf.jaxws.interceptors.WrapperClassInInterceptor) ServiceInvokerInterceptor(org.apache.cxf.interceptor.ServiceInvokerInterceptor)

Aggregations

Interceptor (org.apache.cxf.interceptor.Interceptor)5 Message (org.apache.cxf.message.Message)3 IOException (java.io.IOException)2 OutgoingChainInterceptor (org.apache.cxf.interceptor.OutgoingChainInterceptor)2 AbstractPhaseInterceptor (org.apache.cxf.phase.AbstractPhaseInterceptor)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 ServletException (javax.servlet.ServletException)1 BusException (org.apache.cxf.BusException)1 EndpointSelectionInterceptor (org.apache.cxf.binding.soap.interceptor.EndpointSelectionInterceptor)1 MustUnderstandInterceptor (org.apache.cxf.binding.soap.interceptor.MustUnderstandInterceptor)1 Endpoint (org.apache.cxf.endpoint.Endpoint)1 EndpointException (org.apache.cxf.endpoint.EndpointException)1 Feature (org.apache.cxf.feature.Feature)1 AbstractInDatabindingInterceptor (org.apache.cxf.interceptor.AbstractInDatabindingInterceptor)1 Fault (org.apache.cxf.interceptor.Fault)1 InterceptorChain (org.apache.cxf.interceptor.InterceptorChain)1 LoggingInInterceptor (org.apache.cxf.interceptor.LoggingInInterceptor)1 LoggingOutInterceptor (org.apache.cxf.interceptor.LoggingOutInterceptor)1 MessageSenderInterceptor (org.apache.cxf.interceptor.MessageSenderInterceptor)1