Example #1
0
        private HttpClient Create(MimeFormat format, string url)
        {
            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Clear();

            switch (format)
            {
            case MimeFormat.XML:
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/xml"));
                break;

            case MimeFormat.JSON:
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                break;
            }

            if (url != string.Empty)
            {
                client.BaseAddress = new Uri(url);
            }
            return(client);
        }
Example #2
0
        GetRedmineExceptions(this System.Net.WebResponse webResponse, MimeFormat mimeFormat)
        {
            using (System.IO.Stream dataStream = webResponse.GetResponseStream())
            {
                if (dataStream == null)
                {
                    return(null);
                }

                using (System.IO.StreamReader reader = new System.IO.StreamReader(dataStream))
                {
                    string responseFromServer = reader.ReadToEnd();

                    if (string.IsNullOrEmpty(responseFromServer.Trim()))
                    {
                        return(null);
                    }

                    try
                    {
                        Redmine.Net.Api.Types.PaginatedObjects <Error> result =
                            RedmineSerializer.DeserializeList <Error>(responseFromServer, mimeFormat);

                        return(result.Objects);
                    }
                    catch (System.Exception ex)
                    {
                        Logger.Current.Error(ex.Message);
                    }
                }
                return(null);
            }
        }
Example #3
0
        /// <summary>
        /// Deserializes the specified response.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="response">The response.</param>
        /// <param name="mimeFormat">The MIME format.</param>
        /// <returns></returns>
        /// <exception cref="Redmine.Net.Api.Exceptions.RedmineException">could not deserialize:  + response</exception>
        public static T Deserialize <T>(string response, MimeFormat mimeFormat) where T : class, new()
        {
            if (string.IsNullOrEmpty(response))
            {
                throw new RedmineException("could not deserialize: " + response);
            }

            return(FromXML <T>(response));
        }
Example #4
0
        /// <summary>
        /// Deserializes the list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="response">The response.</param>
        /// <param name="mimeFormat">The MIME format.</param>
        /// <returns></returns>
        /// <exception cref="Redmine.Net.Api.Exceptions.RedmineException">web response is null!</exception>
        public static PaginatedObjects <T> DeserializeList <T>(string response, MimeFormat mimeFormat) where T : class, new()
        {
            if (string.IsNullOrEmpty(response))
            {
                throw new RedmineException("web response is null!");
            }

            return(XmlDeserializeList <T>(response));
        }
Example #5
0
        /// <summary>
        /// Handles the web exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <param name="method">The method.</param>
        /// <param name="mimeFormat">The MIME format.</param>
        /// <exception cref="Redmine.Net.Api.Exceptions.RedmineTimeoutException">Timeout!</exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.NameResolutionFailureException">Bad domain name!</exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.NotFoundException"></exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.InternalServerErrorException"></exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.UnauthorizedException"></exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.ForbiddenException"></exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.ConflictException">The page that you are trying to update is staled!</exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.RedmineException">
        /// </exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.NotAcceptableException"></exception>
        public static void HandleWebException(this WebException exception, string method, MimeFormat mimeFormat)
        {
            if (exception == null) return;

            switch (exception.Status)
            {
                case WebExceptionStatus.Timeout:
                    throw new RedmineTimeoutException("Timeout!", exception);
                case WebExceptionStatus.NameResolutionFailure:
                    throw new NameResolutionFailureException("Bad domain name!", exception);
                case WebExceptionStatus.ProtocolError:
                {
                    var response = (HttpWebResponse) exception.Response;
                    switch ((int) response.StatusCode)
                    {
                        case (int) HttpStatusCode.NotFound:
                            throw new NotFoundException(response.StatusDescription, exception);

                        case (int) HttpStatusCode.InternalServerError:
                            throw new InternalServerErrorException(response.StatusDescription, exception);

                        case (int) HttpStatusCode.Unauthorized:
                            throw new UnauthorizedException(response.StatusDescription, exception);

                        case (int) HttpStatusCode.Forbidden:
                            throw new ForbiddenException(response.StatusDescription, exception);

                        case (int) HttpStatusCode.Conflict:
                            throw new ConflictException("The page that you are trying to update is staled!", exception);

                        case 422:
                            var errors = GetRedmineExceptions(exception.Response, mimeFormat);
                            var message = string.Empty;
                            if (errors != null)
                            {
                                foreach (var error in errors)
                                    message = message + error.Info + "\n";
                            }
                            throw new RedmineException(
                                method + " has invalid or missing attribute parameters: " + message, exception);

                        case (int) HttpStatusCode.NotAcceptable:
                            throw new NotAcceptableException(response.StatusDescription, exception);
                    }
                }
                    break;

                default:
                    throw new RedmineException(exception.Message, exception);
            }
        }
        private static List<Error> GetRedmineExceptions(this WebResponse webResponse, MimeFormat mimeFormat)
        {
            using (var dataStream = webResponse.GetResponseStream())
            {
                if (dataStream == null) return null;
                using (var reader = new StreamReader(dataStream))
                {
                    var responseFromServer = reader.ReadToEnd();

                    if (responseFromServer.Trim().Length > 0)
                    {
                        var errors = RedmineSerializer.DeserializeList<Error>(responseFromServer, mimeFormat);
                        return errors.Objects;
                    }
                }
                return null;
            }
        }
Example #7
0
        public static HttpClient Create(MimeFormat format, string baseUrl)
        {
            HttpClient client = new HttpClient();
            switch (format)
            {
                case MimeFormat.XML:
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/xml"));
                    break;
                case MimeFormat.JSON:
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));
                    break;
            }

            if (baseUrl != string.Empty)
                client.BaseAddress = new Uri(baseUrl);

            return client;
        }
        protected ContentEncodingStream(Stream stream, ICryptoTransform transform, byte[] eolPrependBytes, MimeFormat format)
        {
            if (stream == null)
            throw new ArgumentNullException("stream");
              if (!stream.CanWrite)
            throw new ArgumentException("stream must be writable", "stream");
              if (format == null)
            throw new ArgumentNullException("format");

              var bufferBlockSize = transform.CanTransformMultipleBlocks
            ? (folding == 0 ? 128 : folding)
            : 1;

              this.transform = transform;
              this.folding = format.Folding;
              this.outputBuffer = new byte[bufferBlockSize * transform.OutputBlockSize];
              this.inputBuffer  = new byte[bufferBlockSize * transform.InputBlockSize];
              this.inputCount = 0;
              this.stream = stream;
              this.eol = format.GetEOLBytes();
              this.eolPrependBytes = eolPrependBytes;
        }
        public static Stream CreateEncodingStream(Stream contentStream, ContentTransferEncodingMethod transferEncoding, MimeFormat format)
        {
            if (contentStream == null)
            throw new ArgumentNullException("contentStream");
              if (format == null)
            throw new ArgumentNullException("format");

              switch (transferEncoding) {
            case ContentTransferEncodingMethod.SevenBit:
            case ContentTransferEncodingMethod.EightBit:
            case ContentTransferEncodingMethod.Binary:
              return contentStream;
            case ContentTransferEncodingMethod.Base64:
              return new Base64ContentEncodingStream(contentStream, format);
            case ContentTransferEncodingMethod.QuotedPrintable:
              return new QuotedPrintableContentEncodingStream(contentStream, format);
            case ContentTransferEncodingMethod.UUEncode:
            case ContentTransferEncodingMethod.GZip64:
            default:
              throw new NotSupportedException(string.Format("unsupported transfer encoding: {0}", transferEncoding));
              }
        }
Example #10
0
        private static List<Error> GetRedmineExceptions(this WebResponse webResponse, MimeFormat mimeFormat)
        {
            using (var dataStream = webResponse.GetResponseStream())
            {
                if (dataStream == null) return null;
                using (var reader = new StreamReader(dataStream))
                {
                    var responseFromServer = reader.ReadToEnd();

                    if (string.IsNullOrEmpty(responseFromServer.Trim())) return null;
                    try
                    {
                        var result = RedmineSerializer.DeserializeList<Error>(responseFromServer, mimeFormat);
                        return result.Objects;
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.Message);
                    }
                }
                return null;
            }
        }
Example #11
0
 /// <summary>
 /// Sets the stream to parse.
 /// </summary>
 /// <param name="stream">The stream to parse.</param>
 /// <param name="format">The format of the stream.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="stream"/> is <c>null</c>.
 /// </exception>
 public void SetStream(Stream stream, MimeFormat format)
 {
     SetStream (ParserOptions.Default, stream, format, false);
 }
Example #12
0
 /// <summary>
 /// Serializes the specified object.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="obj">The object.</param>
 /// <param name="mimeFormat">The MIME format.</param>
 /// <returns></returns>
 public static string Serialize <T>(T obj, MimeFormat mimeFormat) where T : class, new()
 {
     return(ToXML(obj));
 }
 public Base64ContentEncodingStream(Stream stream, MimeFormat format)
     : base(stream, new System.Security.Cryptography.ToBase64Transform(), new byte[] {}, format)
 {
 }
Example #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeKit.MimeParser"/> class.
 /// </summary>
 /// <param name="stream">The stream to parse.</param>
 /// <param name="format">The format of the stream.</param>
 /// <param name="persistent"><c>true</c> if the stream is persistent; otherwise <c>false</c>.</param>
 /// <remarks>
 /// <para>If <paramref name="persistent"/> is <c>true</c> and <paramref name="stream"/> is seekable, then
 /// the <see cref="MimeParser"/> will not copy the content of <see cref="MimePart"/>s into memory. Instead,
 /// it will use a <see cref="MimeKit.IO.BoundStream"/> to reference a substream of <paramref name="stream"/>.
 /// This has the potential to not only save mmeory usage, but also improve <see cref="MimeParser"/>
 /// performance.</para>
 /// <para>It should be noted, however, that disposing <paramref name="stream"/> will make it impossible
 /// for <see cref="ContentObject"/> to read the content.</para>
 /// </remarks>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="stream"/> is <c>null</c>.
 /// </exception>
 public MimeParser(Stream stream, MimeFormat format, bool persistent)
     : this(ParserOptions.Default, stream, format, persistent)
 {
 }
Example #15
0
        } // End Function GetExceptionDetails

        /// <summary>
        /// Handles the web exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <param name="method">The method.</param>
        /// <param name="mimeFormat">The MIME format.</param>
        /// <exception cref="Redmine.Net.Api.Exceptions.RedmineTimeoutException">Timeout!</exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.NameResolutionFailureException">Bad domain name!</exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.NotFoundException"></exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.InternalServerErrorException"></exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.UnauthorizedException"></exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.ForbiddenException"></exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.ConflictException">The page that you are trying to update is staled!</exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.RedmineException">
        /// </exception>
        /// <exception cref="Redmine.Net.Api.Exceptions.NotAcceptableException"></exception>
        public static void HandleWebException(this System.Net.WebException exception, string method, MimeFormat mimeFormat)
        {
            if (exception == null)
            {
                return;
            }

            switch (exception.Status)
            {
            case System.Net.WebExceptionStatus.Timeout:
                throw new RedmineTimeoutException("Timeout!", exception);

            case System.Net.WebExceptionStatus.NameResolutionFailure:
                throw new NameResolutionFailureException("Bad domain name!", exception);

            case System.Net.WebExceptionStatus.ProtocolError:
            {
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)exception.Response;
                switch ((int)response.StatusCode)
                {
                case (int)System.Net.HttpStatusCode.NotFound:
                    throw new NotFoundException(response.StatusDescription, exception);

                case (int)System.Net.HttpStatusCode.InternalServerError:
                    string details = GetExceptionDetails(exception);
                    System.Diagnostics.Debug.WriteLine(details);

                    throw new InternalServerErrorException(response.StatusDescription, exception);

                case (int)System.Net.HttpStatusCode.Unauthorized:
                    throw new UnauthorizedException(response.StatusDescription, exception);

                case (int)System.Net.HttpStatusCode.Forbidden:
                    throw new ForbiddenException(response.StatusDescription, exception);

                case (int)System.Net.HttpStatusCode.Conflict:
                    throw new ConflictException("The page that you are trying to update is staled!", exception);

                case 422:
                    System.Collections.Generic.List <Error> errors = GetRedmineExceptions(exception.Response, mimeFormat);
                    string message = string.Empty;
                    if (errors != null)
                    {
                        foreach (Error error in errors)
                        {
                            message = message + error.Info + "\n";
                        }
                    }
                    throw new RedmineException(
                              method + " has invalid or missing attribute parameters: " + message, exception);

                case (int)System.Net.HttpStatusCode.NotAcceptable:
                    throw new NotAcceptableException(response.StatusDescription, exception);
                }
            }
            break;

            default:
                throw new RedmineException(exception.Message, exception);
            }
        }
Example #16
0
 /// <summary>
 /// Users the data.
 /// </summary>
 /// <param name="userId">The user identifier.</param>
 /// <param name="mimeFormat">The MIME format.</param>
 /// <returns></returns>
 public static string UserData(int userId, MimeFormat mimeFormat)
 {
     return mimeFormat == MimeFormat.Xml
         ? "<user_id>" + userId + "</user_id>"
         : "{\"user_id\":\"" + userId + "\"}";
 }
Example #17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeKit.MimeParser"/> class.
 /// </summary>
 /// <param name="stream">The stream to parse.</param>
 /// <param name="format">The format of the stream.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="stream"/> is <c>null</c>.
 /// </exception>
 public MimeParser(Stream stream, MimeFormat format)
     : this(ParserOptions.Default, stream, format, false)
 {
 }
Example #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MimeKit.MimeParser"/> class.
        /// </summary>
        /// <param name="options">The parser options.</param>
        /// <param name="stream">The stream to parse.</param>
        /// <param name="format">The format of the stream.</param>
        /// <param name="persistent"><c>true</c> if the stream is persistent; otherwise <c>false</c>.</param>
        /// <remarks>
        /// <para>If <paramref name="persistent"/> is <c>true</c> and <paramref name="stream"/> is seekable, then
        /// the <see cref="MimeParser"/> will not copy the content of <see cref="MimePart"/>s into memory. Instead,
        /// it will use a <see cref="MimeKit.IO.BoundStream"/> to reference a substream of <paramref name="stream"/>.
        /// This has the potential to not only save mmeory usage, but also improve <see cref="MimeParser"/>
        /// performance.</para>
        /// <para>It should be noted, however, that disposing <paramref name="stream"/> will make it impossible
        /// for <see cref="ContentObject"/> to read the content.</para>
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="options"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="stream"/> is <c>null</c>.</para>
        /// </exception>
        public MimeParser(ParserOptions options, Stream stream, MimeFormat format, bool persistent)
        {
            bounds = new List<Boundary> ();
            headers = new List<Header> ();

            SetStream (options, stream, format, persistent);
        }
Example #19
0
        /// <summary>
        /// Gets the redmine exceptions.
        /// </summary>
        /// <param name="webResponse">The web response.</param>
        /// <param name="mimeFormat">The MIME format.</param>
        /// <returns></returns>
        private static List <Error> GetRedmineExceptions(this WebResponse webResponse, MimeFormat mimeFormat)
        {
            using (var dataStream = webResponse.GetResponseStream())
            {
                if (dataStream == null)
                {
                    return(null);
                }
                using (var reader = new StreamReader(dataStream))
                {
                    var responseFromServer = reader.ReadToEnd();

                    if (string.IsNullOrEmpty(responseFromServer.Trim()))
                    {
                        return(null);
                    }
                    try
                    {
                        var result = RedmineSerializer.DeserializeList <Error>(responseFromServer, mimeFormat);
                        return(result.Objects);
                    }
                    catch (Exception ex)
                    {
                        Logger.Current.Error(ex.Message);
                    }
                }
                return(null);
            }
        }
Example #20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeKit.MimeParser"/> class.
 /// </summary>
 /// <param name="options">The parser options.</param>
 /// <param name="stream">The stream to parse.</param>
 /// <param name="format">The format of the stream.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <para><paramref name="options"/> is <c>null</c>.</para>
 /// <para>-or-</para>
 /// <para><paramref name="stream"/> is <c>null</c>.</para>
 /// </exception>
 public MimeParser(ParserOptions options, Stream stream, MimeFormat format)
     : this(options, stream, format, false)
 {
 }
Example #21
0
        internal static string Encode(MimeHeaderFragment fragment, MimeFormat format)
        {
            format = format ?? MimeFormat.Unspecified;

              return EncodeHeaderFieldBody(fragment, format.Folding - 1/*HT*/, 0, format.GetHeaderFoldingString());
        }
Example #22
0
 /// <summary>
 /// Sets the stream to parse.
 /// </summary>
 /// <param name="stream">The stream to parse.</param>
 /// <param name="format">The format of the stream.</param>
 /// <param name="persistent"><c>true</c> if the stream is persistent; otherwise <c>false</c>.</param>
 /// <remarks>
 /// <para>If <paramref name="persistent"/> is <c>true</c> and <paramref name="stream"/> is seekable, then
 /// the <see cref="MimeParser"/> will not copy the content of <see cref="MimePart"/>s into memory. Instead,
 /// it will use a <see cref="MimeKit.IO.BoundStream"/> to reference a substream of <paramref name="stream"/>.
 /// This has the potential to not only save mmeory usage, but also improve <see cref="MimeParser"/>
 /// performance.</para>
 /// <para>It should be noted, however, that disposing <paramref name="stream"/> will make it impossible
 /// for <see cref="ContentObject"/> to read the content.</para>
 /// </remarks>
 /// <exception cref="System.ArgumentNullException">
 /// <paramref name="stream"/> is <c>null</c>.
 /// </exception>
 public void SetStream(Stream stream, MimeFormat format, bool persistent)
 {
     SetStream (ParserOptions.Default, stream, format, persistent);
 }
Example #23
0
 /// <summary>
 /// Sets the stream to parse.
 /// </summary>
 /// <param name="options">The parser options.</param>
 /// <param name="stream">The stream to parse.</param>
 /// <param name="format">The format of the stream.</param>
 /// <exception cref="System.ArgumentNullException">
 /// <para><paramref name="options"/> is <c>null</c>.</para>
 /// <para>-or-</para>
 /// <para><paramref name="stream"/> is <c>null</c>.</para>
 /// </exception>
 public void SetStream(ParserOptions options, Stream stream, MimeFormat format)
 {
     SetStream (options, stream, format, false);
 }
Example #24
0
        /// <summary>
        /// Sets the stream to parse.
        /// </summary>
        /// <param name="options">The parser options.</param>
        /// <param name="stream">The stream to parse.</param>
        /// <param name="format">The format of the stream.</param>
        /// <param name="persistent"><c>true</c> if the stream is persistent; otherwise <c>false</c>.</param>
        /// <remarks>
        /// <para>If <paramref name="persistent"/> is <c>true</c> and <paramref name="stream"/> is seekable, then
        /// the <see cref="MimeParser"/> will not copy the content of <see cref="MimePart"/>s into memory. Instead,
        /// it will use a <see cref="MimeKit.IO.BoundStream"/> to reference a substream of <paramref name="stream"/>.
        /// This has the potential to not only save mmeory usage, but also improve <see cref="MimeParser"/>
        /// performance.</para>
        /// <para>It should be noted, however, that disposing <paramref name="stream"/> will make it impossible
        /// for <see cref="ContentObject"/> to read the content.</para>
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">
        /// <para><paramref name="options"/> is <c>null</c>.</para>
        /// <para>-or-</para>
        /// <para><paramref name="stream"/> is <c>null</c>.</para>
        /// </exception>
        public void SetStream(ParserOptions options, Stream stream, MimeFormat format, bool persistent)
        {
            if (options == null)
                throw new ArgumentNullException ("options");

            if (stream == null)
                throw new ArgumentNullException ("stream");

            this.persistent = persistent && stream.CanSeek;
            this.options = options.Clone ();
            this.format = format;
            this.stream = stream;

            inputIndex = inputStart;
            inputEnd = inputStart;

            mboxMarkerOffset = 0;
            mboxMarkerLength = 0;

            offset = stream.CanSeek ? stream.Position : 0;
            headers.Clear ();
            headerOffset = 0;
            headerIndex = 0;

            bounds.Clear ();
            if (format == MimeFormat.Mbox) {
                bounds.Add (Boundary.CreateMboxBoundary ());
                mboxMarkerBuffer = new byte[ReadAheadSize];
                state = MimeParserState.MboxMarker;
            } else {
                state = MimeParserState.Initialized;
            }
        }
 public QuotedPrintableContentEncodingStream(Stream stream, MimeFormat format)
     : base(stream, new ToQuotedPrintableTransform(), new byte[] {0x3d}, format)
 {
 }
Example #26
0
 /// <summary>
 /// Users the data.
 /// </summary>
 /// <param name="userId">The user identifier.</param>
 /// <param name="mimeFormat">The MIME format.</param>
 /// <returns></returns>
 public static string UserData(int userId, MimeFormat mimeFormat)
 {
     return(mimeFormat == MimeFormat.Xml
         ? $"<user_id>{userId}</user_id>"
         : $"{{\"user_id\":\"{userId}\"}}");
 }