Search in sources :

Example 11 with ParamConverter

use of javax.ws.rs.ext.ParamConverter in project cxf by apache.

the class JavaTimeTypesParamConverterProviderTest method offsetTime.

@Test
public void offsetTime() {
    OffsetTime offsetTime = OffsetTime.of(10, 33, 24, 5, ZoneOffset.ofHours(2));
    ParamConverter<OffsetTime> converter = (ParamConverter<OffsetTime>) provider.getConverter(offsetTime.getClass(), offsetTime.getClass(), offsetTime.getClass().getAnnotations());
    Assert.assertEquals(offsetTime, converter.fromString(converter.toString(offsetTime)));
}
Also used : ParamConverter(javax.ws.rs.ext.ParamConverter) OffsetTime(java.time.OffsetTime) Test(org.junit.Test)

Example 12 with ParamConverter

use of javax.ws.rs.ext.ParamConverter in project cxf by apache.

the class JavaTimeTypesParamConverterProviderTest method offsetDateTime.

@Test
public void offsetDateTime() {
    OffsetDateTime offsetDateTime = OffsetDateTime.of(2016, 2, 24, 5, 55, 0, 0, ZoneOffset.UTC);
    ParamConverter<OffsetDateTime> converter = (ParamConverter<OffsetDateTime>) provider.getConverter(offsetDateTime.getClass(), offsetDateTime.getClass(), offsetDateTime.getClass().getAnnotations());
    Assert.assertEquals(offsetDateTime, converter.fromString(converter.toString(offsetDateTime)));
}
Also used : OffsetDateTime(java.time.OffsetDateTime) ParamConverter(javax.ws.rs.ext.ParamConverter) Test(org.junit.Test)

Example 13 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)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