1 2 15 package org.wings.session; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 20 import javax.servlet.*; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletRequestWrapper ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.http.HttpServletResponseWrapper ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.nio.CharBuffer ; 28 import java.util.Collections ; 29 import java.util.Enumeration ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 import java.util.regex.Matcher ; 33 import java.util.regex.Pattern ; 34 35 38 public class SmartURLsFilter 39 implements Filter { 40 private final transient static Log log = LogFactory.getLog(SmartURLsFilter.class); 41 private String parameterSeparator = ";"; 42 private String nameValueSeparator = ","; 43 private Pattern encodePattern; 44 private Pattern decodePattern; 45 46 public void init(FilterConfig filterConfig) throws ServletException { 47 if (filterConfig.getInitParameter("wings.servlet.smarturls.parameterSeparator") != null) 48 parameterSeparator = filterConfig.getInitParameter("wings.servlet.smarturls.parameterSeparator"); 49 50 if (filterConfig.getInitParameter("wings.servlet.smarturls.nameValueSeparator") != null) 51 nameValueSeparator = filterConfig.getInitParameter("wings.servlet.smarturls.nameValueSeparator"); 52 53 log.info("wings.servlet.smarturls.parameterSeparator " + parameterSeparator); 54 log.info("wings.servlet.smarturls.nameValueSeparator " + nameValueSeparator); 55 56 encodePattern = Pattern.compile("(" + "\\?|&" + ")([a-zA-Z0-9%+.-[*]_]*)" + 57 "(" + "=" + ")([a-zA-Z0-9%+.-[*]_=/:]*)"); 58 decodePattern = Pattern.compile("(" + parameterSeparator + ")([a-zA-Z0-9%+.-[*]_]*)" + 59 "(" + nameValueSeparator + ")([a-zA-Z0-9%+.-[*]_=/:]*)"); 60 } 61 62 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) 63 throws IOException , ServletException { 64 ServletRequest request = servletRequest; 65 ServletResponse response = servletResponse; 66 67 if (servletRequest instanceof HttpServletRequest ) { 68 HttpServletRequest httpServletRequest = (HttpServletRequest ) servletRequest; 69 70 MyHttpServletRequestWrapper requestWrapper = new MyHttpServletRequestWrapper(httpServletRequest); 71 if (requestWrapper.getPathInfo() == null || requestWrapper.getPathInfo().indexOf('.') == -1) { 72 response = new MyHttpServletResponseWrapper(servletResponse); 73 74 if ("get".equalsIgnoreCase(httpServletRequest.getMethod())) 75 request = requestWrapper; 76 77 log.debug("wrap " + requestWrapper.getPathInfo()); 78 } else 79 log.debug("don't wrap " + requestWrapper.getPathInfo()); 80 } 81 filterChain.doFilter(request, response); 82 } 83 84 public void destroy() { 85 } 86 87 public static final String replace(String s, 88 String toFind, String replace) { 89 StringBuffer erg = new StringBuffer (); 90 91 int lastindex = 0; 92 int indexOf = s.indexOf(toFind); 93 if (indexOf == -1) return s; 94 while (indexOf != -1) { 95 erg.append(s.substring(lastindex, indexOf)).append(replace); 96 lastindex = indexOf + toFind.length(); 97 indexOf = s.indexOf(toFind, lastindex); 98 } 99 100 erg.append(s.substring(lastindex)); 101 102 return erg.toString(); 103 } 104 105 private class MyHttpServletRequestWrapper extends HttpServletRequestWrapper { 106 private Map parameterMap; 107 private String pathInfo; 108 private String queryString; 109 110 public MyHttpServletRequestWrapper(HttpServletRequest httpServletRequest) { 111 super(httpServletRequest); 112 113 pathInfo = httpServletRequest.getPathInfo(); 114 queryString = httpServletRequest.getQueryString(); 115 parameterMap = httpServletRequest.getParameterMap(); 116 log.debug("pathInfo = " + pathInfo); 117 log.debug("queryString = " + queryString); 118 log.debug("parameterMap = " + parameterMap); 119 120 if (pathInfo == null) 121 return; 122 123 int pos = pathInfo.indexOf(parameterSeparator); 124 if (pos != -1) { 125 queryString = pathInfo.substring(pos); 126 pathInfo = pathInfo.substring(0, pos); 127 128 parameterMap = new HashMap (); 129 StringBuffer buffer = new StringBuffer (queryString.length()); 130 Matcher matcher = decodePattern.matcher(queryString); 131 while (matcher.find()) { 132 String param = matcher.group(); 133 pos = param.indexOf(nameValueSeparator); 134 parameterMap.put(param.substring(1, pos), param.substring(pos + 1)); 135 matcher.appendReplacement(buffer, "&$2=$4"); 136 } 137 queryString = buffer.substring(1); 138 log.debug("modified pathInfo = " + pathInfo); 139 log.debug("modified queryString = " + queryString); 140 log.debug("modified parameterMap = " + parameterMap); 141 } 142 } 143 144 public String getRequestURI() { 145 return super.getRequestURI(); 146 } 147 148 public Map getParameterMap() { 149 return parameterMap; 150 } 151 152 public String getPathInfo() { 153 return pathInfo; 154 } 155 156 public String getQueryString() { 157 return queryString; 158 } 159 160 public String getParameter(String string) { 161 Object value = getParameterMap().get(string); 162 if (value instanceof String ) 163 return (String ) value; 164 else 165 return ((String []) value)[0]; 166 } 167 168 public Enumeration getParameterNames() { 169 return Collections.enumeration(getParameterMap().keySet()); 170 } 171 172 public String [] getParameterValues(String string) { 173 Object value = getParameterMap().get(string); 174 if (value instanceof String ) 175 return new String []{(String ) value}; 176 else 177 return (String []) value; 178 } 179 } 180 181 private static class MyHttpServletResponseWrapper extends HttpServletResponseWrapper { 182 public MyHttpServletResponseWrapper(ServletResponse servletResponse) { 183 super((HttpServletResponse ) servletResponse); 184 } 185 186 public ServletOutputStream getOutputStream() throws IOException { 187 final ServletOutputStream superOut = super.getOutputStream(); 188 return new ServletOutputStream() { 189 ByteArrayOutputStream bytes = new ByteArrayOutputStream (1000); 190 191 public void write(int b) throws IOException { 192 bytes.write(b); 193 } 194 195 public void close() throws IOException { 196 encode(bytes, superOut); 197 superOut.close(); 198 } 199 }; 200 } 201 } 202 203 204 private static void encode(ByteArrayOutputStream bytes, ServletOutputStream out) throws IOException { 205 String regex = "(href|src|action) *= *\"([^\"]*)\""; 206 207 SmartURLsFilter smartURLsFilter = new SmartURLsFilter(); 208 String replacement = 209 smartURLsFilter.parameterSeparator + "$2" + 210 smartURLsFilter.nameValueSeparator + "$4"; 211 212 smartURLsFilter.encodePattern = Pattern.compile("(" + "\\?|&" + ")([a-zA-Z0-9%+.-[*]_]*)" + 213 "(" + "=" + ")([a-zA-Z0-9%+.-[*]_=/]*)"); 214 215 CharBuffer chars = CharBuffer.wrap(bytes.toString()); 216 Pattern pattern = Pattern.compile(regex); 217 Matcher matcher = pattern.matcher(chars); 218 int pos = 0; 219 while (matcher.find()) { 220 out.print(chars.subSequence(pos, matcher.start()).toString()); 221 pos = matcher.end(); 222 Matcher matcher2 = smartURLsFilter.encodePattern.matcher(matcher.group(2)); 223 String group2 = matcher2.replaceAll(replacement); 224 225 out.print(matcher.group(1) + "=\"" + group2 + "\""); 226 } 227 out.print(chars.subSequence(pos, bytes.size()).toString()); 228 } 229 } 230 | Popular Tags |