/// <summary>
        /// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
        /// </summary>
        /// <param name="content">The object to write to the HTTP message.</param>
        /// <param name="message">The HTTP message to write to.</param>
        /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
        protected override void WriteInternal(object content, IHttpOutputMessage message)
        {
#if SILVERLIGHT
            // Write to the message stream
            message.Body = delegate(Stream stream)
            {
                DataContractJsonSerializer serializer = this.GetSerializer(content.GetType());
                serializer.WriteObject(stream, content);
            };
#else
            // Get the message encoding
            Encoding encoding = this.GetContentTypeCharset(message.Headers.ContentType, DEFAULT_CHARSET);

            DataContractJsonSerializer serializer = this.GetSerializer(content.GetType());

            // Write to the message stream
            message.Body = delegate(Stream stream)
            {
                // Using JsonReaderWriterFactory directly to set encoding
                using (XmlDictionaryWriter jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(stream, encoding, false))
                {
                    serializer.WriteObject(jsonWriter, content);
                }
            };
#endif
        }
 /// <summary>
 /// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
 /// </summary>
 /// <param name="content">The object to write to the HTTP message.</param>
 /// <param name="message">The HTTP message to write to.</param>
 /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
 protected override void WriteInternal(object content, IHttpOutputMessage message)
 {
     // Write to the message stream
     message.Body = delegate(Stream stream)
     {
         using (Stream contentStream = ((IResource)content).GetStream())
         {
             IoUtils.CopyStream(contentStream, stream);
         }
     };
 }
 /// <summary>
 /// Write an given object to the given HTTP message.
 /// </summary>
 /// <param name="content">
 /// The object to write to the HTTP message. The type of this object must have previously been
 /// passed to the <see cref="M:CanWrite"/> method of this interface, which must have returned <see langword="true"/>.
 /// </param>
 /// <param name="contentType">
 /// The content type to use when writing. May be null to indicate that the default content type of the converter must be used.
 /// If not null, this media type must have previously been passed to the <see cref="M:CanWrite"/> method of this interface,
 /// which must have returned <see langword="true"/>.
 /// </param>
 /// <param name="message">The HTTP message to write to.</param>
 /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
 public void Write(object content, MediaType contentType, IHttpOutputMessage message)
 {
     if (content is NameValueCollection)
     {
         this.WriteForm((NameValueCollection)content, message);
     }
     else if (content is IDictionary <string, object> )
     {
         this.WriteMultipart((IDictionary <string, object>)content, message);
     }
 }
 /// <summary>
 /// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
 /// </summary>
 /// <param name="content">The object to write to the HTTP message.</param>
 /// <param name="message">The HTTP message to write to.</param>
 /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
 protected override void WriteInternal(object content, IHttpOutputMessage message)
 {
     // Write to the message stream
     message.Body = delegate(Stream stream)
     {
         using (StreamWriter writer = new StreamWriter(stream))
             using (JsonTextWriter jsonWriter = new JsonTextWriter(writer))
             {
                 JsonSerializer jsonSerializer = new JsonSerializer();
                 jsonSerializer.Serialize(jsonWriter, content);
             }
     };
 }
        private void WriteForm(NameValueCollection form, IHttpOutputMessage message)
        {
            message.Headers.ContentType = MediaType.APPLICATION_FORM_URLENCODED;

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < form.AllKeys.Length; i++)
            {
                string   name   = form.AllKeys[i];
                string[] values = form.GetValues(name);
                if (values == null)
                {
                    builder.Append(HttpUtils.FormEncode(name));
                }
                else
                {
                    for (int j = 0; j < values.Length; j++)
                    {
                        string value = values[j];
                        builder.Append(HttpUtils.FormEncode(name));
                        builder.Append('=');
                        builder.Append(HttpUtils.FormEncode(value));
                        if (j != (values.Length - 1))
                        {
                            builder.Append('&');
                        }
                    }
                }
                if (i != (form.AllKeys.Length - 1))
                {
                    builder.Append('&');
                }
            }

            // Create a byte array of the data we want to send
            byte[] byteData = DEFAULT_CHARSET.GetBytes(builder.ToString());

//#if !SILVERLIGHT
//            // Set the content length in the message headers
//            message.Headers.ContentLength = byteData.Length;
//#endif

            // Write to the message stream
            message.Body = delegate(Stream stream)
            {
                stream.Write(byteData, 0, byteData.Length);
            };
        }
        /// <summary>
        /// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
        /// </summary>
        /// <param name="content">The object to write to the HTTP message.</param>
        /// <param name="message">The HTTP message to write to.</param>
        /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
        protected override void WriteInternal(object content, IHttpOutputMessage message)
        {
            // Create a byte array of the data we want to send
            byte[] byteData = content as byte[];

//#if !SILVERLIGHT
//            // Set the content length in the message headers
//            message.Headers.ContentLength = byteData.Length;
//#endif

            // Write to the message stream
            message.Body = delegate(Stream stream)
            {
                stream.Write(byteData, 0, byteData.Length);
            };
        }
Example #7
0
        /// <summary>
        /// Write an given object to the given HTTP message.
        /// </summary>
        /// <remarks>
        /// This implementation delegates to <see cref="M:GetDefaultContentType"/> method if a content
        /// type was not provided, and calls <see cref="M:WriteInternal"/>.
        /// </remarks>
        /// <param name="content">
        /// The object to write to the HTTP message. The type of this object must have previously been
        /// passed to the <see cref="M:CanWrite"/> method of this interface, which must have returned <see langword="true"/>.
        /// </param>
        /// <param name="contentType">
        /// The content type to use when writing. May be null to indicate that the default content type of the converter must be used.
        /// If not null, this media type must have previously been passed to the <see cref="M:CanWrite"/> method of this interface,
        /// which must have returned <see langword="true"/>.
        /// </param>
        /// <param name="message">The HTTP message to write to.</param>
        /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
        public virtual void Write(object content, MediaType contentType, IHttpOutputMessage message)
        {
            HttpHeaders headers = message.Headers;

            if (headers.ContentType == null)
            {
                if (contentType == null || contentType.IsWildcardType || contentType.IsWildcardSubtype)
                {
                    contentType = GetDefaultContentType(content);
                }
                if (contentType != null)
                {
                    headers.ContentType = contentType;
                }
            }
            WriteInternal(content, message);
        }
        /// <summary>
        /// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
        /// </summary>
        /// <param name="content">The object to write to the HTTP message.</param>
        /// <param name="message">The HTTP message to write to.</param>
        /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
        protected override void WriteInternal(object content, IHttpOutputMessage message)
        {
            // Get the message encoding
            Encoding encoding = GetContentTypeCharset(message.Headers.ContentType, DEFAULT_CHARSET);

            // Map from object to JsonValue
            JsonValue jsonValue = (content is JsonValue) ? (JsonValue)content : this.mapper.Serialize(content);

            // Create a byte array of the data we want to send
            byte[] byteData = encoding.GetBytes(jsonValue.ToString());

            // Write to the message stream
            message.Body = delegate(Stream stream)
            {
                stream.Write(byteData, 0, byteData.Length);
            };
        }
        /// <summary>
        /// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
        /// </summary>
        /// <param name="content">The object to write to the HTTP message.</param>
        /// <param name="message">The HTTP message to write to.</param>
        /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
        protected override void WriteInternal(object content, IHttpOutputMessage message)
        {
            // Get the message encoding
            Encoding encoding = this.GetContentTypeCharset(message.Headers.ContentType, DEFAULT_CHARSET);

            XmlWriterSettings settings = this.GetXmlWriterSettings();

            settings.Encoding = encoding;

            // Write to the message stream
            message.Body = delegate(Stream stream)
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
                {
                    WriteXml(xmlWriter, content);
                }
            };
        }
Example #10
0
        /// <summary>
        /// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
        /// </summary>
        /// <param name="content">The object to write to the HTTP message.</param>
        /// <param name="message">The HTTP message to write to.</param>
        /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
        protected override void WriteInternal(object content, IHttpOutputMessage message)
        {
            // Get the message encoding
            Encoding encoding = GetContentTypeCharset(message.Headers.ContentType, DEFAULT_CHARSET);

            // Create a byte array of the data we want to send
            byte[] byteData = encoding.GetBytes(content as string);

//#if !SILVERLIGHT
//            // Set the content length in the message headers
//            message.Headers.ContentLength = byteData.Length;
//#endif

            // Write to the message stream
            message.Body = delegate(Stream stream)
            {
                stream.Write(byteData, 0, byteData.Length);
            };
        }
        private void WriteMultipart(IDictionary <string, object> parts, IHttpOutputMessage message)
        {
            string boundary = this.GenerateMultipartBoundary();

            IDictionary <string, string> parameters = new Dictionary <string, string>(1);

            parameters.Add("boundary", boundary);
            MediaType contentType = new MediaType(MediaType.MULTIPART_FORM_DATA, parameters);

            message.Headers.ContentType = contentType;

            message.Body = delegate(Stream stream)
            {
                using (StreamWriter streamWriter = new StreamWriter(stream))
                {
                    streamWriter.NewLine = "\r\n";
                    this.WriteParts(boundary, parts, streamWriter);
                    this.WriteEnd(boundary, streamWriter);
                }
            };
        }
Example #12
0
 /// <summary>
 /// Write an given object to the given HTTP message.
 /// </summary>
 /// <param name="content">
 /// The object to write to the HTTP message. The type of this object must have previously been
 /// passed to the <see cref="M:CanWrite"/> method of this interface, which must have returned <see langword="true"/>.
 /// </param>
 /// <param name="contentType">
 /// The content type to use when writing. May be null to indicate that the default content type of the converter must be used.
 /// If not null, this media type must have previously been passed to the <see cref="M:CanWrite"/> method of this interface,
 /// which must have returned <see langword="true"/>.
 /// </param>
 /// <param name="message">The HTTP message to write to.</param>
 /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
 public override void Write(object content, MediaType contentType, IHttpOutputMessage message)
 {
     base.Write(new FileResource(((FileInfo)content).FullName), contentType, message);
 }
Example #13
0
 /// <summary>
 /// Abstract template method that writes the actual body. Invoked from <see cref="M:Write"/>.
 /// </summary>
 /// <param name="content">The object to write to the HTTP message.</param>
 /// <param name="message">The HTTP message to write to.</param>
 /// <exception cref="HttpMessageNotWritableException">In case of conversion errors</exception>
 protected abstract void WriteInternal(object content, IHttpOutputMessage message);
 protected override void WriteInternal(object content, IHttpOutputMessage message)
 {
     throw new NotSupportedException();
 }