kamaci
kamaci

Reputation: 75127

Modify Path Param of an HttpServletRequest

I have a method which processes that URL:

http://IP:PORT/auth/myapp?Username=username

and accessible from remote. However I can not change external system which uses my app and it sends username within HTTP Header. I mean they access that URL:

http://IP:PORT/auth/myapp

I think that I can get related HTTP Header as follows:

Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
    String headerName = (String)headerNames.nextElement();
    if (headerName.equals("UNAME")) {
        String username = request.getHeader(headerName);
    }   
}

I can not modify whole part of my app and I have to add that info as path parameter into existing request. I mean change that request to:

http://IP:PORT/auth/myapp?Username=username

How can I do that?

PS: Can this piece of code solve the problem that I've described?

if (request.getParameter("Username") == null) {
    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        if (headerName.equals("UNAME")) {
            String username = request.getHeader(headerName);
            response.sendRedirect(request.getRequestURI() + "&Username="+username);
        }
    }
}

Upvotes: 0

Views: 1758

Answers (3)

fabballe
fabballe

Reputation: 771

I think there is two solutions:

  1. Use a filter on your application mapped on each request. The filter will check header and add a parameter if the UNAME header is there
  2. Use Apache Rewrite Module in front of your web application. Rewrite module will add the parameter to the url (check "Module Apache mod_rewrite")

UPDATE: As mohit said used a HttpRequestWrapper combined with a filter

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
        FilterChain filterChain) throws IOException, ServletException {
        //create the MyCustomWrapperRequest object to wrap the HttpServletRequest
        MyCustomWrapperRequest request = new MyCustomWrapperRequest((HttpServletRequest)servletRequest);
        //continue on in the filter chain
        filterChain.doFilter(request, servletResponse);
}

And in MyCustomWrapperRequest override getParameter() function

Upvotes: 0

Mohit
Mohit

Reputation: 1755

If you are accessing 'username' query parameter using HttpServletRequest.getParameter('username') in other parts of your app, you can try using HttpServletRequestWrapper

class MyRequestWrapper extends HttpServletRequestWrapper{

    public String getParameter(String name){
        // if name equals username, call super.getHeader('username')
        //else super.getParameter(name);
    }
}

You can extend this class and override getParameter() method. In your implementation, you get the value from header if parameter name is username else call the super method.

Upvotes: 1

ThatOneCloud
ThatOneCloud

Reputation: 83

It's round-a-bout, but if you also have the HttpServletResponse object, you can send a redirect request to the http://IP:PORT/auth/myapp?Username=username URL

Upvotes: 0

Related Questions