Vaadin WebBrowser
The WebBrowser class in Vaadin 7 provides an easy way to access client computing information. Access the WebBrowser object using the current Page object.
WebBrowser webBrowser = Page.getCurrent().getWebBrowser();
IP address
The getAddress method provides the visible IP address of the client computer / device.
String ipAddress = webBrowser.getAddress(); if ( ipAddress == null ) { // If null, this Vaadin app is probably running inside a portlet. }
Other customer information
The WebBrowser class can easily tell you a lot of client information.
Examples: if the client is a Mac or a touch device (pad or phone), which browser (Safari, Chrome, Firefox, etc.), if TLS (HTTPS), screen size, time zone and daylight saving time, locale, etc. . There is even a way to tell you if the web browser is too old to work well with Vaadin .
HTTP / Servlet
You can get this client information by viewing the HTTP request through the standard Java servlet calls. But the Vaadins WebBrowser class described above is more convenient.
Code example
Here is some actual code from my own application, shown here as an example. It may not be beautiful or perfect code, but it gives you an idea of ​​how to view the fence in a client web browser environment.
In some cases, the Joda-Time library is used to work with the date, as the only dependency for this code.
Get the standard servlet session identifier through some convenience classes ( VaadinSession and WrappedSession ) provided by Vaadin.
String sessionId = VaadinSession.getCurrent().getSession().getId();
Let me get and use this WebBrowser object.
WebBrowser webBrowser = Page.getCurrent().getWebBrowser(); // Environment stuff String ipAddress = webBrowser.getAddress(); // May be null, especially if running in a Portlet. String userAgentInfo = webBrowser.getBrowserApplication(); String touchDevice = String.valueOf( webBrowser.isTouchDevice() ); String screenSize = webBrowser.getScreenWidth() + "x" + webBrowser.getScreenHeight(); String locale = webBrowser.getLocale().toString(); String isHttps = String.valueOf( webBrowser.isSecureConnection() ); // Date-time stuff DateTime serverNow = DateTime.now( DateTimeZone.UTC ); java.util.Date browserCurrentDate = webBrowser.getCurrentDate(); DateTime browserCurrentDateTime = new DateTime( browserCurrentDate , DateTimeZone.UTC ); String serverClientDifference = new Period( serverNow , browserCurrentDateTime ).toString(); int offset = webBrowser.getTimezoneOffset(); int rawOffset = webBrowser.getRawTimezoneOffset(); Boolean isInDst = webBrowser.isDSTInEffect(); int dst = webBrowser.getDSTSavings(); String timeDescription = "ClientNow→" + browserCurrentDateTime + "/ServerNow→" + serverNow + "/ServerClientDiff→" + serverClientDifference + "/OffsetFromUTC→" + offset + "/RawOffsetFromUTC→" + rawOffset + "/InDST→" + isInDst + "/DST→" + dst;
Create a string representation of all this information.
StringBuilder description = new StringBuilder(); description.append( "{ Account=" ).append( accountArg ); // Particular to my own app (login). description.append( " | Username=" ).append( usernameArg ); // Particular to my own app (login). description.append( " | SessionId=" ).append( sessionId ); description.append( " | IP_Address=" ).append( ipAddress ); description.append( " | HTTPS=" ).append( isHttps ); description.append( " | Locale=" ).append( locale ); description.append( " | TouchDevice=" ).append( touchDevice ); description.append( " | ScreenSize=" ).append( screenSize ); description.append( " | UserAgent=" ).append( userAgentInfo ); description.append( " | Time= " ).append( timeDescription ); description.append( " }" );
Output Example:
{ Account= | Username= | SessionId=9309B2FA176D57F4D74CDC9E4E0238A8 | IP_Address=0:0:0:0:0:0:0:1 | HTTPS=false | Locale=en_US | TouchDevice=false | ScreenSize=1920x1080 | UserAgent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/6.2.3 Safari/537.85.12 | Time= ClientNow→2015-03-03T21:11:25.664Z/ServerNow→2015-03-03T21:11:25.680Z/ServerClientDiff→PT-0.016S/OffsetFromUTC→-28800000/RawOffsetFromUTC→-28800000/InDST→false/DST→3600000 }
The observer may notice that the IP address was listed as IPv6 , and not more than ordinary IPv4 . Already reported in Ticket # 8614 .
For Vaadin applications before Vaadin 7, see this forum topic .