Example #1
0
        public ContentType RespondTo(IApiStandartResponce responce, TextWriter output, string path, string contentType,
                                     bool prettify, bool async)
        {
            var settings = new JsonSerializerSettings
            {
                DefaultValueHandling = DefaultValueHandling.Include,
                ContractResolver     = new SerializerContractResolver(responce.Response, responce.ApiContext),
                Converters           = new[] { new JsonStringConverter() }
            };

            string responseJson = JsonConvert.SerializeObject(responce, prettify ? Formatting.Indented : Formatting.None, settings);

            if (string.IsNullOrEmpty(responseJson))
            {
                throw new InvalidOperationException("Failed to serialize object");
            }

            ContentType type;

            if (IsXmlRequest(path, contentType))
            {
                settings.DateParseHandling = DateParseHandling.None;

                type = new ContentType(Constants.XmlContentType)
                {
                    CharSet = "UTF-8"
                };
                responseJson = JsonConvert.DeserializeObject <XDocument>("{\"result\":" + responseJson + "}", settings).ToString(prettify
                                                                                      ? SaveOptions.None
                                                                                      : SaveOptions.DisableFormatting);
            }
            else
            {
                //Just write
                type = new ContentType(Constants.JsonContentType)
                {
                    CharSet = "UTF-8"
                };
            }

            try
            {
                output.Write(responseJson);
            }
            catch (Exception e)
            {
                throw new SerializationException("Failed to write:" + responseJson, e);
            }
            return(type);
        }
 public void RespondTo(IApiStandartResponce responce, HttpContextBase context)
 {
     var contentResponce = (IApiContentResponce) responce.Response;
     if (contentResponce.ContentDisposition != null)
     {
         context.Response.AddHeader("Content-Disposition", contentResponce.ContentDisposition.ToString());
     }
     if (contentResponce.ContentType != null)
     {
         context.Response.ContentType = contentResponce.ContentType.ToString();
     }
     if (contentResponce.ContentEncoding != null)
     {
         context.Response.ContentEncoding = contentResponce.ContentEncoding;
     }
     context.Response.WriteStreamToResponce(contentResponce.ContentStream);
 }
        public void RespondTo(IApiStandartResponce responce, HttpContextBase context)
        {
            var contentResponce = (IApiContentResponce)responce.Response;

            if (contentResponce.ContentDisposition != null)
            {
                context.Response.AddHeader("Content-Disposition", contentResponce.ContentDisposition.ToString());
            }
            if (contentResponce.ContentType != null)
            {
                context.Response.ContentType = contentResponce.ContentType.ToString();
            }
            if (contentResponce.ContentEncoding != null)
            {
                context.Response.ContentEncoding = contentResponce.ContentEncoding;
            }
            context.Response.WriteStreamToResponce(contentResponce.ContentStream);
        }
        public ContentType RespondTo(IApiStandartResponce responce, TextWriter output, string path, string contentType,
                                     bool prettify, bool async)
        {
            var settings = new JsonSerializerSettings
            {
                DefaultValueHandling = DefaultValueHandling.Include,
                ContractResolver = new SerializerContractResolver(responce.Response,responce.ApiContext),
                Converters = new[] { new JsonStringConverter() }
            };

            string responseJson = JsonConvert.SerializeObject(responce, prettify ? Formatting.Indented : Formatting.None,settings);
            if (string.IsNullOrEmpty(responseJson))
                throw new InvalidOperationException("Failed to serialize object");

            ContentType type;
            if (IsXmlRequest(path, contentType))
            {
                settings.DateParseHandling = DateParseHandling.None;

                type = new ContentType(Constants.XmlContentType) { CharSet = "UTF-8" };
                responseJson = JsonConvert.DeserializeObject<XDocument>("{\"result\":" + responseJson + "}", settings).ToString(prettify
                                                                                      ? SaveOptions.None
                                                                                      : SaveOptions.DisableFormatting);
            }
            else
            {
                //Just write
                type = new ContentType(Constants.JsonContentType) { CharSet = "UTF-8" };
            }

            try
            {
                output.Write(responseJson);
            }
            catch (Exception e)
            {
                throw new SerializationException("Failed to write:"+responseJson,e);
            }
            return type;
        }
        public void RespondTo(IApiStandartResponce responce, HttpContextBase httpContext)
        {
            if (responce == null) throw new ArgumentNullException("responce");
            if (httpContext == null) throw new ArgumentNullException("httpContext");

            foreach (var apiSerializer in _serializers)
            {
                var contentType = apiSerializer.RespondTo(responce, httpContext.Response.Output,
                                                                  httpContext.Request.Path, httpContext.Request.ContentType, false, false);
                if (contentType != null)
                {
                    httpContext.Response.ContentType = contentType.ToString();
#if (DEBUG)
                    httpContext.Response.AddHeader("X-Responded", string.Format("{0}", contentType));
#endif
                    return;
                }
            }
#if (DEBUG)
            httpContext.Response.AddHeader("X-Responded", "No");
#endif

        }
 public bool CanRespondTo(IApiStandartResponce responce, HttpContextBase context)
 {
     if (responce == null) throw new ArgumentNullException("responce");
     if (context == null) throw new ArgumentNullException("context");
     return _serializers.Any(x => x.CanRespondTo(responce, context.Request.Path, context.Request.ContentType));
 }
Example #7
0
 public void RespondTo(IApiStandartResponce responce, HttpContextBase context)
 {
     ((IApiDirectResponce)responce.Response).WriteResponce(context.Response);
 }
Example #8
0
 public bool CanRespondTo(IApiStandartResponce responce, HttpContextBase context)
 {
     return(responce.Response is IApiDirectResponce);
 }
 public string Serialize(IApiStandartResponce obj, ApiContext context)
 {
     throw new NotSupportedException();
 }
 public void RespondTo(IApiStandartResponce responce, HttpContextBase context)
 {
     ((IApiDirectResponce) responce.Response).WriteResponce(context.Response);
 }
 public bool CanRespondTo(IApiStandartResponce responce, string path, string contentType)
 {
     return IsJsonRequest(path, contentType) || IsXmlRequest(path, contentType);
 }
Example #12
0
        private Dictionary <string, string> CreateResponse(IApiSerializer apiResponder, IApiStandartResponce responce, ApiContext apiContext)
        {
            var examples = new Dictionary <string, string>();

            foreach (var extension in apiResponder.GetSupportedExtensions())
            {
                if (!_responseFormats.Contains(extension))
                {
                    continue;
                }

                //Create request context
                using (var writer = new StringWriter())
                {
                    var contentType = apiResponder.RespondTo(responce, writer, "dummy" + extension, string.Empty, true, false);
                    writer.Flush();
                    examples[contentType.MediaType] = writer.GetStringBuilder().ToString();
                }
            }
            return(examples);
        }
 public string Serialize(IApiStandartResponce obj, ApiContext context)
 {
     throw new NotSupportedException();
 }
 public bool CanRespondTo(IApiStandartResponce responce, HttpContextBase context)
 {
     if (responce == null) throw new ArgumentNullException("responce");
     if (context == null) throw new ArgumentNullException("context");
     return true;
 }
Example #15
0
 public bool CanRespondTo(IApiStandartResponce responce, string path, string contentType)
 {
     return(IsJsonRequest(path, contentType) || IsXmlRequest(path, contentType));
 }
Example #16
0
        private Dictionary <string, string> CreateResponse(IApiSerializer apiResponder, IApiStandartResponce responce, ApiContext apiContext)
        {
            var examples = new Dictionary <string, string>();

            try
            {
                foreach (var extension in apiResponder.GetSupportedExtensions())
                {
                    //Create request context
                    using (var writer = new StringWriter())
                    {
                        var contentType = apiResponder.RespondTo(responce, writer, "dummy" + extension, string.Empty, true, false);
                        writer.Flush();
                        examples.Add(contentType.MediaType, writer.GetStringBuilder().ToString());
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(examples);
        }
 public bool CanRespondTo(IApiStandartResponce responce, HttpContextBase context)
 {
     return responce.Response is IApiDirectResponce;
 }