Example #1
0
        /// <summary>
        /// The get structure.
        /// </summary>
        /// <param name="structure">
        /// The structure.
        /// </param>
        /// <param name="agencyId">
        /// The agency id.
        /// </param>
        /// <param name="resourceId">
        /// The resource id.
        /// </param>
        /// <param name="version">
        /// The version.
        /// </param>
        /// <returns>
        /// The <see cref="Message"/>.
        /// </returns>
        /// <exception cref="WebFaultException{T}">
        /// Bad Request.
        /// </exception>
        public Message GetStructure(string structure, string agencyId, string resourceId, string version)
        {
            WebOperationContext ctx = WebOperationContext.Current;

            if (!IsStructureValid(structure))
            {
                throw new WebFaultException <string>("Invalid structure: " + structure, HttpStatusCode.BadRequest);
            }

            try
            {
                return(this.ProcessRequest(structure, agencyId, resourceId, version, ctx));
            }
            catch (Exception e)
            {
                Logger.Error(e);
                throw _builder.Build(e);
            }
        }
Example #2
0
        /// <summary>
        /// Streams the structural metadata.
        /// </summary>
        /// <param name="schemaVersion">The schema version.</param>
        /// <param name="stream">The stream.</param>
        /// <param name="streamController">The stream controller.</param>
        /// <param name="encoding">The response encoding.</param>
        public static void StreamXml(SdmxSchema schemaVersion, Stream stream, IStreamController <XmlWriter> streamController, Encoding encoding)
        {
            try
            {
                if (schemaVersion.IsXmlFormat())
                {
                    using (var writer = XmlWriter.Create(stream, new XmlWriterSettings {
                        Indent = true, Encoding = encoding
                    }))
                    {
                        var actions = new Queue <Action>();
                        streamController.StreamTo(writer, actions);
                        writer.Flush();
                    }
                }
                else
                {
                    throw new SdmxNotImplementedException(string.Format(CultureInfo.InvariantCulture, "Not supported {0}", schemaVersion.ToEnglishString()));
                }
            }
            catch (SdmxResponseSizeExceedsLimitException e)
            {
                // error is on payload.
                _logger.Warn(e.Message, e);
            }
            catch (SdmxResponseTooLargeException e)
            {
                // error is on payload.
                _logger.Warn(e.Message, e);
            }
            catch (Exception e)
            {
                _logger.Error(e.Message, e);

                // HACK.
                // The following is needed because for some reason WebOperationContext.CreateStreamResponse closes the connection and does not return a
                // status code and/or error message to the user.
                var normalizedException = _builder.Build(e);

                //// Setting the WebOperationContext does not seem to work....
                //// WebOperationContext.Current.OutgoingResponse.StatusCode = normalizedException.StatusCode;
                try
                {
                    HttpContext.Current.Response.StatusCode = (int)normalizedException.StatusCode;
                }
                catch (HttpException ex)
                {
                    _logger.Error(e.Message, ex);

                    // Not much we can do. We already sent the header Throw the original exception.
                    throw e;
                }

                HttpContext.Current.Response.ContentType = "text/html;";
                using (var streamWriter = new StreamWriter(stream))
                {
                    streamWriter.WriteLine("{0} - {1}", normalizedException.Message, normalizedException.Detail);
                    streamWriter.Flush();
                }
            }
        }