@SuppressWarnings("unchecked") public void register(final Class<?> c) { if (MessageBodyReader.class.isAssignableFrom(c)) { readers.add((Class<MessageBodyReader<?>>) c, MediaTypeUtils.parseMediaTypes(c.getAnnotation(Consumes.class))); } if (MessageBodyWriter.class.isAssignableFrom(c)) { writers.add((Class<MessageBodyWriter<?>>) c, MediaTypeUtils.parseMediaTypes(c.getAnnotation(Produces.class))); } if (ExceptionMapper.class.isAssignableFrom(c)) { exceptionMappers.add((Class<ExceptionMapper<?>>) c, MediaTypeUtils.parseMediaTypes(c.getAnnotation(Produces.class))); } if (ParamConverterProvider.class.isAssignableFrom(c)) { paramConverterProviders.add((ParamConverterProvider) application.get(c)); } }
@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()); } }
@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()); } }
@Override protected void configure() { bind(OptionsMethodProcessor.DefaultOptionsResponseGenerator.class) .to(OptionsResponseGenerator.class) .in(Singleton.class); bind(OptionsMethodProcessor.class) .to(ModelProcessor.class) .in(Singleton.class); bind(ParamConverters.TypeFromStringEnum.class) .to(ParamConverterProvider.class) .in(Singleton.class).ranked(10); bind(ParamConverters.DateProvider.class) .to(ParamConverterProvider.class) .in(Singleton.class).ranked(10); bind(ParamConverters.BooleanProvider.class) .to(ParamConverterProvider.class) .in(Singleton.class).ranked(10); }
public <T> ParamConverter<T> getParamConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) { for (final ParamConverterProvider provider : paramConverterProviders) { final ParamConverter<T> converter = provider.getConverter(rawType, genericType, annotations); if (converter != null) { return converter; } } return null; }
@Test public void shouldRegisterAMultiTypedProviderInstance() { MultiTypedProvider provider = new MultiTypedProvider(); Class[] providerTypes = {ClientRequestFilter.class, ClientResponseFilter.class, MessageBodyReader.class, MessageBodyWriter.class, ReaderInterceptor.class, WriterInterceptor.class, ResponseExceptionMapper.class, ParamConverterProvider.class}; RestClientBuilder builder = RestClientBuilder.newBuilder().register(provider, providerTypes); Configuration configuration = builder.getConfiguration(); assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered"); assertTrue(configuration.isRegistered(provider), MultiTypedProvider.class + " should be registered"); assertEquals(configuration.getContracts(MultiTypedProvider.class).size(), providerTypes.length, "There should be "+providerTypes.length+" provider types registered"); }
@Test public void shouldRegisterAMultiTypedProviderClass() { Class[] providerTypes = {ClientRequestFilter.class, ClientResponseFilter.class, MessageBodyReader.class, MessageBodyWriter.class, ReaderInterceptor.class, WriterInterceptor.class, ResponseExceptionMapper.class, ParamConverterProvider.class}; RestClientBuilder builder = RestClientBuilder.newBuilder().register(MultiTypedProvider.class, providerTypes); Configuration configuration = builder.getConfiguration(); assertTrue(configuration.isRegistered(MultiTypedProvider.class), MultiTypedProvider.class + " should be registered"); assertEquals(configuration.getContracts(MultiTypedProvider.class).size(), providerTypes.length, "There should be "+providerTypes.length+" provider types registered"); }
private static <T> boolean isProvider(final Class<T> clazz) { return MessageBodyReader.class.isAssignableFrom(clazz) || MessageBodyWriter.class.isAssignableFrom(clazz) || ParamConverter.class.isAssignableFrom(clazz) || ContainerRequestFilter.class.isAssignableFrom(clazz) || ContainerResponseFilter.class.isAssignableFrom(clazz) || ReaderInterceptor.class.isAssignableFrom(clazz) || WriterInterceptor.class.isAssignableFrom(clazz) || ParamConverterProvider.class.isAssignableFrom(clazz) || ContextResolver.class.isAssignableFrom(clazz) || new MetaAnnotatedClass<>(clazz).isAnnotationPresent(Provider.class); }
@Override protected void configure() { // Param converter providers bind(OptionParamConverterProvider.class).to(ParamConverterProvider.class).in(Singleton.class); }
@Override protected void configure() { // Param converter providers bind(LazyParamConverterProvider.class).to(ParamConverterProvider.class).in(Singleton.class); }
private static List<ParamConverterProvider> getDefaultParamConverterProviders() { return Collections.singletonList(new UuidParamConverterProvider()); }
/** * Unregisters a @Provider type from this factory. */ public void removeRegistrations(final Class<?> type) { checkNotNull(type); log.debug("Removing registrations for: {}", type.getName()); classContracts.remove(type); removeInstancesOf(type, providerInstances); providerClasses.remove(type); if (ExceptionMapper.class.isAssignableFrom(type)) { removeInstancesOf(type, exceptionMappers.values()); } else if (MessageBodyReader.class.isAssignableFrom(type)) { clearInstancesOf(type, clientMessageBodyReaders, DUMMY_READER); clearInstancesOf(type, serverMessageBodyReaders, DUMMY_READER); } else if (MessageBodyWriter.class.isAssignableFrom(type)) { clearInstancesOf(type, clientMessageBodyWriters, DUMMY_WRITER); clearInstancesOf(type, serverMessageBodyWriters, DUMMY_WRITER); } else if (ContextResolver.class.isAssignableFrom(type)) { Type[] args = Types.getActualTypeArgumentsOfAnInterface(type, ContextResolver.class); contextResolvers.remove(Types.getRawType(args[0])); } else if (Feature.class.isAssignableFrom(type)) { removeInstancesOf(type, featureInstances); removeInstancesOf(type, enabledFeatures); featureClasses.remove(type); } else if (DynamicFeature.class.isAssignableFrom(type)) { removeInstancesOf(type, clientDynamicFeatures); removeInstancesOf(type, serverDynamicFeatures); } else if (ParamConverterProvider.class.isAssignableFrom(type)) { removeInstancesOf(type, paramConverterProviders); } else if (StringConverter.class.isAssignableFrom(type)) { removeInstancesOf(type, stringConverters.values()); } else if (StringParameterUnmarshaller.class.isAssignableFrom(type)) { stringParameterUnmarshallers.values().remove(type); } else { log.warn("Unable to remove registrations for: {}", type.getName()); } }
@Override protected void configure() { // Param converter providers bind(OptionalParamConverterProvider.class).to(ParamConverterProvider.class).in(Singleton.class); }