use of javax.ws.rs.ext.ParamConverter in project jersey by jersey.
the class OptionalParamConverterProvider method getConverter.
@Override
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) {
final List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(genericType);
final ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
if (ctp == null || ctp.rawClass() == String.class) {
return new ParamConverter<T>() {
@Override
public T fromString(final String value) {
return rawType.cast(Optional.fromNullable(value));
}
@Override
public String toString(final T value) throws IllegalArgumentException {
return value.toString();
}
};
}
final Set<ParamConverterProvider> converterProviders = Providers.getProviders(injectionManager, ParamConverterProvider.class);
for (ParamConverterProvider provider : converterProviders) {
@SuppressWarnings("unchecked") final ParamConverter<?> converter = provider.getConverter(ctp.rawClass(), ctp.type(), annotations);
if (converter != null) {
return new ParamConverter<T>() {
@Override
public T fromString(final String value) {
return rawType.cast(Optional.fromNullable(value).transform((Function<String, Object>) s -> converter.fromString(value)));
}
@Override
public String toString(final T value) throws IllegalArgumentException {
return value.toString();
}
};
}
}
return null;
}
use of javax.ws.rs.ext.ParamConverter in project dropwizard by dropwizard.
the class OptionalParamConverterProvider method getConverter.
/**
* {@inheritDoc}
*/
@Override
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) {
if (Optional.class.equals(rawType)) {
final List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(genericType);
final ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
if (ctp == null || ctp.rawClass() == String.class) {
return new ParamConverter<T>() {
@Override
public T fromString(final String value) {
return rawType.cast(Optional.ofNullable(value));
}
@Override
public String toString(final T value) {
return value.toString();
}
};
}
for (ParamConverterProvider provider : Providers.getProviders(locator, ParamConverterProvider.class)) {
final ParamConverter<?> converter = provider.getConverter(ctp.rawClass(), ctp.type(), annotations);
if (converter != null) {
return new ParamConverter<T>() {
@Override
public T fromString(final String value) {
return rawType.cast(Optional.ofNullable(value).map(s -> converter.fromString(value)));
}
@Override
public String toString(final T value) {
return value.toString();
}
};
}
}
}
return null;
}
use of javax.ws.rs.ext.ParamConverter in project camel by apache.
the class EnumQueryParamConverterProvider method getConverter.
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if (rawType.isEnum()) {
try {
final Method valueMethod = rawType.getMethod("value", null);
final Method fromValueMethod = rawType.getMethod("fromValue", String.class);
return new ParamConverter<T>() {
@Override
public T fromString(String value) {
try {
return (T) fromValueMethod.invoke(null, value);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException(e);
}
}
@Override
public String toString(T value) {
try {
return (String) valueMethod.invoke(value);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException(e);
}
}
};
} catch (NoSuchMethodException e) {
LOG.debug("Enumeration {} does not follow JAXB convention for conversion", rawType.getName());
}
}
return null;
}
use of javax.ws.rs.ext.ParamConverter in project cxf by apache.
the class JAXRSClientFactoryBean method initClient.
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 = theProperties == null ? null : (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;
}
});
}
}
use of javax.ws.rs.ext.ParamConverter in project cxf by apache.
the class AbstractClient method convertParamValue.
protected String convertParamValue(Object pValue, Class<?> pClass, Annotation[] anns) {
if (pValue == null && pClass == null) {
return null;
}
ProviderFactory pf = ClientProviderFactory.getInstance(cfg.getEndpoint());
if (pf != null) {
Message m = null;
if (pf.isParamConverterContextsAvailable()) {
m = new MessageImpl();
m.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
m.setExchange(new ExchangeImpl());
m.getExchange().setOutMessage(m);
m.getExchange().put(Endpoint.class, cfg.getEndpoint());
}
@SuppressWarnings("unchecked") ParamConverter<Object> prov = (ParamConverter<Object>) pf.createParameterHandler(pClass, pClass, anns, m);
if (prov != null) {
try {
return prov.toString(pValue);
} finally {
if (m != null) {
pf.clearThreadLocalProxies();
}
}
}
}
final String v = pValue == null ? null : pValue.toString();
if (anns != null && StringUtils.isEmpty(v)) {
final PathParam pp = AnnotationUtils.getAnnotation(anns, PathParam.class);
if (null != pp) {
Object allowEmptyProp = getConfiguration().getBus().getProperty(ALLOW_EMPTY_PATH_VALUES);
if (!PropertyUtils.isTrue(allowEmptyProp)) {
throw new IllegalArgumentException("Value for " + pp.value() + " is not specified");
}
}
}
return v;
}
Aggregations