Wrapping an HttpServletRequest and HttpServletResponse
Sometimes we want to wrap our request and response objects and provide
new functionality as seen in the classes below. Below is an example of
how to wrap these objects. There needs to be a filter that intercepts
the request and response and supply the wrapped objects. The filter needs
to be mapped in web.xml which is not shown. The code below uses another
class I explained in another post (link here).
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class CustomHttpServletRequestWrapper extends
HttpServletRequestWrapper {
private final Map parameterMap;
private static final String caller =
"CustomHttpServletRequestWrapper";
private String method;
public CustomHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
this.parameterMap = copyParameters(request);
this.method = request.getMethod();
}
@SuppressWarnings("unchecked")
private Map copyParameters(HttpServletRequest request) {
final Map result = new HashMap();
result.putAll(request.getParameterMap());
return result;
}
@Override
public String getMethod() {
return method;
}
@Override
public String getParameter(String name) {
String[] values = getParameterValues(name);
return (values != null && values.length > 0) ? values[0] : null;
}
@Override
public Map getParameterMap() {
return parameterMap;
}
@Override
public Enumeration getParameterNames() {
return Collections.enumeration(parameterMap.keySet());
}
@Override
public String[] getParameterValues(String name) {
return parameterMap.get(name);
}
public void setMethod(String method, String reason) {
if (this.method != method) {
final StringBuilder sb = new StringBuilder();
sb.append(" Method changed to ").append(method);
sb.append("\n ").append(reason);
ThreadLogger.message(caller, sb.toString());
this.method = method;
}
}
public void renameParameter(String oldName, String newName) {
String[] values = parameterMap.get(oldName);
if (values != null) {
parameterMap.remove(oldName);
parameterMap.put(newName, values);
}
}
public void removeParameter(String name) {
parameterMap.remove(name);
}
}
import java.io.IOException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class CustomHttpServletResponseWrapper extends
HttpServletResponseWrapper {
private static final String caller = "CustomHttpServletResponseWrapper";
public CustomHttpServletResponseWrapper(HttpServletResponse response) {
super(response);
}
@Override
public void addCookie(Cookie cookie) {
final StringBuilder buffer = new StringBuilder();
final String cookieName = cookie.getName();
final String cookieValue = cookie.getValue();
super.addCookie(cookie);
// generate a debug message indicating the addition of cookie
ThreadLogger.message(caller, " Set cookie[" + cookieName + "] = "
+ cookieValue);
if (cookie.getSecure()) {
buffer.append("\n Secure");
}
}
@Override
public void sendError(int sc) throws IOException {
if (sc >= HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
ThreadLogger.error(caller, "Sending error response " + sc);
} else {
ThreadLogger.warn(caller, " Sending error response " + sc);
}
super.sendError(sc);
}
@Override
public void sendError(int sc, String msg) throws IOException {
if (sc >= HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
ThreadLogger.error(caller, "Sending error response " + sc + "\n "
+ msg);
} else {
ThreadLogger.warn(caller, " Sending error response " + sc + "\n "
+ msg);
}
super.sendError(sc, msg);
}
@Override
public void sendRedirect(String location) throws IOException {
ThreadLogger.message(caller, " Sending redirect to " + location);
super.sendRedirect(location);
}
@Override
public void setHeader(String name, String value) {
super.setHeader(name, value);
}
public void completed() {
ThreadLogger.message(caller, "Response completed.");
ThreadLogger.flush();
}
}
import java.io.IOException;
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;
import javax.servlet.http.HttpServletResponse;
public class CustomFilter implements javax.servlet.Filter {
private String dummyInitParam;
private static final String LOGGER_PREFIX = "[CustomFilter] ";
private static final String caller = "CustomFilter";
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.dummyInitParam = filterConfig
.getInitParameter("existingPlusLoginUrl");
}
@Override
public void destroy() {
ThreadLogger.message(caller, LOGGER_PREFIX + "destroyed");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
final CustomHttpServletRequestWrapper requestWrapper = new CustomHttpServletRequestWrapper(
(HttpServletRequest) request);
final CustomHttpServletResponseWrapper responseWrapper = new CustomHttpServletResponseWrapper(
(HttpServletResponse) response);
try {
doHttpFilter(requestWrapper, responseWrapper, chain);
}
finally {
responseWrapper.completed(); // need to call this to flush
// ThreadLogger
}
}
public void doHttpFilter(CustomHttpServletRequestWrapper request,
CustomHttpServletResponseWrapper response, FilterChain chain)
throws IOException, ServletException {
ThreadLogger.message(caller, "dummyParam: " + dummyInitParam);
// log important stuff
logStartOfRequest(request);
// do other stuff you want to do here.
// turn off caching
setNoCacheHeaders(response);
executeChain(request, response, chain);
}
private void executeChain(CustomHttpServletRequestWrapper request,
HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
ThreadLogger.message(caller, "Executing filter chain");
chain.doFilter(request, response);
}
private void setNoCacheHeaders(HttpServletResponse response) {
response.setHeader("Expires", "Tue, 31 Dec 2010 00:00:00 UTC");
response.setDateHeader("Last-Modified", System.currentTimeMillis());
response.setHeader("Cache-Control",
"no-store, no-cache, must-revalidate, max-age=0");
response.setHeader("Pragma", "no-cache");
}
private void logStartOfRequest(CustomHttpServletRequestWrapper request) {
// This must be called to enableDebug mode if it is set. Set a cookie
// enableDebug = true to enable debug.
// This will log all your request's state
ThreadLogger.logStartOfRequest(caller, request);
}
}
No comments:
Post a Comment