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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. * @since 7.3.2
  234. */
  235. public boolean isWindowsPhone() {
  236. return browserDetails.isWindowsPhone();
  237. }
  238. /**
  239. * Tests if the browser is run on Android.
  240. *
  241. * @return true if run on Android false if the user is not using Android or
  242. * if no information on the browser is present
  243. */
  244. public boolean isAndroid() {
  245. return browserDetails.isAndroid();
  246. }
  247. /**
  248. * Tests if the browser is run in iOS.
  249. *
  250. * @return true if run in iOS false if the user is not using iOS or if no
  251. * information on the browser is present
  252. */
  253. public boolean isIOS() {
  254. return browserDetails.isIOS();
  255. }
  256. /**
  257. * Returns the browser-reported TimeZone offset in milliseconds from GMT.
  258. * This includes possible daylight saving adjustments, to figure out which
  259. * TimeZone the user actually might be in, see
  260. * {@link #getRawTimezoneOffset()}.
  261. *
  262. * @see WebBrowser#getRawTimezoneOffset()
  263. * @return timezone offset in milliseconds, 0 if not available
  264. */
  265. public int getTimezoneOffset() {
  266. return timezoneOffset;
  267. }
  268. /**
  269. * Returns the browser-reported TimeZone offset in milliseconds from GMT
  270. * ignoring possible daylight saving adjustments that may be in effect in
  271. * the browser.
  272. * <p>
  273. * You can use this to figure out which TimeZones the user could actually be
  274. * in by calling {@link TimeZone#getAvailableIDs(int)}.
  275. * </p>
  276. * <p>
  277. * If {@link #getRawTimezoneOffset()} and {@link #getTimezoneOffset()}
  278. * returns the same value, the browser is either in a zone that does not
  279. * currently have daylight saving time, or in a zone that never has daylight
  280. * saving time.
  281. * </p>
  282. *
  283. * @return timezone offset in milliseconds excluding DST, 0 if not available
  284. */
  285. public int getRawTimezoneOffset() {
  286. return rawTimezoneOffset;
  287. }
  288. /**
  289. * Returns the offset in milliseconds between the browser's GMT TimeZone and
  290. * DST.
  291. *
  292. * @return the number of milliseconds that the TimeZone shifts when DST is
  293. * in effect
  294. */
  295. public int getDSTSavings() {
  296. return dstSavings;
  297. }
  298. /**
  299. * Returns whether daylight saving time (DST) is currently in effect in the
  300. * region of the browser or not.
  301. *
  302. * @return true if the browser resides at a location that currently is in
  303. * DST
  304. */
  305. public boolean isDSTInEffect() {
  306. return dstInEffect;
  307. }
  308. /**
  309. * Returns the current date and time of the browser. This will not be
  310. * entirely accurate due to varying network latencies, but should provide a
  311. * close-enough value for most cases. Also note that the returned Date
  312. * object uses servers default time zone, not the clients.
  313. * <p>
  314. * To get the actual date and time shown in the end users computer, you can
  315. * do something like:
  316. *
  317. * <pre>
  318. * WebBrowser browser = ...;
  319. * SimpleTimeZone timeZone = new SimpleTimeZone(browser.getTimezoneOffset(), "Fake client time zone");
  320. * DateFormat format = DateFormat.getDateTimeInstance();
  321. * format.setTimeZone(timeZone);
  322. * myLabel.setValue(format.format(browser.getCurrentDate()));
  323. * </pre>
  324. *
  325. * @return the current date and time of the browser.
  326. * @see #isDSTInEffect()
  327. * @see #getDSTSavings()
  328. * @see #getTimezoneOffset()
  329. */
  330. public Date getCurrentDate() {
  331. return new Date(new Date().getTime() + clientServerTimeDelta);
  332. }
  333. /**
  334. * @return true if the browser is detected to support touch events
  335. */
  336. public boolean isTouchDevice() {
  337. return touchDevice;
  338. }
  339. /**
  340. * For internal use by VaadinServlet/VaadinPortlet only. Updates all
  341. * properties in the class according to the given information.
  342. *
  343. * @param sw
  344. * Screen width
  345. * @param sh
  346. * Screen height
  347. * @param tzo
  348. * TimeZone offset in minutes from GMT
  349. * @param rtzo
  350. * raw TimeZone offset in minutes from GMT (w/o DST adjustment)
  351. * @param dstSavings
  352. * the difference between the raw TimeZone and DST in minutes
  353. * @param dstInEffect
  354. * is DST currently active in the region or not?
  355. * @param curDate
  356. * the current date in milliseconds since the epoch
  357. * @param touchDevice
  358. */
  359. void updateClientSideDetails(String sw, String sh, String tzo, String rtzo,
  360. String dstSavings, String dstInEffect, String curDate,
  361. boolean touchDevice) {
  362. if (sw != null) {
  363. try {
  364. screenHeight = Integer.parseInt(sh);
  365. screenWidth = Integer.parseInt(sw);
  366. } catch (final NumberFormatException e) {
  367. screenHeight = screenWidth = -1;
  368. }
  369. }
  370. if (tzo != null) {
  371. try {
  372. // browser->java conversion: min->ms, reverse sign
  373. timezoneOffset = -Integer.parseInt(tzo) * 60 * 1000;
  374. } catch (final NumberFormatException e) {
  375. timezoneOffset = 0; // default gmt+0
  376. }
  377. }
  378. if (rtzo != null) {
  379. try {
  380. // browser->java conversion: min->ms, reverse sign
  381. rawTimezoneOffset = -Integer.parseInt(rtzo) * 60 * 1000;
  382. } catch (final NumberFormatException e) {
  383. rawTimezoneOffset = 0; // default gmt+0
  384. }
  385. }
  386. if (dstSavings != null) {
  387. try {
  388. // browser->java conversion: min->ms
  389. this.dstSavings = Integer.parseInt(dstSavings) * 60 * 1000;
  390. } catch (final NumberFormatException e) {
  391. this.dstSavings = 0; // default no savings
  392. }
  393. }
  394. if (dstInEffect != null) {
  395. this.dstInEffect = Boolean.parseBoolean(dstInEffect);
  396. }
  397. if (curDate != null) {
  398. try {
  399. long curTime = Long.parseLong(curDate);
  400. clientServerTimeDelta = curTime - new Date().getTime();
  401. } catch (final NumberFormatException e) {
  402. clientServerTimeDelta = 0;
  403. }
  404. }
  405. this.touchDevice = touchDevice;
  406. }
  407. /**
  408. * For internal use by VaadinServlet/VaadinPortlet only. Updates all
  409. * properties in the class according to the given information.
  410. *
  411. * @param request
  412. * the Vaadin request to read the information from
  413. */
  414. public void updateRequestDetails(VaadinRequest request) {
  415. locale = request.getLocale();
  416. address = request.getRemoteAddr();
  417. secureConnection = request.isSecure();
  418. String agent = request.getHeader("user-agent");
  419. if (agent != null) {
  420. browserApplication = agent;
  421. browserDetails = new VBrowserDetails(agent);
  422. }
  423. if (request.getParameter("v-sw") != null) {
  424. updateClientSideDetails(request.getParameter("v-sw"),
  425. request.getParameter("v-sh"),
  426. request.getParameter("v-tzo"),
  427. request.getParameter("v-rtzo"),
  428. request.getParameter("v-dstd"),
  429. request.getParameter("v-dston"),
  430. request.getParameter("v-curdate"),
  431. request.getParameter("v-td") != null);
  432. }
  433. }
  434. /**
  435. * Checks if the browser is so old that it simply won't work with a Vaadin
  436. * application. Can be used to redirect to an alternative page, show
  437. * alternative content or similar.
  438. *
  439. * When this method returns true chances are very high that the browser
  440. * won't work and it does not make sense to direct the user to the Vaadin
  441. * application.
  442. *
  443. * @return true if the browser won't work, false if not the browser is
  444. * supported or might work
  445. */
  446. public boolean isTooOldToFunctionProperly() {
  447. if (browserDetails == null) {
  448. // Don't know, so assume it will work
  449. return false;
  450. }
  451. return browserDetails.isTooOldToFunctionProperly();
  452. }
  453. }