/// <summary>
        /// Ctor
        /// </summary>
        /// <param name="writers">inject with all possible writers</param>
        public DataWriterProvider(IEnumerable <IDataWriter> writers)
        {
            if (writers != null)
            {
                foreach (IDataWriter writer in writers)
                {
                    if (this.DefaultWriter == null)
                    {
                        // TODO: decide less arbitrary way to choose default
                        // without hardcoding value into IDataWriter
                        this.DefaultWriter = writer;
                    }

                    if (!String.IsNullOrEmpty(writer.ContentType))
                    {
                        this.WritersByMime[writer.ContentType] = writer;
                    }

                    if (!String.IsNullOrEmpty(writer.ContentType))
                    {
                        string ext = DataWriterProvider.NormalizeExtension(writer.FileExtension);
                        this.WritersByExt[ext] = writer;
                    }
                }
            }
        }
 public IDataWriter Find(string extension)
 {
     extension = DataWriterProvider.NormalizeExtension(extension);
     if (this.WritersByExt.ContainsKey(extension))
     {
         return(this.WritersByExt[extension]);
     }
     return(null);
 }
        public IDataReader Find(string contentTypeHeader)
        {
            string key = DataWriterProvider.ParseMediaType(contentTypeHeader);

            if (this.ReadersByMime.ContainsKey(key))
            {
                return(this.ReadersByMime[key]);
            }
            return(null);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string ParseMediaType(string type)
        {
            foreach (string mime in DataWriterProvider.SplitTrim(type, ';'))
            {
                // only return first part
                return(mime);
            }

            // if no parts then was empty
            return(String.Empty);
        }
 public IDataWriter Find(string acceptHeader, string contentTypeHeader)
 {
     foreach (string current in DataWriterProvider.ParseHeaders(acceptHeader, contentTypeHeader))
     {
         if (this.WritersByMime.ContainsKey(current))
         {
             return(this.WritersByMime[current]);
         }
     }
     return(null);
 }
        /// <summary>
        /// Parses HTTP headers for Media-Types
        /// </summary>
        /// <param name="accept">HTTP Accept header</param>
        /// <param name="contentType">HTTP Content-Type header</param>
        /// <returns>sequence of Media-Types</returns>
        /// <remarks>
        /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
        /// </remarks>
        public static IEnumerable <string> ParseHeaders(string accept, string contentType)
        {
            string mime;

            // check for a matching accept type
            foreach (string type in DataWriterProvider.SplitTrim(accept, ','))
            {
                mime = DataWriterProvider.ParseMediaType(type);
                if (!String.IsNullOrEmpty(mime))
                {
                    yield return(mime);
                }
            }

            // fallback on content-type
            mime = DataWriterProvider.ParseMediaType(contentType);
            if (!String.IsNullOrEmpty(mime))
            {
                yield return(mime);
            }
        }
 public DataWriterProvider(IEnumerable <IDataWriter> writers)
 {
     if (writers != null)
     {
         foreach (IDataWriter current in writers)
         {
             if (this.DefaultWriter == null)
             {
                 this.DefaultWriter = current;
             }
             if (!string.IsNullOrEmpty(current.ContentType))
             {
                 this.WritersByMime[current.ContentType] = current;
             }
             if (!string.IsNullOrEmpty(current.ContentType))
             {
                 string key = DataWriterProvider.NormalizeExtension(current.FileExtension);
                 this.WritersByExt[key] = current;
             }
         }
     }
 }