Search in sources :

Example 46 with Configuration

use of javax.ws.rs.core.Configuration in project minijax by minijax.

the class SecurityTest method testCookieSessionNotFound.

@Test
public void testCookieSessionNotFound() {
    final String cookie = IdUtils.create().toString();
    final SecurityDao dao = mock(SecurityDao.class);
    final Configuration config = mock(Configuration.class);
    final Security<User> security = new Security<>(dao, config, null, cookie);
    assertFalse(security.isLoggedIn());
    assertNull(security.getUserPrincipal());
}
Also used : Configuration(javax.ws.rs.core.Configuration) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 47 with Configuration

use of javax.ws.rs.core.Configuration in project cxf by apache.

the class JAXRSClientFactoryBean method applyFeatures.

protected void applyFeatures(AbstractClient client) {
    if (getFeatures() != null) {
        getFeatures().forEach(feature -> {
            feature.initialize(client.getConfiguration(), getBus());
        });
    }
    // Process JAX-RS features which are passed through as providers
    final Set<Object> providers = new HashSet<>();
    for (final Object provider : getProviders()) {
        if (provider instanceof Feature) {
            final Feature feature = (Feature) provider;
            final FeatureContextImpl context = new FeatureContextImpl();
            final Configurable<?> configurable = getConfigurableFor(context);
            final Configuration configuration = configurable.getConfiguration();
            final Set<Object> registered = configuration.getInstances();
            if (!configuration.isRegistered(feature)) {
                configurable.register(feature);
                // Disregarding if the feature is enabled or disabled, register only newly added providers,
                // excluding pre-existing ones and the feature instance itself.
                final Set<Object> added = new HashSet<Object>(configuration.getInstances());
                added.remove(feature);
                added.removeAll(registered);
                providers.addAll(added);
            }
        }
    }
    if (!providers.isEmpty()) {
        setProviders(Arrays.asList(providers));
    }
}
Also used : FeatureContextImpl(org.apache.cxf.jaxrs.impl.FeatureContextImpl) Configuration(javax.ws.rs.core.Configuration) Feature(javax.ws.rs.core.Feature) HashSet(java.util.HashSet)

Example 48 with Configuration

use of javax.ws.rs.core.Configuration in project cxf by apache.

the class ServerProviderFactory method setProviders.

@SuppressWarnings("unchecked")
@Override
protected void setProviders(boolean custom, boolean busGlobal, Object... providers) {
    List<Object> allProviders = new LinkedList<>();
    for (Object p : providers) {
        if (p instanceof Feature) {
            FeatureContext featureContext = createServerFeatureContext();
            Feature feature = (Feature) p;
            injectApplicationIntoFeature(feature);
            feature.configure(featureContext);
            Configuration cfg = featureContext.getConfiguration();
            for (Object featureProvider : cfg.getInstances()) {
                Map<Class<?>, Integer> contracts = cfg.getContracts(featureProvider.getClass());
                if (contracts != null && !contracts.isEmpty()) {
                    Class<?> providerCls = ClassHelper.getRealClass(getBus(), featureProvider);
                    allProviders.add(new FilterProviderInfo<Object>(featureProvider.getClass(), providerCls, featureProvider, getBus(), getFilterNameBindings(getBus(), featureProvider), false, contracts));
                } else {
                    allProviders.add(featureProvider);
                }
            }
        } else {
            allProviders.add(p);
        }
    }
    List<ProviderInfo<ContainerRequestFilter>> postMatchRequestFilters = new LinkedList<>();
    List<ProviderInfo<ContainerResponseFilter>> postMatchResponseFilters = new LinkedList<>();
    List<ProviderInfo<? extends Object>> theProviders = prepareProviders(custom, busGlobal, allProviders.toArray(), application);
    super.setCommonProviders(theProviders, RuntimeType.SERVER);
    for (ProviderInfo<? extends Object> provider : theProviders) {
        Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider.getProvider());
        // Check if provider is constrained to server
        if (!constrainedTo(providerCls, RuntimeType.SERVER)) {
            continue;
        }
        if (filterContractSupported(provider, providerCls, ContainerRequestFilter.class)) {
            addContainerRequestFilter(postMatchRequestFilters, (ProviderInfo<ContainerRequestFilter>) provider);
        }
        if (filterContractSupported(provider, providerCls, ContainerResponseFilter.class)) {
            postMatchResponseFilters.add((ProviderInfo<ContainerResponseFilter>) provider);
        }
        if (DynamicFeature.class.isAssignableFrom(providerCls)) {
            // TODO: review the possibility of DynamicFeatures needing to have Contexts injected
            Object feature = provider.getProvider();
            dynamicFeatures.add((DynamicFeature) feature);
        }
        if (filterContractSupported(provider, providerCls, ExceptionMapper.class)) {
            addProviderToList(exceptionMappers, provider);
        }
    }
    Collections.sort(preMatchContainerRequestFilters, new BindingPriorityComparator(ContainerRequestFilter.class, true));
    mapInterceptorFilters(postMatchContainerRequestFilters, postMatchRequestFilters, ContainerRequestFilter.class, true);
    mapInterceptorFilters(containerResponseFilters, postMatchResponseFilters, ContainerResponseFilter.class, false);
    injectContextProxies(exceptionMappers, postMatchContainerRequestFilters.values(), preMatchContainerRequestFilters, containerResponseFilters.values());
}
Also used : Configuration(javax.ws.rs.core.Configuration) DynamicFeature(javax.ws.rs.container.DynamicFeature) Feature(javax.ws.rs.core.Feature) LinkedList(java.util.LinkedList) FeatureContext(javax.ws.rs.core.FeatureContext) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 49 with Configuration

use of javax.ws.rs.core.Configuration in project cxf by apache.

the class ConfigurationImplTest method testChecksConstrainedToAnnotationDuringRegistration.

@Test
public void testChecksConstrainedToAnnotationDuringRegistration() {
    TestHandler handler = new TestHandler();
    LogUtils.getL7dLogger(ConfigurableImpl.class).addHandler(handler);
    try (ConfigurableImpl<Client> configurable = new ConfigurableImpl<>(createClientProxy(), RuntimeType.CLIENT)) {
        Configuration config = configurable.getConfiguration();
        configurable.register(ContainerResponseFilterImpl.class);
        assertEquals(0, config.getInstances().size());
        for (String message : handler.messages) {
            if (message.startsWith("WARN") && message.contains("Null, empty or invalid contracts specified")) {
                // success
                return;
            }
        }
    }
    fail("did not log expected message");
}
Also used : Configuration(javax.ws.rs.core.Configuration) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 50 with Configuration

use of javax.ws.rs.core.Configuration in project cxf by apache.

the class ConfigurationImplTest method testFeatureDisabledInstance.

@Test
public void testFeatureDisabledInstance() {
    FeatureContextImpl featureContext = new FeatureContextImpl();
    Configurable<FeatureContext> configurable = new ConfigurableImpl<>(featureContext, RuntimeType.SERVER);
    featureContext.setConfigurable(configurable);
    Feature feature = new DisablableFeature();
    featureContext.register(feature);
    Configuration config = configurable.getConfiguration();
    assertFalse(config.isEnabled(feature));
}
Also used : Configuration(javax.ws.rs.core.Configuration) Feature(javax.ws.rs.core.Feature) FeatureContext(javax.ws.rs.core.FeatureContext) Test(org.junit.Test)

Aggregations

Configuration (javax.ws.rs.core.Configuration)56 Test (org.junit.Test)28 MinijaxTest (org.minijax.test.MinijaxTest)17 FeatureContext (javax.ws.rs.core.FeatureContext)7 Client (javax.ws.rs.client.Client)5 ContainerResponseFilter (javax.ws.rs.container.ContainerResponseFilter)4 Map (java.util.Map)3 ClientRequestFilter (javax.ws.rs.client.ClientRequestFilter)3 ClientResponseFilter (javax.ws.rs.client.ClientResponseFilter)3 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)3 Feature (javax.ws.rs.core.Feature)3 NewCookie (javax.ws.rs.core.NewCookie)3 List (java.util.List)2 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)2 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)2 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)2 LocalResteasyProviderFactory (org.jboss.resteasy.client.jaxrs.internal.LocalResteasyProviderFactory)2 ResteasyJackson2Provider (org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider)2 ResteasyProviderFactory (org.jboss.resteasy.spi.ResteasyProviderFactory)2 JacksonJaxbJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider)1