1 25 package org.snipsnap.net.filter; 26 27 import org.radeox.util.logging.Logger; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletRequestWrapper ; 31 import java.io.UnsupportedEncodingException ; 32 import java.util.Collections ; 33 import java.util.Enumeration ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 38 public class EncRequestWrapper extends HttpServletRequestWrapper { 39 String encoding = "UTF-8"; 40 Map paramMap = null; 41 42 public EncRequestWrapper(HttpServletRequest request) throws UnsupportedEncodingException { 43 this(request, request.getCharacterEncoding()); 44 } 45 46 public EncRequestWrapper(HttpServletRequest request, String enc) throws UnsupportedEncodingException { 47 super(request); 48 "".getBytes(enc); 49 encoding = enc; 50 51 Map params = request.getParameterMap(); 52 Iterator iterator = params.keySet().iterator(); 53 Map encodedParams = new HashMap (); 54 while (iterator.hasNext()) { 55 String key = (String ) iterator.next(); 56 String [] values = (String []) params.get(key); 57 for(int n = 0; n < values.length; n++) { 58 values[n] = getEncodedString(values[n]); 59 } 60 encodedParams.put(key, values); 61 } 62 paramMap = Collections.unmodifiableMap(encodedParams); 63 } 64 65 private String getEncodedString(String src) { 66 if (src != null) { 67 try { 68 return new String (src.getBytes("iso-8859-1"), encoding); 69 } catch (UnsupportedEncodingException e) { 70 System.err.println(e); 71 Logger.warn("Error: illegal encoding: " + e); 72 } 73 } 74 return src; 75 } 76 77 public String getHeader(String name) { 78 return getEncodedString(super.getHeader(name)); 79 } 80 81 public String getQueryString() { 82 return getEncodedString(super.getQueryString()); 83 } 84 85 public String getRequestURI() { 86 return getEncodedString(super.getRequestURI()); 87 } 88 89 public String getPathInfo() { 90 return getEncodedString(super.getPathInfo()); 91 } 92 93 public String getParameter(String name) { 94 String [] values = (String [])paramMap.get(name); 95 if(null != values && values.length > 0) { 96 return ((String [])paramMap.get(name))[0]; 97 } 98 return null; 99 } 100 101 public Map getParameterMap() { 102 return new HashMap (paramMap); 103 } 104 105 public Enumeration getParameterNames() { 106 final Iterator nameIt = paramMap.keySet().iterator(); 107 return new Enumeration () { 108 public boolean hasMoreElements() { 109 return nameIt.hasNext(); 110 } 111 112 public Object nextElement() { 113 return nameIt.next(); 114 } 115 }; 116 } 117 118 public String [] getParameterValues(String name) { 119 return (String [])paramMap.get(name); 120 } 121 } 122 | Popular Tags |