I've made a ParamConverter
which provides an Instant
(Date) when given a string formatted as either Instant's native ISO-8601, or as an integer number of milliseconds since the epoch. This is working fine, but I also need to be able to support other date formats (the customers are fussy).
To avoid the classic dd/mm/yyyy
vs mm/dd/yyyy
ambiguity, I'd like to have the customer specify their preferred format as part of the request*. e.g:
GET http://api.example.com/filter?since=01/02/2000&dateformat=dd/mm/yyyy
passed to a method which looks like:
@GET
String getFilteredList( final @QueryParam( "since" ) Instant since ) {
...
}
(time & timezone parts omitted for clarity)
So I'd like my ParamConverter<Instant>
to be able to read the dateformat
parameter.
I've been able to use a combination of a filter which sets a ContainerRequestContext
property and an AbstractValueFactoryProvider
to do something similar, but that needs the parameter to have a custom annotation applied and doesn't let it work with QueryParam/FormParam/etc., making it far less useful.
Is there any way to get other parameters, or the request object itself, from inside a ParamConverter?
[*] In the real world this would be from a selection of pre-approved formats, but for now just assume they're providing the input to a DateTimeFormatter
For clarity, here's the code I have:
public class InstantParameterProvider implements ParamConverterProvider {
private static final ParamConverter<Instant> INSTANT_CONVERTER =
new ParamConverter<Instant>( ) {
@Override public final T fromString( final String value ) {
// This is where I would like to get the other parameter's value
// Is it possible?
}
@Override public final String toString( final T value ) {
return value.toString( );
}
};
@SuppressWarnings( "unchecked" )
@Override public <T> ParamConverter<T> getConverter(
final Class<T> rawType,
final Type genericType,
final Annotation[] annotations
) {
if( rawType == Instant.class ) {
return (ParamConverter<T>) INSTANT_CONVERTER;
}
return null;
}
}