Search in sources :

Example 31 with Message

use of org.apache.cxf.message.Message in project ddf by codice.

the class MetricsOutInterceptorTest method testHandleMessageWithTwoWayClientMessageWithLatencyTimeRecorder.

/**
     * Test method for
     * {@link ddf.metrics.interceptor.MetricsOutInterceptor#handleMessage(org.apache.cxf.message.Message)}
     * .
     *
     * @throws InterruptedException
     */
@Test
public void testHandleMessageWithTwoWayClientMessageWithLatencyTimeRecorder() {
    // Setup
    MetricsOutInterceptor outInterceptor = new MetricsOutInterceptor();
    Message mockMessage = mock(Message.class);
    Exchange ex = new ExchangeImpl();
    Bus mockBus = mock(Bus.class);
    LatencyTimeRecorder mockLtr = mock(LatencyTimeRecorder.class);
    ex.put(Bus.class, mockBus);
    ex.put(LatencyTimeRecorder.class, mockLtr);
    when(mockBus.getId()).thenReturn("bus_id");
    when(mockMessage.getExchange()).thenReturn(ex);
    when(mockMessage.get(Message.PARTIAL_RESPONSE_MESSAGE)).thenReturn("false");
    when(mockMessage.get(Message.REQUESTOR_ROLE)).thenReturn(true);
    // Perform test
    outInterceptor.handleMessage(mockMessage);
    // validate that LatencyTimeRecorder.beginHandling was called once
    verify(mockLtr, times(1)).beginHandling();
}
Also used : Exchange(org.apache.cxf.message.Exchange) Bus(org.apache.cxf.Bus) Message(org.apache.cxf.message.Message) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 32 with Message

use of org.apache.cxf.message.Message in project ddf by codice.

the class MetricsOutInterceptorTest method testHandleMessageWithOneWayClientMessage.

/**
     * Test method for
     * {@link ddf.metrics.interceptor.MetricsOutInterceptor#handleMessage(org.apache.cxf.message.Message)}
     * .
     *
     * @throws InterruptedException
     */
@Test
public void testHandleMessageWithOneWayClientMessage() {
    // Setup
    MetricsOutInterceptor outInterceptor = new MetricsOutInterceptor();
    Message mockMessage = mock(Message.class);
    Exchange ex = new ExchangeImpl();
    Bus mockBus = mock(Bus.class);
    InterceptorChain mockIc = mock(InterceptorChain.class);
    ex.put(Bus.class, mockBus);
    ex.setOneWay(true);
    when(mockBus.getId()).thenReturn("bus_id");
    when(mockMessage.getExchange()).thenReturn(ex);
    when(mockMessage.get(Message.PARTIAL_RESPONSE_MESSAGE)).thenReturn("false");
    when(mockMessage.get(Message.REQUESTOR_ROLE)).thenReturn(true);
    when(mockMessage.getInterceptorChain()).thenReturn(mockIc);
    // Perform test
    outInterceptor.handleMessage(mockMessage);
    // validate that LatencyTimeRecorder.beginHandling was called once
    verify(mockMessage, times(1)).getInterceptorChain();
}
Also used : Exchange(org.apache.cxf.message.Exchange) Bus(org.apache.cxf.Bus) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) Message(org.apache.cxf.message.Message) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 33 with Message

use of org.apache.cxf.message.Message in project ddf by codice.

the class MetricsOutInterceptorTest method testHandleMessageWithTwoWayClientMessageWithoutLatencyTimeRecorder.

/**
     * Test method for
     * {@link ddf.metrics.interceptor.MetricsOutInterceptor#handleMessage(org.apache.cxf.message.Message)}
     * .
     *
     * @throws InterruptedException
     */
@Test
public void testHandleMessageWithTwoWayClientMessageWithoutLatencyTimeRecorder() {
    // Setup
    MetricsOutInterceptor outInterceptor = new MetricsOutInterceptor();
    Message mockMessage = mock(Message.class);
    Exchange ex = new ExchangeImpl();
    Bus mockBus = mock(Bus.class);
    ex.put(Bus.class, mockBus);
    when(mockBus.getId()).thenReturn("bus_id");
    when(mockMessage.getExchange()).thenReturn(ex);
    when(mockMessage.get(Message.PARTIAL_RESPONSE_MESSAGE)).thenReturn("false");
    when(mockMessage.get(Message.REQUESTOR_ROLE)).thenReturn(true);
    // Perform test
    outInterceptor.handleMessage(mockMessage);
    // validate that an instance of LatencyTimeRecorder was put onto the
    // exchange
    assertThat(ex.get(LatencyTimeRecorder.class), instanceOf(LatencyTimeRecorder.class));
}
Also used : Exchange(org.apache.cxf.message.Exchange) Bus(org.apache.cxf.Bus) Message(org.apache.cxf.message.Message) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 34 with Message

use of org.apache.cxf.message.Message 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 35 with Message

use of org.apache.cxf.message.Message in project cxf by apache.

the class ClaimsAuthorizingInterceptorTest method prepareMessage.

private Message prepareMessage(Class<?> cls, String methodName, org.apache.cxf.rt.security.claims.Claim... claim) throws Exception {
    ClaimCollection claims = new ClaimCollection();
    claims.addAll(Arrays.asList(claim));
    Set<Principal> roles = SAMLUtils.parseRolesFromClaims(claims, SAMLClaim.SAML_ROLE_ATTRIBUTENAME_DEFAULT, SAML2Constants.ATTRNAME_FORMAT_UNSPECIFIED);
    SecurityContext sc = new SAMLSecurityContext(new SimplePrincipal("user"), roles, claims);
    Message m = new MessageImpl();
    m.setExchange(new ExchangeImpl());
    m.put(SecurityContext.class, sc);
    m.put("org.apache.cxf.resource.method", cls.getMethod(methodName, new Class[] {}));
    return m;
}
Also used : Message(org.apache.cxf.message.Message) SAMLSecurityContext(org.apache.cxf.rt.security.saml.claims.SAMLSecurityContext) SAMLSecurityContext(org.apache.cxf.rt.security.saml.claims.SAMLSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) ClaimCollection(org.apache.cxf.rt.security.claims.ClaimCollection) MessageImpl(org.apache.cxf.message.MessageImpl) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Principal(java.security.Principal) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Aggregations

Message (org.apache.cxf.message.Message)1002 Test (org.junit.Test)507 MessageImpl (org.apache.cxf.message.MessageImpl)291 Exchange (org.apache.cxf.message.Exchange)199 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)169 Endpoint (org.apache.cxf.endpoint.Endpoint)91 Interceptor (org.apache.cxf.interceptor.Interceptor)87 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)85 ArrayList (java.util.ArrayList)83 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)76 List (java.util.List)75 IOException (java.io.IOException)73 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)73 Method (java.lang.reflect.Method)69 Bus (org.apache.cxf.Bus)69 QName (javax.xml.namespace.QName)62 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)55 HashMap (java.util.HashMap)53 Fault (org.apache.cxf.interceptor.Fault)51 ByteArrayInputStream (java.io.ByteArrayInputStream)49