0

I have a URL coming in that is not in a good format (contains brackets) [] I want to intercept the incoming request from the HttpServletRequest object and modify this URL to encode it, then continue the flow of the function.

for example: https://my-website.com/getData/?when[name="john"]&location[place="starbucks"]

I want to handle the encoding of this string once it hits my function.

John Lippson
  • 1,269
  • 5
  • 17
  • 36
  • [This one](https://stackoverflow.com/questions/1413129/modify-request-parameter-with-servlet-filter) could help. Replacing strings with its encoded version is trivial `[ -> %5B , ] -> %5D`. – LMC Feb 02 '18 at 16:23
  • @LuisMuñoz I need to modify the income servlet object to ensure the changes are effective, that's the problem – John Lippson Feb 02 '18 at 16:24
  • but not the servlet itself, just the request, correct? – LMC Feb 02 '18 at 16:25
  • That's correct, but I'm wondering how to modify the request and have it persist in that same incoming object if that makes sense – John Lippson Feb 02 '18 at 16:27
  • Extend `HttpServletRequestWrapper` and override methods such as `getParameter(...)` and `getQueryString()` to handle the encoding. As @LuisMuñoz suggested, create a filter to handle the incoming request. Within the filter, create an instance of your wrapper, and pass your wrapper instance as part of the filter chain. When your function invokes `request.getParameter("when")`, the `request` will be your wrapper (or possibly another wrapper but that should not matter). – Andrew S Feb 02 '18 at 16:58
  • @AndrewS ok, is it possible to modify a uriInfo object as well? I call getRequestUri on the object, but it breaks – John Lippson Feb 02 '18 at 17:57
  • What does _break_ mean? Are you encoding just the query part, and not the whole URL? Add more code (or perhaps post a different question). – Andrew S Feb 02 '18 at 18:00
  • String url = uriInfo.getRequestUri().toString(); breaks because there is an illegal character in the url. I want to modify so the illegal character is gone. IllegalArgumentException – John Lippson Feb 02 '18 at 18:03

0 Answers0