示例#1
0
        public Task <HttpResponseMessage> Write(ResponseWriterContext context)
        {
            var response   = new HttpResponseMessage();
            var streamInfo = context.Response.As <IStreamInfo>() ?? context
                             .RequestContext.Action.Method.GetAttribute <OutputStreamAttribute>();
            var stream     = context.Response.As <OutputStream>()?.Stream ?? context.Response as Stream;
            var bufferSize = streamInfo?.BufferSize ?? _configuration.DownloadBufferSize;

            if (stream != null)
            {
                response.Content = new StreamContent(stream, bufferSize);
                if (streamInfo != null)
                {
                    if (streamInfo.ContentType.IsNotNullOrEmpty())
                    {
                        response.Content.Headers.SetContentType(streamInfo.ContentType);
                    }
                    if (streamInfo.Filename.IsNotNullOrEmpty())
                    {
                        response.Content.Headers.SetAttachmentDisposition(streamInfo.Filename);
                    }
                }
                if (response.Content.Headers.ContentType == null)
                {
                    response.Content.Headers.SetContentType(MimeTypes.ApplicationOctetStream);
                }
            }
            return(response.ToTaskResult());
        }
示例#2
0
        public virtual bool AppliesTo(ResponseWriterContext context)
        {
            var responseType = context.RequestContext.Route.ResponseType?.Type;

            return(context.RequestContext.Route.HasResponse &&
                   (responseType == typeof(OutputStream) || responseType == typeof(Stream)));
        }
示例#3
0
        protected override void WriteToStream(ResponseWriterContext context, Stream output)
        {
            var streamWriter = output.CreateWriter(_configuration.DefaultEncoding,
                                                   _configuration.SerializerBufferSize);
            var writer = System.Xml.XmlWriter.Create(streamWriter, _xmlWriterSettings);

            new XmlSerializer(context.Response.GetType())
            .Serialize(writer, context.Response);
        }
示例#4
0
        protected override void WriteToStream(ResponseWriterContext context, Stream output)
        {
            var streamWriter = output.CreateWriter(_configuration.DefaultEncoding,
                                                   _configuration.SerializerBufferSize);
            var jsonWriter = new JsonTextWriter(streamWriter);

            _serializer.Serialize(jsonWriter, context.Response);
            jsonWriter.Flush();
        }
示例#5
0
        public Task <HttpResponseMessage> Write(ResponseWriterContext context)
        {
            var response = new HttpResponseMessage();
            var data     = GetResponse(context);

            if (data != null)
            {
                response.Content = new StringContent(data, _encoding, _mimeType);
            }
            return(response.ToTaskResult());
        }
示例#6
0
        public override bool AppliesTo(ResponseWriterContext context)
        {
            var responseType = _routeDescriptor.ResponseType?.Type;

            if (!typeof(IRedirectable).IsAssignableFrom(responseType))
            {
                return(false);
            }
            var redirect = context.Response.As <IRedirectable>();

            return(redirect.RedirectStatus.HasValue);
        }
示例#7
0
        public Task <HttpResponseMessage> Write(ResponseWriterContext context)
        {
            var response = new HttpResponseMessage();
            var data     = context.Response?.ToString();

            if (data != null)
            {
                response.Content = new StringContent(data,
                                                     Encoding.UTF8, data.ContainsIgnoreCase("</") ?
                                                     MimeTypes.TextHtml : MimeTypes.TextPlain);
            }
            return(response.ToTaskResult());
        }
示例#8
0
        public Task <HttpResponseMessage> Write(ResponseWriterContext context)
        {
            var redirect = context.Response.As <IRedirectable>();

            return(new HttpResponseMessage
            {
                Headers =
                {
                    Location = new Uri(redirect.RidirectUrl)
                },
                StatusCode = (HttpStatusCode)redirect.Ridirect
            }.ToTaskResult());
        }
示例#9
0
        public virtual bool AppliesTo(ResponseWriterContext context)
        {
            var responseType = context.RequestContext.Route.ResponseType?.Type;

            if (!context.RequestContext.Route.HasResponse ||
                !typeof(IRedirectable).IsAssignableFrom(responseType))
            {
                return(false);
            }
            var redirect = context.Response.As <IRedirectable>();

            return(redirect.Ridirect != RedirectType.None && redirect.RidirectUrl.IsNotNullOrEmpty());
        }
示例#10
0
        public override Task <HttpResponseMessage> WriteResponse(ResponseWriterContext context)
        {
            var redirect = context.Response.As <IRedirectable>();

            if (redirect.RedirectStatus.HasValue)
            {
                _responseMessage.StatusCode = redirect.RedirectStatus.Value;
            }
            if (redirect.RedirectUrl.IsNotNullOrEmpty())
            {
                _responseMessage.Headers.Location = new Uri(redirect.RedirectUrl, UriKind.RelativeOrAbsolute);
            }
            return(_responseMessage.ToTaskResult());
        }
示例#11
0
 public override Task <HttpResponseMessage> WriteResponse(ResponseWriterContext context)
 {
     _responseMessage.Content = GetContent(context);
     if (_responseMessage.Content != null)
     {
         var contentType = _acceptType.Value?.ContentType ?? _mimeTypes.First();
         if (contentType.IsNotNullOrEmpty())
         {
             _responseMessage.Content.Headers.ContentType = new
                                                            MediaTypeHeaderValue(contentType);
         }
     }
     return(_responseMessage.ToTaskResult());
 }
 protected sealed override HttpContent GetContent(ResponseWriterContext context)
 {
     return(new AsyncContent(output =>
     {
         try
         {
             WriteToStream(context, output);
         }
         finally
         {
             if (_configuration.DisposeSerializedObjects)
             {
                 context.Response.As <IDisposable>()?.Dispose();
             }
         }
         return Task.CompletedTask;
     }));
 }
示例#13
0
        public override Task <HttpResponseMessage> WriteResponse(ResponseWriterContext context)
        {
            var outputInfo = context.Response.As <TOutputInfo>() ??
                             _actionMethod.GetAttribute <TAttribute>();
            var data = context.Response.As <OutputBody <T> >()?.Data ?? context.Response as T;

            if (data != null)
            {
                _responseMessage.Content = GetContent(data, outputInfo);
                _responseMessage.Content.Headers.SetContentType(
                    outputInfo?.ContentType ?? GetContentType(data));
                if (outputInfo != null && outputInfo.Filename.IsNotNullOrEmpty())
                {
                    _responseMessage.Content.Headers.SetAttachmentDisposition(outputInfo.Filename);
                }
            }
            return(_responseMessage.ToTaskResult());
        }
示例#14
0
 public virtual bool AppliesTo(ResponseWriterContext context)
 {
     return(context.RequestContext.Route.HasResponse && context
            .RequestContext.Route.ResponseType.Type == typeof(string));
 }
示例#15
0
 public Task <HttpResponseMessage> Write(ResponseWriterContext context)
 {
     return(WriteResponse(context));
 }
示例#16
0
 public abstract Task <HttpResponseMessage> WriteResponse(
     ResponseWriterContext context);
示例#17
0
 public virtual bool AppliesTo(ResponseWriterContext context)
 {
     return(true);
 }
 protected abstract void WriteToStream(ResponseWriterContext context, Stream output);
示例#19
0
 protected override string GetResponse(ResponseWriterContext context)
 {
     return(JsonConvert.SerializeObject(context.Response, _settings));
 }
示例#20
0
 protected abstract HttpContent GetContent(ResponseWriterContext context);
示例#21
0
 protected override string GetResponse(ResponseWriterContext context)
 {
     return(context.Response.SerializeXml(context
                                          .RequestContext.Route.ResponseType.Type));
 }
示例#22
0
 public override bool AppliesTo(ResponseWriterContext context)
 {
     return(_acceptType.Value != null);
 }
示例#23
0
        public override bool AppliesTo(ResponseWriterContext context)
        {
            var responseType = _routeDescriptor.ResponseType?.Type;

            return(responseType == typeof(TWrapper) || responseType == typeof(T));
        }
示例#24
0
 protected abstract string GetResponse(ResponseWriterContext context);
示例#25
0
 protected override HttpContent GetContent(ResponseWriterContext context)
 {
     return(new AsyncContent(GetResponse(context), _encoding));
 }
示例#26
0
 public virtual bool AppliesTo(ResponseWriterContext context)
 {
     return(context.RequestContext.Route.HasResponse && context
            .RequestContext.RequestMessage.AcceptsMimeType(_mimeType));
 }