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;
}
Aggregations