Search in sources :

Example 6 with ParamConverter

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;
}
Also used : Function(com.google.common.base.Function) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) ParamConverter(javax.ws.rs.ext.ParamConverter) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider)

Example 7 with ParamConverter

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;
}
Also used : Inject(javax.inject.Inject) ParamConverter(javax.ws.rs.ext.ParamConverter) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) List(java.util.List) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) Optional(java.util.Optional) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) Providers(org.glassfish.jersey.internal.inject.Providers) Singleton(javax.inject.Singleton) ReflectionHelper(org.glassfish.jersey.internal.util.ReflectionHelper) ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) ParamConverter(javax.ws.rs.ext.ParamConverter) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider)

Example 8 with ParamConverter

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;
}
Also used : ParamConverter(javax.ws.rs.ext.ParamConverter) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with ParamConverter

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;
            }
        });
    }
}
Also used : ParamConverter(javax.ws.rs.ext.ParamConverter) Annotation(java.lang.annotation.Annotation) CookieParam(javax.ws.rs.CookieParam) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Type(java.lang.reflect.Type) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider)

Example 10 with ParamConverter

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;
}
Also used : Message(org.apache.cxf.message.Message) ParamConverter(javax.ws.rs.ext.ParamConverter) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) PathParam(javax.ws.rs.PathParam) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Aggregations

ParamConverter (javax.ws.rs.ext.ParamConverter)13 Test (org.junit.Test)6 ParamConverterProvider (javax.ws.rs.ext.ParamConverterProvider)4 ClassTypePair (org.glassfish.jersey.internal.util.collection.ClassTypePair)3 Annotation (java.lang.annotation.Annotation)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Type (java.lang.reflect.Type)2 Function (com.google.common.base.Function)1 Method (java.lang.reflect.Method)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 OffsetDateTime (java.time.OffsetDateTime)1 OffsetTime (java.time.OffsetTime)1 ZonedDateTime (java.time.ZonedDateTime)1 List (java.util.List)1 Optional (java.util.Optional)1 Nullable (javax.annotation.Nullable)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1