Class JsonResponse
- java.lang.Object
-
- org.eclipse.scout.rt.ui.html.json.JsonResponse
-
public class JsonResponse extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static int
ERR_REJECTED_UPLOAD
static int
ERR_SESSION_TIMEOUT
static int
ERR_STARTUP_FAILED
static int
ERR_UI_PROCESSING
static int
ERR_UNSAFE_UPLOAD
static int
ERR_VERSION_MISMATCH
static String
PROP_ADAPTER_DATA
static String
PROP_COMBINED
static String
PROP_ERROR
static String
PROP_ERROR_CODE
static String
PROP_ERROR_MESSAGE
static String
PROP_EVENTS
static String
PROP_SEQUENCE_NO
static String
PROP_STARTUP_DATA
-
Constructor Summary
Constructors Constructor Description JsonResponse()
JsonResponse(Long sequenceNo)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Map<String,IJsonAdapter<?>>
adapterMap()
JsonEvent
addActionEvent(String eventTarget, String eventType)
JsonEvent
addActionEvent(String eventTarget, String eventType, String eventReference, JSONObject eventData)
Same asaddActionEvent(String, String, JSONObject)
but with an 'eventReference' added to the JSON event.JsonEvent
addActionEvent(String eventTarget, String eventType, JSONObject eventData)
Note: when converting the response to JSON, events on adapters that are also part of this response are ignored, see alsodoAddEvent(JsonEvent)
void
addAdapter(IJsonAdapter<?> adapter)
Adds an adapter to the response.JsonEvent
addPropertyChangeEvent(String id, String propertyName, Object newValue)
protected void
assertWritable()
boolean
containsAdapter(IJsonAdapter<?> adapter)
boolean
containsPropertyChangeEvent(String id, String propertyName)
protected boolean
doAddEvent(JsonEvent event)
When we send a new adapter in the JSON response we have to ignore all events for that adapter, since the adapter data already describes the latest state of the adapter.protected boolean
error()
protected int
errorCode()
protected String
errorMessage()
protected List<JsonEvent>
eventList()
void
fireProcessBufferedEvents()
Causes all registeredIJsonAdapter
s to process their buffered events.List<JsonEvent>
getEventList()
Long
getSequenceNo()
JSONObject
getStartupData()
The (mutable)startupData
object if the response is marked as "startup response",null
otherwise.boolean
isEmpty()
boolean
isMarkedAsError()
boolean
isMarkedAsStartupResponse()
boolean
isWritable()
void
markAsError(int errorCode, String errorMessage)
Marks this JSON response as "error" (default is "success").void
markAsStartupResponse()
void
registerBufferedEventsAdapter(IJsonAdapter<?> adapter)
Registers the given adapter as a holder of buffered events.void
removeJsonAdapter(String id)
Removes all traces of the adapter with the given ID from the current response.JsonEvent
replaceActionEvent(String eventTarget, String eventType, JSONObject eventData)
Note: when converting the response to JSON, events on adapters that are also part of this response are ignored, see alsodoAddEvent(JsonEvent)
protected JSONObject
startupData()
JSONObject
toJson()
Returns a JSON string representation of this instance.protected JSONObject
toJsonInternal()
String
toString()
void
unregisterBufferedEventsAdapter(IJsonAdapter<?> adapter)
-
-
-
Field Detail
-
ERR_STARTUP_FAILED
public static final int ERR_STARTUP_FAILED
- See Also:
- Constant Field Values
-
ERR_SESSION_TIMEOUT
public static final int ERR_SESSION_TIMEOUT
- See Also:
- Constant Field Values
-
ERR_UI_PROCESSING
public static final int ERR_UI_PROCESSING
- See Also:
- Constant Field Values
-
ERR_UNSAFE_UPLOAD
public static final int ERR_UNSAFE_UPLOAD
- See Also:
- Constant Field Values
-
ERR_REJECTED_UPLOAD
public static final int ERR_REJECTED_UPLOAD
- See Also:
- Constant Field Values
-
ERR_VERSION_MISMATCH
public static final int ERR_VERSION_MISMATCH
- See Also:
- Constant Field Values
-
PROP_SEQUENCE_NO
public static final String PROP_SEQUENCE_NO
- See Also:
- Constant Field Values
-
PROP_COMBINED
public static final String PROP_COMBINED
- See Also:
- Constant Field Values
-
PROP_EVENTS
public static final String PROP_EVENTS
- See Also:
- Constant Field Values
-
PROP_ADAPTER_DATA
public static final String PROP_ADAPTER_DATA
- See Also:
- Constant Field Values
-
PROP_STARTUP_DATA
public static final String PROP_STARTUP_DATA
- See Also:
- Constant Field Values
-
PROP_ERROR
public static final String PROP_ERROR
- See Also:
- Constant Field Values
-
PROP_ERROR_CODE
public static final String PROP_ERROR_CODE
- See Also:
- Constant Field Values
-
PROP_ERROR_MESSAGE
public static final String PROP_ERROR_MESSAGE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JsonResponse
public JsonResponse()
-
JsonResponse
public JsonResponse(Long sequenceNo)
-
-
Method Detail
-
getSequenceNo
public Long getSequenceNo()
-
addAdapter
public void addAdapter(IJsonAdapter<?> adapter)
Adds an adapter to the response. All adapters stored on the response are transferred to the client (browser) as JSON object. Only new adapters must be transferred, adapters already transferred to the client can be solely referenced by their ID.Note that in javascript the adapters are not created unless the first event is received.
-
addPropertyChangeEvent
public JsonEvent addPropertyChangeEvent(String id, String propertyName, Object newValue)
- Parameters:
id
- Adapter IDpropertyName
- property namenewValue
- property value
-
addActionEvent
public JsonEvent addActionEvent(String eventTarget, String eventType, JSONObject eventData)
Note: when converting the response to JSON, events on adapters that are also part of this response are ignored, see alsodoAddEvent(JsonEvent)
-
addActionEvent
public JsonEvent addActionEvent(String eventTarget, String eventType, String eventReference, JSONObject eventData)
Same asaddActionEvent(String, String, JSONObject)
but with an 'eventReference' added to the JSON event. This reference is considered additionally to 'eventTarget' when removing events (seeremoveJsonAdapter(String)
).Note: when converting the response to JSON, events on adapters that are also part of this response are ignored, see also
doAddEvent(JsonEvent)
-
replaceActionEvent
public JsonEvent replaceActionEvent(String eventTarget, String eventType, JSONObject eventData)
Note: when converting the response to JSON, events on adapters that are also part of this response are ignored, see alsodoAddEvent(JsonEvent)
-
containsAdapter
public boolean containsAdapter(IJsonAdapter<?> adapter)
-
containsPropertyChangeEvent
public boolean containsPropertyChangeEvent(String id, String propertyName)
-
registerBufferedEventsAdapter
public void registerBufferedEventsAdapter(IJsonAdapter<?> adapter)
Registers the given adapter as a holder of buffered events. Before executingtoJson()
those buffers are consumed automatically. (Additionally, all registered buffers can be consumed manually withfireProcessBufferedEvents()
.)
-
unregisterBufferedEventsAdapter
public void unregisterBufferedEventsAdapter(IJsonAdapter<?> adapter)
-
markAsStartupResponse
public void markAsStartupResponse()
-
isMarkedAsStartupResponse
public boolean isMarkedAsStartupResponse()
-
getStartupData
public JSONObject getStartupData()
The (mutable)startupData
object if the response is marked as "startup response",null
otherwise.
-
markAsError
public void markAsError(int errorCode, String errorMessage)
Marks this JSON response as "error" (default is "success").- Parameters:
errorCode
- An arbitrary number indicating the type of error.errorMessage
- A message describing the error. This message is mostly useful for debugging purposes. Usually, it is not shown to the user, because it is not language-dependent. If possible, the displayed message is translated by the client using theerrorCode
parameter (see Session.js).
-
isMarkedAsError
public boolean isMarkedAsError()
-
isWritable
public boolean isWritable()
- Returns:
true
if this response is still writable (e.g. new adapters and events can be added). AftertoJson()
was called and the resulting JSON was sent to the UI, this method will returnfalse
forever.
-
assertWritable
protected void assertWritable()
-
toJson
public JSONObject toJson()
Returns a JSON string representation of this instance. This method is typically called at the end of a request.Attention: This method is not free of side effects!
- Buffered events are processed. This may lead to the addition of new adapters and events (see implementations of
IJsonAdapter.processBufferedEvents()
). - After that, the request will not be writable anymore! Any attempt to alter the response afterwards
results in an
Assertions.AssertionException
to be thrown.isWritable()
will always returnsfalse
.
"adapterData"
property. All events are transferred in the"events"
property. An ascending sequence number is put in the"#"
property. This method will call thetoJson()
method on all adapter objects. Note that you can NOT create new adapter instances when the toJson() method runs! All new adapter instances must be created before: either in theattachModel()
method or in an event handler method likehandleXYZ()
. The technical reason for this is, first: new adapters are added to the current response (see UiSession), but at the point in time toJson() is called, we already have a new instance of the current response. Second: when we loop through the adapterMap and call toJson() for each adapter, if the adapter would create another adapter in its toJson() method, the adapterMap would grow, which would cause a ConcurrentModificationException. Additionally we should conceptually separate object creation from JSON output creation. - Buffered events are processed. This may lead to the addition of new adapters and events (see implementations of
-
toJsonInternal
protected JSONObject toJsonInternal()
-
fireProcessBufferedEvents
public void fireProcessBufferedEvents()
Causes all registeredIJsonAdapter
s to process their buffered events. This may add some events and adapters to this response. This method is called automatically duringtoJson()
.
-
doAddEvent
protected boolean doAddEvent(JsonEvent event)
When we send a new adapter in the JSON response we have to ignore all events for that adapter, since the adapter data already describes the latest state of the adapter.For property change events this is just an optimization to reduce the response size.
For other event types it may be crucial that the events are not sent.
Example: NodesInserted event on tree must not be sent since the same nodes are already sent by Tree.toJson.
-
removeJsonAdapter
public void removeJsonAdapter(String id)
Removes all traces of the adapter with the given ID from the current response. This includes all events with the given ID as target or "reference". Also, if the adapter was registered as "buffered events adapter", it is unregistered automatically. Any deferred model event for this adapter will therefore not be handled.- Parameters:
id
- Adapter ID to be removed
-
adapterMap
protected Map<String,IJsonAdapter<?>> adapterMap()
-
startupData
protected JSONObject startupData()
-
error
protected boolean error()
-
errorCode
protected int errorCode()
-
errorMessage
protected String errorMessage()
-
isEmpty
public boolean isEmpty()
-
-