protected IHttpActionResult endpoint(string input, string endpointName, string apiVersion, EndpointImplementationDelegate implementation)
        {
            AppInfo   appInfo   = null;
            HeContext heContext = null;
            MobileLoginInfoEndpoint loginEndpoint = null;

            Payload.ResponseVersionInfo responseVersionInfo = null;
            try {
                appInfo   = AppInfo.GetAppInfo();
                heContext = appInfo.OsContext;

                if (appInfo == null || !appInfo.IsApplicationEnabled)
                {
                    return(GetScreenServiceResult(HttpStatusCode.ServiceUnavailable, GetExceptionPayload(new ApplicationBackendUnavailableException("Application Backend Unavailable"))));
                }

                ValidateRequestSecurity();

                heContext.Session.EnableNewRuntimeSessionStorage();
                loginEndpoint = ValidateRequestLogin(appInfo, heContext);

                Payload.RequestPayload requestPayload;
                try {
                    requestPayload = JsonConvert.DeserializeObject <Payload.RequestPayload>(input, BehaviorsConfiguration.SerializerSettings);
                } catch (Exception ex) {
                    throw GetBadRequestException("Failed to parse JSON request content.", ex);
                }

                if (requestPayload.VersionInfo == null)
                {
                    throw GetBadRequestException("Missing Version Info in the request content.");
                }

                responseVersionInfo = new Payload.ResponseVersionInfo(requestPayload.VersionInfo, RunningInfo.ESpaceVersionToken, apiVersion);

                if (responseVersionInfo.HasApiVersionChanged)
                {
                    return(GetScreenServiceResult(new Payload.EmptyDataPayload(), responseVersionInfo, loginEndpoint, heContext));
                }
                else
                {
                    SetCurrentStateFromHeaders();
                    object dataResponsePayload = implementation(heContext, requestPayload.ViewName, requestPayload.ScreenData, requestPayload.InputParameters);
                    return(GetScreenServiceResult(dataResponsePayload, responseVersionInfo, loginEndpoint, heContext));
                }
            } catch (Exception ex) {
                DatabaseAccess.FreeupResources(false);

                var licensingException = ex as LicensingException;
                if (licensingException != null)
                {
                    // Already logged
                    return(GetScreenServiceResult(HttpStatusCode.ServiceUnavailable, GetExceptionPayload(new ApplicationBackendUnavailableException("Application Backend Unavailable"))));
                }


                HttpStatusCode statusCode          = HttpStatusCode.OK;
                var            exposeRestException = ex as ExposeRestException;
                if (exposeRestException != null)
                {
                    statusCode = exposeRestException.StatusCode;
                }

                // Currently for security reasons we log it here and don't let the handler in client side decide. This can generate many "non-errors" to be logged if exceptions are used for flow control
                string errorLogId     = ErrorLog.LogApplicationError(ex, heContext, "");
                var    loggingContext = LoggingHelper.GetLoggingContext();
                if (loggingContext != null)
                {
                    loggingContext.ErrorLogId = errorLogId;
                }

                return(GetScreenServiceResult(statusCode, new Payload.EmptyDataPayload(), responseVersionInfo, GetExceptionPayload(ex), loginEndpoint, heContext));
            }
        }
        // TODO JMR Remove this method and merge it with the above overload when the Obsolete overload is removed
        private IHttpActionResult GetScreenServiceResult(HttpStatusCode status, object dataPayload, Payload.ResponseVersionInfo versionInfo, Payload.ExceptionPayload exception, MobileLoginInfoEndpoint loginEndpoint, LoginInfo loginInfo, Func <string> rolesInfo)
        {
            var responseMessage = new HttpResponseMessage(status);

            string rolesInfoserialized = null;

            if (loginEndpoint != null && loginInfo != null)
            {
                if (rolesInfo != null)
                {
                    rolesInfoserialized = loginInfo.UpdateRolesHashAndCalculateRolesInfo(rolesInfo());
                }
                loginEndpoint.WriteLoginInfoToResponse(loginInfo);
            }

            responseMessage.Content = new ObjectContent(typeof(Payload.ResponsePayload),
                                                        new Payload.ResponsePayload(versionInfo, dataPayload, exception, rolesInfoserialized), JsonMediaTypeFormater);

            RestServiceHttpUtils.AddNoCacheHeaders(responseMessage);

            if (DebuggerHelper.IsRunning)
            {
                if (DebuggerHelper.StopImmediately)
                {
                    RestServiceHttpUtils.AddCustomHeader(responseMessage, StopImmediatelyHeaderName, DebuggerHelper.StopImmediately.ToString());
                }
                else if (DebuggerHelper.RunToBreakpoint != null)
                {
                    RestServiceHttpUtils.AddCustomHeader(responseMessage, RunToBreakpointHeaderName, DebuggerHelper.RunToBreakpoint.ToString(true));
                }
            }

            return(ResponseMessage(responseMessage));
        }
 protected IHttpActionResult GetScreenServiceResult(object dataPayload, Payload.ResponseVersionInfo versionInfo, MobileLoginInfoEndpoint loginEndpoint, HeContext context)
 {
     return(GetScreenServiceResult(HttpStatusCode.OK, dataPayload, versionInfo, /*exception*/ null, loginEndpoint, context));
 }
        protected IHttpActionResult GetScreenServiceResult(HttpStatusCode status, object dataPayload, Payload.ResponseVersionInfo versionInfo, Payload.ExceptionPayload exception, MobileLoginInfoEndpoint loginEndpoint, HeContext context)
        {
            var hasNewRuntimeSession = context != null && context.Session != null && context.Session.HasNewRuntimeSessionStorage;

            return(GetScreenServiceResult(status, dataPayload, versionInfo, exception, loginEndpoint, hasNewRuntimeSession ? context.Session.NewRuntimeLoginInfo : null, hasNewRuntimeSession ? context.Session.GetRolesInfo() : null));
        }
 protected IHttpActionResult GetScreenServiceResult(object dataPayload, Payload.ResponseVersionInfo versionInfo, MobileLoginInfoEndpoint loginEndpoint, LoginInfo loginInfo, Func <string> rolesInfo)
 {
     return(GetScreenServiceResult(HttpStatusCode.OK, dataPayload, versionInfo, /*exception*/ null, loginEndpoint, loginInfo, rolesInfo));
 }