1 7 package com.inversoft.junit.internal.http; 8 9 10 import java.io.File ; 11 import javax.servlet.RequestDispatcher ; 12 import javax.servlet.http.HttpServletRequest ; 13 14 import com.inversoft.junit.URL; 15 16 17 43 public class HttpServletRequestWrapper 44 extends javax.servlet.http.HttpServletRequestWrapper { 45 46 49 URL url; 50 51 52 58 public HttpServletRequestWrapper(HttpServletRequest request) { 59 super(request); 60 } 61 62 69 public HttpServletRequestWrapper(HttpServletRequest request, URL url) { 70 super(request); 71 this.url = url; 72 } 73 74 75 79 85 public String getContextPath() { 86 if ((url != null) && (url.getContextPath() != null)) { 87 return url.getContextPath(); 88 } 89 90 return super.getContextPath(); 91 } 92 93 99 public String getPathInfo() { 100 if (url != null && url.getPathInfo() != null) { 101 return url.getPathInfo(); 102 } 103 104 return super.getPathInfo(); 105 } 106 107 113 public String getPathTranslated() { 114 String pathTranslated; 115 116 if (url != null && url.getPathInfo() != null) { 117 118 String pathInfo = url.getPathInfo(); 119 120 if (super.getRealPath("/") == null) { 123 pathTranslated = null; 124 } else { 125 126 String newPathInfo = 128 (pathInfo.startsWith("/") ? pathInfo.substring(1) : pathInfo); 129 130 if (super.getRealPath("/").endsWith("/")) { 131 pathTranslated = 132 super.getRealPath("/") 133 + newPathInfo.replace('/', File.separatorChar); 134 } else { 135 pathTranslated = 136 super.getRealPath("/") 137 + File.separatorChar 138 + newPathInfo.replace('/', File.separatorChar); 139 } 140 } 141 } else { 142 pathTranslated = super.getPathTranslated(); 143 } 144 145 return pathTranslated; 146 } 147 148 154 public String getProtocol() { 155 if (url != null && url.getProtocol() != null) { 156 return url.getProtocol(); 157 } 158 159 return super.getProtocol(); 160 } 161 162 168 public String getQueryString() { 169 if (url != null && url.getQueryString() != null) { 170 return url.getQueryString(); 171 } 172 173 return super.getQueryString(); 174 } 175 176 186 public RequestDispatcher getRequestDispatcher(String thePath) { 187 188 if (thePath == null) { 189 return null; 190 } 191 192 String fullPath; 193 194 if (thePath.startsWith("/")) { 198 fullPath = thePath; 199 } else { 200 String pI = getPathInfo(); 201 if (pI == null) { 202 fullPath = catPath(getServletPath(), thePath); 203 } else { 204 fullPath = catPath(getServletPath() + pI, thePath); 205 } 206 207 if (fullPath == null) { 208 return null; 209 } 210 } 211 212 return new RequestDispatcherWrapper( 213 super.getRequest().getRequestDispatcher(fullPath)); 214 } 215 216 222 public String getRequestURI() { 223 if (url != null) { 224 225 return super.getContextPath() 226 + ((super.getServletPath() == null) ? "" : super.getServletPath()) 227 + ((super.getPathInfo() == null) ? "" : super.getPathInfo()); 228 } 229 230 return super.getRequestURI(); 231 } 232 233 239 public String getServerName() { 240 if ((url != null) && (url.getServerName() != null)) { 241 return url.getServerName(); 242 } 243 244 return super.getServerName(); 245 } 246 247 253 public int getServerPort() { 254 if (url != null && url.getServerPort() != -1) { 255 return url.getServerPort(); 256 } 257 258 return super.getServerPort(); 259 } 260 261 267 public String getServletPath() { 268 if (url != null && url.getServletPath() != null) { 269 return url.getServletPath(); 270 } 271 272 return super.getServletPath(); 273 } 274 275 279 288 String catPath(String theLookupPath, String thePath) { 289 int index = theLookupPath.lastIndexOf("/"); 291 theLookupPath = theLookupPath.substring(0, index); 292 293 while (thePath.startsWith("../")) { 295 if (theLookupPath.length() > 0) { 296 index = theLookupPath.lastIndexOf("/"); 297 theLookupPath = theLookupPath.substring(0, index); 298 } else { 299 return null; 301 } 302 303 index = thePath.indexOf("../") + 3; 304 thePath = thePath.substring(index); 305 } 306 307 return theLookupPath + "/" + thePath; 308 } 309 } 310 | Popular Tags |