Added Proxy support

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@899 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2010-09-23 08:59:52 +00:00
commit 2cc6277ce2
4 changed files with 201 additions and 102 deletions

View file

@ -28,6 +28,7 @@ using System.Windows.Forms;
using Greenshot.Core; using Greenshot.Core;
using Greenshot.Helpers; using Greenshot.Helpers;
using GreenshotConfluencePlugin; using GreenshotConfluencePlugin;
using GreenshotCore.Helpers;
/// <summary> /// <summary>
/// For details see the Confluence API site /// For details see the Confluence API site
@ -47,57 +48,58 @@ namespace Confluence {
#endregion #endregion
public class ConfluenceConnector { 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 const string AUTH_FAILED_EXCEPTION_NAME = "com.atlassian.confluence.rpc.AuthenticationFailedException";
private string credentials = null; private string credentials = null;
private DateTime loggedInTime = DateTime.Now; private DateTime loggedInTime = DateTime.Now;
private bool loggedIn = false; private bool loggedIn = false;
private ConfluenceSoapServiceService confluence; private ConfluenceSoapServiceService confluence;
private int timeout; private int timeout;
private string url; private string url;
private Dictionary<string, string> userMap = new Dictionary<string, string>(); private Dictionary<string, string> userMap = new Dictionary<string, string>();
public ConfluenceConnector(string url, int timeout) { public ConfluenceConnector(string url, int timeout) {
this.url = url; this.url = url;
this.timeout = timeout; this.timeout = timeout;
confluence = new ConfluenceSoapServiceService(); confluence = new ConfluenceSoapServiceService();
confluence.Url = url; confluence.Url = url;
} confluence.Proxy = NetworkHelper.CreateProxy(new Uri(url));
}
~ConfluenceConnector() { ~ConfluenceConnector() {
logout(); logout();
} }
/// <summary> /// <summary>
/// Internal login which catches the exceptions /// Internal login which catches the exceptions
/// </summary> /// </summary>
/// <returns>true if login was done sucessfully</returns> /// <returns>true if login was done sucessfully</returns>
private bool doLogin(string user, string password) { private bool doLogin(string user, string password) {
try { try {
this.credentials = confluence.login(user, password); this.credentials = confluence.login(user, password);
this.loggedInTime = DateTime.Now; this.loggedInTime = DateTime.Now;
this.loggedIn = true; this.loggedIn = true;
} catch (Exception e) { } catch (Exception e) {
// check if auth failed // check if auth failed
if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) { if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) {
return false; return false;
} }
// Not an authentication issue // Not an authentication issue
this.loggedIn = false; this.loggedIn = false;
this.credentials = null; this.credentials = null;
e.Data.Add("user", user); e.Data.Add("user", user);
e.Data.Add("url", url); e.Data.Add("url", url);
throw e; throw e;
} }
return true; return true;
} }
public void login() { public void login() {
logout(); logout();
try { try {
// Get the system name, so the user knows where to login to // Get the system name, so the user knows where to login to
string systemName = url.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX,""); string systemName = url.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX,"");
CredentialsDialog dialog = new CredentialsDialog(systemName); CredentialsDialog dialog = new CredentialsDialog(systemName);
dialog.Name = null; dialog.Name = null;
while (dialog.Show(dialog.Name) == DialogResult.OK) { while (dialog.Show(dialog.Name) == DialogResult.OK) {
if (doLogin(dialog.Name, dialog.Password)) { if (doLogin(dialog.Name, dialog.Password)) {
@ -122,45 +124,45 @@ namespace Confluence {
// exception handling ... // exception handling ...
LOG.Error("Problem using the credentials dialog", e); LOG.Error("Problem using the credentials dialog", e);
} }
} }
public void logout() { public void logout() {
if (credentials != null) { if (credentials != null) {
confluence.logout(credentials); confluence.logout(credentials);
credentials = null; credentials = null;
loggedIn = false; loggedIn = false;
} }
} }
private void checkCredentials() { private void checkCredentials() {
if (loggedIn) { if (loggedIn) {
if (loggedInTime.AddMinutes(timeout-1).CompareTo(DateTime.Now) < 0) { if (loggedInTime.AddMinutes(timeout-1).CompareTo(DateTime.Now) < 0) {
logout(); logout();
login(); login();
} }
} else { } else {
login(); login();
} }
} }
public bool isLoggedIn() { public bool isLoggedIn() {
return loggedIn; return loggedIn;
} }
public void addAttachment(long pageId, string mime, string comment, string filename, byte[] buffer) { public void addAttachment(long pageId, string mime, string comment, string filename, byte[] buffer) {
checkCredentials(); checkCredentials();
RemoteAttachment attachment = new RemoteAttachment(); RemoteAttachment attachment = new RemoteAttachment();
// Comment is ignored, see: http://jira.atlassian.com/browse/CONF-9395 // Comment is ignored, see: http://jira.atlassian.com/browse/CONF-9395
attachment.comment = comment; attachment.comment = comment;
attachment.fileName = filename; attachment.fileName = filename;
attachment.contentType = mime; attachment.contentType = mime;
confluence.addAttachment(credentials, pageId, attachment, buffer); confluence.addAttachment(credentials, pageId, attachment, buffer);
} }
public Page getPage(string spaceKey, string pageTitle) { public Page getPage(string spaceKey, string pageTitle) {
checkCredentials(); checkCredentials();
RemotePage page = confluence.getPage(credentials, spaceKey, pageTitle); RemotePage page = confluence.getPage(credentials, spaceKey, pageTitle);
return new Page(page); return new Page(page);
} }
} }
} }

View file

@ -98,6 +98,7 @@
<Compile Include="Helpers\ImageHelper.cs" /> <Compile Include="Helpers\ImageHelper.cs" />
<Compile Include="Helpers\ImageOutput.cs" /> <Compile Include="Helpers\ImageOutput.cs" />
<Compile Include="Helpers\MailHelper.cs" /> <Compile Include="Helpers\MailHelper.cs" />
<Compile Include="Helpers\NetworkHelper.cs" />
<Compile Include="Helpers\Objects.cs" /> <Compile Include="Helpers\Objects.cs" />
<Compile Include="Helpers\PluginHelper.cs" /> <Compile Include="Helpers\PluginHelper.cs" />
<Compile Include="Helpers\PrintHelper.cs" /> <Compile Include="Helpers\PrintHelper.cs" />

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
using System;
using System.Drawing;
using System.Net;
using Greenshot.Core;
namespace GreenshotCore.Helpers {
/// <summary>
/// Description of NetworkHelper.
/// </summary>
public class NetworkHelper {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(NetworkHelper));
private static CoreConfiguration config = IniConfig.GetIniSection<CoreConfiguration>();
/// <summary>
/// Download the FavIcon as a Bitmap
/// </summary>
/// <param name="baseUri"></param>
/// <returns>Bitmap with the FavIcon</returns>
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;
}
/// <summary>
/// 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
/// </summary>
/// <param name="url"></param>
/// <returns>IWebProxy filled with all the proxy details or null if none is set/wanted</returns>
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;
}
}
}

View file

@ -22,72 +22,77 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using Greenshot.Core; using Greenshot.Core;
using Greenshot.Helpers; using Greenshot.Helpers;
using GreenshotCore.Helpers;
using GreenshotJiraPlugin; using GreenshotJiraPlugin;
namespace Jira { namespace Jira {
#region transport classes #region transport classes
public class JiraFilter { public class JiraFilter {
public JiraFilter(string name, string id) { public JiraFilter(string name, string id) {
this.name = name; this.Name = name;
this.id = id; this.Id = id;
} }
public string name { public string Name {
get; get;
set; set;
} }
public string id; public string Id {
} get;
set;
}
}
public class JiraIssue { public class JiraIssue {
public JiraIssue(string key, DateTime? created, string reporter, string assignee, string project, string summary, string description, string environment, string [] attachmentNames) { public JiraIssue(string key, DateTime? created, string reporter, string assignee, string project, string summary, string description, string environment, string [] attachmentNames) {
this.key = key; this.Key = key;
this.created = created; this.Created = created;
this.reporter = reporter; this.Reporter = reporter;
this.assignee = assignee; this.Assignee = assignee;
this.project = project; this.Project = project;
this.summary = summary; this.Summary = summary;
this.description = description; this.Description = description;
this.environment = environment; this.Environment = environment;
this.attachmentNames = attachmentNames; this.AttachmentNames = attachmentNames;
} }
public string key { public string Key {
get; get;
private set; private set;
} }
public DateTime? created { public DateTime? Created {
get; get;
private set; private set;
} }
public string reporter { public string Reporter {
get; get;
private set; private set;
} }
public string assignee { public string Assignee {
get; get;
private set; private set;
} }
public string project { public string Project {
get; get;
private set; private set;
} }
public string summary { public string Summary {
get; get;
private set; private set;
} }
public string description { public string Description {
get; get;
private set; private set;
} }
public string environment { public string Environment {
get; get;
private set; private set;
} }
public string[] attachmentNames { public string[] AttachmentNames {
get; get;
private set; private set;
} }
@ -95,11 +100,11 @@ namespace Jira {
#endregion #endregion
public class JiraConnector { 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 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 DateTime loggedInTime = DateTime.Now;
private bool loggedIn = false; private bool loggedIn;
private JiraSoapServiceService jira; private JiraSoapServiceService jira;
private int timeout; private int timeout;
private string url; private string url;
@ -110,6 +115,7 @@ namespace Jira {
this.timeout = timeout; this.timeout = timeout;
jira = new JiraSoapServiceService(); jira = new JiraSoapServiceService();
jira.Url = url; jira.Url = url;
jira.Proxy = NetworkHelper.CreateProxy(new Uri(url));
} }
~JiraConnector() { ~JiraConnector() {
@ -135,7 +141,7 @@ namespace Jira {
this.credentials = null; this.credentials = null;
e.Data.Add("user", user); e.Data.Add("user", user);
e.Data.Add("url", url); e.Data.Add("url", url);
throw e; throw;
} }
return true; return true;
} }