Search in sources :

Example 11 with ClientRunContext

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();
        }
    }
}
Also used : ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) JSONObject(org.json.JSONObject) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable)

Example 12 with ClientRunContext

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());
        }
    }
}
Also used : JSONObject(org.json.JSONObject) ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) FutureCancelledError(org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError) RunMonitor(org.eclipse.scout.rt.platform.context.RunMonitor) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable)

Example 13 with ClientRunContext

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;
}
Also used : ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable)

Example 14 with ClientRunContext

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();
        }
    };
}
Also used : ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) ILookupRow(org.eclipse.scout.rt.shared.services.lookup.ILookupRow) ILookupRowFetchedCallback(org.eclipse.scout.rt.shared.services.lookup.ILookupRowFetchedCallback) ToStringBuilder(org.eclipse.scout.rt.platform.util.ToStringBuilder) EventListenerList(org.eclipse.scout.rt.platform.util.EventListenerList) List(java.util.List) ArrayList(java.util.ArrayList) ILookupCall(org.eclipse.scout.rt.shared.services.lookup.ILookupCall)

Example 15 with ClientRunContext

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();
        }
    };
}
Also used : ClientRunContext(org.eclipse.scout.rt.client.context.ClientRunContext) ILookupRow(org.eclipse.scout.rt.shared.services.lookup.ILookupRow) ILookupRowFetchedCallback(org.eclipse.scout.rt.shared.services.lookup.ILookupRowFetchedCallback) ToStringBuilder(org.eclipse.scout.rt.platform.util.ToStringBuilder) EventListenerList(org.eclipse.scout.rt.platform.util.EventListenerList) List(java.util.List) ArrayList(java.util.ArrayList) ILookupCall(org.eclipse.scout.rt.shared.services.lookup.ILookupCall)

Aggregations

ClientRunContext (org.eclipse.scout.rt.client.context.ClientRunContext)19 ArrayList (java.util.ArrayList)10 List (java.util.List)9 ILookupRow (org.eclipse.scout.rt.shared.services.lookup.ILookupRow)9 ILookupRowFetchedCallback (org.eclipse.scout.rt.shared.services.lookup.ILookupRowFetchedCallback)9 ToStringBuilder (org.eclipse.scout.rt.platform.util.ToStringBuilder)8 ILookupCall (org.eclipse.scout.rt.shared.services.lookup.ILookupCall)8 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)7 ILookupRowProvider (org.eclipse.scout.rt.client.ui.form.fields.smartfield.ILookupRowProvider)4 EventListenerList (org.eclipse.scout.rt.platform.util.EventListenerList)4 FutureCancelledError (org.eclipse.scout.rt.platform.util.concurrent.FutureCancelledError)3 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)3 Test (org.junit.Test)3 IOutline (org.eclipse.scout.rt.client.ui.desktop.outline.IOutline)2 RunMonitor (org.eclipse.scout.rt.platform.context.RunMonitor)2 BlockingCountDownLatch (org.eclipse.scout.rt.testing.platform.util.BlockingCountDownLatch)2 JSONObject (org.json.JSONObject)2 HashSet (java.util.HashSet)1 Callable (java.util.concurrent.Callable)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1