Search in sources :

Example 16 with ParamConverter

use of javax.ws.rs.ext.ParamConverter in project dropwizard by dropwizard.

the class OptionalParamConverterProvider method getConverter.

/**
 * {@inheritDoc}
 */
@Override
@Nullable
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.fromNullable(value));
                }

                @Override
                public String toString(final T value) {
                    return value.toString();
                }
            };
        }
        for (ParamConverterProvider provider : Providers.getProviders(manager, 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) {
                        final Object convertedValue = value == null ? null : converter.fromString(value);
                        return rawType.cast(Optional.fromNullable(convertedValue));
                    }

                    @Override
                    public String toString(final T value) {
                        return value.toString();
                    }
                };
            }
        }
    }
    return null;
}
Also used : ClassTypePair(org.glassfish.jersey.internal.util.collection.ClassTypePair) ParamConverter(javax.ws.rs.ext.ParamConverter) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) Nullable(javax.annotation.Nullable)

Aggregations

ParamConverter (javax.ws.rs.ext.ParamConverter)16 Test (org.junit.Test)6 ParamConverterProvider (javax.ws.rs.ext.ParamConverterProvider)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Type (java.lang.reflect.Type)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 ClassTypePair (org.glassfish.jersey.internal.util.collection.ClassTypePair)3 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 Response (javax.ws.rs.core.Response)2 PathSegmentImpl (org.apache.cxf.jaxrs.impl.PathSegmentImpl)2 Function (com.google.common.base.Function)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 OffsetDateTime (java.time.OffsetDateTime)1