Reputation: 75127
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
Reputation: 771
I think there is two solutions:
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
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
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