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 15KB

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