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.

WebBrowser.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.util.Date;
  6. import java.util.Locale;
  7. import com.vaadin.shared.VBrowserDetails;
  8. import com.vaadin.terminal.Terminal;
  9. import com.vaadin.terminal.WrappedRequest;
  10. /**
  11. * Class that provides information about the web browser the user is using.
  12. * Provides information such as browser name and version, screen resolution and
  13. * IP address.
  14. *
  15. * @author Vaadin Ltd.
  16. */
  17. public class WebBrowser implements Terminal {
  18. private int screenHeight = 0;
  19. private int screenWidth = 0;
  20. private String browserApplication = null;
  21. private Locale locale;
  22. private String address;
  23. private boolean secureConnection;
  24. private int timezoneOffset = 0;
  25. private int rawTimezoneOffset = 0;
  26. private int dstSavings;
  27. private boolean dstInEffect;
  28. private boolean touchDevice;
  29. private VBrowserDetails browserDetails;
  30. private long clientServerTimeDelta;
  31. /**
  32. * There is no default-theme for this terminal type.
  33. *
  34. * @return Always returns null.
  35. */
  36. @Override
  37. public String getDefaultTheme() {
  38. return null;
  39. }
  40. /*
  41. * (non-Javadoc)
  42. *
  43. * @see com.vaadin.terminal.Terminal#getScreenHeight()
  44. */
  45. @Override
  46. public int getScreenHeight() {
  47. return screenHeight;
  48. }
  49. /*
  50. * (non-Javadoc)
  51. *
  52. * @see com.vaadin.terminal.Terminal#getScreenWidth()
  53. */
  54. @Override
  55. public int getScreenWidth() {
  56. return screenWidth;
  57. }
  58. /**
  59. * Get the browser user-agent string.
  60. *
  61. * @return The raw browser userAgent string
  62. */
  63. public String getBrowserApplication() {
  64. return browserApplication;
  65. }
  66. /**
  67. * Gets the IP-address of the web browser. If the application is running
  68. * inside a portlet, this method will return null.
  69. *
  70. * @return IP-address in 1.12.123.123 -format
  71. */
  72. public String getAddress() {
  73. return address;
  74. }
  75. /** Get the default locate of the browser. */
  76. public Locale getLocale() {
  77. return locale;
  78. }
  79. /** Is the connection made using HTTPS? */
  80. public boolean isSecureConnection() {
  81. return secureConnection;
  82. }
  83. /**
  84. * Tests whether the user is using Firefox.
  85. *
  86. * @return true if the user is using Firefox, false if the user is not using
  87. * Firefox or if no information on the browser is present
  88. */
  89. public boolean isFirefox() {
  90. if (browserDetails == null) {
  91. return false;
  92. }
  93. return browserDetails.isFirefox();
  94. }
  95. /**
  96. * Tests whether the user is using Internet Explorer.
  97. *
  98. * @return true if the user is using Internet Explorer, false if the user is
  99. * not using Internet Explorer or if no information on the browser
  100. * is present
  101. */
  102. public boolean isIE() {
  103. if (browserDetails == null) {
  104. return false;
  105. }
  106. return browserDetails.isIE();
  107. }
  108. /**
  109. * Tests whether the user is using Safari.
  110. *
  111. * @return true if the user is using Safari, false if the user is not using
  112. * Safari or if no information on the browser is present
  113. */
  114. public boolean isSafari() {
  115. if (browserDetails == null) {
  116. return false;
  117. }
  118. return browserDetails.isSafari();
  119. }
  120. /**
  121. * Tests whether the user is using Opera.
  122. *
  123. * @return true if the user is using Opera, false if the user is not using
  124. * Opera or if no information on the browser is present
  125. */
  126. public boolean isOpera() {
  127. if (browserDetails == null) {
  128. return false;
  129. }
  130. return browserDetails.isOpera();
  131. }
  132. /**
  133. * Tests whether the user is using Chrome.
  134. *
  135. * @return true if the user is using Chrome, false if the user is not using
  136. * Chrome or if no information on the browser is present
  137. */
  138. public boolean isChrome() {
  139. if (browserDetails == null) {
  140. return false;
  141. }
  142. return browserDetails.isChrome();
  143. }
  144. /**
  145. * Tests whether the user is using Chrome Frame.
  146. *
  147. * @return true if the user is using Chrome Frame, false if the user is not
  148. * using Chrome or if no information on the browser is present
  149. */
  150. public boolean isChromeFrame() {
  151. if (browserDetails == null) {
  152. return false;
  153. }
  154. return browserDetails.isChromeFrame();
  155. }
  156. /**
  157. * Tests whether the user's browser is Chrome Frame capable.
  158. *
  159. * @return true if the user can use Chrome Frame, false if the user can not
  160. * or if no information on the browser is present
  161. */
  162. public boolean isChromeFrameCapable() {
  163. if (browserDetails == null) {
  164. return false;
  165. }
  166. return browserDetails.isChromeFrameCapable();
  167. }
  168. /**
  169. * Gets the major version of the browser the user is using.
  170. *
  171. * <p>
  172. * Note that Internet Explorer in IE7 compatibility mode might return 8 in
  173. * some cases even though it should return 7.
  174. * </p>
  175. *
  176. * @return The major version of the browser or -1 if not known.
  177. */
  178. public int getBrowserMajorVersion() {
  179. if (browserDetails == null) {
  180. return -1;
  181. }
  182. return browserDetails.getBrowserMajorVersion();
  183. }
  184. /**
  185. * Gets the minor version of the browser the user is using.
  186. *
  187. * @see #getBrowserMajorVersion()
  188. *
  189. * @return The minor version of the browser or -1 if not known.
  190. */
  191. public int getBrowserMinorVersion() {
  192. if (browserDetails == null) {
  193. return -1;
  194. }
  195. return browserDetails.getBrowserMinorVersion();
  196. }
  197. /**
  198. * Tests whether the user is using Linux.
  199. *
  200. * @return true if the user is using Linux, false if the user is not using
  201. * Linux or if no information on the browser is present
  202. */
  203. public boolean isLinux() {
  204. return browserDetails.isLinux();
  205. }
  206. /**
  207. * Tests whether the user is using Mac OS X.
  208. *
  209. * @return true if the user is using Mac OS X, false if the user is not
  210. * using Mac OS X or if no information on the browser is present
  211. */
  212. public boolean isMacOSX() {
  213. return browserDetails.isMacOSX();
  214. }
  215. /**
  216. * Tests whether the user is using Windows.
  217. *
  218. * @return true if the user is using Windows, false if the user is not using
  219. * Windows or if no information on the browser is present
  220. */
  221. public boolean isWindows() {
  222. return browserDetails.isWindows();
  223. }
  224. /**
  225. * Returns the browser-reported TimeZone offset in milliseconds from GMT.
  226. * This includes possible daylight saving adjustments, to figure out which
  227. * TimeZone the user actually might be in, see
  228. * {@link #getRawTimezoneOffset()}.
  229. *
  230. * @see WebBrowser#getRawTimezoneOffset()
  231. * @return timezone offset in milliseconds, 0 if not available
  232. */
  233. public Integer getTimezoneOffset() {
  234. return timezoneOffset;
  235. }
  236. /**
  237. * Returns the browser-reported TimeZone offset in milliseconds from GMT
  238. * ignoring possible daylight saving adjustments that may be in effect in
  239. * the browser.
  240. * <p>
  241. * You can use this to figure out which TimeZones the user could actually be
  242. * in by calling {@link TimeZone#getAvailableIDs(int)}.
  243. * </p>
  244. * <p>
  245. * If {@link #getRawTimezoneOffset()} and {@link #getTimezoneOffset()}
  246. * returns the same value, the browser is either in a zone that does not
  247. * currently have daylight saving time, or in a zone that never has daylight
  248. * saving time.
  249. * </p>
  250. *
  251. * @return timezone offset in milliseconds excluding DST, 0 if not available
  252. */
  253. public Integer getRawTimezoneOffset() {
  254. return rawTimezoneOffset;
  255. }
  256. /**
  257. * Gets the difference in minutes between the browser's GMT TimeZone and
  258. * DST.
  259. *
  260. * @return the amount of minutes that the TimeZone shifts when DST is in
  261. * effect
  262. */
  263. public int getDSTSavings() {
  264. return dstSavings;
  265. }
  266. /**
  267. * Determines whether daylight savings time (DST) is currently in effect in
  268. * the region of the browser or not.
  269. *
  270. * @return true if the browser resides at a location that currently is in
  271. * DST
  272. */
  273. public boolean isDSTInEffect() {
  274. return dstInEffect;
  275. }
  276. /**
  277. * Returns the current date and time of the browser. This will not be
  278. * entirely accurate due to varying network latencies, but should provide a
  279. * close-enough value for most cases. Also note that the returned Date
  280. * object uses servers default time zone, not the clients.
  281. *
  282. * @return the current date and time of the browser.
  283. * @see #isDSTInEffect()
  284. * @see #getDSTSavings()
  285. * @see #getTimezoneOffset()
  286. */
  287. public Date getCurrentDate() {
  288. return new Date(new Date().getTime() + clientServerTimeDelta);
  289. }
  290. /**
  291. * @return true if the browser is detected to support touch events
  292. */
  293. public boolean isTouchDevice() {
  294. return touchDevice;
  295. }
  296. /**
  297. * For internal use by AbstractApplicationServlet/AbstractApplicationPortlet
  298. * only. Updates all properties in the class according to the given
  299. * information.
  300. *
  301. * @param sw
  302. * Screen width
  303. * @param sh
  304. * Screen height
  305. * @param tzo
  306. * TimeZone offset in minutes from GMT
  307. * @param rtzo
  308. * raw TimeZone offset in minutes from GMT (w/o DST adjustment)
  309. * @param dstSavings
  310. * the difference between the raw TimeZone and DST in minutes
  311. * @param dstInEffect
  312. * is DST currently active in the region or not?
  313. * @param curDate
  314. * the current date in milliseconds since the epoch
  315. * @param touchDevice
  316. */
  317. void updateClientSideDetails(String sw, String sh, String tzo, String rtzo,
  318. String dstSavings, String dstInEffect, String curDate,
  319. boolean touchDevice) {
  320. if (sw != null) {
  321. try {
  322. screenHeight = Integer.parseInt(sh);
  323. screenWidth = Integer.parseInt(sw);
  324. } catch (final NumberFormatException e) {
  325. screenHeight = screenWidth = 0;
  326. }
  327. }
  328. if (tzo != null) {
  329. try {
  330. // browser->java conversion: min->ms, reverse sign
  331. timezoneOffset = -Integer.parseInt(tzo) * 60 * 1000;
  332. } catch (final NumberFormatException e) {
  333. timezoneOffset = 0; // default gmt+0
  334. }
  335. }
  336. if (rtzo != null) {
  337. try {
  338. // browser->java conversion: min->ms, reverse sign
  339. rawTimezoneOffset = -Integer.parseInt(rtzo) * 60 * 1000;
  340. } catch (final NumberFormatException e) {
  341. rawTimezoneOffset = 0; // default gmt+0
  342. }
  343. }
  344. if (dstSavings != null) {
  345. try {
  346. // browser->java conversion: min->ms
  347. this.dstSavings = Integer.parseInt(dstSavings) * 60 * 1000;
  348. } catch (final NumberFormatException e) {
  349. this.dstSavings = 0; // default no savings
  350. }
  351. }
  352. if (dstInEffect != null) {
  353. this.dstInEffect = Boolean.parseBoolean(dstInEffect);
  354. }
  355. if (curDate != null) {
  356. try {
  357. long curTime = Long.parseLong(curDate);
  358. clientServerTimeDelta = curTime - new Date().getTime();
  359. } catch (final NumberFormatException e) {
  360. clientServerTimeDelta = 0;
  361. }
  362. }
  363. this.touchDevice = touchDevice;
  364. }
  365. /**
  366. * For internal use by AbstractApplicationServlet/AbstractApplicationPortlet
  367. * only. Updates all properties in the class according to the given
  368. * information.
  369. *
  370. * @param request
  371. * the wrapped request to read the information from
  372. */
  373. void updateRequestDetails(WrappedRequest request) {
  374. locale = request.getLocale();
  375. address = request.getRemoteAddr();
  376. secureConnection = request.isSecure();
  377. String agent = request.getHeader("user-agent");
  378. if (agent != null) {
  379. browserApplication = agent;
  380. browserDetails = new VBrowserDetails(agent);
  381. }
  382. if (request.getParameter("sw") != null) {
  383. updateClientSideDetails(request.getParameter("sw"),
  384. request.getParameter("sh"), request.getParameter("tzo"),
  385. request.getParameter("rtzo"), request.getParameter("dstd"),
  386. request.getParameter("dston"),
  387. request.getParameter("curdate"),
  388. request.getParameter("td") != null);
  389. }
  390. }
  391. /**
  392. * Checks if the browser is so old that it simply won't work with a Vaadin
  393. * application. Can be used to redirect to an alternative page, show
  394. * alternative content or similar.
  395. *
  396. * When this method returns true chances are very high that the browser
  397. * won't work and it does not make sense to direct the user to the Vaadin
  398. * application.
  399. *
  400. * @return true if the browser won't work, false if not the browser is
  401. * supported or might work
  402. */
  403. public boolean isTooOldToFunctionProperly() {
  404. if (browserDetails == null) {
  405. // Don't know, so assume it will work
  406. return false;
  407. }
  408. return browserDetails.isTooOldToFunctionProperly();
  409. }
  410. }