Package com.vaadin.client.communication
Class MessageHandler
- java.lang.Object
-
- com.vaadin.client.communication.MessageHandler
-
public class MessageHandler extends Object
A MessageHandler is responsible for handling all incoming messages (JSON) from the server (state changes, RPCs and other updates) and ensuring that the connectors are updated accordingly.- Since:
- 1.0
- Author:
- Vaadin Ltd
-
-
Field Summary
Fields Modifier and Type Field Description static StringJSON_COMMUNICATION_PREFIXstatic StringJSON_COMMUNICATION_SUFFIXprotected intlastProcessingTimeHolds the time spent rendering the last request.protected inttotalProcessingTimeHolds the total time spent rendering requests during the lifetime of the session.
-
Constructor Summary
Constructors Constructor Description MessageHandler(Registry registry)Creates a new instance connected to the given registry.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringgetCsrfToken()Gets the token (synchronizer token pattern) that the server uses to protect against CSRF (Cross Site Request Forgery) attacks.intgetLastSeenServerSyncId()Gets the server id included in the last received response.StringgetPushId()Gets the push connection identifier for this session.protected voidhandleJSON(ValueMap valueMap)voidhandleMessage(ValueMap json)Handles a received UIDL JSON text, parsing it, and passing it on to the appropriate handlers, while logging timing information.booleanisInitialUidlHandled()Checks if the first UIDL has been handled.static ValueMapparseJson(String jsonText)Unwraps and parses the given JSON, originating from the server.static ValueMapparseWrappedJson(String wrappedJsonText)Parse the given wrapped JSON, received from the server, to a ValueMap.voidresumeResponseHandling(Object lock)Resumes the rendering process once all locks have been removed.voidsetNextResponseSessionExpiredHandler(Command nextResponseSessionExpiredHandler)Sets a temporary handler for session expiration.static StringstripJSONWrapping(String jsonWithWrapping)Strips the JSON wrapping from the given json string with wrapping.voidsuspendReponseHandling(Object lock)This method can be used to postpone rendering of a response for a short period of time (e.g.
-
-
-
Field Detail
-
JSON_COMMUNICATION_PREFIX
public static final String JSON_COMMUNICATION_PREFIX
- See Also:
- Constant Field Values
-
JSON_COMMUNICATION_SUFFIX
public static final String JSON_COMMUNICATION_SUFFIX
- See Also:
- Constant Field Values
-
lastProcessingTime
protected int lastProcessingTime
Holds the time spent rendering the last request.
-
totalProcessingTime
protected int totalProcessingTime
Holds the total time spent rendering requests during the lifetime of the session.
-
-
Constructor Detail
-
MessageHandler
public MessageHandler(Registry registry)
Creates a new instance connected to the given registry.- Parameters:
registry- the global registry
-
-
Method Detail
-
handleMessage
public void handleMessage(ValueMap json)
Handles a received UIDL JSON text, parsing it, and passing it on to the appropriate handlers, while logging timing information.- Parameters:
json- The JSON to handle
-
handleJSON
protected void handleJSON(ValueMap valueMap)
-
suspendReponseHandling
public void suspendReponseHandling(Object lock)
This method can be used to postpone rendering of a response for a short period of time (e.g. to avoid the rendering process during animation).- Parameters:
lock- the lock
-
resumeResponseHandling
public void resumeResponseHandling(Object lock)
Resumes the rendering process once all locks have been removed.- Parameters:
lock- the lock
-
getLastSeenServerSyncId
public int getLastSeenServerSyncId()
Gets the server id included in the last received response.This id can be used by connectors to determine whether new data has been received from the server to avoid doing the same calculations multiple times.
No guarantees are made for the structure of the id other than that there will be a new unique value every time a new response with data from the server is received.
The initial id when no request has yet been processed is -1.
- Returns:
- an id identifying the response
-
getCsrfToken
public String getCsrfToken()
Gets the token (synchronizer token pattern) that the server uses to protect against CSRF (Cross Site Request Forgery) attacks.- Returns:
- the CSRF token string
-
getPushId
public String getPushId()
Gets the push connection identifier for this session. Used when establishing a push connection with the client.- Returns:
- the push connection identifier string
-
isInitialUidlHandled
public boolean isInitialUidlHandled()
Checks if the first UIDL has been handled.- Returns:
- true if the initial UIDL has already been processed, false otherwise
-
stripJSONWrapping
public static String stripJSONWrapping(String jsonWithWrapping)
Strips the JSON wrapping from the given json string with wrapping. If the given string is not wrapped as expected, returns null- Parameters:
jsonWithWrapping- the JSON received from the server- Returns:
- an unwrapped JSON string or null if the given string was not wrapped
-
parseJson
public static ValueMap parseJson(String jsonText)
Unwraps and parses the given JSON, originating from the server.- Parameters:
jsonText- the json from the server- Returns:
- A parsed ValueMap or null if the input could not be parsed (or was null)
-
parseWrappedJson
public static ValueMap parseWrappedJson(String wrappedJsonText)
Parse the given wrapped JSON, received from the server, to a ValueMap.- Parameters:
wrappedJsonText- the json, wrapped as done by the server- Returns:
- a ValueMap, or null if the wrapping was incorrect or json could not be parsed
-
setNextResponseSessionExpiredHandler
public void setNextResponseSessionExpiredHandler(Command nextResponseSessionExpiredHandler)
Sets a temporary handler for session expiration. This handler will be triggered if and only if the next server message tells that the session has expired.- Parameters:
nextResponseSessionExpiredHandler- the handler to use or null to remove a previously set handler
-
-