Reputation: 3859
I have a filter , I'm using Springs delegatingFilterProxy to intercept incoming requests for a web service. Depending on whether a flag is set i want to allow the request to continue otherwise i want to set a request header which when checked by the endpoint will indicate whether to allow the request to continue or not. I want to place the code in the filter to abstract the logic of checking the flag.
How can i modify the request header in a filter (simple code example would be great) and is it acceptable practice, or should i be sending the response from the filter instead of pushing the decision down to the endpoint?
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(somecondition)
set request header and forward to endpoint
else
forward to endpoint without extra header
Thanks
Upvotes: 1
Views: 1784
Reputation: 12880
Request attributes are better suited for this purpose ServletRequest.get/setAttribute()
. Is there any reason you cannot use them?
Upvotes: 1
Reputation: 388446
You need to create a FakeRequest to solve it
FakeHeadersRequest
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class FakeHeadersRequest extends HttpServletRequestWrapper {
private Map<String, List<String>> headers = new HashMap<String, List<String>>();
public FakeHeadersRequest(HttpServletRequest request) {
super(request);
}
@Override
public String getHeader(String name) {
if (headers.containsKey(name)) {
return headers.get(name).get(0);
}
return super.getHeader(name);
}
public void addHeader(String header, String value) {
List<String> list = headers.get(header);
if (list == null) {
list = new ArrayList<String>();
headers.put(header, list);
}
list.add(value);
}
public void setHeader(String header, String value) {
List<String> list = new ArrayList<String>();
headers.put(header, list);
list.add(value);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Enumeration getHeaderNames() {
List<String> list = new ArrayList<String>();
HttpServletRequest request = (HttpServletRequest) getRequest();
Enumeration<String> e = request.getHeaderNames();
while (e.hasMoreElements()) {
list.add(e.nextElement());
}
list.addAll(headers.keySet());
return Collections.enumeration(list);
}
}
FakeHeaderFilter
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class FakeHeaderFilter implements Filter {
public void init(FilterConfig filterConfig) {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
boolean condition = false;
if (condition && request instanceof HttpServletRequest) {
FakeHeadersRequest req = new FakeHeadersRequest(
(HttpServletRequest) request);
req.setHeader("new-header", "some-value");
chain.doFilter(req, response);
} else {
chain.doFilter(request, response);
}
}
public void destroy() {
}
}
Also refer this
Upvotes: 2