javax.ws.rs.ext.ParamConverterProvider Java Examples

The following examples show how to use javax.ws.rs.ext.ParamConverterProvider. 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: ParamConverterUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the string-based representation of the value to the instance of particular type
 * using parameter converter provider and available parameter converter.
 * @param type type to convert from string-based representation
 * @param provider parameter converter provider to use
 * @param value the string-based representation to convert
 * @return instance of particular type converter from its string representation
 */
@SuppressWarnings("unchecked")
public static< T > T getValue(final Class< T > type, final ParamConverterProvider provider,
        final String value) {

    if (String.class.isAssignableFrom(type)) {
        return (T)value;
    }

    if (provider != null) {
        final ParamConverter< T > converter = provider.getConverter(type, null, new Annotation[0]);
        if (converter != null) {
            return converter.fromString(value);
        }
    }

    throw new IllegalArgumentException(String.format(
            "Unable to convert string '%s' to instance of class '%s': no appropriate converter provided",
            value, type.getName()));
}
 
Example #3
Source File: ProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
public <T> ParamConverter<T> createParameterHandler(Class<T> paramType,
                                                    Type genericType,
                                                    Annotation[] anns,
                                                    Message m) {

    anns = anns != null ? anns : new Annotation[]{};
    for (ProviderInfo<ParamConverterProvider> pi : paramConverters) {
        injectContextValues(pi, m);
        ParamConverter<T> converter = pi.getProvider().getConverter(paramType, genericType, anns);
        if (converter != null) {
            return converter;
        }
        pi.clearThreadLocalProxies();
    }
    return null;
}
 
Example #4
Source File: RsAddonFeature.java    From ameba with MIT License 6 votes vote down vote up
@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);
}
 
Example #5
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 #6
Source File: RESTService.java    From tomee with Apache License 2.0 5 votes vote down vote up
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) ||
            Feature.class.isAssignableFrom(clazz) ||
            new MetaAnnotatedClass<>(clazz).isAnnotationPresent(Provider.class);
}
 
Example #7
Source File: ParamConverterUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the instance of particular type into its string-based representation
 * using parameter converter provider and available parameter converter.
 * @param type type to convert to string-based representation
 * @param provider parameter converter provider to use
 * @param value the typed instance to convert to string representation
 * @return string-based representation of the instance of particular type
 */
public static< T > String getString(final Class< T > type, final ParamConverterProvider provider,
        final T value) {

    if (provider != null) {
        final ParamConverter< T > converter = provider.getConverter(type, null, new Annotation[0]);
        if (converter != null) {
            return converter.toString(value);
        }
    }

    return value == null ? null : value.toString();
}
 
Example #8
Source File: AdditionalRegistrationTest.java    From microprofile-rest-client with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example #9
Source File: ProviderFactoryTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testParameterHandlerProvider() throws Exception {
    ProviderFactory pf = ServerProviderFactory.getInstance();
    ParamConverterProvider h = new CustomerParameterHandler();
    pf.registerUserProvider(h);
    ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class, null,
                                                            new MessageImpl());
    assertSame(h2, h);
}
 
Example #10
Source File: ProviderFactory.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void checkParamConverterContexts() {
    for (ProviderInfo<ParamConverterProvider> pi : paramConverters) {
        if (pi.contextsAvailable()) {
            paramConverterContextsAvailable = true;
        }
    }

}
 
Example #11
Source File: AdditionalRegistrationTest.java    From microprofile-rest-client with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example #12
Source File: ParamConverterProvidersProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void postConstruct() {
	paramConverterProviders = BeanUtil.getBeanInstances(beanManager, ParamConverterProvider.class);

	paramConverterProviders.add(new ParamConverterProviderImpl());

	Collections.sort(paramConverterProviders, new DescendingPriorityComparator());
}
 
Example #13
Source File: PortletParamProducer.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
private <T> ParamConverter _getParamConverter(Class<?> rawType, Type baseType, Annotation[] annotations) {

		for (ParamConverterProvider paramConverterProvider : paramConverterProviders) {

			ParamConverter<T> paramConverter = paramConverterProvider.getConverter((Class<T>) rawType, baseType,
					annotations);

			if (paramConverter != null) {
				return paramConverter;
			}
		}

		return null;
	}
 
Example #14
Source File: ParamConverterProviderManager.java    From portals-pluto with Apache License 2.0 5 votes vote down vote up
public <T> ParamConverter getParamConverter(Class<?> rawType, Type baseType, Annotation[] annotations) {

		for (ParamConverterProvider paramConverterProvider : paramConverterProviders) {
			ParamConverter<T> paramConverter = paramConverterProvider.getConverter((Class<T>) rawType, baseType,
					annotations);

			if (paramConverter != null) {
				return paramConverter;
			}
		}

		return null;
	}
 
Example #15
Source File: ParamConverterProvidersProducer.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
@ApplicationScoped
@ParamConverterProviders
@Produces
public List<ParamConverterProvider> getParamConverterProviders() {
	return paramConverterProviders;
}
 
Example #16
Source File: ProviderFactory.java    From cxf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected void setCommonProviders(List<ProviderInfo<? extends Object>> theProviders) {
    List<ProviderInfo<ReaderInterceptor>> readInts =
        new LinkedList<>();
    List<ProviderInfo<WriterInterceptor>> writeInts =
        new LinkedList<>();
    for (ProviderInfo<? extends Object> provider : theProviders) {
        Class<?> providerCls = ClassHelper.getRealClass(bus, provider.getProvider());

        if (filterContractSupported(provider, providerCls, MessageBodyReader.class)) {
            addProviderToList(messageReaders, provider);
        }

        if (filterContractSupported(provider, providerCls, MessageBodyWriter.class)) {
            addProviderToList(messageWriters, provider);
        }

        if (filterContractSupported(provider, providerCls, ContextResolver.class)) {
            addProviderToList(contextResolvers, provider);
        }

        if (ContextProvider.class.isAssignableFrom(providerCls)) {
            addProviderToList(contextProviders, provider);
        }

        if (filterContractSupported(provider, providerCls, ReaderInterceptor.class)) {
            readInts.add((ProviderInfo<ReaderInterceptor>)provider);
        }

        if (filterContractSupported(provider, providerCls, WriterInterceptor.class)) {
            writeInts.add((ProviderInfo<WriterInterceptor>)provider);
        }

        if (filterContractSupported(provider, providerCls, ParamConverterProvider.class)) {
            paramConverters.add((ProviderInfo<ParamConverterProvider>)provider);
        }
    }
    sortReaders();
    sortWriters();
    sortContextResolvers();

    mapInterceptorFilters(readerInterceptors, readInts, ReaderInterceptor.class, true);
    mapInterceptorFilters(writerInterceptors, writeInts, WriterInterceptor.class, true);

    injectContextProxies(messageReaders, messageWriters, contextResolvers, paramConverters,
        readerInterceptors.values(), writerInterceptors.values());
    checkParamConverterContexts();
}
 
Example #17
Source File: JAXRSClientFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void initClient(AbstractClient client, Endpoint ep, boolean addHeaders) {

        if (username != null) {
            AuthorizationPolicy authPolicy = new AuthorizationPolicy();
            authPolicy.setUserName(username);
            authPolicy.setPassword(password);
            ep.getEndpointInfo().addExtensor(authPolicy);
        }

        client.getConfiguration().setConduitSelector(getConduitSelector(ep));
        client.getConfiguration().setBus(getBus());
        client.getConfiguration().getOutInterceptors().addAll(getOutInterceptors());
        client.getConfiguration().getOutInterceptors().addAll(ep.getOutInterceptors());
        client.getConfiguration().getInInterceptors().addAll(getInInterceptors());
        client.getConfiguration().getInInterceptors().addAll(ep.getInInterceptors());
        client.getConfiguration().getInFaultInterceptors().addAll(getInFaultInterceptors());

        applyFeatures(client);

        if (headers != null && addHeaders) {
            client.headers(headers);
        }
        ClientProviderFactory factory = ClientProviderFactory.createInstance(getBus());
        setupFactory(factory, ep);

        final Map<String, Object> theProperties = super.getProperties();
        final boolean encodeClientParameters = PropertyUtils.isTrue(theProperties, "url.encode.client.parameters");
        if (encodeClientParameters) {
            final String encodeClientParametersList =
                (String)getProperties().get("url.encode.client.parameters.list");
            factory.registerUserProvider(new ParamConverterProvider() {

                @SuppressWarnings("unchecked")
                @Override
                public <T> ParamConverter<T> getConverter(Class<T> cls, Type t, Annotation[] anns) {
                    if (cls == String.class
                        && AnnotationUtils.getAnnotation(anns, HeaderParam.class) == null
                        && AnnotationUtils.getAnnotation(anns, CookieParam.class) == null) {
                        return (ParamConverter<T>) new UrlEncodingParamConverter(encodeClientParametersList);
                    }
                    return null;
                }

            });
        }
    }
 
Example #18
Source File: LuceneDocumentMetadata.java    From cxf with Apache License 2.0 4 votes vote down vote up
public LuceneDocumentMetadata withFieldTypeConverter(final ParamConverterProvider provider) {
    this.converterProvider = provider;
    return this;
}
 
Example #19
Source File: LuceneDocumentMetadata.java    From cxf with Apache License 2.0 4 votes vote down vote up
public ParamConverterProvider getFieldTypeConverter() {
    return converterProvider;
}
 
Example #20
Source File: AbstractSearchConditionVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void setFieldTypeConverter(final ParamConverterProvider provider) {
    this.converterProvider = provider;
}
 
Example #21
Source File: AbstractSearchConditionVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public ParamConverterProvider getFieldTypeConverter() {
    return converterProvider;
}
 
Example #22
Source File: OptionalParamBinder.java    From dropwizard-java8 with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {
    // Param converter providers
    bind(OptionalParamConverterProvider.class).to(ParamConverterProvider.class).in(Singleton.class);
}
 
Example #23
Source File: SisuResteasyProviderFactory.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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());
  }
}
 
Example #24
Source File: ParamConverterProviderManager.java    From portals-pluto with Apache License 2.0 3 votes vote down vote up
@PostConstruct
public void postConstruct() {

	paramConverterProviders = BeanUtil.getBeanInstances(_beanManager, ParamConverterProvider.class);

	Collections.sort(paramConverterProviders, new DescendingPriorityComparator());
}