1 7 package com.inversoft.verge.util.url; 8 9 10 import java.io.UnsupportedEncodingException ; 11 import java.net.URI ; 12 import java.net.URISyntaxException ; 13 import java.net.URLEncoder ; 14 15 import javax.servlet.ServletRequest ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 import com.inversoft.util.StringTools; 20 import com.inversoft.verge.util.url.config.CategoryConfig; 21 import com.inversoft.verge.util.url.config.URLConfigRegistry; 22 23 24 64 public class URLGenerator { 65 66 69 private URLGenerator() { 70 } 71 72 73 81 public static boolean isCategoryValid(ServletRequest request, String category) { 82 return (URLConfigRegistry.getInstance(request).lookupCategory(category) != null); 83 } 84 85 94 public static CategoryConfig getCategory(ServletRequest request, String category) { 95 return URLConfigRegistry.getInstance(request).lookupCategory(category); 96 } 97 98 110 public static CategoryConfig getCategory(ServletRequest request, String category, 111 String defaultCategory) { 112 CategoryConfig cc = 113 URLConfigRegistry.getInstance(request).lookupCategory(category); 114 if (cc == null) { 115 cc = URLConfigRegistry.getInstance(request).lookupCategory(defaultCategory); 116 } 117 118 return cc; 119 } 120 121 122 126 127 131 public static String generateLocalURL(String baseURI, String context) { 132 StringBuffer buf = new StringBuffer (baseURI.length()); 133 generateLocalURL(buf, baseURI, context); 134 return buf.toString(); 135 } 136 137 148 public static void generateLocalURL(StringBuffer buf, String baseURI, 149 String context) { 150 boolean contextEmpty = StringTools.isTrimmedEmpty(context); 154 boolean empty = StringTools.isTrimmedEmpty(baseURI); 155 if (!contextEmpty) { 156 boolean relative = (!empty && (baseURI.charAt(0) != '/')); 157 if (!relative) { 158 buf.append(context); 159 } 160 } 161 162 if (!empty) { 163 buf.append(baseURI); 164 } 165 } 166 167 168 172 173 177 public static String generateURL(String baseURI, String context) 178 throws URISyntaxException { 179 StringBuffer buf = new StringBuffer (baseURI.length()); 180 generateURL(buf, baseURI, context); 181 return buf.toString(); 182 } 183 184 195 public static void generateURL(StringBuffer buf, String baseURI, String context) 196 throws URISyntaxException { 197 198 boolean contextEmpty = StringTools.isTrimmedEmpty(context); 199 200 if (baseURI == null) { 203 if (!contextEmpty) { 204 buf.append(context); 205 } 206 207 return; 208 } 209 210 URI uri = null; 212 int queryStart = baseURI.indexOf("?"); 213 if (queryStart != -1) { 214 try { 215 baseURI = baseURI.substring(0, queryStart + 1) + 216 URLEncoder.encode(baseURI.substring(queryStart + 1), 217 "UTF-8"); 218 } catch (UnsupportedEncodingException uee) { 219 throw new AssertionError (uee); 221 } 222 223 } 224 uri = new URI (baseURI); 225 226 boolean absolute = uri.isAbsolute(); 227 if (absolute) { 228 String scheme = uri.getScheme(); 229 if (scheme != null) { 230 buf.append(scheme); 231 buf.append("://"); 232 } 233 234 String host = uri.getHost(); 235 if (host != null) { 236 buf.append(host); 237 } 238 239 int port = uri.getPort(); 240 if (port != -1) { 241 buf.append(":"); 242 buf.append(port); 243 } 244 } 245 246 String path = uri.getPath(); 247 generateLocalURL(buf, path, context); 248 249 String query = uri.getQuery(); 250 if (query != null) { 251 buf.append("?"); 252 buf.append(query); 253 } 254 255 String fragment = uri.getFragment(); 256 if (fragment != null) { 257 buf.append("#"); 258 buf.append(fragment); 259 } 260 } 261 262 263 267 268 273 public static String generateURL(CategoryConfig category, 274 HttpServletRequest request) { 275 StringBuffer buf = new StringBuffer (); 276 generateURL(buf, category, request); 277 return buf.toString(); 278 } 279 280 285 public static String generateURL(CategoryConfig category, String baseURI, 286 HttpServletRequest request) 287 throws URISyntaxException { 288 StringBuffer buf = new StringBuffer (); 289 generateURL(buf, category, baseURI, request); 290 return buf.toString(); 291 } 292 293 298 public static String generateURL(CategoryConfig category, String baseURI, 299 boolean context, HttpServletRequest request) 300 throws URISyntaxException { 301 StringBuffer buf = new StringBuffer (); 302 generateURL(buf, category, baseURI, context, request); 303 return buf.toString(); 304 } 305 306 311 public static String generateURL(CategoryConfig category, String baseURI, 312 boolean context, HttpServletRequest request, HttpServletResponse response) 313 throws URISyntaxException { 314 StringBuffer newBuf = new StringBuffer (); 315 generateURL(newBuf, category, baseURI, context, request, response); 316 return newBuf.toString(); 317 } 318 319 323 public static void generateURL(StringBuffer buf, CategoryConfig category, 324 HttpServletRequest request) { 325 try { 326 generateURL(buf, category, null, request); 327 } catch (URISyntaxException use) { 328 throw new AssertionError ("FATAL: threw URISyntaxException " + 329 use.toString()); 330 } 331 } 332 333 338 public static void generateURL(StringBuffer buf, CategoryConfig category, 339 String baseURI, HttpServletRequest request) 340 throws URISyntaxException { 341 generateURL(buf, category, baseURI, true, request); 342 } 343 344 350 public static void generateURL(StringBuffer buf, CategoryConfig category, 351 String baseURI, boolean context, HttpServletRequest request, 352 HttpServletResponse response) 353 throws URISyntaxException { 354 StringBuffer newBuf = new StringBuffer (); 355 generateURL(newBuf, category, baseURI, context, request); 356 buf.append(response.encodeURL(newBuf.toString())); 357 } 358 359 443 public static void generateURL(StringBuffer buf, CategoryConfig category, 444 String baseURI, boolean context, HttpServletRequest request) 445 throws URISyntaxException { 446 447 String localContext = null; 449 if (context) { 450 localContext = request.getContextPath(); 451 } 452 453 if (category != null) { 455 internalGenerate(buf, category, baseURI, localContext, request); 456 } else { 457 generateURL(buf, baseURI, localContext); 458 } 459 } 460 461 464 private static void internalGenerate(StringBuffer url, CategoryConfig cc, 465 String baseURI, String context, HttpServletRequest request) 466 throws URISyntaxException { 467 468 URI uri = null; 469 if (baseURI != null) { 470 int queryStart = baseURI.indexOf("?"); 471 if (queryStart != -1) { 472 try { 473 baseURI = baseURI.substring(0, queryStart + 1) + 474 URLEncoder.encode(baseURI.substring(queryStart + 1), 475 "UTF-8"); 476 } catch (UnsupportedEncodingException uee) { 477 throw new AssertionError (uee); 479 } 480 481 } 482 483 uri = new URI (baseURI); 484 } 485 486 String protocol = cc.getProtocol(); 488 boolean https = false; 489 if (protocol != null) { 490 https = cc.isHttps(); 491 } else { 492 protocol = (uri == null) ? null : uri.getScheme(); 493 if (protocol == null) { 494 protocol = request.getScheme(); 495 } 496 497 protocol = protocol.toLowerCase(); 498 https = protocol.equals("https"); 499 } 500 501 url.append(protocol); 502 url.append("://"); 503 504 String host = cc.getHost(); 506 if (host == null) { 507 host = (uri == null) ? null : uri.getHost(); 508 if (host == null) { 509 host = request.getServerName(); 510 } 511 } 512 url.append(host); 513 514 int port = cc.getPort(); 516 if (port == -1) { 517 518 port = (uri == null) ? -1 : uri.getPort(); 522 if (port == -1) { 523 String scheme = (uri == null) ? null : uri.getScheme(); 524 if (scheme != null && scheme.equals("http")) { 525 port = 80; 526 } else if (scheme != null && scheme.equals("https")) { 527 port = 443; 528 } 529 530 if (port == -1) { 531 port = request.getServerPort(); 532 } 533 } 534 } 535 536 if ((!https && port != 80) || 537 (https && port != 443)) { 538 url.append(":"); 539 url.append(port); 540 } 541 542 String localContext = cc.getContext(); 544 if (localContext == null) { 545 localContext = context; 546 } 547 548 if (localContext != null) { 549 if (!StringTools.isTrimmedEmpty(localContext) && 550 localContext.charAt(0) != '/') { 551 url.append("/"); 552 } 553 url.append(localContext); 554 } 555 556 String path = (uri == null) ? null : uri.getPath(); 558 if (path != null) { 559 if (!StringTools.isTrimmedEmpty(path) && path.charAt(0) != '/') { 560 url.append("/"); 561 } 562 url.append(path); 563 } 564 565 String query = (uri == null) ? null : uri.getQuery(); 567 if (query != null) { 568 url.append("?").append(query); 569 } 570 571 String fragment = (uri == null) ? null : uri.getFragment(); 572 if (fragment != null) { 573 url.append("#").append(fragment); 574 } 575 } 576 } | Popular Tags |