1 12 13 59 package com.openedit.util; 60 61 import java.io.UnsupportedEncodingException ; 62 import java.net.URLDecoder ; 63 import java.net.URLEncoder ; 64 65 import javax.servlet.http.HttpServletRequest ; 66 import javax.servlet.http.HttpServletResponse ; 67 68 import org.apache.commons.logging.Log; 69 import org.apache.commons.logging.LogFactory; 70 71 72 77 public class URLUtilities 78 { 79 80 public static final String URL_PATH_SEPARATOR = "/"; 81 private static Log log = LogFactory.getLog(URLUtilities.class); 82 83 87 private HttpServletRequest fieldRequest; 88 private HttpServletResponse fieldResponse; 89 90 public URLUtilities( 91 HttpServletRequest request, HttpServletResponse response) 92 { 93 this.fieldRequest = request; 94 this.fieldResponse = response; 95 } 96 97 103 public static String getPathWithoutContext(String inContext, String fullpath, String inDefault) 104 { 105 String nameOnly = fullpath; 106 if (fullpath.startsWith(inContext)){ 107 nameOnly = fullpath.substring(inContext.length()); 108 } 109 110 if (nameOnly.equals("/") || (nameOnly.length() == 0)) 111 { 112 nameOnly += inDefault; 113 } 114 else if (nameOnly.indexOf('.') == -1) 115 { 116 if ( !nameOnly.endsWith("/")) 117 { 118 nameOnly += '/'; 119 } 120 nameOnly += inDefault; 121 } 122 123 return nameOnly; 124 } 125 126 131 public String buildBasePath(String path) 132 { 133 StringBuffer ctx = fieldRequest.getRequestURL(); 134 String servername = ctx.substring(0, ctx.indexOf("/", 7)); 136 if (path.lastIndexOf('/') > -1) 137 { 138 path = path.substring(0, path.lastIndexOf('/')); 139 140 return servername + path + "/"; 141 } 142 else 143 { 144 return servername + "/"; 145 } 146 } 147 148 153 public String buildRoot() 154 { 155 StringBuffer ctx = fieldRequest.getRequestURL(); 156 String servername = ctx.substring(0, ctx.indexOf("/", 7)); 157 158 return servername + "/"; 159 } 160 161 166 public String buildSecure(String path) 167 { 168 return buildSecure(path, 0); 169 } 170 171 181 public String buildSecure(String path, int port) 182 { 183 return build(path, "https", port); 184 } 185 186 193 public String buildStandard(String path) 194 { 195 return buildStandard(path, 0); 196 } 197 198 208 public String buildStandard(String path, int port) 209 { 210 return build(path, "http", port); 211 } 212 213 222 public String encode(String s) 223 { 224 if (s == null) 225 { 226 return null; 227 } 228 229 return URLEncoder.encode(s); 230 } 231 232 239 public String getOriginalUrl() 240 { 241 String path = fieldRequest.getRequestURI(); 242 String home = relativeHomePrefix(); 243 path = path.substring(home.length()); 244 return encode( path ); 245 } 246 251 public String getOriginalPath() 252 { 253 String requestedPath = getRequest().getRequestURI(); 254 try 255 { 256 requestedPath = URLDecoder.decode(requestedPath, "UTF-8"); 257 } 258 catch (UnsupportedEncodingException ex) 259 { 260 log.error( ex ); 261 } 262 263 String contextPath = getRequest().getContextPath(); 264 if ( requestedPath.startsWith( contextPath ) ) 265 { 266 requestedPath = requestedPath.substring(contextPath.length()); 267 } 268 return requestedPath; 269 } 270 271 272 278 public String requestPath() 279 { 280 StringBuffer ctx = fieldRequest.getRequestURL(); 281 String requestPath = ctx.substring(ctx.indexOf("/", 7)); 283 return requestPath; 284 } 285 289 290 public String requestPathWithArguments() 291 { 292 String path = fieldRequest.getRequestURI(); 293 if ( fieldRequest.getQueryString() != null && fieldRequest.getQueryString().length() > 0) 294 { 295 path = path + "?" + fieldRequest.getQueryString(); 296 } 297 return path; 298 } 299 303 304 public String requestPathWithArgumentsNoContext() 305 { 306 String path = fieldRequest.getRequestURI(); 307 if ( fieldRequest.getQueryString() != null && fieldRequest.getQueryString().length() > 0) 308 { 309 path = path + "?" + fieldRequest.getQueryString(); 310 } 311 String home = relativeHomePrefix(); 312 path = path.substring(home.length()); 313 return path; 314 } 315 320 public String siteRoot() 321 { 322 StringBuffer ctx = fieldRequest.getRequestURL(); 323 String siteRoot = ctx.substring( 0, ctx.indexOf("/", 8) ); return siteRoot; 325 } 326 327 334 public static String xmlEscape(String inStr) 335 { 336 if ( inStr == null ) 337 { 338 return null; 339 } 340 inStr = inStr.replaceAll("&", "&"); 342 343 inStr = inStr.replaceAll("<", "<"); 344 inStr = inStr.replaceAll(">", ">"); 345 inStr = inStr.replaceAll("\"", """); 346 347 return inStr; 349 } 350 351 public static String xmlUnescape(String inStr) 352 { 353 if ( inStr == null ) 354 { 355 return null; 356 } 357 inStr = inStr.replaceAll("&","&"); 359 360 inStr = inStr.replaceAll("<", "<"); 361 inStr = inStr.replaceAll(">", ">"); 362 inStr = inStr.replaceAll(""", "\""); 363 364 return inStr; 366 } 367 368 377 public String relativeHomePrefix() 378 { 379 String rootdir = fieldRequest.getContextPath(); 380 381 if ((rootdir != null) && (rootdir.length() > 0)) 382 { 383 if ( rootdir.endsWith("/")) 384 { 385 rootdir = rootdir.substring(0,rootdir.length() - 1); 386 } 387 return rootdir; 388 } 389 else 390 { 391 return ""; 392 } 393 394 } 395 396 406 protected String build(String path, String protocol, int port) 407 { 408 String serverName = fieldRequest.getServerName(); 409 String contextPath = fieldRequest.getContextPath(); 410 411 log.debug("Server name: " + serverName); 412 log.debug("Context path: " + contextPath); 413 414 if (!contextPath.endsWith(URL_PATH_SEPARATOR)) 415 { 416 contextPath = contextPath + URL_PATH_SEPARATOR; 417 } 418 419 if (path.startsWith(URL_PATH_SEPARATOR)) 420 { 421 path = path.substring(1); 422 } 423 424 String requestPath = contextPath + path; 425 log.debug("Request path: " + requestPath); 426 427 StringBuffer buffer = new StringBuffer (); 428 buffer.append(protocol); 429 buffer.append("://"); 430 buffer.append(serverName); 431 432 int realPort = fieldRequest.getServerPort(); 433 434 if (port > 0) 435 { 436 realPort = port; 437 } 438 439 if ( 440 (realPort > 0) && 441 !((protocol.equals("http") && (realPort == 80)) || 442 (protocol.equals("https") && (realPort == 443)))) 443 { 444 buffer.append(":"); 445 buffer.append(realPort); 446 } 447 448 if (!requestPath.startsWith(URL_PATH_SEPARATOR)) 449 { 450 buffer.append(URL_PATH_SEPARATOR); 451 } 452 453 buffer.append(requestPath); 454 455 log.debug("URL: '" + buffer + "'"); 456 457 return buffer.toString(); 458 } 459 public PathUtilities getPathUtilities() 460 { 461 return new PathUtilities(); 462 } 463 464 public HttpServletResponse getResponse() 465 { 466 return fieldResponse; 467 } 468 469 public void setResponse(HttpServletResponse inResponse) 470 { 471 fieldResponse = inResponse; 472 } 473 474 public HttpServletRequest getRequest() 475 { 476 return fieldRequest; 477 } 478 479 public void setRequest(HttpServletRequest inRequest) 480 { 481 fieldRequest = inRequest; 482 } 483 } 484 | Popular Tags |