diff --git a/GreenshotConfluencePlugin/Confluence.cs b/GreenshotConfluencePlugin/Confluence.cs index 212cf0100..2e0589230 100644 --- a/GreenshotConfluencePlugin/Confluence.cs +++ b/GreenshotConfluencePlugin/Confluence.cs @@ -28,6 +28,7 @@ using System.Windows.Forms; using Greenshot.Core; using Greenshot.Helpers; using GreenshotConfluencePlugin; +using GreenshotCore.Helpers; /// /// For details see the Confluence API site @@ -47,57 +48,58 @@ namespace Confluence { #endregion public class ConfluenceConnector { - private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ConfluenceConnector)); + private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ConfluenceConnector)); private const string AUTH_FAILED_EXCEPTION_NAME = "com.atlassian.confluence.rpc.AuthenticationFailedException"; - private string credentials = null; - private DateTime loggedInTime = DateTime.Now; - private bool loggedIn = false; - private ConfluenceSoapServiceService confluence; - private int timeout; - private string url; - private Dictionary userMap = new Dictionary(); + private string credentials = null; + private DateTime loggedInTime = DateTime.Now; + private bool loggedIn = false; + private ConfluenceSoapServiceService confluence; + private int timeout; + private string url; + private Dictionary userMap = new Dictionary(); - public ConfluenceConnector(string url, int timeout) { - this.url = url; - this.timeout = timeout; - confluence = new ConfluenceSoapServiceService(); - confluence.Url = url; - } + public ConfluenceConnector(string url, int timeout) { + this.url = url; + this.timeout = timeout; + confluence = new ConfluenceSoapServiceService(); + confluence.Url = url; + confluence.Proxy = NetworkHelper.CreateProxy(new Uri(url)); + } - ~ConfluenceConnector() { - logout(); - } - /// - /// Internal login which catches the exceptions - /// - /// true if login was done sucessfully - private bool doLogin(string user, string password) { - try { - this.credentials = confluence.login(user, password); - this.loggedInTime = DateTime.Now; + ~ConfluenceConnector() { + logout(); + } + /// + /// Internal login which catches the exceptions + /// + /// true if login was done sucessfully + private bool doLogin(string user, string password) { + try { + this.credentials = confluence.login(user, password); + this.loggedInTime = DateTime.Now; this.loggedIn = true; - } catch (Exception e) { - // check if auth failed - if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) { + } catch (Exception e) { + // check if auth failed + if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) { return false; - } - // Not an authentication issue - this.loggedIn = false; - this.credentials = null; - e.Data.Add("user", user); - e.Data.Add("url", url); - throw e; - } - return true; - } + } + // Not an authentication issue + this.loggedIn = false; + this.credentials = null; + e.Data.Add("user", user); + e.Data.Add("url", url); + throw e; + } + return true; + } - public void login() { - logout(); - try { - // Get the system name, so the user knows where to login to - string systemName = url.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX,""); - CredentialsDialog dialog = new CredentialsDialog(systemName); + public void login() { + logout(); + try { + // Get the system name, so the user knows where to login to + string systemName = url.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX,""); + CredentialsDialog dialog = new CredentialsDialog(systemName); dialog.Name = null; while (dialog.Show(dialog.Name) == DialogResult.OK) { if (doLogin(dialog.Name, dialog.Password)) { @@ -122,45 +124,45 @@ namespace Confluence { // exception handling ... LOG.Error("Problem using the credentials dialog", e); } - } + } - public void logout() { - if (credentials != null) { - confluence.logout(credentials); - credentials = null; - loggedIn = false; - } - } + public void logout() { + if (credentials != null) { + confluence.logout(credentials); + credentials = null; + loggedIn = false; + } + } - private void checkCredentials() { - if (loggedIn) { + private void checkCredentials() { + if (loggedIn) { if (loggedInTime.AddMinutes(timeout-1).CompareTo(DateTime.Now) < 0) { - logout(); - login(); - } - } else { - login(); - } - } + logout(); + login(); + } + } else { + login(); + } + } - public bool isLoggedIn() { - return loggedIn; - } - - public void addAttachment(long pageId, string mime, string comment, string filename, byte[] buffer) { - checkCredentials(); + public bool isLoggedIn() { + return loggedIn; + } + + public void addAttachment(long pageId, string mime, string comment, string filename, byte[] buffer) { + checkCredentials(); RemoteAttachment attachment = new RemoteAttachment(); // Comment is ignored, see: http://jira.atlassian.com/browse/CONF-9395 attachment.comment = comment; attachment.fileName = filename; attachment.contentType = mime; confluence.addAttachment(credentials, pageId, attachment, buffer); - } - - public Page getPage(string spaceKey, string pageTitle) { - checkCredentials(); - RemotePage page = confluence.getPage(credentials, spaceKey, pageTitle); - return new Page(page); - } - } + } + + public Page getPage(string spaceKey, string pageTitle) { + checkCredentials(); + RemotePage page = confluence.getPage(credentials, spaceKey, pageTitle); + return new Page(page); + } + } } diff --git a/GreenshotCore/GreenshotCore.csproj b/GreenshotCore/GreenshotCore.csproj index 636cc2e72..f5e41ae81 100644 --- a/GreenshotCore/GreenshotCore.csproj +++ b/GreenshotCore/GreenshotCore.csproj @@ -98,6 +98,7 @@ + diff --git a/GreenshotCore/Helpers/NetworkHelper.cs b/GreenshotCore/Helpers/NetworkHelper.cs new file mode 100644 index 000000000..0af5f5a25 --- /dev/null +++ b/GreenshotCore/Helpers/NetworkHelper.cs @@ -0,0 +1,90 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +using System; +using System.Drawing; +using System.Net; + +using Greenshot.Core; + +namespace GreenshotCore.Helpers { + /// + /// Description of NetworkHelper. + /// + public class NetworkHelper { + private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(NetworkHelper)); + private static CoreConfiguration config = IniConfig.GetIniSection(); + + /// + /// Download the FavIcon as a Bitmap + /// + /// + /// Bitmap with the FavIcon + public static Bitmap DownloadFavIcon(Uri baseUri) { + Uri url = new Uri(baseUri, new Uri("favicon.ico")); + try { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Proxy = NetworkHelper.CreateProxy(url); + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + if (request.HaveResponse) { + Image image = Image.FromStream(response.GetResponseStream()); + return (image.Height > 16 && image.Width > 16) ? new Bitmap(image, 16, 16) : new Bitmap(image); + } + + } catch (Exception e) { + LOG.Error("Problem downloading the FavIcon from: " + baseUri.ToString(), e); + } + return null; + } + + /// + /// Create a IWebProxy Object which can be used to access the Internet + /// This method will check the configuration if the proxy is allowed to be used. + /// Usages can be found in the DownloadFavIcon or Jira and Confluence plugins + /// + /// + /// IWebProxy filled with all the proxy details or null if none is set/wanted + public static IWebProxy CreateProxy(Uri uri) { + IWebProxy proxyToUse = null; + if (config.UseProxy) { + proxyToUse = WebRequest.DefaultWebProxy; + if (proxyToUse != null) { + proxyToUse.Credentials = CredentialCache.DefaultCredentials; + if (LOG.IsDebugEnabled) { + // check the proxy for the Uri + if (!proxyToUse.IsBypassed(uri)) { + Uri proxyUri = proxyToUse.GetProxy(uri); + if (proxyUri != null) { + LOG.Debug("Using proxy: " + proxyUri.ToString() + " for " + uri.ToString()); + } else { + LOG.Debug("No proxy found!"); + } + } else { + LOG.Debug("Proxy bypass for: " + uri.ToString()); + } + } + } else { + LOG.Debug("No proxy found!"); + } + } + return proxyToUse; + } + } +} diff --git a/GreenshotJiraPlugin/Jira.cs b/GreenshotJiraPlugin/Jira.cs index a75335b2a..632cda052 100644 --- a/GreenshotJiraPlugin/Jira.cs +++ b/GreenshotJiraPlugin/Jira.cs @@ -22,72 +22,77 @@ using System; using System.Collections; using System.Collections.Generic; using System.IO; +using System.Net; using System.Text; using System.Windows.Forms; using Greenshot.Core; using Greenshot.Helpers; +using GreenshotCore.Helpers; using GreenshotJiraPlugin; namespace Jira { #region transport classes public class JiraFilter { public JiraFilter(string name, string id) { - this.name = name; - this.id = id; + this.Name = name; + this.Id = id; } - public string name { + public string Name { get; set; } - public string id; - } + public string Id { + get; + set; + } + } public class JiraIssue { public JiraIssue(string key, DateTime? created, string reporter, string assignee, string project, string summary, string description, string environment, string [] attachmentNames) { - this.key = key; - this.created = created; - this.reporter = reporter; - this.assignee = assignee; - this.project = project; - this.summary = summary; - this.description = description; - this.environment = environment; - this.attachmentNames = attachmentNames; + this.Key = key; + this.Created = created; + this.Reporter = reporter; + this.Assignee = assignee; + this.Project = project; + this.Summary = summary; + this.Description = description; + this.Environment = environment; + this.AttachmentNames = attachmentNames; } - public string key { + public string Key { get; private set; } - public DateTime? created { + public DateTime? Created { get; private set; } - public string reporter { + public string Reporter { get; private set; } - public string assignee { + public string Assignee { get; private set; } - public string project { + public string Project { get; private set; } - public string summary { + public string Summary { get; private set; } - public string description { + public string Description { get; private set; } - public string environment { + public string Environment { get; private set; } - public string[] attachmentNames { + public string[] AttachmentNames { get; private set; } @@ -95,11 +100,11 @@ namespace Jira { #endregion public class JiraConnector { - private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(JiraConnector)); + private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(JiraConnector)); private const string AUTH_FAILED_EXCEPTION_NAME = "com.atlassian.jira.rpc.exception.RemoteAuthenticationException"; - private string credentials = null; + private string credentials; private DateTime loggedInTime = DateTime.Now; - private bool loggedIn = false; + private bool loggedIn; private JiraSoapServiceService jira; private int timeout; private string url; @@ -110,6 +115,7 @@ namespace Jira { this.timeout = timeout; jira = new JiraSoapServiceService(); jira.Url = url; + jira.Proxy = NetworkHelper.CreateProxy(new Uri(url)); } ~JiraConnector() { @@ -135,7 +141,7 @@ namespace Jira { this.credentials = null; e.Data.Add("user", user); e.Data.Add("url", url); - throw e; + throw; } return true; }