/// <summary> /// Gets a JSON object from the JIRA server using an HTTP GET /// </summary> /// <param name="server">The URL of the server e.g. http://myserver.com</param> /// <param name="path">The REST path send the request to e.g. /rest/api/2/project</param> /// <param name="username">The user ID required for authentication. null or empty will use anonymous access.</param> /// <param name="password">The password required for authentication encoded using Base64. null or empty will use anonymous access.</param> /// <returns>A JSON object containing the results</returns> internal static JToken GetJson(string server, string path, string username, Base64String password, out string error) { JToken result; error = null; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(server + path); httpWebRequest.ContentType = "application/json"; httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.Method = "GET"; httpWebRequest.UserAgent = agent; httpWebRequest.Proxy = WebRequest.GetSystemWebProxy(); // Add the required authentication header if (!string.IsNullOrEmpty(username) && password.Length > 0) { Base64String authInfo = password.prefix(username + ":"); httpWebRequest.Headers["Authorization"] = "Basic " + authInfo; } try { HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (JsonTextReader streamReader = new JsonTextReader(new System.IO.StreamReader(httpResponse.GetResponseStream()))) { result = JToken.ReadFrom(streamReader); } return(result); } catch (WebException e) { error = e.Message; return(null); } }
/// <summary> /// Construct new model by copying the contents of another model. /// </summary> /// <param name="other">The other model to copy from.</param> public JiraConnectionModel(JiraConnectionModel other) { this.server_name = other.ServerName; this.user = other.User; this.password = other.Password; this.selected_project = other.SelectedProject; }
/// <summary> /// Called when the value of the property the PasswordBox is bound to changes. /// We'll update the bound property with the updated password value, unless we're /// already in the middle of updating it because the user changed it in the UI. /// </summary> /// <param name="d">The PasswordBox whose value was changed.</param> /// <param name="e">The event arguments</param> private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { PasswordBox box = d as PasswordBox; // only handle this event when the property is attached to a PasswordBox // and when the BindPassword attached property has been set to true if (box == null || !GetBindPassword(box)) { return; } // avoid recursive updating by ignoring the box's changed event box.PasswordChanged -= HandlePasswordChanged; Base64String newPassword = (Base64String)e.NewValue; if (!GetUpdatingPassword(box)) { box.Password = newPassword; } box.PasswordChanged += HandlePasswordChanged; }
public static void SetBoundPassword(DependencyObject dp, Base64String value) { dp.SetValue(BoundPassword, value); }
/// <summary> /// Sends a JSON object to a JIRA server using an HTTP POST /// and gets the JSON response from the server. /// </summary> /// <param name="server"></param> /// <param name="path">The REST path send the request to e.g. /rest/api/2/project</param> /// <param name="username">The user ID required for authentication. null or empty will use anonymous access.</param> /// <param name="password">The password required for authentication. null or empty will use anonymous access.</param> /// <param name="json_request">The JSON object to include in the POST</param> /// <returns></returns> internal static JToken PostJson(string server, string path, string username, Base64String password, JToken json_request, out string error) { JToken result; error = null; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(server + path); httpWebRequest.ContentType = "application/json"; httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.Method = "POST"; httpWebRequest.UserAgent = agent; httpWebRequest.Proxy = WebRequest.GetSystemWebProxy(); // Add the required authentication header if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password)) { Base64String authInfo = password.prefix(username + ":"); httpWebRequest.Headers["Authorization"] = "Basic " + authInfo; } if (null != json_request) { using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write(json_request.ToString()); streamWriter.Flush(); } } try { HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (JsonTextReader streamReader = new JsonTextReader(new System.IO.StreamReader(httpResponse.GetResponseStream()))) { result = JToken.ReadFrom(streamReader); } return result; } catch (WebException e) { error = e.Message; return null; } }
public JiraComm(string server, string user, Base64String password) { this.Server = server; this.User = user; this.Password = password; }
/// <summary> /// Attach a file to a JIRA issue (bug). /// <seealso cref="https://docs.atlassian.com/jira/REST/latest/#idp1726000"/> /// </summary> /// <param name="issueKey">THe key of the bug to attach the file to e.g. TSCB-14.</param> /// <param name="attachment">The declaration of the file to be attached.</param> /// <returns>A FileAttached object.</returns> internal static JiraFileAttached AttachFile(string server, string user, Base64String password, string issueKey, IBugAttachment attachment, out string error) { JiraFileAttached fileAttached = new JiraFileAttached(); string boundaryMarker = "-------------------------" + DateTime.Now.Ticks.ToString(); HttpWebRequest httpWebRequest = HttpWebRequest.Create(server + "/rest/api/2/issue/" + issueKey + "/attachments") as HttpWebRequest; httpWebRequest.Method = "POST"; httpWebRequest.UserAgent = agent; httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundaryMarker; httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.Headers.Add("X-Atlassian-Token: nocheck"); httpWebRequest.KeepAlive = true; httpWebRequest.Proxy = WebRequest.GetSystemWebProxy(); // Add the required authentication header if (!string.IsNullOrEmpty(user) && password.Length > 0) { Base64String authInfo = password.prefix(user + ":"); httpWebRequest.Headers["Authorization"] = "Basic " + authInfo; } FileInfo fi = new FileInfo(attachment.FileName); string contentHeader = "--" + boundaryMarker + "\r\n"; contentHeader += "Content-Disposition: form-data; name=\"file\"; filename=\"" + HttpUtility.HtmlEncode(fi.Name) + "\"\r\n"; contentHeader += "Content-Type: application/octet-stream\r\n"; //TODO If attaching something other than .zip files, this will have to change. contentHeader += "\r\n"; byte[] contentHeaderBytes = System.Text.Encoding.UTF8.GetBytes(contentHeader); try { FileStream fs = new FileStream(attachment.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] requestBytes = new byte[65536]; int bytesRead = 0; using (Stream requestStream = httpWebRequest.GetRequestStream()) { requestStream.Write(contentHeaderBytes, 0, contentHeaderBytes.Length); while ((bytesRead = fs.Read(requestBytes, 0, requestBytes.Length)) != 0) { requestStream.Write(requestBytes, 0, bytesRead); } fs.Close(); string closingBoundary = "\r\n--" + boundaryMarker + "--"; byte[] closingBoundaryBytes = System.Text.Encoding.UTF8.GetBytes(closingBoundary); requestStream.Write(closingBoundaryBytes, 0, closingBoundaryBytes.Length); } } catch (FileNotFoundException e) { error = e.Message; return null; } //READ RESPONSE FROM STREAM try { using (JsonTextReader streamReader = new JsonTextReader(new StreamReader(httpWebRequest.GetResponse().GetResponseStream()))) { // JIRA supports attaching multiple files at once. Hence it returns an array of FileAttached objects. // Since we only attach one file at a time, we only need to parse the first one and return it. JArray result = (JArray)JArray.ReadFrom(streamReader); JsonConvert.PopulateObject(result[0].ToString(), fileAttached); } error = null; return fileAttached; } catch (WebException e) { error = e.Message; // Get the HTML response returned, if any //StreamReader rdr = new StreamReader(e.Response.GetResponseStream()); //string response = rdr.ReadToEnd(); return null; } }
/// <summary> /// Constructor that copies another Base64String value /// </summary> /// <param name="encodedString"></param> public Base64String(Base64String encodedString) { this.base64value = encodedString.base64value; }
/// <summary> /// Attach a file to a JIRA issue (bug). /// <seealso cref="https://docs.atlassian.com/jira/REST/latest/#idp1726000"/> /// </summary> /// <param name="issueKey">THe key of the bug to attach the file to e.g. TSCB-14.</param> /// <param name="attachment">The declaration of the file to be attached.</param> /// <returns>A FileAttached object.</returns> internal static JiraFileAttached AttachFile(string server, string user, Base64String password, string issueKey, IBugAttachment attachment, out string error) { JiraFileAttached fileAttached = new JiraFileAttached(); string boundaryMarker = "-------------------------" + DateTime.Now.Ticks.ToString(); HttpWebRequest httpWebRequest = HttpWebRequest.Create(server + "/rest/api/2/issue/" + issueKey + "/attachments") as HttpWebRequest; httpWebRequest.Method = "POST"; httpWebRequest.UserAgent = agent; httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundaryMarker; httpWebRequest.ProtocolVersion = HttpVersion.Version11; httpWebRequest.Headers.Add("X-Atlassian-Token: nocheck"); httpWebRequest.KeepAlive = true; httpWebRequest.Proxy = WebRequest.GetSystemWebProxy(); // Add the required authentication header if (!string.IsNullOrEmpty(user) && password.Length > 0) { Base64String authInfo = password.prefix(user + ":"); httpWebRequest.Headers["Authorization"] = "Basic " + authInfo; } FileInfo fi = new FileInfo(attachment.FileName); string contentHeader = "--" + boundaryMarker + "\r\n"; contentHeader += "Content-Disposition: form-data; name=\"file\"; filename=\"" + HttpUtility.HtmlEncode(fi.Name) + "\"\r\n"; contentHeader += "Content-Type: application/octet-stream\r\n"; //TODO If attaching something other than .zip files, this will have to change. contentHeader += "\r\n"; byte[] contentHeaderBytes = System.Text.Encoding.UTF8.GetBytes(contentHeader); try { FileStream fs = new FileStream(attachment.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] requestBytes = new byte[65536]; int bytesRead = 0; using (Stream requestStream = httpWebRequest.GetRequestStream()) { requestStream.Write(contentHeaderBytes, 0, contentHeaderBytes.Length); while ((bytesRead = fs.Read(requestBytes, 0, requestBytes.Length)) != 0) { requestStream.Write(requestBytes, 0, bytesRead); } fs.Close(); string closingBoundary = "\r\n--" + boundaryMarker + "--"; byte[] closingBoundaryBytes = System.Text.Encoding.UTF8.GetBytes(closingBoundary); requestStream.Write(closingBoundaryBytes, 0, closingBoundaryBytes.Length); } } catch (FileNotFoundException e) { error = e.Message; return(null); } //READ RESPONSE FROM STREAM try { using (JsonTextReader streamReader = new JsonTextReader(new StreamReader(httpWebRequest.GetResponse().GetResponseStream()))) { // JIRA supports attaching multiple files at once. Hence it returns an array of FileAttached objects. // Since we only attach one file at a time, we only need to parse the first one and return it. JArray result = (JArray)JArray.ReadFrom(streamReader); JsonConvert.PopulateObject(result[0].ToString(), fileAttached); } error = null; return(fileAttached); } catch (WebException e) { error = e.Message; // Get the HTML response returned, if any //StreamReader rdr = new StreamReader(e.Response.GetResponseStream()); //string response = rdr.ReadToEnd(); return(null); } }