Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

UnsupportedBrowserHandler.java 4.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2011 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.io.IOException;
  18. import java.io.Writer;
  19. import com.vaadin.Application;
  20. /**
  21. * A {@link RequestHandler} that presents an informative page if the browser in
  22. * use is unsupported. Recognizes Chrome Frame and allow it to be used.
  23. *
  24. * <p>
  25. * This handler is usually added to the application by
  26. * {@link AbstractCommunicationManager}.
  27. * </p>
  28. */
  29. @SuppressWarnings("serial")
  30. public class UnsupportedBrowserHandler implements RequestHandler {
  31. /** Cookie used to ignore browser checks */
  32. public static final String FORCE_LOAD_COOKIE = "vaadinforceload=1";
  33. @Override
  34. public boolean handleRequest(Application application,
  35. WrappedRequest request, WrappedResponse response)
  36. throws IOException {
  37. if (request.getBrowserDetails() != null) {
  38. // Check if the browser is supported
  39. // If Chrome Frame is available we'll assume it's ok
  40. WebBrowser b = request.getBrowserDetails().getWebBrowser();
  41. if (b.isTooOldToFunctionProperly() && !b.isChromeFrameCapable()) {
  42. // bypass if cookie set
  43. String c = request.getHeader("Cookie");
  44. if (c == null || !c.contains(FORCE_LOAD_COOKIE)) {
  45. writeBrowserTooOldPage(request, response);
  46. return true; // request handled
  47. }
  48. }
  49. }
  50. return false; // pass to next handler
  51. }
  52. /**
  53. * Writes a page encouraging the user to upgrade to a more current browser.
  54. *
  55. * @param request
  56. * @param response
  57. * @throws IOException
  58. */
  59. protected void writeBrowserTooOldPage(WrappedRequest request,
  60. WrappedResponse response) throws IOException {
  61. Writer page = response.getWriter();
  62. WebBrowser b = request.getBrowserDetails().getWebBrowser();
  63. page.write("<html><body><h1>I'm sorry, but your browser is not supported</h1>"
  64. + "<p>The version ("
  65. + b.getBrowserMajorVersion()
  66. + "."
  67. + b.getBrowserMinorVersion()
  68. + ") of the browser you are using "
  69. + " is outdated and not supported.</p>"
  70. + "<p>You should <b>consider upgrading</b> to a more up-to-date browser.</p> "
  71. + "<p>The most popular browsers are <b>"
  72. + " <a href=\"https://www.google.com/chrome\">Chrome</a>,"
  73. + " <a href=\"http://www.mozilla.com/firefox\">Firefox</a>,"
  74. + (b.isWindows() ? " <a href=\"http://windows.microsoft.com/en-US/internet-explorer/downloads/ie\">Internet Explorer</a>,"
  75. : "")
  76. + " <a href=\"http://www.opera.com/browser\">Opera</a>"
  77. + " and <a href=\"http://www.apple.com/safari\">Safari</a>.</b><br/>"
  78. + "Upgrading to the latest version of one of these <b>will make the web safer, faster and better looking.</b></p>"
  79. + (b.isIE() ? "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js\"></script>"
  80. + "<p>If you can not upgrade your browser, please consider trying <a onclick=\"CFInstall.check({mode:'overlay'});return false;\" href=\"http://www.google.com/chromeframe\">Chrome Frame</a>.</p>"
  81. : "") //
  82. + "<p><sub><a onclick=\"document.cookie='"
  83. + FORCE_LOAD_COOKIE
  84. + "';window.location.reload();return false;\" href=\"#\">Continue without updating</a> (not recommended)</sub></p>"
  85. + "</body>\n" + "</html>");
  86. page.close();
  87. }
  88. }