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); }
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); } }
/// <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)); }
/// <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)); }
/// <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; } }
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)); } }
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; } }
/// <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); }
/// <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) { }
/// <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) { }
} // 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); } }
/// <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 + "\"}"; }
/// <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) { }
/// <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); }
/// <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); } }
/// <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) { }
internal static string Encode(MimeHeaderFragment fragment, MimeFormat format) { format = format ?? MimeFormat.Unspecified; return EncodeHeaderFieldBody(fragment, format.Folding - 1/*HT*/, 0, format.GetHeaderFoldingString()); }
/// <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); }
/// <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); }
/// <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) { }
/// <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}\"}}"); }