1 12 package org.displaytag.util; 13 14 import java.io.UnsupportedEncodingException ; 15 import java.net.URLEncoder ; 16 import java.util.Enumeration ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 import org.apache.commons.lang.ObjectUtils; 24 import org.apache.commons.lang.StringUtils; 25 import org.apache.commons.lang.UnhandledException; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.displaytag.Messages; 29 30 31 38 public class DefaultRequestHelper implements RequestHelper 39 { 40 41 44 private static Log log = LogFactory.getLog(DefaultRequestHelper.class); 45 46 49 private HttpServletRequest request; 50 51 54 private HttpServletResponse response; 55 56 61 public DefaultRequestHelper(HttpServletRequest servletRequest, HttpServletResponse servletResponse) 62 { 63 this.request = servletRequest; 64 this.response = servletResponse; 65 } 66 67 70 public Href getHref() 71 { 72 String requestURI = this.request.getRequestURI(); 73 Href href = new DefaultHref(this.response.encodeURL(requestURI)); 75 href.setParameterMap(getParameterMap()); 76 return href; 77 } 78 79 82 public String getParameter(String key) 83 { 84 return this.request.getParameter(key); 86 } 87 88 91 public Integer getIntParameter(String key) 92 { 93 String value = this.request.getParameter(key); 94 95 if (value != null) 96 { 97 try 98 { 99 return new Integer (value); 100 } 101 catch (NumberFormatException e) 102 { 103 log.debug(Messages.getString("RequestHelper.invalidparameter", new Object []{key, value})); 106 } 107 } 108 109 return null; 110 } 111 112 115 public Map getParameterMap() 116 { 117 118 Map map = new HashMap (); 119 120 Enumeration parametersName = this.request.getParameterNames(); 122 123 while (parametersName.hasMoreElements()) 124 { 125 String paramName = (String ) parametersName.nextElement(); 127 128 String [] originalValues = (String []) ObjectUtils.defaultIfNull( 130 this.request.getParameterValues(paramName), 131 new String [0]); 132 String [] values = new String [originalValues.length]; 133 134 for (int i = 0; i < values.length; i++) 135 { 136 try 137 { 138 values[i] = URLEncoder.encode(StringUtils.defaultString(originalValues[i]), StringUtils 139 .defaultString(response.getCharacterEncoding(), "UTF8")); } 141 catch (UnsupportedEncodingException e) 142 { 143 throw new UnhandledException(e); 144 } 145 } 146 map.put(paramName, values); 147 148 } 149 150 return map; 152 } 153 154 } | Popular Tags |