Code quality changes [skip ci]

This commit is contained in:
Robin 2016-08-16 10:37:55 +02:00
commit 798ca503a5
108 changed files with 1981 additions and 2258 deletions

View file

@ -26,22 +26,18 @@ using GreenshotConfluencePlugin;
using GreenshotConfluencePlugin.confluence;
using GreenshotPlugin.Core;
/// <summary>
/// For details see the Confluence API site
/// 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;
Id = page.id;
Title = page.title;
SpaceKey = page.space;
Url = page.url;
Content = page.content;
}
public Page(RemoteSearchResult searchResult, string space) {
id = searchResult.id;
Id = searchResult.id;
Title = searchResult.title;
SpaceKey = space;
Url = searchResult.url;
@ -49,12 +45,12 @@ namespace Confluence {
}
public Page(RemotePageSummary pageSummary) {
id = pageSummary.id;
Id = pageSummary.id;
Title = pageSummary.title;
SpaceKey = pageSummary.space;
Url =pageSummary.url;
}
public long id {
public long Id {
get;
set;
}
@ -91,46 +87,52 @@ namespace Confluence {
}
#endregion
/// <summary>
/// For details see the Confluence API site
/// See: http://confluence.atlassian.com/display/CONFDEV/Remote+API+Specification
/// </summary>
public class ConfluenceConnector : IDisposable {
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 V2_FAILED = "AXIS";
private static readonly ConfluenceConfiguration config = IniConfig.GetIniSection<ConfluenceConfiguration>();
private string credentials = null;
private DateTime loggedInTime = DateTime.Now;
private bool loggedIn = false;
private ConfluenceSoapServiceService confluence;
private readonly int timeout;
private string url;
private readonly Cache<string, RemotePage> pageCache = new Cache<string, RemotePage>(60 * config.Timeout);
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(ConfluenceConnector));
private const string AuthFailedExceptionName = "com.atlassian.confluence.rpc.AuthenticationFailedException";
private const string V2Failed = "AXIS";
private static readonly ConfluenceConfiguration Config = IniConfig.GetIniSection<ConfluenceConfiguration>();
private string _credentials;
private DateTime _loggedInTime = DateTime.Now;
private bool _loggedIn;
private ConfluenceSoapServiceService _confluence;
private readonly int _timeout;
private string _url;
private readonly Cache<string, RemotePage> _pageCache = new Cache<string, RemotePage>(60 * Config.Timeout);
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (confluence != null) {
logout();
protected void Dispose(bool disposing) {
if (_confluence != null) {
Logout();
}
if (disposing) {
if (confluence != null) {
confluence.Dispose();
confluence = null;
if (_confluence != null) {
_confluence.Dispose();
_confluence = null;
}
}
}
public ConfluenceConnector(string url, int timeout) {
this.timeout = timeout;
init(url);
_timeout = timeout;
Init(url);
}
private void init(string url) {
this.url = url;
confluence = new ConfluenceSoapServiceService();
confluence.Url = url;
confluence.Proxy = NetworkHelper.CreateProxy(new Uri(url));
private void Init(string url) {
_url = url;
_confluence = new ConfluenceSoapServiceService
{
Url = url,
Proxy = NetworkHelper.CreateProxy(new Uri(url))
};
}
~ConfluenceConnector() {
@ -141,41 +143,43 @@ namespace Confluence {
/// Internal login which catches the exceptions
/// </summary>
/// <returns>true if login was done sucessfully</returns>
private bool doLogin(string user, string password) {
private bool DoLogin(string user, string password) {
try {
credentials = confluence.login(user, password);
loggedInTime = DateTime.Now;
loggedIn = true;
_credentials = _confluence.login(user, password);
_loggedInTime = DateTime.Now;
_loggedIn = true;
} catch (Exception e) {
// Check if confluence-v2 caused an error, use v1 instead
if (e.Message.Contains(V2_FAILED) && url.Contains("v2")) {
init(url.Replace("v2", "v1"));
return doLogin(user, password);
if (e.Message.Contains(V2Failed) && _url.Contains("v2")) {
Init(_url.Replace("v2", "v1"));
return DoLogin(user, password);
}
// check if auth failed
if (e.Message.Contains(AUTH_FAILED_EXCEPTION_NAME)) {
if (e.Message.Contains(AuthFailedExceptionName)) {
return false;
}
// Not an authentication issue
loggedIn = false;
credentials = null;
_loggedIn = false;
_credentials = null;
e.Data.Add("user", user);
e.Data.Add("url", url);
e.Data.Add("url", _url);
throw;
}
return true;
}
public void login() {
logout();
public void Login() {
Logout();
try {
// Get the system name, so the user knows where to login to
string systemName = url.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX1,"");
systemName = url.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX2, "");
CredentialsDialog dialog = new CredentialsDialog(systemName);
dialog.Name = null;
string systemName = _url.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX1,"");
systemName = systemName.Replace(ConfluenceConfiguration.DEFAULT_POSTFIX2, "");
CredentialsDialog dialog = new CredentialsDialog(systemName)
{
Name = null
};
while (dialog.Show(dialog.Name) == DialogResult.OK) {
if (doLogin(dialog.Name, dialog.Password)) {
if (DoLogin(dialog.Name, dialog.Password)) {
if (dialog.SaveChecked) {
dialog.Confirm(true);
}
@ -185,7 +189,7 @@ namespace Confluence {
dialog.Confirm(false);
} catch (ApplicationException e) {
// exception handling ...
LOG.Error("Problem using the credentials dialog", e);
Log.Error("Problem using the credentials dialog", e);
}
// For every windows version after XP show an incorrect password baloon
dialog.IncorrectPassword = true;
@ -195,119 +199,119 @@ namespace Confluence {
}
} catch (ApplicationException e) {
// exception handling ...
LOG.Error("Problem using the credentials dialog", e);
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) {
if (loggedInTime.AddMinutes(timeout-1).CompareTo(DateTime.Now) < 0) {
logout();
login();
private void CheckCredentials() {
if (_loggedIn) {
if (_loggedInTime.AddMinutes(_timeout-1).CompareTo(DateTime.Now) < 0) {
Logout();
Login();
}
} else {
login();
Login();
}
}
public bool isLoggedIn {
public bool IsLoggedIn {
get {
return loggedIn;
return _loggedIn;
}
}
public void addAttachment(long pageId, string mime, string comment, string filename, IBinaryContainer image) {
checkCredentials();
public void AddAttachment(long pageId, string mime, string comment, string filename, IBinaryContainer image) {
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, image.ToByteArray());
_confluence.addAttachment(_credentials, pageId, attachment, image.ToByteArray());
}
public Page getPage(string spaceKey, string pageTitle) {
public Page GetPage(string spaceKey, string pageTitle) {
RemotePage page = null;
string cacheKey = spaceKey + pageTitle;
if (pageCache.Contains(cacheKey)) {
page = pageCache[cacheKey];
if (_pageCache.Contains(cacheKey)) {
page = _pageCache[cacheKey];
}
if (page == null) {
checkCredentials();
page = confluence.getPage(credentials, spaceKey, pageTitle);
pageCache.Add(cacheKey, page);
CheckCredentials();
page = _confluence.getPage(_credentials, spaceKey, pageTitle);
_pageCache.Add(cacheKey, page);
}
return new Page(page);
}
public Page getPage(long pageId) {
public Page GetPage(long pageId) {
RemotePage page = null;
string cacheKey = "" + pageId;
if (pageCache.Contains(cacheKey)) {
page = pageCache[cacheKey];
if (_pageCache.Contains(cacheKey)) {
page = _pageCache[cacheKey];
}
if (page == null) {
checkCredentials();
page = confluence.getPage(credentials, pageId);
pageCache.Add(cacheKey, page);
CheckCredentials();
page = _confluence.getPage(_credentials, pageId);
_pageCache.Add(cacheKey, page);
}
return new Page(page);
}
public Page getSpaceHomepage(Space spaceSummary) {
checkCredentials();
RemoteSpace spaceDetail = confluence.getSpace(credentials, spaceSummary.Key);
RemotePage page = confluence.getPage(credentials, spaceDetail.homePage);
public Page GetSpaceHomepage(Space spaceSummary) {
CheckCredentials();
RemoteSpace spaceDetail = _confluence.getSpace(_credentials, spaceSummary.Key);
RemotePage page = _confluence.getPage(_credentials, spaceDetail.homePage);
return new Page(page);
}
public List<Space> getSpaceSummaries() {
checkCredentials();
RemoteSpaceSummary [] spaces = confluence.getSpaces(credentials);
public List<Space> GetSpaceSummaries() {
CheckCredentials();
RemoteSpaceSummary [] spaces = _confluence.getSpaces(_credentials);
List<Space> returnSpaces = new List<Space>();
foreach(RemoteSpaceSummary space in spaces) {
returnSpaces.Add(new Space(space));
}
returnSpaces.Sort((x, y) => string.Compare(x.Name, y.Name));
returnSpaces.Sort((x, y) => string.CompareOrdinal(x.Name, y.Name));
return returnSpaces;
}
public List<Page> getPageChildren(Page parentPage) {
checkCredentials();
public List<Page> GetPageChildren(Page parentPage) {
CheckCredentials();
List<Page> returnPages = new List<Page>();
RemotePageSummary[] pages = confluence.getChildren(credentials, parentPage.id);
RemotePageSummary[] pages = _confluence.getChildren(_credentials, parentPage.Id);
foreach(RemotePageSummary page in pages) {
returnPages.Add(new Page(page));
}
returnPages.Sort((x, y) => string.Compare(x.Title, y.Title));
returnPages.Sort((x, y) => string.CompareOrdinal(x.Title, y.Title));
return returnPages;
}
public List<Page> getPageSummaries(Space space) {
checkCredentials();
public List<Page> GetPageSummaries(Space space) {
CheckCredentials();
List<Page> returnPages = new List<Page>();
RemotePageSummary[] pages = confluence.getPages(credentials, space.Key);
RemotePageSummary[] pages = _confluence.getPages(_credentials, space.Key);
foreach(RemotePageSummary page in pages) {
returnPages.Add(new Page(page));
}
returnPages.Sort((x, y) => string.Compare(x.Title, y.Title));
returnPages.Sort((x, y) => string.CompareOrdinal(x.Title, y.Title));
return returnPages;
}
public List<Page> searchPages(string query, string space) {
checkCredentials();
public List<Page> SearchPages(string query, string space) {
CheckCredentials();
List<Page> results = new List<Page>();
foreach(RemoteSearchResult searchResult in confluence.search(credentials, query, 20)) {
LOG.DebugFormat("Got result of type {0}", searchResult.type);
foreach(RemoteSearchResult searchResult in _confluence.search(_credentials, query, 20)) {
Log.DebugFormat("Got result of type {0}", searchResult.type);
if ("page".Equals(searchResult.type)) {
results.Add(new Page(searchResult, space));
}