Ejemplo n.º 1
0
        public async Task <TransformalizeResponse <TransformalizeTaskPart> > ValidateParameters(TransformalizeRequest request)
        {
            var response = new TransformalizeResponse <TransformalizeTaskPart>(request.Format)
            {
                ContentItem = await GetByIdOrAliasAsync(request.ContentItemId)
            };

            if (response.ContentItem == null)
            {
                SetupNotFoundResponse(request, response);
                return(response);
            }

            if (request.Secure && !CanAccess(response.ContentItem))
            {
                SetupPermissionsResponse(request, response);
                return(response);
            }

            response.Process = LoadForParameters(response.ContentItem, request.InternalParameters);
            if (response.Process.Status != 200)
            {
                SetupLoadErrorResponse(request, response);
                return(response);
            }

            response.Valid = true;
            return(response);
        }
Ejemplo n.º 2
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()
                       ));
        }
Ejemplo n.º 3
0
        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());
            }
        }
Ejemplo n.º 4
0
        public async Task <TransformalizeResponse <TransformalizeFormPart> > ValidateForm(TransformalizeRequest request)
        {
            var response = new TransformalizeResponse <TransformalizeFormPart>(request.Format)
            {
                ContentItem = await GetByIdOrAliasAsync(request.ContentItemId)
            };

            if (response.ContentItem == null)
            {
                SetupNotFoundResponse(request, response);
                return(response);
            }

            if (request.Secure && !CanAccess(response.ContentItem))
            {
                SetupPermissionsResponse(request, response);
                return(response);
            }

            response.Part = response.ContentItem.As <TransformalizeFormPart>();
            if (response.Part == null)
            {
                SetupWrongTypeResponse(request, response);
                return(response);
            }

            response.Process = LoadForForm(response.ContentItem, request.InternalParameters, request.Format);

            if (response.Process.Connections.Where(c => c.Table != "[default]").Count() != 1)
            {
                SetupCustomErrorResponse(request, response, "Missing form table in connection.");
                return(response);
            }

            if (response.Process.Parameters.Where(p => p.PrimaryKey).Count() != 1)
            {
                SetupCustomErrorResponse(request, response, "One parameter must be desigated as the primary key.");
                return(response);
            }

            if (response.Process.Status != 200)
            {
                SetupLoadErrorResponse(request, response);
                return(response);
            }

            response.Valid = true;
            return(response);
        }
        public void SetupNotFoundResponse <T>(TransformalizeRequest request, TransformalizeResponse <T> response)
        {
            _logger.Warn(() => $"User {request.User} requested missing content item {request.ContentItemId}.");

            response.Process.Status  = 404;
            response.Process.Message = "Not Found";

            if (request.Format == null)
            {
                response.ActionResult = LogResult(response);
            }
            else
            {
                response.ActionResult = ContentResult(request, response);
            }
        }
        public void SetupLoadErrorResponse <T>(TransformalizeRequest request, TransformalizeResponse <T> response)
        {
            // process already has a non 200 status

            _logger.Warn(() => $"User {request.User} received error trying to load {response.ContentItem.DisplayText}.");

            if (request.Format == null)
            {
                response.ActionResult = LogResult(response);
            }
            else
            {
                response.Process.Connections.Clear();
                response.Process.Log.AddRange(_logger.Log);
                response.ActionResult = ContentResult(request, response);
            }
        }
        public void SetupCustomErrorResponse <T>(TransformalizeRequest request, TransformalizeResponse <T> response, string error)
        {
            response.Process.Status  = 500;
            response.Process.Message = error;

            _logger.Warn(() => $"User {request.User} received error trying to load {response.ContentItem.DisplayText}.");

            if (request.Format == null)
            {
                response.ActionResult = LogResult(response);
            }
            else
            {
                response.Process.Connections.Clear();
                response.Process.Log.AddRange(_logger.Log);
                response.ActionResult = ContentResult(request, response);
            }
        }
        public void SetupPermissionsResponse <T>(TransformalizeRequest request, TransformalizeResponse <T> response)
        {
            _logger.Warn(() => $"User {request.User} may not access {response.ContentItem.DisplayText}.");

            response.Process = new Transformalize.Configuration.Process()
            {
                Name = "401", Status = 401, Message = "Unauthorized"
            };

            if (request.Format == null)
            {
                response.ActionResult = LogResult(response);
            }
            else
            {
                response.ActionResult = ContentResult(request, response);
            }
        }
Ejemplo n.º 9
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"]
                       ));
        }
        public void SetupWrongTypeResponse <T>(TransformalizeRequest request, TransformalizeResponse <T> response)
        {
            response.Process.Status  = 422;
            response.Process.Message = Common.InvalidContentTypeMessage;

            _logger.Warn(() => $"User {request.User} requested {response.ContentItem.ContentType} from the report service.");

            if (request.Format == null)
            {
                response.ActionResult = LogResult(response);
            }
            else
            {
                response.Process.Connections.Clear();
                response.ActionResult = new ContentResult {
                    Content = response.Process.Serialize(), ContentType = request.ContentType
                };
            }
        }
Ejemplo n.º 11
0
        public async Task <TransformalizeResponse <ContentPart> > Validate(TransformalizeRequest request)
        {
            var response = new TransformalizeResponse <ContentPart>(request.Format)
            {
                ContentItem = await GetByIdOrAliasAsync(request.ContentItemId)
            };

            if (response.ContentItem == null)
            {
                SetupNotFoundResponse(request, response);
                return(response);
            }

            if (request.Secure && !CanAccess(response.ContentItem))
            {
                SetupPermissionsResponse(request, response);
                return(response);
            }

            // part and process left null

            response.Valid = true;
            return(response);
        }
 public void SetupWrongTypeResponse <T1>(TransformalizeRequest request, TransformalizeResponse <T1> response)
 {
     _arrangementService.SetupWrongTypeResponse(request, response);
 }
 public void SetupLoadErrorResponse <TPart>(TransformalizeRequest request, TransformalizeResponse <TPart> response)
 {
     _arrangementService.SetupLoadErrorResponse(request, response);
 }
 public BulkActionViewModel(TransformalizeResponse <TransformalizeTaskPart> task, Process summary)
 {
     Summary = summary;
     Task    = task;
 }
Ejemplo n.º 15
0
 public ExecuteViewModel()
 {
     TransformalizeResponse = new TransformalizeResponse();
 }
Ejemplo n.º 16
0
 public ExecuteViewModel() {
     TransformalizeResponse = new TransformalizeResponse();
 }
Ejemplo n.º 17
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());
            }
        }
Ejemplo n.º 18
0
        public async Task <TransformalizeResponse <TransformalizeReportPart> > Validate(TransformalizeRequest request)
        {
            var response = new TransformalizeResponse <TransformalizeReportPart>(request.Format)
            {
                ContentItem = await GetByIdOrAliasAsync(request.ContentItemId)
            };

            if (response.ContentItem == null)
            {
                SetupNotFoundResponse(request, response);
                return(response);
            }

            if (request.Secure && !CanAccess(response.ContentItem))
            {
                SetupPermissionsResponse(request, response);
                return(response);
            }

            response.Part = response.ContentItem.As <TransformalizeReportPart>();
            if (response.Part == null)
            {
                SetupWrongTypeResponse(request, response);
                return(response);
            }

            switch (request.Mode)
            {
            case "calendar":
                response.Process = LoadForCalendar(response.ContentItem);
                break;

            case "stream-calendar":
                response.Process = LoadForCalendarStream(response.ContentItem);
                break;

            case "map":
                response.Process = LoadForMap(response.ContentItem);
                break;

            case "stream-map":
                response.Process = LoadForMapStream(response.ContentItem);
                break;

            case "stream":
                response.Process = LoadForStream(response.ContentItem);
                break;

            default:
                response.Process = LoadForReport(response.ContentItem, request.Format);
                break;
            }

            if (response.Process.Status != 200)
            {
                SetupLoadErrorResponse(request, response);
                return(response);
            }

            if (request.ValidateParameters && !response.Process.Parameters.All(p => p.Valid))
            {
                SetupInvalidParametersResponse(request, response);
                return(response);
            }

            response.Valid = true;
            return(response);
        }
 public void SetupPermissionsResponse <TPart>(TransformalizeRequest request, TransformalizeResponse <TPart> response)
 {
     _arrangementService.SetupPermissionsResponse(request, response);
 }
 public void SetupCustomErrorResponse <TPart>(TransformalizeRequest request, TransformalizeResponse <TPart> response, string error)
 {
     _arrangementService.SetupCustomErrorResponse(request, response, error);
 }
 public ContentResult ContentResult <T>(TransformalizeRequest request, TransformalizeResponse <T> response)
 {
     return(new ContentResult {
         Content = response.Process.Serialize(), ContentType = request.ContentType
     });
 }
 public void SetupInvalidParametersResponse <TPart>(TransformalizeRequest request, TransformalizeResponse <TPart> response)
 {
     _arrangementService.SetupInvalidParametersResponse(request, response);
 }
        public void SetupInvalidParametersResponse <T>(TransformalizeRequest request, TransformalizeResponse <T> response)
        {
            response.Process.Status  = 422;
            response.Process.Message = Common.InvalidParametersMessage;

            if (request.Format == null)
            {
                foreach (var parameter in response.Process.Parameters.Where(p => !p.Valid))
                {
                    foreach (var message in parameter.Message.Split('|'))
                    {
                        _logger.Warn(() => message.Replace("{", "{{").Replace("}", "}}"));
                    }
                }
                response.ActionResult = LogResult(response);
            }
            else
            {
                response.Process.Connections.Clear();
                response.Process.Log.AddRange(_logger.Log);
                response.ActionResult = ContentResult(request, response);
            }
        }
 public ViewResult LogResult <T>(TransformalizeResponse <T> response)
 {
     return(View("Log", new LogViewModel(_logger.Log, response.Process, response.ContentItem)));
 }
        private static TransformalizeResponse <TransformalizeTaskPart> TransferRequiredParameters(BulkActionReviewRequest request, TransformalizeResponse <TransformalizeTaskPart> response)
        {
            var existing = new HashSet <string>(response.Process.Parameters.Select(p => p.Name));

            if (!existing.Contains(Common.TaskReferrer))
            {
                response.Process.Parameters.Add(new Parameter {
                    Name = Common.TaskReferrer, Value = request.TaskReferrer
                });
            }

            if (!existing.Contains(Common.TaskContentItemId))
            {
                response.Process.Parameters.Add(new Parameter {
                    Name = Common.TaskContentItemId, Value = request.TaskContentItemId
                });
            }

            if (!existing.Contains(Common.ReportContentItemId))
            {
                response.Process.Parameters.Add(new Parameter {
                    Name = Common.ReportContentItemId, Value = request.ReportContentItemId
                });
            }

            return(response);
        }