コード例 #1
0
ファイル: LoaderWorker.cs プロジェクト: aleDsz/AppServer
        /// <summary>
        /// Process endpoint request and retrieve a Rest Response properly
        /// </summary>
        /// <param name="path">Request path</param>
        /// <param name="method">HTTP method</param>
        /// <param name="request">Rest Request</param>
        /// <returns>Rest Response</returns>
        public RestResponse Process(string path, string method, RestRequest request)
        {
            try {
                return(_restProcessor.Process(path, method, request));
            } catch (Exception ex) {
                var exceptionName = ex.InnerException switch {
                    Exception iex => iex.GetType().Name,
                    null => ex.GetType().Name
                };

                IRestExceptionHandler handler = _restProcessor.GetExceptionHandler(exceptionName);

                if (handler != null)
                {
                    return(handler.HandleException(ex.InnerException));
                }

                Logger.Error(ex.Message);

                return(new RestResponse(
                           "Internal Server Error",
                           "plain/text",
                           System.Net.HttpStatusCode.InternalServerError
                           ));
            }
        }
コード例 #2
0
        /// <summary>
        /// Process endpoint request and retrieve a Rest Response properly
        /// </summary>
        /// <param name="path">Request path</param>
        /// <param name="method">HTTP method</param>
        /// <param name="request">Rest Request</param>
        /// <returns>Rest Response</returns>
        public RestResponse Process(string path, string method, RestRequest request)
        {
            try {
                return(_restProcessor.Process(path, method, request));
            } catch (Exception ex) {
                var exceptionName             = ex.InnerException.GetType().Name;
                IRestExceptionHandler handler = _restProcessor.GetExceptionHandler(exceptionName);

                if (handler != null)
                {
                    return(handler.HandleException(ex.InnerException));
                }

                throw ex;
            }
        }
コード例 #3
0
        RestResult ProcessHttpCalls(HttpListenerRequest request)
        {
            string[]    ePath = request.Url.AbsolutePath.Split(new char[] { '/' }, 2, StringSplitOptions.RemoveEmptyEntries);
            RestRequest req   = new RestRequest(request);

            if (ePath.Length == 0)
            {
                return(new RestResult(new ErrorObject {
                    ErrorCode = ErrorCodes.NotFound,
                    Message = "No application specified",
                    ErrorField = "url"
                }.ToJSON(), MimeTypes.JSON, HttpStatusCode.NotFound));
            }
            string path   = ePath.Length > 1 ? "/" + ePath[1] : "/";
            string method = request.HttpMethod;
            string app    = ePath[0];

            if (app != "remoteSigner")
            {
                /*return new RestResult(new ErrorObject {
                 *  ErrorCode = ErrorCodes.NotFound,
                 *  Message = $"Application {app} not found",
                 *  ErrorField = "url"
                 * }.ToJSON(), MimeTypes.JSON, HttpStatusCode.NotFound);*/
                // Bypass
                path = "/" + ePath[0] + path;
            }

            Logger.Debug("HTTP Server", $"Processing HTTP Call for App {app}: {method} {path}");

            if (restProcessor.ContainsEndPoint(path, method))
            {
                try {
                    return(restProcessor.CallEndPoint(path, method, req));
                } catch (Exception e) {
                    string exceptionName          = e.InnerException.GetType().Name;
                    string baseName               = e.InnerException.GetType().BaseType.Name;
                    IRestExceptionHandler handler = restProcessor.GetExceptionHandler(exceptionName) ?? restProcessor.GetExceptionHandler(baseName);
                    if (handler != null)
                    {
                        return(handler.HandleException(e.InnerException));
                    }
                    RestResult result = new RestResult();
                    string     exceptionMessage;
                    if (e.InnerException != null)   // In the rest exceptions the real exception will be at InnerException.
                    {
                        Logger.Debug("HTTP Server", $"Exception when calling application {app} in endpoint {method} {path} \r\n {e.InnerException}");
                        exceptionMessage = e.InnerException.ToString();
                    }
                    else     // But if we got a internal exception at AppServer, it will be in the root.
                    {
                        Logger.Debug("HTTP Server", $"Exception when calling application {app} in endpoint {method} {path} \r\n {e}");
                        exceptionMessage = e.ToString();
                    }
                    result.StatusCode  = HttpStatusCode.InternalServerError;
                    result.ContentType = "text/plain";
                    result.Result      = Encoding.UTF8.GetBytes(exceptionMessage);
                    return(result);
                }
            }
            return(new RestResult(new ErrorObject {
                ErrorCode = ErrorCodes.NotFound,
                Message = "Endpoint not found",
                ErrorField = "url"
            }.ToJSON(), MimeTypes.JSON, HttpStatusCode.NotFound));
        }