Beispiel #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
            List <PCAxis.Paxiom.Selection> selections;

            try
            {
                selections = PCAxisRepository.BuildSelections(builder, tableQuery);
            }

            catch (Exception ex)
            {
                var message = ex is HttpException ? ex.Message : "Parameter error";

                cacheResponse.ResponseData = context.Response.ContentEncoding.GetBytes(Error(message, false));
                context.SendJSONError(cacheResponse, 400, true);
                // Logs usage
                _usageLogger.Info(String.Format("url={0}, type=error, caller={1}, cached=false, message=Parameter error", context.Request.RawUrl, context.Request.UserHostAddress));
                return;
            }

            // 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 = WebSerializerSwitch.GetSerializer(tableQuery.Response.Format);

            //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));
        }
Beispiel #2
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));
        }