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();
            }

        }
Пример #2
0
        public ActionResult MetaData(int id) {

            Response.AddHeader("Access-Control-Allow-Origin", "*");
            ConfigurationPart part;
            ApiRequest request;

            var query = _transformalize.GetQuery();

            foreach (var rejection in _apiService.Rejections(id, out request, out part)) {
                return rejection.ContentResult(
                    query["format"],
                    query["flavor"]
                );
            }

            request.RequestType = ApiRequestType.MetaData;
            var transformalizeRequest = new TransformalizeRequest(part, query, null, Logger);
            var logger = new TransformalizeLogger(transformalizeRequest.Part.Title(), part.LogLevel, Logger, OrchardVersion, _moduleVersion);

            var errors = transformalizeRequest.Root.Errors();
            if (errors.Any()) {
                var bad = new TransformalizeResponse();
                request.Status = 501;
                request.Message = "Configuration Problem" + errors.Length.Plural();
                bad.Log.AddRange(errors.Select(p => new[] { DateTime.Now.ToString(CultureInfo.InvariantCulture), "error", ".", ".", p }));
                return new ApiResponse(request, "<tfl></tfl>", bad).ContentResult(
                    query["format"],
                    query["flavor"]
                );
            }

            var metaData = new MetaDataWriter(ProcessFactory.CreateSingle(transformalizeRequest.Root.Processes[0], logger, new Options { Mode = "metadata" })).Write();
            return new ApiResponse(request, metaData).ContentResult(
                query["format"],
                query["flavor"]
            );
        }
Пример #3
0
        public ActionResult Execute(int id) {

            Response.AddHeader("Access-Control-Allow-Origin", "*");
            ConfigurationPart part;
            ApiRequest request;

            var query = _transformalize.GetQuery();

            foreach (var rejection in _apiService.Rejections(id, out request, out part)) {
                return rejection.ContentResult(
                    query["format"],
                    query["flavor"]
                );
            }

            request.RequestType = ApiRequestType.Execute;
            _transformalize.InitializeFiles(part, query);

            // ready
            var processes = new TransformalizeResponse();
            var transformalizeRequest = new TransformalizeRequest(part, query, null, Logger);

            try {
                processes = _transformalize.Run(transformalizeRequest);
            } catch (Exception ex) {
                request.Status = 500;
                request.Message = ex.Message + " " + WebUtility.HtmlEncode(ex.StackTrace);
            }

            return new ApiResponse(request, transformalizeRequest.Configuration, processes).ContentResult(
                transformalizeRequest.Query["format"].ToString(),
                transformalizeRequest.Query["flavor"].ToString()
            );
        }
Пример #4
0
 public ApiResponse(ApiRequest request, string configuration, TransformalizeResponse response) {
     _response = response;
     _request = request;
     _configuration = configuration;
 }
Пример #5
0
 public ApiResponse(ApiRequest request, string configuration, TransformalizeResponse response)
 {
     _response      = response;
     _request       = request;
     _configuration = configuration;
 }
Пример #6
0
        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();
            }
        }