Java Code Examples for javax.ws.rs.core.Configuration#getContracts()

The following examples show how to use javax.ws.rs.core.Configuration#getContracts() . 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: AdditionalRegistrationTest.java    From microprofile-rest-client with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRegisterAMultiTypedProviderInstanceWithPriorities() {
    MultiTypedProvider provider = new MultiTypedProvider();
    Map<Class<?>, Integer> priorities = new HashMap<>();
    priorities.put(ClientRequestFilter.class, 500);
    priorities.put(ClientResponseFilter.class, 501);
    priorities.put(MessageBodyReader.class, 502);
    priorities.put(MessageBodyWriter.class, 503);
    priorities.put(ReaderInterceptor.class, 504);
    priorities.put(WriterInterceptor.class, 505);
    priorities.put(ResponseExceptionMapper.class, 506);
    priorities.put(ParamConverterProvider.class, 507);
    RestClientBuilder builder = RestClientBuilder.newBuilder().register(provider, priorities);
    Configuration configuration = builder.getConfiguration();
    assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered");
    assertTrue(configuration.isRegistered(provider), MultiTypedProvider.class + " should be registered");
    Map<Class<?>, Integer> contracts = configuration.getContracts(MultiTypedProvider.class);
    assertEquals(contracts.size(), priorities.size(),
        "There should be "+priorities.size()+" provider types registered");
    for(Map.Entry<Class<?>, Integer> priority : priorities.entrySet()) {
        Integer contractPriority = contracts.get(priority.getKey());
        assertEquals(contractPriority, priority.getValue(), "The priority for "+priority.getKey()+" should be "+priority.getValue());
    }
}
 
Example 2
Source File: AdditionalRegistrationTest.java    From microprofile-rest-client with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldRegisterAMultiTypedProviderClassWithPriorities() {
    Map<Class<?>, Integer> priorities = new HashMap<>();
    priorities.put(ClientRequestFilter.class, 500);
    priorities.put(ClientResponseFilter.class, 501);
    priorities.put(MessageBodyReader.class, 502);
    priorities.put(MessageBodyWriter.class, 503);
    priorities.put(ReaderInterceptor.class, 504);
    priorities.put(WriterInterceptor.class, 505);
    priorities.put(ResponseExceptionMapper.class, 506);
    priorities.put(ParamConverterProvider.class, 507);
    RestClientBuilder builder = RestClientBuilder.newBuilder().register(MultiTypedProvider.class, priorities);
    Configuration configuration = builder.getConfiguration();
    assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered");
    Map<Class<?>, Integer> contracts = configuration.getContracts(MultiTypedProvider.class);
    assertEquals(contracts.size(), priorities.size(),
        "There should be "+priorities.size()+" provider types registered");
    for(Map.Entry<Class<?>, Integer> priority : priorities.entrySet()) {
        Integer contractPriority = contracts.get(priority.getKey());
        assertEquals(contractPriority, priority.getValue(), "The priority for "+priority.getKey()+" should be "+priority.getValue());
    }
}
 
Example 3
Source File: AdditionalRegistrationTest.java    From microprofile-rest-client with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRegisterInstanceWithPriority() {
    Integer priority = 1000;
    TestClientRequestFilter instance = new TestClientRequestFilter();
    RestClientBuilder builder = RestClientBuilder.newBuilder().register(instance, priority);
    Configuration configuration = builder.getConfiguration();
    assertTrue(configuration.isRegistered(TestClientRequestFilter.class), TestClientRequestFilter.class + " should be registered");
    assertTrue(configuration.isRegistered(instance), TestClientRequestFilter.class + " should be registered");
    Map<Class<?>, Integer> contracts = configuration.getContracts(TestClientRequestFilter.class);
    assertEquals(contracts.size(), 1, "There should be a registered contract for "+TestClientRequestFilter.class);
    assertEquals(contracts.get(ClientRequestFilter.class), priority, "The priority for "+TestClientRequestFilter.class+" should be 1000");
}
 
Example 4
Source File: AdditionalRegistrationTest.java    From microprofile-rest-client with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRegisterProvidersWithPriority() {
    Integer priority = 1000;
    RestClientBuilder builder = RestClientBuilder.newBuilder().register(TestClientRequestFilter.class, priority);
    Configuration configuration = builder.getConfiguration();
    assertTrue(configuration.isRegistered(TestClientRequestFilter.class), TestClientRequestFilter.class + " should be registered");
    Map<Class<?>, Integer> contracts = configuration.getContracts(TestClientRequestFilter.class);
    assertEquals(contracts.size(), 1, "There should be a registered contract for "+TestClientRequestFilter.class);
    assertEquals(contracts.get(ClientRequestFilter.class), priority, "The priority for "+TestClientRequestFilter.class+" should be 1000");
}
 
Example 5
Source File: ServerProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doApplyDynamicFeatures(ClassResourceInfo cri) {
    Set<OperationResourceInfo> oris = cri.getMethodDispatcher().getOperationResourceInfos();
    for (OperationResourceInfo ori : oris) {
        String nameBinding = DEFAULT_FILTER_NAME_BINDING
            + ori.getClassResourceInfo().getServiceClass().getName()
            + "."
            + ori.getMethodToInvoke().toString();
        for (DynamicFeature feature : dynamicFeatures) {
            FeatureContext featureContext = createServerFeatureContext();
            feature.configure(new ResourceInfoImpl(ori), featureContext);
            Configuration cfg = featureContext.getConfiguration();
            for (Object provider : cfg.getInstances()) {
                Map<Class<?>, Integer> contracts = cfg.getContracts(provider.getClass());
                if (contracts != null && !contracts.isEmpty()) {
                    Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider);
                    registerUserProvider(new FilterProviderInfo<Object>(provider.getClass(),
                        providerCls,
                        provider,
                        getBus(),
                        Collections.singleton(nameBinding),
                        true,
                        contracts));
                    ori.addNameBindings(Collections.singletonList(nameBinding));
                }
            }
        }
    }
    Collection<ClassResourceInfo> subs = cri.getSubResources();
    for (ClassResourceInfo sub : subs) {
        if (sub != cri) {
            doApplyDynamicFeatures(sub);
        }
    }
}
 
Example 6
Source File: ConfigurationImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void registerParentProvider(Object o, Configuration parent) {
    Map<Class<?>, Integer> contracts = parent.getContracts(o.getClass());
    if (contracts != null) {
        providers.put(o, contracts);
    } else {
        register(o, AnnotationUtils.getBindingPriority(o.getClass()), 
                    ConfigurableImpl.getImplementedContracts(o, new Class<?>[]{}));
    }
}
 
Example 7
Source File: ConfigurationImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubClassIsRegisteredOnConfigurable() {
    FeatureContextImpl featureContext = new FeatureContextImpl();
    Configurable<FeatureContext> configurable = new ConfigurableImpl<>(featureContext, RuntimeType.SERVER);
    featureContext.setConfigurable(configurable);
    featureContext.register(ContainerResponseFilterSubClassImpl.class);
    Configuration config = configurable.getConfiguration();
    Map<Class<?>, Integer> contracts = config.getContracts(ContainerResponseFilter.class);
    assertEquals(1, contracts.size());
    assertTrue(contracts.containsKey(ContainerResponseFilter.class));
}
 
Example 8
Source File: ConfigurationImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testServerFilterContractsOnClientIsRejected() {
    try (ConfigurableImpl<Client> configurable 
            = new ConfigurableImpl<>(createClientProxy(), RuntimeType.CLIENT)) {
        Configuration config = configurable.getConfiguration();
        configurable.register(TestFilter.class);
        Map<Class<?>, Integer> contracts = config.getContracts(TestFilter.class);
        assertTrue(contracts.containsKey(ClientRequestFilter.class));
        assertTrue(contracts.containsKey(ClientResponseFilter.class));
        assertFalse(contracts.containsKey(ContainerRequestFilter.class));
        assertFalse(contracts.containsKey(ContainerResponseFilter.class));
    }
}
 
Example 9
Source File: ConfigurationImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientFilterWithNestedInterfacesIsAccepted() {
    try (ConfigurableImpl<Client> configurable 
            = new ConfigurableImpl<>(createClientProxy(), RuntimeType.CLIENT)) {
        Configuration config = configurable.getConfiguration();
        configurable.register(NestedInterfaceTestFilter.class);
        Map<Class<?>, Integer> contracts = config.getContracts(NestedInterfaceTestFilter.class);
        assertTrue(contracts.containsKey(ClientRequestFilter.class));
        assertTrue(contracts.containsKey(ClientResponseFilter.class));
    }
}
 
Example 10
Source File: ConfigurationImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testClientFilterContractsOnServerFeatureIsRejected() {
    FeatureContextImpl featureContext = new FeatureContextImpl();
    Configurable<FeatureContext> configurable = new ConfigurableImpl<>(featureContext, RuntimeType.SERVER);
    featureContext.setConfigurable(configurable);
    featureContext.register(TestFilter.class);
    Configuration config = configurable.getConfiguration();
    Map<Class<?>, Integer> contracts = config.getContracts(TestFilter.class);
    assertFalse(contracts.containsKey(ClientRequestFilter.class));
    assertFalse(contracts.containsKey(ClientResponseFilter.class));
    assertTrue(contracts.containsKey(ContainerRequestFilter.class));
    assertTrue(contracts.containsKey(ContainerResponseFilter.class));
}
 
Example 11
Source File: ShiroAuthenticationFeature.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public boolean configure(FeatureContext fc) {
    
    Configuration configuration = fc.getConfiguration();
    
    if(_LOG.isInfoEnabled()) {
        @SuppressWarnings("unchecked")
        Map<String, Object> applicationProps = (Map<String, Object>) configuration.getProperty(JAX_RS_APPLICATION_SERVICE_PROPERTIES);
        _LOG.info("Registering the Shiro Authentication feature with application {}", 
                applicationProps.getOrDefault(JAX_RS_NAME, "<No Name found in application configuration>"));
    }
    
    if(realms.isEmpty()) {
        _LOG.warn("There are no authentication realms available. Users may not be able to authenticate.");
    } else {
        _LOG.debug("Using the authentication realms {}.", realms);
    }

    _LOG.debug("Registering the Shiro SecurityManagerAssociatingFilter");
    fc.register(new SecurityManagerAssociatingFilter(manager), AUTHENTICATION);

    Map<Class<?>, Integer> contracts = configuration.getContracts(ExceptionMapper.class);
    if(contracts.isEmpty()) {
        _LOG.debug("Registering the Shiro ExceptionMapper");
        // Only register the ExceptionMapper if it isn't already registered
        fc.register(ExceptionMapper.class, AUTHENTICATION);
    } else if(AUTHENTICATION < contracts.getOrDefault(javax.ws.rs.ext.ExceptionMapper.class, USER)) {
        _LOG.debug("Updating the priority of the Shiro ExceptionMapper from {} to {}",
                contracts.getOrDefault(javax.ws.rs.ext.ExceptionMapper.class, USER),
                AUTHORIZATION);
        // Update the priority if it's registered too low
        contracts.put(javax.ws.rs.ext.ExceptionMapper.class, AUTHENTICATION);
    }

    contracts = configuration.getContracts(SubjectPrincipalRequestFilter.class);
    if(contracts.isEmpty()) {
        _LOG.debug("Registering the Shiro SubjectPrincipalRequestFilter");
        // Only register the SubjectPrincipalRequestFilter if it isn't already registered
        // and make sure it always comes after the SecurityManagerAssociatingFilter
        fc.register(SubjectPrincipalRequestFilter.class, AUTHENTICATION + 1);
    } else if(AUTHENTICATION < contracts.getOrDefault(ContainerRequestFilter.class, USER)) {
        _LOG.debug("Updating the priority of the Shiro SubjectPrincipalRequestFilter from {} to {}",
                contracts.getOrDefault(ContainerRequestFilter.class, USER),
                AUTHENTICATION + 1);
        // Update the priority if it's registered too low
        contracts.put(ContainerRequestFilter.class, AUTHENTICATION + 1);
    }
    
    return true;
}
 
Example 12
Source File: ShiroAuthorizationFeature.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Override
public boolean configure(FeatureContext fc) {

    Configuration configuration = fc.getConfiguration();

    if(_LOG.isInfoEnabled()) {
        @SuppressWarnings("unchecked")
        Map<String, Object> applicationProps = (Map<String, Object>) configuration.getProperty(JAX_RS_APPLICATION_SERVICE_PROPERTIES);
        _LOG.info("Registering the Shiro Authorization feature with application {}",
                applicationProps.getOrDefault(JAX_RS_NAME, "<No Name found in application configuration>"));
    }

    Map<Class<?>, Integer> contracts = configuration.getContracts(ExceptionMapper.class);
    if(contracts.isEmpty()) {
        _LOG.debug("Registering the Shiro ExceptionMapper");
        // Only register the ExceptionMapper if it isn't already registered
        fc.register(ExceptionMapper.class, AUTHORIZATION);
    } else if(AUTHORIZATION < contracts.getOrDefault(javax.ws.rs.ext.ExceptionMapper.class, USER)) {
        _LOG.debug("Updating the priority of the Shiro ExceptionMapper from {} to {}",
                contracts.getOrDefault(javax.ws.rs.ext.ExceptionMapper.class, USER),
                AUTHORIZATION);
        // Update the priority if it's registered too low
        contracts.put(javax.ws.rs.ext.ExceptionMapper.class, AUTHORIZATION);
    }

    contracts = configuration.getContracts(SubjectPrincipalRequestFilter.class);
    if(contracts.isEmpty()) {
        _LOG.debug("Registering the Shiro SubjectPrincipalRequestFilter");
        // Only register the SubjectPrincipalRequestFilter if it isn't already registered
        fc.register(SubjectPrincipalRequestFilter.class, AUTHORIZATION);
    } else if(AUTHORIZATION < contracts.getOrDefault(ContainerRequestFilter.class, USER)) {
        _LOG.debug("Updating the priority of the Shiro SubjectPrincipalRequestFilter from {} to {}",
                contracts.getOrDefault(ContainerRequestFilter.class, USER),
                AUTHORIZATION);
        // Update the priority if it's registered too low
        contracts.put(ContainerRequestFilter.class, AUTHORIZATION);
    }

    _LOG.debug("Registering the Shiro ShiroAnnotationFilterFeature");
    fc.register(ShiroAnnotationFilterFeature.class, Priorities.AUTHORIZATION);
    return true;
}
 
Example 13
Source File: ClientImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public Builder request() {
    checkClosed();
    Map<String, Object> configProps = getConfiguration().getProperties();

    initTargetClientIfNeeded(configProps);

    ClientProviderFactory pf =
        ClientProviderFactory.getInstance(WebClient.getConfig(targetClient).getEndpoint());
    List<Object> providers = new LinkedList<>();
    List<org.apache.cxf.feature.Feature> cxfFeatures =
        new LinkedList<>();
    Configuration cfg = configImpl.getConfiguration();
    for (Object p : cfg.getInstances()) {
        if (p instanceof org.apache.cxf.feature.Feature) {
            cxfFeatures.add((org.apache.cxf.feature.Feature)p);
        } else if (!(p instanceof Feature)) {
            Map<Class<?>, Integer> contracts = cfg.getContracts(p.getClass());
            if (contracts == null || contracts.isEmpty()) {
                providers.add(p);
            } else {
                final Class<?> providerCls = ClassHelper.getRealClass(pf.getBus(), p);
                providers.add(new FilterProviderInfo<Object>(p.getClass(),
                    providerCls, p, pf.getBus(), contracts));
            }
        }
    }

    pf.setUserProviders(providers);
    ClientConfiguration clientCfg = WebClient.getConfig(targetClient);

    clientCfg.getRequestContext().putAll(configProps);
    clientCfg.getRequestContext().put(Client.class.getName(), ClientImpl.this);
    clientCfg.getRequestContext().put(Configuration.class.getName(),
                                                              getConfiguration());

    // Response auto-close
    Boolean responseAutoClose = getBooleanValue(configProps.get(HTTP_RESPONSE_AUTOCLOSE_PROP));
    if (responseAutoClose != null) {
        clientCfg.getResponseContext().put("response.stream.auto.close", responseAutoClose);
    }
    // TLS
    TLSClientParameters tlsParams = secConfig.getTlsClientParams();
    if (tlsParams.getSSLSocketFactory() != null
        || tlsParams.getTrustManagers() != null
        || tlsParams.getHostnameVerifier() != null) {
        clientCfg.getHttpConduit().setTlsClientParameters(tlsParams);
    }
    // Executor for the asynchronous calls
    Object executorServiceProp = configProps.get(AbstractClient.EXECUTOR_SERVICE_PROPERTY);
    if (executorServiceProp != null) {
        clientCfg.getResponseContext().put(AbstractClient.EXECUTOR_SERVICE_PROPERTY, executorServiceProp);
    }
    setConnectionProperties(configProps, clientCfg);
    // CXF Features
    for (org.apache.cxf.feature.Feature cxfFeature : cxfFeatures) {
        cxfFeature.initialize(clientCfg, clientCfg.getBus());
    }
    // Start building the invocation
    return new InvocationBuilderImpl(WebClient.fromClient(targetClient),
                                     getConfiguration());
}