Esempio n. 1
0
        public List<ApiResponse> Rejections(int id, out ApiRequest request, out ConfigurationPart part) {

            var context = _orchardServices.WorkContext.HttpContext;
            request = new ApiRequest(ApiRequestType.Configuration) { Stopwatch = _stopwatch };
            var response = new List<ApiResponse>();

            if (id == 0) {
                Logger.Error("No Configuration for id 0.  Requested by {1} at {2}.", context.User.Identity.IsAuthenticated ? context.User.Identity.Name : "Anonymous", context.Request.UserHostAddress);
                response.Add(NotFound(request));
                part = null;
                return response;
            }

            part = _orchardServices.ContentManager.Get(id).As<ConfigurationPart>();

            if (part == null) {
                Logger.Error("No Configuration for id {0}.  Requested by {1} at {2}.", id, context.User.Identity.IsAuthenticated ? context.User.Identity.Name : "Anonymous", context.Request.UserHostAddress);
                response.Add(NotFound(request));
                return response;
            }

            if (context.Request.IsLocal || part.IsInAllowedRange(context.Request.UserHostAddress)) {
                return response;
            }

            if (_orchardServices.Authorizer.Authorize(global::Orchard.Core.Contents.Permissions.ViewContent, part)) {
                return response;
            }

            Logger.Error("Not authorized to run {0}.  Requested by {1} at {2}.", part.As<TitlePart>().Title, context.User.Identity.Name, context.Request.UserHostAddress);
            response.Add(Unathorized(request));
            return response;
        }
        public static string GetContent(ApiRequest request, string configuration, TransformalizeResponse response) {

            var content = string.Empty;
            var results = string.Empty;
            var builder = new StringBuilder();
            var processes = string.Empty;

            switch (request.RequestType) {
                case ApiRequestType.MetaData:
                    builder.Append(configuration);
                    builder.InsertFormat(builder.LastIndexOf('<'), RESPONSE_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, string.Empty, LogsToXml(response.Log), content);
                    return builder.ToString();

                case ApiRequestType.Configuration:
                    builder.Append(configuration);
                    builder.InsertFormat(builder.LastIndexOf('<'), MINIMAL_RESPONSE_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds);
                    return builder.ToString();

                case ApiRequestType.Enqueue:
                    builder.AppendFormat(XML_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, string.Empty, processes, results, LogsToXml(response.Log), content);
                    return builder.ToString();

                case ApiRequestType.Execute:

                    switch (request.Flavor) {
                        case "attributes":
                            processes = SecureConfiguration(configuration);
                            results = new XmlResultsToAttributesHandler().Handle(response.Processes);
                            break;
                        case "table":
                            content = new XmlResultsToHtmlTable().Handle(response.Processes);
                            break;
                        default:
                            processes = SecureConfiguration(configuration);
                            results = new XmlResultsToDictionaryHandler().Handle(response.Processes);
                            break;
                    }
                    builder.AppendFormat(XML_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, string.Empty, processes, results, LogsToXml(response.Log), content);
                    return builder.ToString();

                default:
                    if (request.Status == 200) {
                        request.Status = 400;
                        request.Message = "Bad Request";
                    }
                    builder.AppendFormat(XML_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, string.Empty, processes, results, LogsToXml(response.Log), content);
                    return builder.ToString();
            }

        }
 public static string GetErrorContent(string format, ApiRequest request) {
     var empty = format.StartsWith("<") ? string.Empty : "[]";
     return string.Format(
         format,
         request.RequestType.ToString().ToLower(),
         request.Status,
         request.Message,
         request.Stopwatch.ElapsedMilliseconds,
         empty,
         empty,
         empty,
         empty
         );
 }
        protected ApiResponse Run(
            ApiRequest request,
            TransformalizeRequest transformalizeRequest
        ) {

            var errorNumber = 500;
            try {
                errorNumber++;
                var tflResponse = _transformalize.Run(transformalizeRequest);

                errorNumber++;
                return new ApiResponse(
                    request,
                    transformalizeRequest.Configuration,
                    tflResponse
                );
            } catch (Exception ex) {
                request.Status = errorNumber;
                request.Message = ex.Message;
                return new ApiResponse(
                    request,
                    transformalizeRequest.Configuration,
                    new TransformalizeResponse()
                );
            }
        }
Esempio n. 5
0
 public ApiResponse Unathorized(ApiRequest request) {
     request.Status = 401;
     request.Message = "Unauthorized";
     return new ApiResponse(request, "<transformalize><processes></processes></transformalize>");
 }
Esempio n. 6
0
 public ApiResponse NotFound(ApiRequest request) {
     request.Status = 404;
     request.Message = "Not Found";
     return new ApiResponse(request, "<transformalize><processes></processes></transformalize>");
 }
Esempio n. 7
0
 public ApiResponse(ApiRequest request, string configuration, TransformalizeResponse response) {
     _response = response;
     _request = request;
     _configuration = configuration;
 }
Esempio n. 8
0
 public ApiResponse(ApiRequest request, string configuration)
 {
     _request = request;
     _configuration = configuration;
 }
        public static string GetContent(ApiRequest request, string configuration, TransformalizeResponse response) {

            var builder = new StringBuilder();
            var processes = "[]";
            var environments = "[]";
            var results = "[]";
            const string content = "{}";

            var tfl = new TflRoot(configuration);
            var settings = new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            switch (request.RequestType) {

                case ApiRequestType.MetaData:
                    processes = JsonConvert.SerializeObject(tfl.Processes, Formatting.None, settings);
                    builder.AppendFormat(JSON_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, environments, processes, results, LogsToJson(response.Log), content);
                    return builder.ToString();

                case ApiRequestType.Configuration:
                    processes = JsonConvert.SerializeObject(tfl.Processes, Formatting.None, settings);
                    environments = JsonConvert.SerializeObject(tfl.Environments, Formatting.None, settings);
                    builder.AppendFormat(JSON_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, environments, processes, results, LogsToJson(response.Log), content);
                    return builder.ToString();

                case ApiRequestType.Enqueue:
                    builder.AppendFormat(JSON_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, environments, processes, results, LogsToJson(response.Log), content);
                    return builder.ToString();

                case ApiRequestType.Execute:
                    foreach (var process in tfl.Processes) {
                        process.Connections = new List<TflConnection>();
                    }
                    processes = JsonConvert.SerializeObject(tfl.Processes, Formatting.None, settings);
                    switch (request.Flavor) {
                        case "arrays":
                            results = new JsonResultsToArrayHandler().Handle(response.Processes);
                            break;
                        case "array":
                            goto case "arrays";
                        case "dictionaries":
                            results = new JsonResultsToDictionaryHandler().Handle(response.Processes);
                            break;
                        case "dictionary":
                            goto case "dictionaries";
                        default:
                            results = new JsonResultsToObjectHandler().Handle(response.Processes);
                            break;
                    }
                    builder.AppendFormat(JSON_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, environments, processes, results, LogsToJson(response.Log), content);
                    return builder.ToString();

                default:
                    if (request.Status == 200) {
                        request.Status = 400;
                        request.Message = "Bad Request";
                    }
                    builder.AppendFormat(JSON_TEMPLATE, request.RequestType, request.Status, request.Message, request.Stopwatch.ElapsedMilliseconds, environments, processes, results, LogsToJson(response.Log), content);
                    return builder.ToString();
            }
        }