1 12 package org.eclipse.help.internal.webapp.data; 13 import java.io.IOException ; 14 import java.net.InetAddress ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.Enumeration ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Locale ; 22 import java.util.StringTokenizer ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 import javax.servlet.http.Cookie ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.eclipse.core.runtime.Platform; 31 import org.eclipse.help.internal.HelpPlugin; 32 import org.eclipse.help.internal.base.BaseHelpSystem; 33 import org.eclipse.help.internal.base.HelpBasePlugin; 34 import org.eclipse.help.internal.base.util.TString; 35 36 public class UrlUtil { 37 private static final String invalidXML[] = {"&", ">", "<", "\""}; private static final String escapedXML[] = { 40 "&", ">", "<", """}; 42 static final Pattern safariPattern = Pattern.compile( 44 "Safari/(\\d+)(?:\\.|\\s|$)", Pattern.CASE_INSENSITIVE); 46 private static String defaultLocale; 48 private static Collection locales; 51 52 private static final int INFOCENTER_DIRECTION_BY_LOCALE = 1; 53 private static final int INFOCENTER_DIRECTION_LTR = 2; 54 private static final int INFOCENTER_DIRECTION_RTL = 3; 55 private static int infocenterDirection = INFOCENTER_DIRECTION_BY_LOCALE; 56 57 60 public static String JavaScriptEncode(String str) { 61 if (str == null) return null; 62 char[] wordChars = new char[str.length()]; 63 str.getChars(0, str.length(), wordChars, 0); 64 StringBuffer jsEncoded = new StringBuffer (); 65 for (int j = 0; j < wordChars.length; j++) { 66 int unicode = wordChars[j]; 67 if (('A' <= unicode && unicode <= 'Z') 69 || ('a' <= unicode && unicode <= 'z')) { 70 jsEncoded.append(wordChars[j]); 71 continue; 72 } 73 String charInHex = Integer.toString(unicode, 16).toUpperCase(); 75 switch (charInHex.length()) { 76 case 1 : 77 jsEncoded.append("\\u000").append(charInHex); break; 79 case 2 : 80 jsEncoded.append("\\u00").append(charInHex); break; 82 case 3 : 83 jsEncoded.append("\\u0").append(charInHex); break; 85 default : 86 jsEncoded.append("\\u").append(charInHex); break; 88 } 89 } 90 return jsEncoded.toString(); 91 } 92 93 96 public static String htmlEncode(String str) { 97 98 for (int i = 0; i < invalidXML.length; i++) 99 str = TString.change(str, invalidXML[i], escapedXML[i]); 100 return str; 101 } 102 103 public static boolean isLocalRequest(HttpServletRequest request) { 104 String reqIP = request.getRemoteAddr(); 105 if ("127.0.0.1".equals(reqIP)) { return true; 107 } 108 109 try { 110 String hostname = InetAddress.getLocalHost().getHostName(); 111 InetAddress [] addr = InetAddress.getAllByName(hostname); 112 for (int i = 0; i < addr.length; i++) { 113 if (addr[i].getHostAddress().equals(reqIP)) 115 return true; 116 } 117 } catch (IOException ioe) { 118 } 119 return false; 120 } 121 122 129 public static String getHelpURL(String url) { 130 if (url == null || url.length() == 0) 131 url = "about:blank"; else if (url.startsWith("http:/") || url.startsWith("https:/")); else if (url.startsWith("file:/") || url.startsWith("jar:file:/")) url = "../topic/" + url; else 136 url = "../topic" + url; return url; 138 } 139 140 148 public static int[] getTopicPath(String path) { 149 if (path.startsWith("/help/nav/")) { path = path.substring(10); 151 StringTokenizer tok = new StringTokenizer (path, "_"); int[] array = new int[tok.countTokens()]; 153 for (int i=0;i<array.length;++i) { 154 array[i] = Integer.parseInt(tok.nextToken()); 155 } 156 return array; 157 } 158 else { 159 String href = path.substring(path.indexOf('/', 6)); 161 return HelpPlugin.getTocManager().getTopicPath(href); 162 } 163 } 164 165 public static boolean isBot(HttpServletRequest request) { 166 String agent = request.getHeader("User-Agent"); if (agent==null) 168 return false; 169 agent=agent.toLowerCase(Locale.ENGLISH); 170 return agent.indexOf("bot") >= 0 || agent.indexOf("crawl") >= 0 || request.getParameter("bot") != null; } 174 175 public static boolean isGecko(HttpServletRequest request) { 176 String agent = request.getHeader("User-Agent"); return isGecko(agent); 178 } 179 180 public static boolean isGecko(String agent) { 181 if (agent==null) 182 return false; 183 agent=agent.toLowerCase(Locale.ENGLISH); 184 return agent.indexOf("gecko/") >= 0; } 188 189 public static boolean isIE(HttpServletRequest request) { 190 String agent = request.getHeader("User-Agent"); return isIE(agent); 192 } 193 194 public static boolean isIE(String agent) { 195 if (agent==null) 196 return false; 197 agent=agent.toLowerCase(Locale.ENGLISH); 198 199 if (agent.startsWith("bobby/")) { return true; 203 } 204 206 return (agent.indexOf("msie") >= 0); } 208 209 public static String getIEVersion(HttpServletRequest request) { 210 String agent = request.getHeader("User-Agent"); return getIEVersion(agent); 212 } 213 214 public static String getIEVersion(String agent) { 215 if (agent==null) 216 return "0"; 218 agent=agent.toLowerCase(Locale.ENGLISH); 219 if (agent.startsWith("bobby/")) { return "5.5"; } 224 226 int start = agent.indexOf("msie ") + "msie ".length(); if (start < "msie ".length() || start >= agent.length()) return "0"; int end = agent.indexOf(";", start); if (end <= start) 231 return "0"; return agent.substring(start, end); 233 } 234 235 public static boolean isKonqueror(HttpServletRequest request) { 236 String agent = request.getHeader("User-Agent"); return isKonqueror(agent); 238 } 239 240 public static boolean isKonqueror(String agent) { 241 if (agent==null) 242 return false; 243 agent=agent.toLowerCase(Locale.ENGLISH); 244 return agent.indexOf("konqueror") >= 0; } 246 247 253 public static boolean isMozilla(HttpServletRequest request) { 254 String agent = request.getHeader("User-Agent"); return isMozilla(agent); 256 } 257 258 public static boolean isMozilla(String agent) { 259 if (agent==null) 260 return false; 261 agent=agent.toLowerCase(Locale.ENGLISH); 262 return agent.indexOf("mozilla/5") >= 0; } 264 265 public static String getMozillaVersion(HttpServletRequest request) { 266 String agent = request.getHeader("User-Agent"); return getMozillaVersion(agent); 268 } 269 270 public static String getMozillaVersion(String agent) { 271 if (agent==null) 272 return "0"; agent=agent.toLowerCase(Locale.ENGLISH); 274 if (agent.indexOf("mozilla/5") < 0) return "0"; int start = agent.indexOf("rv:") + "rv:".length(); if (start < "rv:".length() || start >= agent.length()) return "0"; int end = agent.indexOf(")", start); if (end <= start) 281 return "0"; return agent.substring(start, end); 283 } 284 285 public static boolean isOpera(HttpServletRequest request) { 286 String agent = request.getHeader("User-Agent"); return isOpera(agent); 288 } 289 290 public static boolean isOpera(String agent) { 291 if (agent==null) 292 return false; 293 agent=agent.toLowerCase(Locale.ENGLISH); 294 return agent.indexOf("opera") >= 0; } 296 297 public static String getOperaVersion(String agent) { 298 if (agent==null) 299 return "0"; agent=agent.toLowerCase(Locale.ENGLISH); 301 final String OperaPrefix = "opera/"; int start = agent.indexOf(OperaPrefix) + OperaPrefix.length(); 303 if (start < OperaPrefix.length() || start >= agent.length()) 304 return "0"; int end = agent.indexOf(" (", start); if (end <= start) 307 return "0"; return agent.substring(start, end); 309 } 310 311 public static boolean isSafari(HttpServletRequest request) { 312 String agent = request.getHeader("User-Agent"); return isSafari(agent); 314 } 315 316 public static boolean isSafari(String agent) { 317 if (agent==null) 318 return false; 319 agent=agent.toLowerCase(Locale.ENGLISH); 320 return agent.indexOf("safari/") >= 0; } 322 323 public static String getSafariVersion(HttpServletRequest request) { 324 String agent = request.getHeader("User-Agent"); return getSafariVersion(agent); 326 } 327 328 public static String getSafariVersion(String agent) { 329 String version = "0"; if (agent==null) 331 return version; 332 agent=agent.toLowerCase(Locale.ENGLISH); 333 Matcher m = safariPattern.matcher(agent); 334 boolean matched = m.find(); 335 if (matched) { 336 version = m.group(1); 337 while (version.length() < 3) { 338 version = "0" + version; } 340 } 341 return version; 342 } 343 351 public static Locale getLocaleObj(HttpServletRequest request, 352 HttpServletResponse response) { 353 String localeStr = getLocale(request, response); 354 return getLocale(localeStr); 355 } 356 357 364 public static Locale getLocale(String localeStr) { 365 if (localeStr.length() >= 5) { 366 return new Locale (localeStr.substring(0, 2), localeStr.substring(3, 367 5)); 368 } else if (localeStr.length() >= 2) { 369 return new Locale (localeStr.substring(0, 2), ""); } else { 371 return Locale.getDefault(); 372 } 373 } 374 382 public static String getLocale(HttpServletRequest request, 383 HttpServletResponse response) { 384 if (defaultLocale == null) { 385 initializeNL(); 386 } 387 if ((BaseHelpSystem.getMode() != BaseHelpSystem.MODE_INFOCENTER) 388 || request == null) { 389 return defaultLocale; 390 } 391 392 String forcedLocale = getForcedLocale(request, response); 394 if (forcedLocale != null) { 395 if (locales == null) { 396 return forcedLocale; 398 } 399 if (locales.contains(forcedLocale)) { 401 return forcedLocale; 402 } 403 if (forcedLocale.length() > 2) { 405 String ll = forcedLocale.substring(0, 2); 406 if (locales.contains(ll)) { 407 return ll; 408 } 409 } 410 } 411 412 if (locales == null) { 414 return request.getLocale().toString(); 416 } 417 for (Enumeration e = request.getLocales(); e.hasMoreElements();) { 419 String locale = ((Locale ) e.nextElement()).toString(); 420 if (locale.length() >= 5) { 421 String ll_CC = locale.substring(0, 5); 422 if (locales.contains(ll_CC)) { 423 return ll_CC; 425 } 426 } 427 if (locale.length() >= 2) { 428 String ll = locale.substring(0, 2); 429 if (locales.contains(ll)) { 430 return ll; 432 } 433 } 434 } 435 return defaultLocale; 437 } 438 439 449 private static String getForcedLocale(HttpServletRequest request, 450 HttpServletResponse response) { 451 String forcedLocale = request.getParameter("lang"); if (forcedLocale != null) { 454 if (response != null) { 456 Cookie cookieTest = new Cookie ("lang", forcedLocale); response.addCookie(cookieTest); 458 } 459 } else { 460 Cookie [] cookies = request.getCookies(); 462 if (cookies != null) { 463 for (int c = 0; c < cookies.length; c++) { 464 if ("lang".equals(cookies[c].getName())) { forcedLocale = cookies[c].getValue(); 466 break; 467 } 468 } 469 } 470 } 471 472 if (forcedLocale != null) { 474 if (forcedLocale.length() >= 5) { 475 forcedLocale = forcedLocale.substring(0, 2) + "_" + forcedLocale.substring(3, 5); 477 } else if (forcedLocale.length() >= 2) { 478 forcedLocale = forcedLocale.substring(0, 2); 479 } 480 } 481 return forcedLocale; 482 } 483 488 private static synchronized void initializeNL() { 489 if (defaultLocale != null) { 490 return; 492 } 493 initializeLocales(); 494 if ((BaseHelpSystem.getMode() == BaseHelpSystem.MODE_INFOCENTER)) { 495 initializeIcDirection(); 496 } 497 498 } 499 502 private static void initializeLocales() { 503 defaultLocale = Platform.getNL(); 505 if (defaultLocale == null) { 506 defaultLocale = Locale.getDefault().toString(); 507 } 508 if (BaseHelpSystem.getMode() != BaseHelpSystem.MODE_INFOCENTER) { 509 return; 510 } 511 512 List infocenterLocales = null; 514 515 String [] args = Platform.getCommandLineArgs(); 517 boolean localeOption = false; 518 for (int i = 0; i < args.length; i++) { 519 if ("-locales".equalsIgnoreCase(args[i])) { localeOption = true; 521 infocenterLocales = new ArrayList (); 522 continue; 523 } else if (args[i].startsWith("-")) { localeOption = false; 525 continue; 526 } 527 if (localeOption) { 528 infocenterLocales.add(args[i]); 529 } 530 } 531 if (infocenterLocales == null) { 533 StringTokenizer tokenizer = new StringTokenizer (HelpBasePlugin 534 .getDefault().getPluginPreferences().getString("locales"), " ,\t"); while (tokenizer.hasMoreTokens()) { 537 if (infocenterLocales == null) { 538 infocenterLocales = new ArrayList (); 539 } 540 infocenterLocales.add(tokenizer.nextToken()); 541 } 542 } 543 544 if (infocenterLocales != null) { 546 locales = new HashSet (10, 0.4f); 547 for (Iterator it = infocenterLocales.iterator(); it.hasNext();) { 548 String locale = (String ) it.next(); 549 if (locale.length() >= 5) { 550 locales.add(locale.substring(0, 2).toLowerCase(Locale.ENGLISH) + "_" + locale.substring(3, 5).toUpperCase(Locale.ENGLISH)); 552 553 } else if (locale.length() >= 2) { 554 locales.add(locale.substring(0, 2).toLowerCase(Locale.ENGLISH)); 555 } 556 } 557 } 558 } 559 560 private static void initializeIcDirection() { 561 String orientation = System.getProperty("eclipse.orientation"); if ("rtl".equals(orientation)) { infocenterDirection = INFOCENTER_DIRECTION_RTL; 565 return; 566 } else if ("ltr".equals(orientation)) { infocenterDirection = INFOCENTER_DIRECTION_LTR; 568 return; 569 } 570 String [] args = Platform.getCommandLineArgs(); 572 for (int i = 0; i < args.length; i++) { 573 if ("-dir".equalsIgnoreCase(args[i])) { if ((i + 1) < args.length 575 && "rtl".equalsIgnoreCase(args[i + 1])) { infocenterDirection = INFOCENTER_DIRECTION_RTL; 577 return; 578 } 579 infocenterDirection = INFOCENTER_DIRECTION_LTR; 580 return; 581 } 582 } 583 } 585 586 public static boolean isRTL(HttpServletRequest request, 587 HttpServletResponse response) { 588 if (BaseHelpSystem.getMode() != BaseHelpSystem.MODE_INFOCENTER) { 589 return BaseHelpSystem.isRTL(); 590 } 591 { 592 if (infocenterDirection == INFOCENTER_DIRECTION_RTL) { 593 return true; 594 } else if (infocenterDirection == INFOCENTER_DIRECTION_LTR) { 595 return false; 596 } 597 String locale = getLocale(request, response); 598 if (locale.startsWith("ar") || locale.startsWith("fa") || locale.startsWith("he") || locale.startsWith("iw") | locale.startsWith("ur")) { return true; 602 } 603 return false; 604 } 605 } 606 607 610 private static int getMajorVersion(String version) { 611 int result = 0; 612 for (int i = 0; i < version.length(); i++) { 613 char next = version.charAt(i); 614 if (next >= '0' && next <= '9') { 615 result = result * 10 + next - '0'; 616 } else { 617 break; 618 } 619 } 620 return result; 621 } 622 623 public static boolean isAdvanced(String agent) { 624 if (agent == null) return false; 625 if (isIE(agent) && "5.5".compareTo(getIEVersion(agent)) <= 0) return true; if (isMozilla(agent) && isGecko(agent)) return true; 627 if (isSafari(agent) && "120".compareTo(getSafariVersion(agent)) <= 0) return true; if (isOpera(agent) && getMajorVersion(getOperaVersion(agent)) >= 9) return true; 629 return false; 630 } 631 } 632 | Popular Tags |