RAP ClientServices explained
December 17, 2012 | 3 min ReadOne of the bigger changes in the RAP 2.0 API is the introduction of client services. These are features that interact with the client, but aren’t directly related to the widget toolkit. Since there are now multiple types of clients that can be connected (mainly our default WebClient, Tabris for iOS and Tabris for Android), as well as different environments (browser, OS version, hardware), not all of these features may be available at all times.
The client service API accounts for that. In RAP 2.0, the client is represented by a class implementing the Client
interface (WebClient
by default). An instance of this class can be obtained from RWT.getClient()
, which selects an implementation that matches the currently connected client. This implementation then provides the client services via the getService
method. For example:
MyService service = RWT.getClient().getService( MyService.class );
If the client connected to the current session supports the service, an instance is returned, otherwise null. (Also, within one session it will always return the same instance for the same service.) If you can be certain that the service is supported by all clients you are targeting, you can use it immediately, otherwise you should check for null first.
Right now, all services that are part of RAP are supported by the WebClient at all times. Some of them provide features that already existed in RAP 1.x in some form, some of them are completely new. There will certainly be even more in the future, especially ones that are based on HTML5-only features. As such, their availability at runtime may depend on the currently connected browser.
For now there is:
- ClientInfo: Provides you with the clients timezone offset and locale setting.
- BrowserNavigation: Allows you to support the browser’s back/forward button and deep linking.
- ExitConfirmation: Lets you set a message that is displayed if the users tries to close the window or navigate to another website.
- UrlLauncher: Open any given URL without leaving the application or influencing the client. May be used to open other websites, view files, start mailing or calling a specific person. May also support opening save dialogs in the future.
- JavaScriptLoader: This new feature allows the server to add JavaScript to an already running client. The service also keeps track of which files have already been loaded. This makes it easy to develop custom widgets/components that are loaded as they are needed, keeping the initial loading time for the user as short as possible. (Fittingly, we also introduce a new custom component API in 2.0).
- JavaScriptExecuter: This service allows you access the client directly. If there is a simple, client-related task that RAP does not provide any Java API for (yet), this may be a way around that. Of course one should be very careful with this feature. Using it incorrectly may disrupt or even crash the entire client. Just make sure your script can not throw any exceptions and does not mess with any clients internals.
Update: You can try all services in the updated Controls Demo.