Search in sources :

Example 21 with PlatformError

use of org.eclipse.scout.rt.platform.exception.PlatformError 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

PlatformError (org.eclipse.scout.rt.platform.exception.PlatformError)21 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)7 VetoException (org.eclipse.scout.rt.platform.exception.VetoException)6 ArrayList (java.util.ArrayList)5 IFormField (org.eclipse.scout.rt.client.ui.form.fields.IFormField)5 HashSet (java.util.HashSet)4 IForm (org.eclipse.scout.rt.client.ui.form.IForm)4 PlatformExceptionTranslator (org.eclipse.scout.rt.platform.exception.PlatformExceptionTranslator)4 IRunnable (org.eclipse.scout.rt.platform.util.concurrent.IRunnable)4 Holder (org.eclipse.scout.rt.platform.holders.Holder)3 IHolder (org.eclipse.scout.rt.platform.holders.IHolder)3 IPropertyHolder (org.eclipse.scout.rt.shared.data.form.IPropertyHolder)3 IExtensibleObject (org.eclipse.scout.rt.shared.extension.IExtensibleObject)3 IOException (java.io.IOException)2 ITreeNode (org.eclipse.scout.rt.client.ui.basic.tree.ITreeNode)2 IDesktop (org.eclipse.scout.rt.client.ui.desktop.IDesktop)2 IOutline (org.eclipse.scout.rt.client.ui.desktop.outline.IOutline)2 OrderedCollection (org.eclipse.scout.rt.platform.util.collection.OrderedCollection)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1