Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Copyright 2000-2016 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 String timeZoneId;
  41. private boolean touchDevice;
  42. private VBrowserDetails browserDetails;
  43. private long clientServerTimeDelta;
  44. /**
  45. * Gets the height of the screen in pixels. This is the full screen
  46. * resolution and not the height available for the application.
  47. *
  48. * @return the height of the screen in pixels.
  49. */
  50. public int getScreenHeight() {
  51. return screenHeight;
  52. }
  53. /**
  54. * Gets the width of the screen in pixels. This is the full screen
  55. * resolution and not the width available for the application.
  56. *
  57. * @return the width of the screen in pixels.
  58. */
  59. public int getScreenWidth() {
  60. return screenWidth;
  61. }
  62. /**
  63. * Get the browser user-agent string.
  64. *
  65. * @return The raw browser userAgent string
  66. */
  67. public String getBrowserApplication() {
  68. return browserApplication;
  69. }
  70. /**
  71. * Gets the IP-address of the web browser. If the application is running
  72. * inside a portlet, this method will return null.
  73. *
  74. * @return IP-address in 1.12.123.123 -format
  75. */
  76. public String getAddress() {
  77. return address;
  78. }
  79. /** Get the default locate of the browser. */
  80. public Locale getLocale() {
  81. return locale;
  82. }
  83. /** Is the connection made using HTTPS? */
  84. public boolean isSecureConnection() {
  85. return secureConnection;
  86. }
  87. /**
  88. * Tests whether the user is using Firefox.
  89. *
  90. * @return true if the user is using Firefox, false if the user is not using
  91. * Firefox or if no information on the browser is present
  92. */
  93. public boolean isFirefox() {
  94. if (browserDetails == null) {
  95. return false;
  96. }
  97. return browserDetails.isFirefox();
  98. }
  99. /**
  100. * Tests whether the user is using Internet Explorer.
  101. *
  102. * @return true if the user is using Internet Explorer, false if the user is
  103. * not using Internet Explorer or if no information on the browser
  104. * is present
  105. */
  106. public boolean isIE() {
  107. if (browserDetails == null) {
  108. return false;
  109. }
  110. return browserDetails.isIE();
  111. }
  112. /**
  113. * Tests whether the user is using Edge.
  114. *
  115. * @since 7.5.3
  116. * @return true if the user is using Edge, false if the user is not using
  117. * Edge or if no information on the browser is present
  118. */
  119. public boolean isEdge() {
  120. if (browserDetails == null) {
  121. return false;
  122. }
  123. return browserDetails.isEdge();
  124. }
  125. /**
  126. * Tests whether the user is using Safari. Note that Chrome on iOS is not
  127. * detected as Safari but as Chrome although the underlying browser engine
  128. * is the same.
  129. *
  130. * @return true if the user is using Safari, false if the user is not using
  131. * Safari or if no information on the browser is present
  132. */
  133. public boolean isSafari() {
  134. if (browserDetails == null) {
  135. return false;
  136. }
  137. return browserDetails.isSafari();
  138. }
  139. /**
  140. * Tests whether the user is using Opera.
  141. *
  142. * @return true if the user is using Opera, false if the user is not using
  143. * Opera or if no information on the browser is present
  144. */
  145. public boolean isOpera() {
  146. if (browserDetails == null) {
  147. return false;
  148. }
  149. return browserDetails.isOpera();
  150. }
  151. /**
  152. * Tests whether the user is using Chrome.
  153. *
  154. * @return true if the user is using Chrome, false if the user is not using
  155. * Chrome or if no information on the browser is present
  156. */
  157. public boolean isChrome() {
  158. if (browserDetails == null) {
  159. return false;
  160. }
  161. return browserDetails.isChrome();
  162. }
  163. /**
  164. * Tests whether the user is using Chrome Frame.
  165. *
  166. * @return true if the user is using Chrome Frame, false if the user is not
  167. * using Chrome or if no information on the browser is present
  168. */
  169. public boolean isChromeFrame() {
  170. if (browserDetails == null) {
  171. return false;
  172. }
  173. return browserDetails.isChromeFrame();
  174. }
  175. /**
  176. * Tests whether the user's browser is Chrome Frame capable.
  177. *
  178. * @return true if the user can use Chrome Frame, false if the user can not
  179. * or if no information on the browser is present
  180. */
  181. public boolean isChromeFrameCapable() {
  182. if (browserDetails == null) {
  183. return false;
  184. }
  185. return browserDetails.isChromeFrameCapable();
  186. }
  187. /**
  188. * Gets the major version of the browser the user is using.
  189. *
  190. * <p>
  191. * Note that Internet Explorer in IE7 compatibility mode might return 8 in
  192. * some cases even though it should return 7.
  193. * </p>
  194. *
  195. * @return The major version of the browser or -1 if not known.
  196. */
  197. public int getBrowserMajorVersion() {
  198. if (browserDetails == null) {
  199. return -1;
  200. }
  201. return browserDetails.getBrowserMajorVersion();
  202. }
  203. /**
  204. * Gets the minor version of the browser the user is using.
  205. *
  206. * @see #getBrowserMajorVersion()
  207. *
  208. * @return The minor version of the browser or -1 if not known.
  209. */
  210. public int getBrowserMinorVersion() {
  211. if (browserDetails == null) {
  212. return -1;
  213. }
  214. return browserDetails.getBrowserMinorVersion();
  215. }
  216. /**
  217. * Gets the complete browser version as string. The version is given by the
  218. * browser through the user agent string and usually consists of
  219. * dot-separated numbers. Note that the string may contain characters other
  220. * than dots and digits.
  221. *
  222. * @return the complete browser version or {@code null} if unknown
  223. * @since
  224. */
  225. public String getBrowserVersion() {
  226. return browserDetails != null ? browserDetails.getBrowserVersion()
  227. : null;
  228. }
  229. /**
  230. * Tests whether the user is using Linux.
  231. *
  232. * @return true if the user is using Linux, false if the user is not using
  233. * Linux or if no information on the browser is present
  234. */
  235. public boolean isLinux() {
  236. return browserDetails.isLinux();
  237. }
  238. /**
  239. * Tests whether the user is using Mac OS X.
  240. *
  241. * @return true if the user is using Mac OS X, false if the user is not
  242. * using Mac OS X or if no information on the browser is present
  243. */
  244. public boolean isMacOSX() {
  245. return browserDetails.isMacOSX();
  246. }
  247. /**
  248. * Tests whether the user is using Windows.
  249. *
  250. * @return true if the user is using Windows, false if the user is not using
  251. * Windows or if no information on the browser is present
  252. */
  253. public boolean isWindows() {
  254. return browserDetails.isWindows();
  255. }
  256. /**
  257. * Tests whether the user is using Windows Phone.
  258. *
  259. * @return true if the user is using Windows Phone, false if the user is not
  260. * using Windows Phone or if no information on the browser is
  261. * present
  262. * @since 7.3.2
  263. */
  264. public boolean isWindowsPhone() {
  265. return browserDetails.isWindowsPhone();
  266. }
  267. /**
  268. * Tests if the browser is run on Android.
  269. *
  270. * @return true if run on Android false if the user is not using Android or
  271. * if no information on the browser is present
  272. */
  273. public boolean isAndroid() {
  274. return browserDetails.isAndroid();
  275. }
  276. /**
  277. * Tests if the browser is run in iOS.
  278. *
  279. * @return true if run in iOS false if the user is not using iOS or if no
  280. * information on the browser is present
  281. */
  282. public boolean isIOS() {
  283. return browserDetails.isIOS();
  284. }
  285. /**
  286. * Tests if the browser is run on IPhone.
  287. *
  288. * @return true if run on IPhone false if the user is not using IPhone or if
  289. * no information on the browser is present
  290. * @since 7.3.3
  291. */
  292. public boolean isIPhone() {
  293. return browserDetails.isIPhone();
  294. }
  295. /**
  296. * Tests if the browser is run on IPad.
  297. *
  298. * @return true if run on IPad false if the user is not using IPad or if no
  299. * information on the browser is present
  300. * @since 7.3.3
  301. */
  302. public boolean isIPad() {
  303. return browserDetails.isIPad();
  304. }
  305. /**
  306. * Tests if the browser is run on ChromeOS (e.g. a Chromebook).
  307. *
  308. * @return true if run on ChromeOS false if the user is not using ChromeOS
  309. * or if no information on the browser is present
  310. * @since 8.1.1
  311. */
  312. public boolean isChromeOS() {
  313. return browserDetails.isChromeOS();
  314. }
  315. /**
  316. * Returns the browser-reported TimeZone offset in milliseconds from GMT.
  317. * This includes possible daylight saving adjustments, to figure out which
  318. * TimeZone the user actually might be in, see
  319. * {@link #getRawTimezoneOffset()}.
  320. *
  321. * @see WebBrowser#getRawTimezoneOffset()
  322. * @return timezone offset in milliseconds, 0 if not available
  323. */
  324. public int getTimezoneOffset() {
  325. return timezoneOffset;
  326. }
  327. /**
  328. * Returns the TimeZone Id (like "Europe/Helsinki") provided by the browser
  329. * (if the browser supports this feature).
  330. *
  331. * @return the TimeZone Id if provided by the browser, null otherwise.
  332. * @see <a href=
  333. * "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/resolvedOptions">Intl.DateTimeFormat.prototype.resolvedOptions()</a>
  334. * @since 8.2
  335. */
  336. public String getTimeZoneId() {
  337. return timeZoneId;
  338. }
  339. /**
  340. * Returns the browser-reported TimeZone offset in milliseconds from GMT
  341. * ignoring possible daylight saving adjustments that may be in effect in
  342. * the browser.
  343. * <p>
  344. * You can use this to figure out which TimeZones the user could actually be
  345. * in by calling {@link TimeZone#getAvailableIDs(int)}.
  346. * </p>
  347. * <p>
  348. * If {@link #getRawTimezoneOffset()} and {@link #getTimezoneOffset()}
  349. * returns the same value, the browser is either in a zone that does not
  350. * currently have daylight saving time, or in a zone that never has daylight
  351. * saving time.
  352. * </p>
  353. *
  354. * @return timezone offset in milliseconds excluding DST, 0 if not available
  355. */
  356. public int getRawTimezoneOffset() {
  357. return rawTimezoneOffset;
  358. }
  359. /**
  360. * Returns the offset in milliseconds between the browser's GMT TimeZone and
  361. * DST.
  362. *
  363. * @return the number of milliseconds that the TimeZone shifts when DST is
  364. * in effect
  365. */
  366. public int getDSTSavings() {
  367. return dstSavings;
  368. }
  369. /**
  370. * Returns whether daylight saving time (DST) is currently in effect in the
  371. * region of the browser or not.
  372. *
  373. * @return true if the browser resides at a location that currently is in
  374. * DST
  375. */
  376. public boolean isDSTInEffect() {
  377. return dstInEffect;
  378. }
  379. /**
  380. * Returns the current date and time of the browser. This will not be
  381. * entirely accurate due to varying network latencies, but should provide a
  382. * close-enough value for most cases. Also note that the returned Date
  383. * object uses servers default time zone, not the clients.
  384. * <p>
  385. * To get the actual date and time shown in the end users computer, you can
  386. * do something like:
  387. *
  388. * <pre>
  389. * WebBrowser browser = ...;
  390. * SimpleTimeZone timeZone = new SimpleTimeZone(browser.getTimezoneOffset(), "Fake client time zone");
  391. * DateFormat format = DateFormat.getDateTimeInstance();
  392. * format.setTimeZone(timeZone);
  393. * myLabel.setValue(format.format(browser.getCurrentDate()));
  394. * </pre>
  395. *
  396. * @return the current date and time of the browser.
  397. * @see #isDSTInEffect()
  398. * @see #getDSTSavings()
  399. * @see #getTimezoneOffset()
  400. */
  401. public Date getCurrentDate() {
  402. return new Date(new Date().getTime() + clientServerTimeDelta);
  403. }
  404. /**
  405. * @return true if the browser is detected to support touch events
  406. */
  407. public boolean isTouchDevice() {
  408. return touchDevice;
  409. }
  410. /**
  411. * For internal use by VaadinServlet/VaadinPortlet only. Updates all
  412. * properties in the class according to the given information.
  413. *
  414. * @param sw
  415. * Screen width
  416. * @param sh
  417. * Screen height
  418. * @param tzo
  419. * TimeZone offset in minutes from GMT
  420. * @param rtzo
  421. * raw TimeZone offset in minutes from GMT (w/o DST adjustment)
  422. * @param dstSavings
  423. * the difference between the raw TimeZone and DST in minutes
  424. * @param dstInEffect
  425. * is DST currently active in the region or not?
  426. * @param curDate
  427. * the current date in milliseconds since the epoch
  428. * @param touchDevice
  429. */
  430. void updateClientSideDetails(String sw, String sh, String tzo, String rtzo,
  431. String dstSavings, String dstInEffect, String tzId, String curDate,
  432. boolean touchDevice) {
  433. if (sw != null) {
  434. try {
  435. screenHeight = Integer.parseInt(sh);
  436. screenWidth = Integer.parseInt(sw);
  437. } catch (final NumberFormatException e) {
  438. screenHeight = screenWidth = -1;
  439. }
  440. }
  441. if (tzo != null) {
  442. try {
  443. // browser->java conversion: min->ms, reverse sign
  444. timezoneOffset = -Integer.parseInt(tzo) * 60 * 1000;
  445. } catch (final NumberFormatException e) {
  446. timezoneOffset = 0; // default gmt+0
  447. }
  448. }
  449. if (rtzo != null) {
  450. try {
  451. // browser->java conversion: min->ms, reverse sign
  452. rawTimezoneOffset = -Integer.parseInt(rtzo) * 60 * 1000;
  453. } catch (final NumberFormatException e) {
  454. rawTimezoneOffset = 0; // default gmt+0
  455. }
  456. }
  457. if (dstSavings != null) {
  458. try {
  459. // browser->java conversion: min->ms
  460. this.dstSavings = Integer.parseInt(dstSavings) * 60 * 1000;
  461. } catch (final NumberFormatException e) {
  462. this.dstSavings = 0; // default no savings
  463. }
  464. }
  465. if (dstInEffect != null) {
  466. this.dstInEffect = Boolean.parseBoolean(dstInEffect);
  467. }
  468. if (tzId == null || "undefined".equals(tzId)) {
  469. timeZoneId = null;
  470. } else {
  471. timeZoneId = tzId;
  472. }
  473. if (curDate != null) {
  474. try {
  475. long curTime = Long.parseLong(curDate);
  476. clientServerTimeDelta = curTime - new Date().getTime();
  477. } catch (final NumberFormatException e) {
  478. clientServerTimeDelta = 0;
  479. }
  480. }
  481. this.touchDevice = touchDevice;
  482. }
  483. /**
  484. * For internal use by VaadinServlet/VaadinPortlet only. Updates all
  485. * properties in the class according to the given information.
  486. *
  487. * @param request
  488. * the Vaadin request to read the information from
  489. */
  490. public void updateRequestDetails(VaadinRequest request) {
  491. locale = request.getLocale();
  492. address = request.getRemoteAddr();
  493. secureConnection = request.isSecure();
  494. // Headers are case insensitive according to the specification but are
  495. // case sensitive in Weblogic portal...
  496. String agent = request.getHeader("User-Agent");
  497. if (agent != null) {
  498. browserApplication = agent;
  499. browserDetails = new VBrowserDetails(agent);
  500. }
  501. if (request.getParameter("v-sw") != null) {
  502. updateClientSideDetails(request.getParameter("v-sw"),
  503. request.getParameter("v-sh"), request.getParameter("v-tzo"),
  504. request.getParameter("v-rtzo"),
  505. request.getParameter("v-dstd"),
  506. request.getParameter("v-dston"),
  507. request.getParameter("v-tzid"),
  508. request.getParameter("v-curdate"),
  509. request.getParameter("v-td") != null);
  510. }
  511. }
  512. /**
  513. * Checks if the browser is so old that it simply won't work with a Vaadin
  514. * application. Can be used to redirect to an alternative page, show
  515. * alternative content or similar.
  516. *
  517. * When this method returns true chances are very high that the browser
  518. * won't work and it does not make sense to direct the user to the Vaadin
  519. * application.
  520. *
  521. * @return true if the browser won't work, false if not the browser is
  522. * supported or might work
  523. */
  524. public boolean isTooOldToFunctionProperly() {
  525. if (browserDetails == null) {
  526. // Don't know, so assume it will work
  527. return false;
  528. }
  529. return browserDetails.isTooOldToFunctionProperly();
  530. }
  531. /**
  532. * Checks if the browser supports ECMAScript 6, based on the user agent.
  533. *
  534. * @return <code>true</code> if the browser supports ES6, <code>false</code>
  535. * otherwise.
  536. * @since 8.1
  537. */
  538. public boolean isEs6Supported() {
  539. if (browserDetails == null) {
  540. // Don't know, so assume no
  541. return false;
  542. }
  543. return browserDetails.isEs6Supported();
  544. }
  545. }