選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

WebBrowser.java 17KB

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