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;
}
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)));
}
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)));
}
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)));
}
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)));
}
Aggregations