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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 Edge.
  113. *
  114. * @since 7.5.3
  115. * @return true if the user is using Edge, false if the user is not using
  116. * Edge or if no information on the browser is present
  117. */
  118. public boolean isEdge() {
  119. if (browserDetails == null) {
  120. return false;
  121. }
  122. return browserDetails.isEdge();
  123. }
  124. /**
  125. * Tests whether the user is using Safari.
  126. *
  127. * @return true if the user is using Safari, false if the user is not using
  128. * Safari or if no information on the browser is present
  129. */
  130. public boolean isSafari() {
  131. if (browserDetails == null) {
  132. return false;
  133. }
  134. return browserDetails.isSafari();
  135. }
  136. /**
  137. * Tests whether the user is using Opera.
  138. *
  139. * @return true if the user is using Opera, false if the user is not using
  140. * Opera or if no information on the browser is present
  141. */
  142. public boolean isOpera() {
  143. if (browserDetails == null) {
  144. return false;
  145. }
  146. return browserDetails.isOpera();
  147. }
  148. /**
  149. * Tests whether the user is using Chrome.
  150. *
  151. * @return true if the user is using Chrome, false if the user is not using
  152. * Chrome or if no information on the browser is present
  153. */
  154. public boolean isChrome() {
  155. if (browserDetails == null) {
  156. return false;
  157. }
  158. return browserDetails.isChrome();
  159. }
  160. /**
  161. * Tests whether the user is using Chrome Frame.
  162. *
  163. * @return true if the user is using Chrome Frame, false if the user is not
  164. * using Chrome or if no information on the browser is present
  165. */
  166. public boolean isChromeFrame() {
  167. if (browserDetails == null) {
  168. return false;
  169. }
  170. return browserDetails.isChromeFrame();
  171. }
  172. /**
  173. * Tests whether the user's browser is Chrome Frame capable.
  174. *
  175. * @return true if the user can use Chrome Frame, false if the user can not
  176. * or if no information on the browser is present
  177. */
  178. public boolean isChromeFrameCapable() {
  179. if (browserDetails == null) {
  180. return false;
  181. }
  182. return browserDetails.isChromeFrameCapable();
  183. }
  184. /**
  185. * Gets the major version of the browser the user is using.
  186. *
  187. * <p>
  188. * Note that Internet Explorer in IE7 compatibility mode might return 8 in
  189. * some cases even though it should return 7.
  190. * </p>
  191. *
  192. * @return The major version of the browser or -1 if not known.
  193. */
  194. public int getBrowserMajorVersion() {
  195. if (browserDetails == null) {
  196. return -1;
  197. }
  198. return browserDetails.getBrowserMajorVersion();
  199. }
  200. /**
  201. * Gets the minor version of the browser the user is using.
  202. *
  203. * @see #getBrowserMajorVersion()
  204. *
  205. * @return The minor version of the browser or -1 if not known.
  206. */
  207. public int getBrowserMinorVersion() {
  208. if (browserDetails == null) {
  209. return -1;
  210. }
  211. return browserDetails.getBrowserMinorVersion();
  212. }
  213. /**
  214. * Tests whether the user is using Linux.
  215. *
  216. * @return true if the user is using Linux, false if the user is not using
  217. * Linux or if no information on the browser is present
  218. */
  219. public boolean isLinux() {
  220. return browserDetails.isLinux();
  221. }
  222. /**
  223. * Tests whether the user is using Mac OS X.
  224. *
  225. * @return true if the user is using Mac OS X, false if the user is not
  226. * using Mac OS X or if no information on the browser is present
  227. */
  228. public boolean isMacOSX() {
  229. return browserDetails.isMacOSX();
  230. }
  231. /**
  232. * Tests whether the user is using Windows.
  233. *
  234. * @return true if the user is using Windows, false if the user is not using
  235. * Windows or if no information on the browser is present
  236. */
  237. public boolean isWindows() {
  238. return browserDetails.isWindows();
  239. }
  240. /**
  241. * Tests whether the user is using Windows Phone.
  242. *
  243. * @return true if the user is using Windows Phone, false if the user is not
  244. * using Windows Phone or if no information on the browser is
  245. * present
  246. * @since 7.3.2
  247. */
  248. public boolean isWindowsPhone() {
  249. return browserDetails.isWindowsPhone();
  250. }
  251. /**
  252. * Tests if the browser is run on Android.
  253. *
  254. * @return true if run on Android false if the user is not using Android or
  255. * if no information on the browser is present
  256. */
  257. public boolean isAndroid() {
  258. return browserDetails.isAndroid();
  259. }
  260. /**
  261. * Tests if the browser is run in iOS.
  262. *
  263. * @return true if run in iOS false if the user is not using iOS or if no
  264. * information on the browser is present
  265. */
  266. public boolean isIOS() {
  267. return browserDetails.isIOS();
  268. }
  269. /**
  270. * Tests if the browser is run on IPhone.
  271. *
  272. * @return true if run on IPhone false if the user is not using IPhone or if
  273. * no information on the browser is present
  274. * @since 7.3.3
  275. */
  276. public boolean isIPhone() {
  277. return browserDetails.isIPhone();
  278. }
  279. /**
  280. * Tests if the browser is run on IPad.
  281. *
  282. * @return true if run on IPad false if the user is not using IPad or if no
  283. * information on the browser is present
  284. * @since 7.3.3
  285. */
  286. public boolean isIPad() {
  287. return browserDetails.isIPad();
  288. }
  289. /**
  290. * Returns the browser-reported TimeZone offset in milliseconds from GMT.
  291. * This includes possible daylight saving adjustments, to figure out which
  292. * TimeZone the user actually might be in, see
  293. * {@link #getRawTimezoneOffset()}.
  294. *
  295. * @see WebBrowser#getRawTimezoneOffset()
  296. * @return timezone offset in milliseconds, 0 if not available
  297. */
  298. public int getTimezoneOffset() {
  299. return timezoneOffset;
  300. }
  301. /**
  302. * Returns the browser-reported TimeZone offset in milliseconds from GMT
  303. * ignoring possible daylight saving adjustments that may be in effect in
  304. * the browser.
  305. * <p>
  306. * You can use this to figure out which TimeZones the user could actually be
  307. * in by calling {@link TimeZone#getAvailableIDs(int)}.
  308. * </p>
  309. * <p>
  310. * If {@link #getRawTimezoneOffset()} and {@link #getTimezoneOffset()}
  311. * returns the same value, the browser is either in a zone that does not
  312. * currently have daylight saving time, or in a zone that never has daylight
  313. * saving time.
  314. * </p>
  315. *
  316. * @return timezone offset in milliseconds excluding DST, 0 if not available
  317. */
  318. public int getRawTimezoneOffset() {
  319. return rawTimezoneOffset;
  320. }
  321. /**
  322. * Returns the offset in milliseconds between the browser's GMT TimeZone and
  323. * DST.
  324. *
  325. * @return the number of milliseconds that the TimeZone shifts when DST is
  326. * in effect
  327. */
  328. public int getDSTSavings() {
  329. return dstSavings;
  330. }
  331. /**
  332. * Returns whether daylight saving time (DST) is currently in effect in the
  333. * region of the browser or not.
  334. *
  335. * @return true if the browser resides at a location that currently is in
  336. * DST
  337. */
  338. public boolean isDSTInEffect() {
  339. return dstInEffect;
  340. }
  341. /**
  342. * Returns the current date and time of the browser. This will not be
  343. * entirely accurate due to varying network latencies, but should provide a
  344. * close-enough value for most cases. Also note that the returned Date
  345. * object uses servers default time zone, not the clients.
  346. * <p>
  347. * To get the actual date and time shown in the end users computer, you can
  348. * do something like:
  349. *
  350. * <pre>
  351. * WebBrowser browser = ...;
  352. * SimpleTimeZone timeZone = new SimpleTimeZone(browser.getTimezoneOffset(), "Fake client time zone");
  353. * DateFormat format = DateFormat.getDateTimeInstance();
  354. * format.setTimeZone(timeZone);
  355. * myLabel.setValue(format.format(browser.getCurrentDate()));
  356. * </pre>
  357. *
  358. * @return the current date and time of the browser.
  359. * @see #isDSTInEffect()
  360. * @see #getDSTSavings()
  361. * @see #getTimezoneOffset()
  362. */
  363. public Date getCurrentDate() {
  364. return new Date(new Date().getTime() + clientServerTimeDelta);
  365. }
  366. /**
  367. * @return true if the browser is detected to support touch events
  368. */
  369. public boolean isTouchDevice() {
  370. return touchDevice;
  371. }
  372. /**
  373. * For internal use by VaadinServlet/VaadinPortlet only. Updates all
  374. * properties in the class according to the given information.
  375. *
  376. * @param sw
  377. * Screen width
  378. * @param sh
  379. * Screen height
  380. * @param tzo
  381. * TimeZone offset in minutes from GMT
  382. * @param rtzo
  383. * raw TimeZone offset in minutes from GMT (w/o DST adjustment)
  384. * @param dstSavings
  385. * the difference between the raw TimeZone and DST in minutes
  386. * @param dstInEffect
  387. * is DST currently active in the region or not?
  388. * @param curDate
  389. * the current date in milliseconds since the epoch
  390. * @param touchDevice
  391. */
  392. void updateClientSideDetails(String sw, String sh, String tzo, String rtzo,
  393. String dstSavings, String dstInEffect, String curDate,
  394. boolean touchDevice) {
  395. if (sw != null) {
  396. try {
  397. screenHeight = Integer.parseInt(sh);
  398. screenWidth = Integer.parseInt(sw);
  399. } catch (final NumberFormatException e) {
  400. screenHeight = screenWidth = -1;
  401. }
  402. }
  403. if (tzo != null) {
  404. try {
  405. // browser->java conversion: min->ms, reverse sign
  406. timezoneOffset = -Integer.parseInt(tzo) * 60 * 1000;
  407. } catch (final NumberFormatException e) {
  408. timezoneOffset = 0; // default gmt+0
  409. }
  410. }
  411. if (rtzo != null) {
  412. try {
  413. // browser->java conversion: min->ms, reverse sign
  414. rawTimezoneOffset = -Integer.parseInt(rtzo) * 60 * 1000;
  415. } catch (final NumberFormatException e) {
  416. rawTimezoneOffset = 0; // default gmt+0
  417. }
  418. }
  419. if (dstSavings != null) {
  420. try {
  421. // browser->java conversion: min->ms
  422. this.dstSavings = Integer.parseInt(dstSavings) * 60 * 1000;
  423. } catch (final NumberFormatException e) {
  424. this.dstSavings = 0; // default no savings
  425. }
  426. }
  427. if (dstInEffect != null) {
  428. this.dstInEffect = Boolean.parseBoolean(dstInEffect);
  429. }
  430. if (curDate != null) {
  431. try {
  432. long curTime = Long.parseLong(curDate);
  433. clientServerTimeDelta = curTime - new Date().getTime();
  434. } catch (final NumberFormatException e) {
  435. clientServerTimeDelta = 0;
  436. }
  437. }
  438. this.touchDevice = touchDevice;
  439. }
  440. /**
  441. * For internal use by VaadinServlet/VaadinPortlet only. Updates all
  442. * properties in the class according to the given information.
  443. *
  444. * @param request
  445. * the Vaadin request to read the information from
  446. */
  447. public void updateRequestDetails(VaadinRequest request) {
  448. locale = request.getLocale();
  449. address = request.getRemoteAddr();
  450. secureConnection = request.isSecure();
  451. // Headers are case insensitive according to the specifiation but are
  452. // case sensitive in Weblogic portal...
  453. String agent = request.getHeader("User-Agent");
  454. if (agent != null) {
  455. browserApplication = agent;
  456. browserDetails = new VBrowserDetails(agent);
  457. }
  458. if (request.getParameter("v-sw") != null) {
  459. updateClientSideDetails(request.getParameter("v-sw"),
  460. request.getParameter("v-sh"),
  461. request.getParameter("v-tzo"),
  462. request.getParameter("v-rtzo"),
  463. request.getParameter("v-dstd"),
  464. request.getParameter("v-dston"),
  465. request.getParameter("v-curdate"),
  466. request.getParameter("v-td") != null);
  467. }
  468. }
  469. /**
  470. * Checks if the browser is so old that it simply won't work with a Vaadin
  471. * application. Can be used to redirect to an alternative page, show
  472. * alternative content or similar.
  473. *
  474. * When this method returns true chances are very high that the browser
  475. * won't work and it does not make sense to direct the user to the Vaadin
  476. * application.
  477. *
  478. * @return true if the browser won't work, false if not the browser is
  479. * supported or might work
  480. */
  481. public boolean isTooOldToFunctionProperly() {
  482. if (browserDetails == null) {
  483. // Don't know, so assume it will work
  484. return false;
  485. }
  486. return browserDetails.isTooOldToFunctionProperly();
  487. }
  488. }