2

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());
Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44
  • Maybe you can do it in a `ParamConverter`. [See example](http://stackoverflow.com/a/31627624/2587435) – Paul Samsotha May 19 '17 at 12:59
  • Nope, ParamConverters are for a different purpose – Esteban Rincon May 20 '17 at 02:28
  • My point is that what you're trying to do is not possible. So you will need to find another way. – Paul Samsotha May 20 '17 at 02:30
  • Maybe even some kind of hack. It's just not meant to work this way, as you want to access the selected resource method before it's selected, and you can't change anything about the URI after it's been selected. So you need to look for another way to get the results you want – Paul Samsotha May 20 '17 at 02:37
  • How did you finally solve it ? I am having the exact same problem!! – Ajay Sep 06 '17 at 09:16
  • 3
    @Ajay I used a ContainerRequestFilter with `@PreMatching` annotation and this made the filter global and applies before actual resource matching occurs. – Esteban Rincon Sep 07 '17 at 13:28

0 Answers0