Search in sources :

Example 6 with IUiSession

use of org.eclipse.scout.rt.ui.html.IUiSession in project scout.rt by eclipse.

the class UnloadRequestHandler method handleUnloadRequest.

protected void handleUnloadRequest(HttpServletRequest req, HttpServletResponse resp, String uiSessionId) {
    LOG.info("Unloading UI session with ID {} (requested by UI)", uiSessionId);
    final HttpSession httpSession = req.getSession();
    final ISessionStore sessionStore = BEANS.get(HttpSessionHelper.class).getSessionStore(httpSession);
    IUiSession uiSession = sessionStore.getUiSession(uiSessionId);
    if (uiSession != null) {
        final ReentrantLock uiSessionLock = uiSession.uiSessionLock();
        uiSessionLock.lock();
        try {
            uiSession.dispose();
        } finally {
            uiSessionLock.unlock();
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ISessionStore(org.eclipse.scout.rt.ui.html.ISessionStore) HttpSessionHelper(org.eclipse.scout.rt.ui.html.HttpSessionHelper) HttpSession(javax.servlet.http.HttpSession) IUiSession(org.eclipse.scout.rt.ui.html.IUiSession)

Example 7 with IUiSession

use of org.eclipse.scout.rt.ui.html.IUiSession in project scout.rt by eclipse.

the class JsonMessageRequestHandler method handlePost.

@Override
public boolean handlePost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    // serve only /json
    String pathInfo = req.getPathInfo();
    if (ObjectUtility.notEquals(pathInfo, "/json")) {
        return false;
    }
    // only accept requests with mime type = application/json
    String contentType = req.getContentType();
    if (contentType == null || !contentType.contains(MimeType.JSON.getType())) {
        LOG.info("Request with wrong content type received, ignoring. ContentType: {}, IP:{}", contentType, req.getRemoteAddr());
        resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return true;
    }
    final long startNanos = System.nanoTime();
    if (LOG.isDebugEnabled()) {
        LOG.debug("JSON request started");
    }
    IUiSession uiSession = null;
    JsonRequest jsonRequest = null;
    try {
        // disable caching
        m_httpCacheControl.checkAndSetCacheHeaders(req, resp, null);
        JSONObject jsonObject = m_jsonRequestHelper.readJsonRequest(req);
        jsonRequest = new JsonRequest(jsonObject);
        if (jsonRequest.getRequestType() == RequestType.PING_REQUEST) {
            // No UI session required for ping
            handlePingRequest(resp);
            return true;
        }
        // Resolve UI session
        if (jsonRequest.getRequestType() == RequestType.STARTUP_REQUEST) {
            JsonStartupRequest jsonStartupRequest = new JsonStartupRequest(jsonRequest);
            if (!validateVersion(jsonStartupRequest, resp)) {
                return true;
            }
            // Always create a new UI Session on startup
            uiSession = createUiSession(req, resp, jsonStartupRequest);
        } else {
            // Get and validate existing UI session
            uiSession = UiSession.get(req, jsonRequest);
            if (!validateUiSession(uiSession, resp, jsonRequest)) {
                return true;
            }
            // Touch the session (except for poll requests --> max idle timeout)
            if (jsonRequest.getRequestType() != RequestType.POLL_REQUEST) {
                uiSession.touch();
            }
        }
        // Associate subsequent processing with the uiSession and jsonRequest.
        RunContexts.copyCurrent().withThreadLocal(IUiSession.CURRENT, uiSession).withThreadLocal(JsonRequest.CURRENT, jsonRequest).withDiagnostics(BEANS.all(IUiRunContextDiagnostics.class)).run(new IRunnable() {

            @Override
            public void run() throws Exception {
                handleJsonRequest(IUiSession.CURRENT.get(), JsonRequest.CURRENT.get(), req, resp);
            }
        }, DefaultExceptionTranslator.class);
    } catch (Exception | PlatformError e) {
        if (jsonRequest == null || uiSession == null || jsonRequest.getRequestType() == RequestType.STARTUP_REQUEST) {
            // Send a special error code when an error happens during initialization, because
            // the UI has no translated texts to show in this case.
            LOG.error("Error while initializing UI session", e);
            writeJsonResponse(resp, m_jsonRequestHelper.createStartupFailedResponse());
        } else {
            LOG.error("Unexpected error while processing JSON request", e);
            writeJsonResponse(resp, m_jsonRequestHelper.createUnrecoverableFailureResponse());
        }
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("JSON request completed in {} ms", StringUtility.formatNanos(System.nanoTime() - startNanos));
        }
    }
    return true;
}
Also used : JSONObject(org.json.JSONObject) PlatformError(org.eclipse.scout.rt.platform.exception.PlatformError) IUiSession(org.eclipse.scout.rt.ui.html.IUiSession) IRunnable(org.eclipse.scout.rt.platform.util.concurrent.IRunnable) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

IUiSession (org.eclipse.scout.rt.ui.html.IUiSession)7 HttpSession (javax.servlet.http.HttpSession)3 ISessionStore (org.eclipse.scout.rt.ui.html.ISessionStore)3 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 ServletException (javax.servlet.ServletException)2 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)2 HttpSessionHelper (org.eclipse.scout.rt.ui.html.HttpSessionHelper)2 JSONObject (org.json.JSONObject)2 Locale (java.util.Locale)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 FileUploadException (org.apache.commons.fileupload.FileUploadException)1 PlatformError (org.eclipse.scout.rt.platform.exception.PlatformError)1 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)1 IJsonObject (org.eclipse.scout.rt.ui.html.json.IJsonObject)1 JsonRequest (org.eclipse.scout.rt.ui.html.json.JsonRequest)1 JsonStartupRequest (org.eclipse.scout.rt.ui.html.json.JsonStartupRequest)1 Test (org.junit.Test)1