1 16 17 package org.springframework.web.filter; 18 19 import java.io.IOException ; 20 21 import javax.servlet.FilterChain ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 49 public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter { 50 51 public static final String DEFAULT_BEFORE_MESSAGE_PREFIX = "Before request ["; 52 53 public static final String DEFAULT_BEFORE_MESSAGE_SUFFIX = "]"; 54 55 public static final String DEFAULT_AFTER_MESSAGE_PREFIX = "After request ["; 56 57 public static final String DEFAULT_AFTER_MESSAGE_SUFFIX = "]"; 58 59 60 private boolean includeQueryString = false; 61 62 private String beforeMessagePrefix = DEFAULT_BEFORE_MESSAGE_PREFIX; 63 64 private String beforeMessageSuffix = DEFAULT_BEFORE_MESSAGE_SUFFIX; 65 66 private String afterMessagePrefix = DEFAULT_AFTER_MESSAGE_PREFIX; 67 68 private String afterMessageSuffix = DEFAULT_AFTER_MESSAGE_SUFFIX; 69 70 71 76 public void setIncludeQueryString(boolean includeQueryString) { 77 this.includeQueryString = includeQueryString; 78 } 79 80 83 protected boolean isIncludeQueryString() { 84 return includeQueryString; 85 } 86 87 91 public void setBeforeMessagePrefix(String beforeMessagePrefix) { 92 this.beforeMessagePrefix = beforeMessagePrefix; 93 } 94 95 99 public void setBeforeMessageSuffix(String beforeMessageSuffix) { 100 this.beforeMessageSuffix = beforeMessageSuffix; 101 } 102 103 107 public void setAfterMessagePrefix(String afterMessagePrefix) { 108 this.afterMessagePrefix = afterMessagePrefix; 109 } 110 111 115 public void setAfterMessageSuffix(String afterMessageSuffix) { 116 this.afterMessageSuffix = afterMessageSuffix; 117 } 118 119 120 127 protected void doFilterInternal( 128 HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 129 throws ServletException , IOException { 130 131 beforeRequest(request, getBeforeMessage(request)); 132 try { 133 filterChain.doFilter(request, response); 134 } 135 finally { 136 afterRequest(request, getAfterMessage(request)); 137 } 138 } 139 140 144 private String getBeforeMessage(HttpServletRequest request) { 145 return createMessage(request, this.beforeMessagePrefix, this.beforeMessageSuffix); 146 } 147 148 152 private String getAfterMessage(HttpServletRequest request) { 153 return createMessage(request, this.afterMessagePrefix, this.afterMessageSuffix); 154 } 155 156 165 protected String createMessage(HttpServletRequest request, String prefix, String suffix) { 166 StringBuffer buffer = new StringBuffer (); 167 buffer.append(prefix); 168 buffer.append(request.getRequestURI()); 169 if (isIncludeQueryString()) { 170 buffer.append('?'); 171 buffer.append(request.getQueryString()); 172 } 173 buffer.append(suffix); 174 return buffer.toString(); 175 } 176 177 178 184 protected abstract void beforeRequest(HttpServletRequest request, String message); 185 186 192 protected abstract void afterRequest(HttpServletRequest request, String message); 193 194 } 195 | Popular Tags |