Refactorings for the new IniConfig

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@853 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2010-08-24 09:54:32 +00:00
commit 14b0863bcd
34 changed files with 338 additions and 499 deletions

View file

@ -33,6 +33,7 @@ using Greenshot.Core;
/// See: http://confluence.atlassian.com/display/CONFDEV/Remote+API+Specification
/// </summary>
namespace Confluence {
#region transport classes
public class Page {
public Page(RemotePage page) {
id = page.id;
@ -42,101 +43,43 @@ namespace Confluence {
set;
}
}
#endregion
public class ConfluenceConnector {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ConfluenceConnector));
private const string CONFLUENCE_URL_PROPERTY = "url";
private const string CONFLUENCE_USER_PROPERTY = "user";
private const string CONFLUENCE_PASSWORD_PROPERTY = "password";
private const int DEFAULT_TIMEOUT = 29;
public const string CONFIG_FILENAME = "confluence.properties";
private const string DEFAULT_CONFLUENCE_URL = "http://confluence/rpc/soap-axis/confluenceservice-v1?wsdl";
private string configurationPath = null;
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 string tmpPassword = null;
private Properties config;
private ConfluenceConfiguration config;
private ConfluenceSoapServiceService confluence;
private Dictionary<string, string> userMap = new Dictionary<string, string>();
public ConfluenceConnector(string configurationPath) {
this.configurationPath = configurationPath;
this.config = LoadConfig();
this.config = IniConfig.GetIniSection<ConfluenceConfiguration>();
confluence = new ConfluenceSoapServiceService();
confluence.Url = config.GetProperty(CONFLUENCE_URL_PROPERTY);
confluence.Url = config.Url;
}
~ConfluenceConnector() {
logout();
}
public bool HasPassword() {
return config.ContainsKey(CONFLUENCE_PASSWORD_PROPERTY);
}
public void SetTmpPassword(string password) {
tmpPassword = password;
}
private Properties LoadConfig() {
Properties config = null;
string filename = Path.Combine(configurationPath, CONFIG_FILENAME);
if (File.Exists(filename)) {
LOG.Debug("Loading configuration from: " + filename);
config = Properties.read(filename);
}
bool changed = false;
if (config == null) {
config = new Properties();
changed = true;
}
if (!config.ContainsKey(CONFLUENCE_URL_PROPERTY)) {
config.AddProperty(CONFLUENCE_URL_PROPERTY, DEFAULT_CONFLUENCE_URL);
changed = true;
}
if (!config.ContainsKey(CONFLUENCE_USER_PROPERTY)) {
config.AddProperty(CONFLUENCE_USER_PROPERTY, Environment.UserName);
changed = true;
}
if (changed) {
SaveConfig(config);
}
return config;
}
private void SaveConfig(Properties config) {
string filename = Path.Combine(configurationPath, CONFIG_FILENAME);
LOG.Debug("Saving configuration to: " + filename);
StringBuilder comment = new StringBuilder();
comment.AppendLine("# The configuration file for the Confluence Plugin");
comment.AppendLine("#");
comment.AppendLine("# Example settings:");
comment.AppendLine("# " + CONFLUENCE_URL_PROPERTY + "=" + DEFAULT_CONFLUENCE_URL);
comment.AppendLine("# " + CONFLUENCE_USER_PROPERTY + "=Username");
config.write(filename, comment.ToString());
}
public void login() {
logout();
try {
if (HasPassword()) {
this.credentials = confluence.login(config.GetProperty(CONFLUENCE_USER_PROPERTY), config.GetProperty(CONFLUENCE_PASSWORD_PROPERTY));
} else if (tmpPassword != null) {
this.credentials = confluence.login(config.GetProperty(CONFLUENCE_USER_PROPERTY), tmpPassword);
if (config.HasPassword()) {
this.credentials = confluence.login(config.User, config.Password);
} else if (config.HasTmpPassword()) {
this.credentials = confluence.login(config.User, config.TmpPassword);
} else {
LoginForm pwForm = new LoginForm();
pwForm.User = config.GetProperty(CONFLUENCE_USER_PROPERTY);
pwForm.Url = config.GetProperty(CONFLUENCE_URL_PROPERTY);
DialogResult result = pwForm.ShowDialog();
if (result == DialogResult.OK) {
tmpPassword = pwForm.Password;
if (!pwForm.User.Equals(config.GetProperty(CONFLUENCE_USER_PROPERTY)) ||!pwForm.Url.Equals(config.GetProperty(CONFLUENCE_URL_PROPERTY))) {
config.ChangeProperty(CONFLUENCE_USER_PROPERTY, pwForm.User);
config.ChangeProperty(CONFLUENCE_URL_PROPERTY, pwForm.Url);
confluence.Url = config.GetProperty(CONFLUENCE_URL_PROPERTY);
SaveConfig(config);
}
this.credentials = confluence.login(config.GetProperty(CONFLUENCE_USER_PROPERTY), tmpPassword);
if (config.ShowConfigDialog()) {
if (config.HasPassword()) {
this.credentials = confluence.login(config.User, config.Password);
} else if (config.HasTmpPassword()) {
this.credentials = confluence.login(config.User, config.TmpPassword);
}
} else {
throw new Exception("User pressed cancel!");
}
@ -146,12 +89,13 @@ namespace Confluence {
} catch (Exception e) {
this.loggedIn = false;
this.credentials = null;
e.Data.Add("user",config.GetProperty(CONFLUENCE_USER_PROPERTY));
e.Data.Add("url",config.GetProperty(CONFLUENCE_URL_PROPERTY));
if (e.Message.Contains("com.atlassian.confluence.rpc.AuthenticationFailedException")) {
e.Data.Add("user",config.User);
e.Data.Add("url",config.Url);
if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) {
// Login failed due to wrong user or password, password should be removed!
this.tmpPassword = null;
throw new Exception(e.Message.Replace("com.atlassian.confluence.rpc.AuthenticationFailedException: ",""));
config.Password = null;
config.TmpPassword = null;
throw new Exception(e.Message.Replace(AUTH_FAILED_EXCEPTION_NAME+ ": ",""));
}
throw e;
}
@ -167,7 +111,7 @@ namespace Confluence {
private void checkCredentials() {
if (loggedIn) {
if (loggedInTime.AddMinutes(DEFAULT_TIMEOUT).CompareTo(DateTime.Now) < 0) {
if (loggedInTime.AddMinutes(config.Timeout-1).CompareTo(DateTime.Now) < 0) {
logout();
login();
}