1 5 6 13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers; 14 15 import java.io.BufferedReader ; 16 import java.io.IOException ; 17 import java.io.UnsupportedEncodingException ; 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.Vector ; 24 import org.apache.commons.lang.StringUtils ; 25 import org.exoplatform.Constants; 26 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.utils.CustomRequestWrapperUtil; 27 28 import javax.servlet.ServletInputStream ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletRequestWrapper ; 31 import javax.servlet.http.HttpSession ; 32 33 34 35 43 public class CustomRequestWrapper extends HttpServletRequestWrapper { 44 45 private String pathInfo; 46 private String servletPath; 47 private String query; 48 private String windowId; 49 private boolean redirected; 50 private HttpSession sharedSession; 51 private String contextPath; 52 private Map parameterMap ; 53 54 public CustomRequestWrapper(HttpServletRequest httpServletRequest) { 55 super(httpServletRequest); 56 } 57 58 public void fillCustomRequestWrapper(HttpServletRequest httpServletRequest, 59 String windowId) { 60 super.setRequest(httpServletRequest); 61 this.windowId = windowId; 62 this.parameterMap = httpServletRequest.getParameterMap() ; 63 } 64 65 public void emptyCustomRequestWrapper() { 66 this.windowId = null; 67 } 68 69 public Enumeration getAttributeNames() { 70 Enumeration e = super.getAttributeNames(); 71 Vector v = new Vector (); 72 while (e.hasMoreElements()) { 73 String s = (String ) e.nextElement(); 74 s = CustomRequestWrapperUtil.decodeRequestAttribute(windowId, s); 75 v.add(s); 76 } 77 return v.elements(); 78 } 79 80 public Object getAttribute(String s) { 81 return super.getAttribute(CustomRequestWrapperUtil.encodeAttribute(windowId, s)); 82 } 83 84 public void removeAttribute(String s) { 85 super.removeAttribute(CustomRequestWrapperUtil.encodeAttribute(windowId, s)); 86 } 87 88 public void setAttribute(String s, Object o) { 89 super.setAttribute(CustomRequestWrapperUtil.encodeAttribute(windowId, s), o); 90 } 91 92 public Map getParameterMap() { 93 Map superMap = super.getParameterMap(); 94 if(redirected){ 95 Map filteredMap = new HashMap (); 96 Set keys = superMap.keySet(); 97 for (Iterator iter = keys.iterator(); iter.hasNext();) { 98 String element = (String ) iter.next(); 99 if(!element.startsWith(Constants.PARAMETER_ENCODER)) 100 filteredMap.put(element, superMap.get(element)); 101 } 102 return filteredMap; 103 } 104 return superMap; 105 } 106 107 public void setParameterMap(Map map) { 108 this.parameterMap = map ; 109 } 110 111 public boolean isRedirected() { 112 return redirected; 113 } 114 115 public void setRedirected(boolean b) { 116 redirected = b; 117 } 118 119 public int getContentLength() { 120 if(redirected) 121 return 0; 122 return super.getContentLength(); 123 } 124 125 public StringBuffer getRequestURL() { 126 if(redirected) 127 return null; 128 return super.getRequestURL(); 129 } 130 131 public String getCharacterEncoding() { 132 return super.getCharacterEncoding(); 135 } 136 137 public String getContentType() { 138 if(redirected) 139 return null; 140 return super.getContentType(); 141 } 142 143 public ServletInputStream getInputStream() throws IOException { 144 if(redirected) 145 return null; 146 return super.getInputStream(); 147 } 148 149 public BufferedReader getReader() throws IOException { 150 if(redirected) 151 return null; 152 return super.getReader(); 153 } 154 155 public String getRealPath(String arg0) { 156 if(redirected) 157 return null; 158 return super.getRealPath(arg0); 159 } 160 161 public String getRemoteAddr() { 162 if(redirected) 163 return null; 164 return super.getRemoteAddr(); 165 } 166 167 public String getRemoteHost() { 168 if(redirected) 169 return null; 170 return super.getRemoteHost(); 171 } 172 173 public void setCharacterEncoding(String arg0) 174 throws UnsupportedEncodingException { 175 if(redirected) 176 return; 177 super.setCharacterEncoding(arg0); 178 } 179 180 public String getProtocol() { 181 if(redirected) 182 return null; 183 return super.getProtocol(); 184 } 185 186 public HttpSession getSession(){ 187 return getSession(true); 188 } 189 190 public HttpSession getSession(boolean b){ 191 if( sharedSession != null) 192 return sharedSession; 193 else{ 194 return super.getSession(b); 195 } 196 } 197 198 public void setSharedSession(HttpSession session) { 199 sharedSession = session; 200 } 201 202 public boolean isRequestedSessionIdValid() { 203 if( sharedSession != null){ 204 return true; 205 } 206 return super.isRequestedSessionIdValid(); 207 } 208 209 public void setContextPath(String string) { 210 contextPath = string; 211 } 212 213 public String getContextPath() { 214 if(redirected && contextPath != null){ 215 return contextPath; 216 } 217 return super.getContextPath(); 218 } 219 220 public void setRedirectedPath(String path) { 221 if(path == null || path.length() == 0) 222 path = "/"; 223 224 String [] key = StringUtils.split(path, "?") ; 225 String firstPart = ""; 226 if(key.length > 1){ 227 query = key[1]; 228 firstPart = key[0]; 229 } else { 230 firstPart = path; 231 } 232 233 if(firstPart.indexOf("/", 1)>0){ 234 servletPath = firstPart.substring(0, firstPart.indexOf("/", 1)); 235 pathInfo = firstPart.substring(firstPart.indexOf("/", 1)); 236 } else { 237 servletPath = firstPart; 238 pathInfo = null; 239 } 240 } 241 242 public String getPathInfo() { 243 if(redirected){ 244 return pathInfo; 245 } 246 return super.getPathInfo(); 247 } 248 249 public String getServletPath() { 250 if(redirected){ 251 return servletPath; 252 } 253 return super.getServletPath(); 254 } 255 256 public String getQueryString() { 257 if(redirected){ 258 return query; 259 } 260 return super.getQueryString(); 261 } 262 263 public String getRequestURI() { 264 if(redirected){ 265 return getContextPath() + 266 ((servletPath == null) ? "" : servletPath) + 267 ((pathInfo == null) ? "" : pathInfo); 268 } 269 return super.getRequestURI(); 270 } 271 272 } 273 | Popular Tags |