1 13 package info.magnolia.cms.util; 14 15 import info.magnolia.cms.beans.runtime.Document; 16 import info.magnolia.cms.beans.runtime.MultipartForm; 17 18 import java.io.UnsupportedEncodingException ; 19 import java.net.URLDecoder ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.servlet.http.HttpServletRequest ; 24 25 import org.apache.commons.lang.StringUtils; 26 27 28 33 public class RequestFormUtil { 34 35 private MultipartForm form; 36 37 private HttpServletRequest request; 38 39 public RequestFormUtil(HttpServletRequest request) { 40 this(request, Resource.getPostedForm(request)); 41 } 42 43 public RequestFormUtil(HttpServletRequest request, MultipartForm form) { 44 super(); 45 this.form = form; 46 this.request = request; 47 } 48 49 53 54 public String getParameter(String name) { 55 return RequestFormUtil.getParameter(this.request, this.form, name); 56 } 57 58 public static String getParameter(HttpServletRequest request, String name) { 59 return RequestFormUtil.getParameter(request, Resource.getPostedForm(request), name); 60 } 61 62 69 public static String getParameter(HttpServletRequest request, MultipartForm from, String name) { 70 String param = null; 71 if (from != null) { 72 param = from.getParameter(name); 73 } 74 if (param == null) { 75 if (request.getMethod().equals("GET")) { 76 param = getURLParameterDecoded(request, name, "UTF8"); 77 } 78 else { 79 param = request.getParameter(name); 80 } 81 } 82 return param; 83 84 } 85 86 public String getParameter(String name, String defaultValue) { 87 return RequestFormUtil.getParameter(this.request, this.form, name, defaultValue); 88 } 89 90 public static String getParameter(HttpServletRequest request, String name, String defaultValue) { 91 return RequestFormUtil.getParameter(request, Resource.getPostedForm(request), name, defaultValue); 92 } 93 94 100 public static String getParameter(HttpServletRequest request, MultipartForm from, String name, String defaultValue) { 101 String param = getParameter(request, from, name); 102 if (param == null) { 103 param = defaultValue; 104 } 105 return param; 106 } 107 108 113 public static String getURLParameterDecoded(HttpServletRequest request, String name, String charset) { 114 return (String ) getURLParametersDecoded(request, charset).get(name); 115 } 116 117 123 public static Map getURLParametersDecoded(HttpServletRequest request, String charset) { 124 Map map = new HashMap (); 125 String queryString = request.getQueryString(); 126 if (queryString != null) { 127 String [] params = request.getQueryString().split("&"); 128 for (int i = 0; i < params.length; i++) { 129 String name = StringUtils.substringBefore(params[i], "="); 130 String value = StringUtils.substringAfter(params[i], "="); 131 try { 132 value = URLDecoder.decode(value, charset); 133 } 134 catch (UnsupportedEncodingException e) { 135 } 137 map.put(name, value); 138 } 139 } 140 return map; 141 } 142 143 public MultipartForm getForm() { 144 return form; 145 } 146 147 151 public Document getDocument(String name) { 152 return form.getDocument(name); 153 } 154 155 159 public Map getDocuments() { 160 if (form == null) { 161 return new HashMap (); 162 } 163 164 return form.getDocuments(); 165 } 166 167 171 public Map getParameters() { 172 return getParameters(this.request); 173 } 174 175 public static Map getParameters(HttpServletRequest request) { 176 MultipartForm form = Resource.getPostedForm(request); 177 if (form == null) { 178 if (request.getMethod() == "GET") { 180 return getURLParametersDecoded(request, "UTF8"); 181 } 182 return request.getParameterMap(); 183 } 184 return form.getParameters(); 185 } 186 187 191 public String [] getParameterValues(String name) { 192 if (this.form != null) { 193 return this.form.getParameterValues(name); 194 } 195 return request.getParameterValues(name); 196 } 197 198 202 public void removeParameter(String name) { 203 form.removeParameter(name); 204 } 205 206 } | Popular Tags |