How to handle http params in jetty?

40 views Asked by At

I'm trying to handle requests like this ...

"http://someINSANEdomainFORsite.coolsuffix/" + Some text

For example:

  • http://someINSANEdomainFORsite.coolsuffix/stackoverflowIsBest
  • http://someINSANEdomainFORsite.coolsuffix/JettyCool

I don't know how to do this I think there is some way to handle this, becouse its extremely common situation. Maybe there is some ContextHandler to do this or there isnt.

I tried to search some examples

1

There are 1 answers

0
Joakim Erdfelt On BEST ANSWER

Those examples are just paths in a request, not http params, nor http parameters, nor query strings, nor form details.

If using Jetty 12, and a Jetty Handler, it would look like this ...

public class GetPathHandler extends Handler.Abstract
{
    @Override
    public boolean handle(Request request, Response response, Callback callback) throws Exception
    {
        String path = request.getHttpURI().getPath();
        String canonicalPath = request.getHttpURI().getCanonicalPath();
        String decodedPath = request.getHttpURI().getDecodedPath();

        String utf8Text = "path = " + path +
            "\ncanonicalPath = " + canonicalPath +
            "\ndecodedPath = " + decodedPath;

        response.getHeaders().put(HttpHeader.CONTENT_TYPE, "text/plain; charset=utf-8");
        Content.Sink.write(response, true, utf8Text, callback);
        return true;
    }
}

The differences between those paths are ...

  • .getPath() is the raw path, as-is given to Jetty.
  • .getDecodedPath() is the decoded raw path.
  • .getCanonicalPath() is the decoded raw path and then normalized.