Search in sources :

Example 1 with ParamConverterProvider

use of javax.ws.rs.ext.ParamConverterProvider 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.fromNullable(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) {
                        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)

Example 2 with ParamConverterProvider

use of javax.ws.rs.ext.ParamConverterProvider 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 3 with ParamConverterProvider

use of javax.ws.rs.ext.ParamConverterProvider 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)

Aggregations

ParamConverter (javax.ws.rs.ext.ParamConverter)3 ParamConverterProvider (javax.ws.rs.ext.ParamConverterProvider)3 ClassTypePair (org.glassfish.jersey.internal.util.collection.ClassTypePair)3 Function (com.google.common.base.Function)1 Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)1 Providers (org.glassfish.jersey.internal.inject.Providers)1 ReflectionHelper (org.glassfish.jersey.internal.util.ReflectionHelper)1