use of org.eclipse.scout.rt.client.context.ClientRunContext in project scout.rt by eclipse.
the class UiSession method processFileUpload.
@Override
public JSONObject processFileUpload(HttpServletRequest req, HttpServletResponse res, final IBinaryResourceConsumer resourceConsumer, final List<BinaryResource> uploadResources, final Map<String, String> uploadProperties) {
final ClientRunContext clientRunContext = ClientRunContexts.copyCurrent().withSession(m_clientSession, true);
m_httpContext.set(req, res);
try {
m_processingJsonRequest = true;
try {
// 1. Process the JSON request.
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
resourceConsumer.consumeBinaryResource(uploadResources, uploadProperties);
}
}, createFileUploadModelJobInput(clientRunContext));
// 2. Wait for all model jobs of the session.
BEANS.get(UiJobs.class).awaitModelJobs(m_clientSession, ExceptionHandler.class);
} finally {
// Reset this flag _before_ the "response-to-json" job (#3), because writing to the response while transforming would be unsafe and unreliable.
m_processingJsonRequest = false;
}
// 3. Transform the response to JSON.
final IFuture<JSONObject> future = ModelJobs.schedule(newResponseToJsonTransformer(), ModelJobs.newInput(clientRunContext.copy().withRunMonitor(// separate RunMonitor to not cancel 'response-to-json' job once processing is cancelled
BEANS.get(RunMonitor.class))).withName("Transforming response to JSON").withExecutionHint(UiJobs.EXECUTION_HINT_RESPONSE_TO_JSON).withExceptionHandling(null, // Propagate exception to caller (UIServlet)
false));
try {
return BEANS.get(UiJobs.class).awaitAndGet(future);
} catch (ThreadInterruptedError e) {
// NOSONAR
future.cancel(true);
return null;
} catch (FutureCancelledError e) {
// NOSONAR
return null;
}
} finally {
m_httpContext.clear();
if (m_disposing) {
dispose();
}
}
}
use of org.eclipse.scout.rt.client.context.ClientRunContext in project scout.rt by eclipse.
the class UiSession method processJsonRequest.
@Override
public JSONObject processJsonRequest(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final JsonRequest jsonRequest) {
if (isAlreadyProcessed(jsonRequest)) {
JSONObject response = m_responseHistory.getResponseForRequest(jsonRequest.getSequenceNo());
LOG.debug("Request #{} was already processed. Sending back response from history.", jsonRequest.getSequenceNo());
return response;
}
final ClientRunContext clientRunContext = ClientRunContexts.copyCurrent().withSession(m_clientSession, true);
m_httpContext.set(servletRequest, servletResponse);
m_currentJsonRequest = jsonRequest;
try {
m_processingJsonRequest = true;
try {
// 1. Process the JSON request.
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
processJsonRequestInternal();
}
}, createJsonRequestModelJobInput(jsonRequest, clientRunContext));
// 2. Wait for all model jobs of the session.
BEANS.get(UiJobs.class).awaitModelJobs(m_clientSession, ExceptionHandler.class);
} finally {
// Reset this flag _before_ the "response-to-json" job (#3), because writing to the response while transforming would be unsafe and unreliable.
m_processingJsonRequest = false;
}
// 3. Transform the response to JSON.
final IFuture<JSONObject> future = ModelJobs.schedule(newResponseToJsonTransformer(), ModelJobs.newInput(clientRunContext.copy().withRunMonitor(// separate RunMonitor to not cancel 'response-to-json' job once processing is cancelled
BEANS.get(RunMonitor.class))).withName("Transforming response to JSON").withExecutionHint(UiJobs.EXECUTION_HINT_RESPONSE_TO_JSON).withExecutionHint(UiJobs.EXECUTION_HINT_POLL_REQUEST, jsonRequest.getRequestType() == RequestType.POLL_REQUEST).withExceptionHandling(null, // Propagate exception to caller (UIServlet)
false));
try {
return BEANS.get(UiJobs.class).awaitAndGet(future);
} catch (ThreadInterruptedError e) {
// NOSONAR
future.cancel(true);
return null;
} catch (FutureCancelledError e) {
// NOSONAR
return null;
}
} finally {
setRequestProcessed(jsonRequest);
m_httpContext.clear();
m_currentJsonRequest = null;
if (m_disposing) {
dispose();
}
if (LOG.isDebugEnabled()) {
LOG.debug("Adapter count after request: {}", m_jsonAdapterRegistry.size());
}
}
}
use of org.eclipse.scout.rt.client.context.ClientRunContext in project scout.rt by eclipse.
the class UiSession method dispose.
@Override
public void dispose() {
// on the 2nd call, the desktop is already gone.
if (!m_disposing) {
// Current thread is the model thread if dispose is called by clientSession.stop(), otherwise (e.g. page reload) dispose is called from the UI thread
if (ModelJobs.isModelThread()) {
getClientSession().getDesktop().getUIFacade().fireGuiDetached();
} else {
final ClientRunContext clientRunContext = ClientRunContexts.copyCurrent().withSession(m_clientSession, true);
ModelJobs.schedule(new IRunnable() {
@Override
public void run() throws Exception {
getClientSession().getDesktop().getUIFacade().fireGuiDetached();
}
}, ModelJobs.newInput(clientRunContext).withName("Detaching Gui").withExceptionHandling(null, // Propagate exception to caller (UIServlet)
false));
}
}
if (isProcessingJsonRequest()) {
// If there is a request in progress just mark the session as being disposed.
// The actual disposing happens before returning to the client, see processJsonRequest().
m_disposing = true;
return;
}
LOG.info("Disposing UI session with ID {}...", m_uiSessionId);
if (m_disposed) {
LOG.trace("UI session with ID {} already disposed.", m_uiSessionId);
return;
}
m_disposed = true;
// also stops and removes client session if necessary
sessionStore().unregisterUiSession(this);
uninstallUiDataAvailableListener();
// Notify waiting requests - should not delay web-container shutdown
signalPoller();
m_jsonAdapterRegistry.disposeAdapters();
m_httpContext.clear();
m_currentJsonResponse = null;
}
use of org.eclipse.scout.rt.client.context.ClientRunContext in project scout.rt by eclipse.
the class AbstractContentAssistField method newByKeyLookupRowProvider.
// ==== Lookup row fetching strategies ==== //
/**
* Creates a {@link ILookupRowProvider} to fetch a row by key.
*
* @see LookupCall#getDataByKey()
* @see LookupCall#getDataByAllInBackground(ILookupRowFetchedCallback)
*/
protected ILookupRowProvider<LOOKUP_KEY> newByKeyLookupRowProvider(final LOOKUP_KEY key) {
return new ILookupRowProvider<LOOKUP_KEY>() {
@Override
public void beforeProvide(ILookupCall<LOOKUP_KEY> lookupCall) {
prepareKeyLookup(lookupCall, key);
}
@Override
public void afterProvide(ILookupCall<LOOKUP_KEY> lookupCall, List<ILookupRow<LOOKUP_KEY>> result) {
interceptFilterLookupResult(lookupCall, result);
interceptFilterKeyLookupResult(lookupCall, result);
cleanupResultList(result);
}
@Override
public void provideSync(ILookupCall<LOOKUP_KEY> lookupCall, ILookupRowFetchedCallback<LOOKUP_KEY> callback) {
callback.onSuccess(provide(lookupCall));
}
@Override
public IFuture<Void> provideAsync(ILookupCall<LOOKUP_KEY> lookupCall, ILookupRowFetchedCallback<LOOKUP_KEY> callback, ClientRunContext clientRunContext) {
return lookupCall.getDataByKeyInBackground(clientRunContext, callback);
}
@SuppressWarnings("unchecked")
@Override
public List<ILookupRow<LOOKUP_KEY>> provide(ILookupCall<LOOKUP_KEY> lookupCall) {
return (List<ILookupRow<LOOKUP_KEY>>) lookupCall.getDataByKey();
}
@Override
public String toString() {
ToStringBuilder sb = new ToStringBuilder(this).attr("Key Lookup").attr("key", key);
return sb.toString();
}
};
}
use of org.eclipse.scout.rt.client.context.ClientRunContext in project scout.rt by eclipse.
the class AbstractContentAssistField method newByAllLookupRowProvider.
/**
* Creates a {@link ILookupRowProvider} to fetch all rows.
*
* @see LookupCall#getDataByAll()
* @see LookupCall#getDataByAllInBackground(ILookupRowFetchedCallback)
*/
protected ILookupRowProvider<LOOKUP_KEY> newByAllLookupRowProvider(final String browseHint, final TriState activeState) {
return new ILookupRowProvider<LOOKUP_KEY>() {
@Override
public void beforeProvide(ILookupCall<LOOKUP_KEY> lookupCall) {
prepareBrowseLookup(lookupCall, browseHint, activeState);
}
@Override
public void afterProvide(ILookupCall<LOOKUP_KEY> lookupCall, List<ILookupRow<LOOKUP_KEY>> result) {
interceptFilterLookupResult(lookupCall, result);
interceptFilterBrowseLookupResult(lookupCall, result);
cleanupResultList(result);
}
@Override
public void provideSync(ILookupCall<LOOKUP_KEY> lookupCall, ILookupRowFetchedCallback<LOOKUP_KEY> callback) {
callback.onSuccess(provide(lookupCall));
}
@Override
public IFuture<Void> provideAsync(ILookupCall<LOOKUP_KEY> lookupCall, ILookupRowFetchedCallback<LOOKUP_KEY> callback, ClientRunContext clientRunContext) {
return lookupCall.getDataByAllInBackground(clientRunContext, callback);
}
@SuppressWarnings("unchecked")
@Override
public List<ILookupRow<LOOKUP_KEY>> provide(ILookupCall<LOOKUP_KEY> lookupCall) {
return (List<ILookupRow<LOOKUP_KEY>>) lookupCall.getDataByAll();
}
@Override
public String toString() {
ToStringBuilder sb = new ToStringBuilder(this).attr("All Lookup").attr("browseHint", browseHint).attr("activeState", activeState);
return sb.toString();
}
};
}
Aggregations