So I have to create a filter that adds/formats date strings.
My implementation was to create a ContainerRequestFilter
and perform formatting there; then add this custom filter through @NameBinding
.
My problem seems to be that @NameBinding
is ignored when used with @PreMatching
, thus not being able to work since I also use reflection to extract properties from my filter/annotation.
So after performing formatting in filter, the idea is to use:
uriBuilder.replaceQueryParam(startDateQueryParamName, formattedString);
but even if I add a hardcoded value, the value is still the original.
Say I make a request: .../api/x?startDate=1234-01-01T00:00:00
And I hardcode in filter:
`uriBuilder.replaceQueryParam(startDateQueryParamName, "2020-05-05T00:00:00");`
I still get 1234-01-01T00:00:00
in resource method:
@GET
@Path("/t1")
@Produces(MediaType.TEXT_PLAIN)
@StartEndDateFilter(required = true)
public String testLocalDateTime(@QueryParam("startDate") LocalDateTime startDate, @QueryParam("endDate") LocalDateTime endDate, @Context UriInfo urinfo) {
MultivaluedMap<String, String> m = urinfo.getQueryParameters();
String d = startDate == null ? "nothin " : startDate.toString();
String e = endDate == null ? "nothin " : endDate.toString();
return String.format("start: %s \nend: %s", d, e);
}
So, I thought maybe using @PreMatching
would help but, as I mentioned, this shows a warning:
Warning: @PreMatching provider, class com.api.DateRangeFilter, also annotated with a name binding annotation. Name binding will be ignored.
And on top of that, when I call requestContext.setRequestUri(uriBuilder.build());
I get the following error when I call the endpoint:
Warning: StandardWrapperValve[com.api.Ap]: Servlet.service() for servlet com.api.Ap threw exception java.lang.IllegalStateException: Method could be called only in pre-matching request filter. at org.glassfish.jersey.server.ContainerRequest.setRequestUri(ContainerRequest.java:411) at com.api.DateRangeFilter.filter(DateRangeFilter.java:153)
line 153 is:
requestContext.setRequestUri(uriBuilder.build());