Esempio n. 1
0
        /// <summary>
        /// Gets the outgoing response.
        /// </summary>
        /// <param name="operationContext">The operation context.</param>
        /// <param name="response">The response.</param>
        /// <param name="responseStream">The response stream.</param>
        /// <returns>Outgoing response.</returns>
        private static Message GetOutgoingResponse(WebOperationContext operationContext, HttpResponseMessage response, Stream responseStream)
        {
            Message responseMessage;

            if (response.Content == null)
            {
                responseMessage = operationContext.CreateTextResponse(string.Empty);
            }
            else
            {
                responseMessage = response.Content.Headers.ContentType == null?
                                  operationContext.CreateStreamResponse(responseStream, "text/plain; charset=us-ascii") :
                                      operationContext.CreateStreamResponse(responseStream, response.Content.Headers.ContentType.ToString());
            }

            operationContext.OutgoingResponse.StatusCode = response.StatusCode;
            foreach (var header in response.Headers)
            {
                header.Value.ToList().ForEach(headerVal =>
                                              operationContext.OutgoingResponse.Headers.Add(header.Key, headerVal));
            }

            foreach (var header in response.Content.Headers)
            {
                header.Value.ToList().ForEach(headerVal =>
                                              operationContext.OutgoingResponse.Headers.Add(header.Key, headerVal));
            }

            return(responseMessage);
        }
        static Message ReturnFile(WebOperationContext ctx, FileInfo fInfo, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            ctx.OutgoingResponse.StatusCode = statusCode;
            ctx.OutgoingResponse.ETag       = GetETag(fInfo);

            return(ctx.CreateStreamResponse(File.OpenRead(fInfo.FullName), MimeMapping.GetMimeMapping(fInfo.Name)));
        }
Esempio n. 3
0
        public override Message CreateResponse(object response)
        {
            var stream = response as Stream;

            if (stream == null)
            {
                var serializer = new DataContractJsonSerializer(response.GetType());
                return(_webOperationContext.CreateJsonResponse(response, serializer));
            }
            return(_webOperationContext.CreateStreamResponse(stream, GetResponseContentType()));
        }
Esempio n. 4
0
        /// <summary>
        /// Processes the request.
        /// </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>
        /// <param name="ctx">The current <see cref="WebOperationContext"/>.</param>
        /// <returns>
        /// The <see cref="Message" />.
        /// </returns>
        /// <exception cref="System.Web.HttpBrowserCapabilitiesBase"></exception>
        /// <exception cref="WebFaultException{String}">
        /// Cannot serve content type
        /// </exception>
        /// <exception cref="WebFaultException{String}">Cannot serve content type</exception>
        private Message ProcessRequest(string structure, string agencyId, string resourceId, string version, WebOperationContext ctx)
        {
            Match match = Regex.Match(resourceId, @"[A-Za-z0-9\-]+$", RegexOptions.IgnoreCase);

            if (!match.Success)
            {
                throw new WebFaultException(HttpStatusCode.BadRequest);
            }

            var defaultMediaType = StructureMediaType.GetFromEnum(StructureMediaEnumType.Structure).MediaTypeName;
            var requestAccept    = ctx.IncomingRequest.Accept;

            Logger.Info("Got request Content type= " + requestAccept);

            IList <ContentType>      acceptHeaderElements = ctx.IncomingRequest.GetAcceptHeaderElements();
            Func <ContentType, bool> predicate            = type => StructureMediaType.GetTypeFromName(type.MediaType) != null;
            var contentType = RestUtils.GetContentType(ctx, predicate, defaultMediaType);

            string requestedVersion = HeaderUtils.GetVersionFromAccept(acceptHeaderElements, contentType.MediaType);

            var selectedStructureMediaType   = StructureMediaType.GetTypeFromName(contentType.MediaType);
            var selectedMediaTypeWithVersion = selectedStructureMediaType.GetMediaTypeVersion(requestedVersion);

            if (selectedMediaTypeWithVersion == null)
            {
                Logger.Error("Cannot serve content type: " + requestAccept);
                throw new WebFaultException <string>("Cannot serve content type: " + requestAccept, HttpStatusCode.NotAcceptable);
            }

            Logger.Debug("Select mediatype with version if required: " + selectedMediaTypeWithVersion);

            SdmxSchema schemaVersion = RestUtils.GetVersionFromMediaType(selectedMediaTypeWithVersion);

            var context    = HttpContext.Current;
            var controller = this._controllerBuilder.BuildQueryStructureRest(schemaVersion, context.User as DataflowPrincipal);

            Logger.Info("Selected representation info for the controller: format =" + "TODO" + " , smdx_schema=" + version);

            IRestStructureQuery query = BuildRestQueryBean(structure, agencyId, resourceId, version, ctx.IncomingRequest.UriTemplateMatch.QueryParameters);
            var streamController      = controller.ParseRequest(query);
            var charSetEncoding       = RestUtils.GetCharSetEncoding(contentType);

            var responseContentType = RestUtils.GetContentType(contentType, selectedMediaTypeWithVersion);

            selectedMediaTypeWithVersion.CharSet = charSetEncoding.WebName;
            return(ctx.CreateStreamResponse(
                       stream => RestUtils.StreamXml(schemaVersion, stream, streamController, charSetEncoding), responseContentType));
        }
Esempio n. 5
0
        /// <summary>
        /// The process request.
        /// </summary>
        /// <param name="flowRef">
        /// The flow ref.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="providerRef">
        /// The provider ref.
        /// </param>
        /// <param name="ctx">
        /// The ctx.
        /// </param>
        /// <returns>
        /// The <see cref="Message"/>.
        /// </returns>
        /// <exception cref="WebFaultException{T}">
        /// </exception>
        private Message ProcessRequest(string flowRef, string key, string providerRef, WebOperationContext ctx)
        {
            var requestAccept = ctx.IncomingRequest.Accept;

            Logger.Info("Got request Content type= " + requestAccept);

            IList <ContentType> acceptHeaderElements = ctx.IncomingRequest.GetAcceptHeaderElements();

            var defaultMediaType = DataMediaType.GetFromEnum(DataMediaEnumType.GenericData).MediaType;
            var contentType      = RestUtils.GetContentType(ctx, h => DataMediaType.GetTypeFromName(h.MediaType) != null, defaultMediaType);

            string requestedVersion = HeaderUtils.GetVersionFromAccept(acceptHeaderElements, contentType.MediaType);

            var datamediaType = DataMediaType.GetTypeFromName(contentType.MediaType);
            var selectedMediaTypeWithVersion = datamediaType.GetMediaTypeVersion(requestedVersion);

            if (selectedMediaTypeWithVersion == null)
            {
                Logger.Error("Cannot serve content type: " + requestAccept);
                throw new WebFaultException <string>("Cannot serve content type: " + requestAccept, HttpStatusCode.NotAcceptable);
            }

            Logger.Debug("Select mediatype with version if required: " + selectedMediaTypeWithVersion);

            BaseDataFormat format  = DataMediaType.GetTypeFromName(selectedMediaTypeWithVersion.MediaType).Format;
            SdmxSchema     version = GetVersionFromMediaType(selectedMediaTypeWithVersion, format);

            Logger.Info("Selected representation info for the controller: format =" + format + " , smdx_schema=" + version);
            IRestDataQuery query   = this.BuildQueryBean(flowRef, key, providerRef, ctx.IncomingRequest.UriTemplateMatch.QueryParameters);
            HttpContext    context = HttpContext.Current;

            var controller          = this._controllerBuilder.BuildDataRest(context.User as DataflowPrincipal, format, version);
            var streamController    = controller.ParseRequest(query);
            var charSetEncoding     = RestUtils.GetCharSetEncoding(contentType);
            var responseContentType = RestUtils.GetContentType(contentType, selectedMediaTypeWithVersion);

            selectedMediaTypeWithVersion.CharSet = charSetEncoding.WebName;
            return(ctx.CreateStreamResponse(stream => RestUtils.StreamXml(version, stream, streamController, charSetEncoding), responseContentType));
        }
        /// <summary>
        /// Processes the request.
        /// </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>
        /// <param name="ctx">The current <see cref="WebOperationContext"/>.</param>
        /// <returns>
        /// The <see cref="Message" />.
        /// </returns>
        /// <exception cref="System.Web.HttpBrowserCapabilitiesBase"></exception>
        /// <exception cref="WebFaultException{String}">
        /// Cannot serve content type
        /// </exception>
        /// <exception cref="WebFaultException{String}">Cannot serve content type</exception>
        private Message ProcessRequest(string structure, string agencyId, string resourceId, string version, WebOperationContext ctx)
        {
            Match match = Regex.Match(resourceId, @"[A-Za-z0-9\-]+$", RegexOptions.IgnoreCase);
            if (!match.Success)
            {
                throw new WebFaultException(HttpStatusCode.BadRequest);
            }

            var defaultMediaType = StructureMediaType.GetFromEnum(StructureMediaEnumType.Structure).MediaTypeName;
            var requestAccept = ctx.IncomingRequest.Accept;
            Logger.Info("Got request Content type= " + requestAccept);

            IList<ContentType> acceptHeaderElements = ctx.IncomingRequest.GetAcceptHeaderElements();
            Func<ContentType, bool> predicate = type => StructureMediaType.GetTypeFromName(type.MediaType) != null;
            var contentType = RestUtils.GetContentType(ctx, predicate, defaultMediaType);

            string requestedVersion = HeaderUtils.GetVersionFromAccept(acceptHeaderElements, contentType.MediaType);

            var selectedStructureMediaType = StructureMediaType.GetTypeFromName(contentType.MediaType);
            var selectedMediaTypeWithVersion = selectedStructureMediaType.GetMediaTypeVersion(requestedVersion);

            if (selectedMediaTypeWithVersion == null)
            {
                Logger.Error("Cannot serve content type: " + requestAccept);
                throw new WebFaultException<string>("Cannot serve content type: " + requestAccept, HttpStatusCode.NotAcceptable);
            }

            Logger.Debug("Select mediatype with version if required: " + selectedMediaTypeWithVersion);

            SdmxSchema schemaVersion = RestUtils.GetVersionFromMediaType(selectedMediaTypeWithVersion);

            var context = HttpContext.Current;
            var controller = this._controllerBuilder.BuildQueryStructureRest(schemaVersion, context.User as DataflowPrincipal);

            Logger.Info("Selected representation info for the controller: format =" + "TODO" + " , smdx_schema=" + version);

            IRestStructureQuery query = BuildRestQueryBean(structure, agencyId, resourceId, version, ctx.IncomingRequest.UriTemplateMatch.QueryParameters);
            var streamController = controller.ParseRequest(query);
            var charSetEncoding = RestUtils.GetCharSetEncoding(contentType);

            var responseContentType = RestUtils.GetContentType(contentType, selectedMediaTypeWithVersion);
            selectedMediaTypeWithVersion.CharSet = charSetEncoding.WebName;
            return ctx.CreateStreamResponse(
                stream => RestUtils.StreamXml(schemaVersion, stream, streamController, charSetEncoding), responseContentType);
        }
Esempio n. 7
0
        static Message CreateOwinResponse(
            WebOperationContext webResponse,
            string status,
            IDictionary<string, string> headers,
            BodyDelegate body)
        {
            //TODO: hardenning

            var statusCode = int.Parse(status.Substring(0, 3));
            webResponse.OutgoingResponse.StatusCode = (HttpStatusCode) statusCode;
            webResponse.OutgoingResponse.StatusDescription = status.Substring(4);

            foreach (var header in Split(headers))
            {
                webResponse.OutgoingResponse.Headers.Add(header.Key, header.Value);
            }

            string contentType;
            if (!headers.TryGetValue("Content-Type", out contentType))
                contentType = "text/plain";

            return webResponse.CreateStreamResponse(
                stream =>
                {
                    var done = new ManualResetEvent(false);
                    body(
                        (data, _) =>
                        {
                            stream.Write(data.Array, data.Offset, data.Count);
                            return false;
                        },
                        ex => done.Set(),
                        () => done.Set()
                        );
                    done.WaitOne();
                },
                contentType);
        }
Esempio n. 8
0
        /// <summary>
        /// The process request.
        /// </summary>
        /// <param name="flowRef">
        /// The flow ref.
        /// </param>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <param name="providerRef">
        /// The provider ref.
        /// </param>
        /// <param name="ctx">
        /// The ctx.
        /// </param>
        /// <returns>
        /// The <see cref="Message"/>.
        /// </returns>
        /// <exception cref="WebFaultException{T}">
        /// </exception>
        private Message ProcessRequest(string flowRef, string key, string providerRef, WebOperationContext ctx)
        {
            var requestAccept = ctx.IncomingRequest.Accept;

            Logger.Info("Got request Content type= " + requestAccept);

            IList<ContentType> acceptHeaderElements = ctx.IncomingRequest.GetAcceptHeaderElements();

            var defaultMediaType = DataMediaType.GetFromEnum(DataMediaEnumType.GenericData).MediaType;
            var contentType = RestUtils.GetContentType(ctx, h => DataMediaType.GetTypeFromName(h.MediaType) != null, defaultMediaType);

            string requestedVersion = HeaderUtils.GetVersionFromAccept(acceptHeaderElements, contentType.MediaType);

            var datamediaType = DataMediaType.GetTypeFromName(contentType.MediaType);
            var selectedMediaTypeWithVersion = datamediaType.GetMediaTypeVersion(requestedVersion);

            if (selectedMediaTypeWithVersion == null)
            {
                Logger.Error("Cannot serve content type: " + requestAccept);
                throw new WebFaultException<string>("Cannot serve content type: " + requestAccept, HttpStatusCode.NotAcceptable);
            }

            Logger.Debug("Select mediatype with version if required: " + selectedMediaTypeWithVersion);

            BaseDataFormat format = DataMediaType.GetTypeFromName(selectedMediaTypeWithVersion.MediaType).Format;
            SdmxSchema version = GetVersionFromMediaType(selectedMediaTypeWithVersion, format);

            Logger.Info("Selected representation info for the controller: format =" + format + " , smdx_schema=" + version);
            IRestDataQuery query = this.BuildQueryBean(flowRef, key, providerRef, ctx.IncomingRequest.UriTemplateMatch.QueryParameters);
            HttpContext context = HttpContext.Current;

            var controller = this._controllerBuilder.BuildDataRest(context.User as DataflowPrincipal, format, version);
            var streamController = controller.ParseRequest(query);
            var charSetEncoding = RestUtils.GetCharSetEncoding(contentType);
            var responseContentType = RestUtils.GetContentType(contentType, selectedMediaTypeWithVersion);
            selectedMediaTypeWithVersion.CharSet = charSetEncoding.WebName;
            return ctx.CreateStreamResponse(stream => RestUtils.StreamXml(version, stream, streamController, charSetEncoding), responseContentType);
        }
 static Message ReturnFile(WebOperationContext ctx, FileInfo fInfo, HttpStatusCode statusCode = HttpStatusCode.OK)
 {
     ctx.OutgoingResponse.StatusCode = statusCode;
     ctx.OutgoingResponse.ETag       = GetETag(fInfo);
     return(ctx.CreateStreamResponse(File.OpenRead(fInfo.FullName), MIME[fInfo.Extension.ToLowerInvariant()]));
 }