Exemplo n.º 1
0
        /// <summary>
        /// Serializes and sends table data back to the client
        /// </summary>
        /// <param name="context"></param>
        private void SendTableData(HttpContext context, string language, string db, string[] tablePath, ResponseBucket cacheResponse)
        {
            //// TODO: Limit data size?
            //string data = "";
            //using (var stream = new StreamReader(context.Request.InputStream))
            //{
            //    data = stream.ReadToEnd();
            //}

            var tableQuery = JsonHelper.Deserialize <TableQuery>(cacheResponse.PostData) as TableQuery;

            if (tableQuery.Response == null || tableQuery.Response.Format == null)
            {
                tableQuery.Response = new QueryResponse()
                {
                    Format = _defaultResponseFormat
                };
            }

            // Initialize the builder
            PCAxis.Paxiom.IPXModelBuilder builder = GetPXBuilder(language, db, tablePath);
            builder.DoNotApplyCurrentValueSet = true;  // DoNotApplyCurrentValueSet means the "client that made the request" is an api(, not a GUI) so that
                                                       // CNMM2.4 property DefaultInGUI (for Valueset/grouping) should not be used
            builder.SetPreferredLanguage(language);
            builder.BuildForSelection();

            // Process selections
            var selections = PCAxisRepository.BuildSelections(builder, tableQuery);

            // Check that the number of selected cells do not exceed the limit
            long cellCount = 1;

            foreach (var sel in selections)
            {
                if (sel.ValueCodes.Count > 0)
                {
                    cellCount *= sel.ValueCodes.Count;
                }
            }
            if (cellCount > Settings.Current.FetchCellLimit)
            {
                Write403Response(context);
                return;
            }

            builder.BuildForPresentation(selections.ToArray());

            // Output handling starts here
            context.Response.Clear();

            IWebSerializer serializer;

            switch (tableQuery.Response.Format != null ? tableQuery.Response.Format.ToLower() : null)
            {
            case null:
            case "px":
                serializer = new PxSerializer();
                break;

            case "csv":
                serializer = new CsvSerializer();
                break;

            case "json":
                serializer = new JsonSerializer();
                break;

            case "json-stat":
                serializer = new JsonStatSeriaizer();
                break;

            case "json-stat2":
                serializer = new JsonStat2Seriaizer();
                break;

            case "xlsx":
                serializer = new XlsxSerializer();
                break;

            //case "png":
            //    int? width = tableQuery.Response.GetParamInt("width");
            //    int? height = tableQuery.Response.GetParamInt("height");
            //    string encoding = tableQuery.Response.GetParamString("encoding");
            //    serializer = new ChartSerializer(width, height, encoding);
            //    break;
            case "sdmx":
                serializer = new SdmxDataSerializer();
                break;

            default:
                throw new NotImplementedException("Serialization for " + tableQuery.Response.Format + " is not implemented");
            }
            //serializer.Serialize(builder.Model, context.Response);
            serializer.Serialize(builder.Model, cacheResponse);
            //context.Response.AddHeader("Content-Type", cacheResponse.ContentType);
            //context.Response.OutputStream.Write(cacheResponse.ResponseData, 0, cacheResponse.ResponseData.Length);
            context.Send(cacheResponse, true);
            //Logs usage
            _usageLogger.Info(String.Format("url={0}, type=data, caller={3}, cached=false, format={1}, matrix-size={2}", context.Request.RawUrl, tableQuery.Response.Format, builder.Model.Data.MatrixSize, context.Request.UserHostAddress));
        }
Exemplo n.º 2
0
        public IActionResult Post(string language, string db, string path, [FromBody] TableQuery tableQuery)
        {
            if (!_dbSource.Languages.Contains(language))
            {
                //TODO log invalid language
                return(NotFound());
            }
            //Check database validity
            if (db != _dbSource.DatabaseId)
            {
                //TODO log invalid database
                return(NotFound());
            }
            //Check that it is a table
            var item = _dbSource.GetMenu(db, language, path);

            if (!(item is TableLink))
            {
                //TODO log not a table
                return(NotFound());
            }

            var builder = _dbSource.GetBuilder(language, path);

            builder.DoNotApplyCurrentValueSet = true;  // DoNotApplyCurrentValueSet means the "client that made the request" is an api(, not a GUI) so that
                                                       // CNMM2.4 property DefaultInGUI (for Valueset/grouping) should not be used
            builder.SetPreferredLanguage(language);
            builder.BuildForSelection();

            // Process selections
            var selections = builder.BuildSelections(tableQuery);

            if (selections == null)
            {
                //TODO logg parameter error
                return(NotFound());
            }

            // Check that the number of selected cells do not exceed the limit
            long cellCount = 1;

            foreach (var sel in selections)
            {
                if (sel.ValueCodes.Count > 0)
                {
                    cellCount *= sel.ValueCodes.Count;
                }
            }

            //if (cellCount > Settings.Current.FetchCellLimit)
            //{
            //    Write403Response(context);
            //    return;
            //}

            builder.BuildForPresentation(selections.ToArray());

            IWebSerializer serializer;

            switch (tableQuery.Response.Format != null ? tableQuery.Response.Format.ToLower() : null)
            {
            case null:
            case "px":
                serializer = new PxSerializer();
                break;

            //case "csv":
            //    serializer = new CsvSerializer();
            //    break;
            //case "json":
            //    serializer = new JsonSerializer();
            //    break;
            //case "json-stat":
            //    serializer = new JsonStatSeriaizer();
            //    break;
            //case "json-stat2":
            //    serializer = new JsonStat2Seriaizer();
            //    break;
            //case "xlsx":
            //    serializer = new XlsxSerializer();
            //    break;
            //case "sdmx":
            //    serializer = new SdmxDataSerializer();
            //    break;
            default:
                throw new NotImplementedException("Serialization for " + tableQuery.Response.Format + " is not implemented");
            }
            var data = serializer.Serialize(builder.Model);

            serializer.Serialize(builder.Model);
            Response.ContentType = serializer.ContentType;
            Response.Body.WriteAsync(data, 0, data.Length);
            return(Ok());

            //return Ok("Welcome to PxWeb API v1");
        }