Search in sources :

Example 1 with ParamConverter

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

the class AbstractParamConverterProvider method getConverter.

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
    if (AbstractParam.class.isAssignableFrom(rawType)) {
        final String parameterName = JerseyParameterNameProvider.getParameterNameFromAnnotations(annotations).orElse("Parameter");
        final Constructor<T> constructor;
        try {
            constructor = rawType.getConstructor(String.class, String.class);
        } catch (NoSuchMethodException ignored) {
            // leaving Jersey to handle these parameters as it normally would.
            return null;
        }
        return new ParamConverter<T>() {

            @Override
            @SuppressWarnings("unchecked")
            public T fromString(String value) {
                if (rawType != NonEmptyStringParam.class && Strings.isNullOrEmpty(value)) {
                    return null;
                }
                try {
                    return _fromString(value);
                } catch (InvocationTargetException ex) {
                    final Throwable cause = ex.getCause();
                    if (cause instanceof WebApplicationException) {
                        throw (WebApplicationException) cause;
                    } else {
                        throw new ExtractorException(cause);
                    }
                } catch (final Exception ex) {
                    throw new ProcessingException(ex);
                }
            }

            protected T _fromString(String value) throws Exception {
                return constructor.newInstance(value, parameterName);
            }

            @Override
            public String toString(T value) throws IllegalArgumentException {
                if (value == null) {
                    throw new IllegalArgumentException(LocalizationMessages.METHOD_PARAMETER_CANNOT_BE_NULL("value"));
                }
                return value.toString();
            }
        };
    }
    return null;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ParamConverter(javax.ws.rs.ext.ParamConverter) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExtractorException(org.glassfish.jersey.internal.inject.ExtractorException) ProcessingException(javax.ws.rs.ProcessingException)

Example 2 with ParamConverter

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

the class JavaTimeTypesParamConverterProviderTest method localDate.

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

Example 3 with ParamConverter

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

the class JavaTimeTypesParamConverterProviderTest method localDateTime.

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

Example 4 with ParamConverter

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

the class JavaTimeTypesParamConverterProviderTest method zonedDateTime.

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

Example 5 with ParamConverter

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

the class JavaTimeTypesParamConverterProviderTest method localTime.

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

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