You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnsupportedBrowserHandler.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2000-2018 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. /**
  20. * A {@link RequestHandler} that presents an informative page if the browser in
  21. * use is unsupported. Recognizes Chrome Frame and allow it to be used.
  22. *
  23. * <p>
  24. * This handler is usually added to the application by
  25. * {@link LegacyCommunicationManager}.
  26. * </p>
  27. */
  28. @SuppressWarnings("serial")
  29. public class UnsupportedBrowserHandler extends SynchronizedRequestHandler {
  30. /** Cookie used to ignore browser checks. */
  31. public static final String FORCE_LOAD_COOKIE = "vaadinforceload=1";
  32. @Override
  33. public boolean synchronizedHandleRequest(VaadinSession session,
  34. VaadinRequest request, VaadinResponse response) throws IOException {
  35. // Check if the browser is supported
  36. // If Chrome Frame is available we'll assume it's ok
  37. WebBrowser b = session.getBrowser();
  38. if (b.isTooOldToFunctionProperly() && !b.isChromeFrameCapable()) {
  39. // bypass if cookie set
  40. String c = request.getHeader("Cookie");
  41. if (c == null || !c.contains(FORCE_LOAD_COOKIE)) {
  42. response.setNoCacheHeaders();
  43. writeBrowserTooOldPage(request, response);
  44. return true; // request handled
  45. }
  46. }
  47. return false; // pass to next handler
  48. }
  49. /**
  50. * Writes a page encouraging the user to upgrade to a more current browser.
  51. *
  52. * @param request
  53. * @param response
  54. * @throws IOException
  55. */
  56. protected void writeBrowserTooOldPage(VaadinRequest request,
  57. VaadinResponse response) throws IOException {
  58. try (Writer page = response.getWriter()) {
  59. WebBrowser b = VaadinSession.getCurrent().getBrowser();
  60. // @formatter:off
  61. page.write(
  62. "<html>"
  63. + "<head>"
  64. + " <style>"
  65. + " html {"
  66. + " background: #fff;"
  67. + " color: #444;"
  68. + " font: 400 1em/1.5 \"Helvetica Neue\", Roboto, \"Segoe UI\", sans-serif;"
  69. + " padding: 2em;"
  70. + " }"
  71. + " body {"
  72. + " margin: 2em auto;"
  73. + " width: 27em;"
  74. + " max-width: 100%;"
  75. + " }"
  76. + " h1 {"
  77. + " line-height: 1.1;"
  78. + " margin: 2em 0 1em;"
  79. + " color: #000;"
  80. + " font-weight: 400;"
  81. + " }"
  82. + " em {"
  83. + " font-size: 1.2em;"
  84. + " font-style: normal;"
  85. + " display: block;"
  86. + " margin-bottom: 1.2em;"
  87. + " }"
  88. + " p {"
  89. + " margin: 0.5em 0 0;"
  90. + " }"
  91. + " a {"
  92. + " text-decoration: none;"
  93. + " color: #007df0;"
  94. + " }"
  95. + " sub {"
  96. + " display: block;"
  97. + " margin-top: 2.5em;"
  98. + " text-align: center;"
  99. + " border-top: 1px solid #eee;"
  100. + " padding-top: 2em;"
  101. + " }"
  102. + " sub,"
  103. + " small {"
  104. + " color: #999;"
  105. + " }"
  106. + " </style>"
  107. + "</head>"
  108. + "<body><h1>I'm sorry, but your browser is not supported</h1>"
  109. + "<p>The version (" + b.getBrowserMajorVersion()
  110. + "." + b.getBrowserMinorVersion()
  111. + ") of the browser you are using "
  112. + " is outdated and not supported.</p>"
  113. + "<p>You should <b>consider upgrading</b> to a more up-to-date browser.</p> "
  114. + "<p>The most popular browsers are <b>"
  115. + " <a href=\"https://www.google.com/chrome\">Chrome</a>,"
  116. + " <a href=\"http://www.mozilla.com/firefox\">Firefox</a>,"
  117. + (b.isWindows()
  118. ? " <a href=\"http://windows.microsoft.com/en-US/internet-explorer/downloads/ie\">Internet Explorer</a>,"
  119. : "")
  120. + " <a href=\"http://www.opera.com/browser\">Opera</a>"
  121. + " and <a href=\"http://www.apple.com/safari\">Safari</a>.</b><br/>"
  122. + "Upgrading to the latest version of one of these <b>will make the web safer, faster and better looking.</b></p>"
  123. + (b.isIE()
  124. ? "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js\"></script>"
  125. + "<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>"
  126. : "")
  127. + "<p><sub><a onclick=\"document.cookie='"
  128. + FORCE_LOAD_COOKIE
  129. + "';window.location.reload();return false;\" href=\"#\">Continue without updating</a> (not recommended)</sub></p>"
  130. + "</body>\n" + "</html>");
  131. // @formatter:on
  132. }
  133. }
  134. }