diff options
author | spingel | 2007-10-26 16:52:41 +0000 |
---|---|---|
committer | spingel | 2007-10-26 16:52:41 +0000 |
commit | 5e7418ae445ae4d9013e7216eeff745200b7314a (patch) | |
tree | 19345780928290a0c37c80f9b36bab83c83b227d | |
parent | 97543c42290397a293cedda105a3d5d32e423609 (diff) | |
download | org.eclipse.mylyn.tasks-5e7418ae445ae4d9013e7216eeff745200b7314a.tar.gz org.eclipse.mylyn.tasks-5e7418ae445ae4d9013e7216eeff745200b7314a.tar.xz org.eclipse.mylyn.tasks-5e7418ae445ae4d9013e7216eeff745200b7314a.zip |
RESOLVED - bug 207531: prompt for authentication credentials as needed
https://bugs.eclipse.org/bugs/show_bug.cgi?id=207531
23 files changed, 359 insertions, 243 deletions
diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/AbstractTracClient.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/AbstractTracClient.java index cae0147f7..b787401e5 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/AbstractTracClient.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/AbstractTracClient.java @@ -16,8 +16,10 @@ import java.net.URL; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.methods.PostMethod; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.mylyn.internal.trac.core.model.TracComponent; import org.eclipse.mylyn.internal.trac.core.model.TracMilestone; import org.eclipse.mylyn.internal.trac.core.model.TracPriority; @@ -27,6 +29,7 @@ import org.eclipse.mylyn.internal.trac.core.model.TracTicketResolution; import org.eclipse.mylyn.internal.trac.core.model.TracTicketStatus; import org.eclipse.mylyn.internal.trac.core.model.TracTicketType; import org.eclipse.mylyn.internal.trac.core.model.TracVersion; +import org.eclipse.mylyn.web.core.AbstractWebLocation; import org.eclipse.mylyn.web.core.WebClientUtil; /** @@ -34,27 +37,34 @@ import org.eclipse.mylyn.web.core.WebClientUtil; */ public abstract class AbstractTracClient implements ITracClient { - private static final String LOGIN_COOKIE_NAME = "trac_auth"; + protected static final String USER_AGENT = "TracConnector"; - protected String username; + private static final String LOGIN_COOKIE_NAME = "trac_auth"; - protected String password; + protected static final IProgressMonitor DEFAULT_MONITOR = new NullProgressMonitor(); + + protected final String repositoryUrl; - protected URL repositoryUrl; + protected final Version version; - protected Version version; + protected final AbstractWebLocation location; protected TracClientData data; - protected Proxy proxy; - public AbstractTracClient(URL repositoryUrl, Version version, String username, String password, Proxy proxy) { - this.repositoryUrl = repositoryUrl; + this.repositoryUrl = repositoryUrl.toString(); this.version = version; - this.username = username; - this.password = password; - this.proxy = proxy; + this.location = null; + + this.data = new TracClientData(); + } + + public AbstractTracClient(AbstractWebLocation location, Version version) { + this.location = location; + this.version = version; + this.repositoryUrl = location.getUrl(); + this.data = new TracClientData(); } @@ -62,15 +72,15 @@ public abstract class AbstractTracClient implements ITracClient { return version; } - protected boolean hasAuthenticationCredentials() { - return username != null && username.length() > 0; + protected boolean credentialsValid(UsernamePasswordCredentials credentials) { + return credentials != null && credentials.getUserName().length() > 0; } - protected void authenticateAccountManager(HttpClient httpClient) throws IOException, TracLoginException { + protected void authenticateAccountManager(HttpClient httpClient, UsernamePasswordCredentials credentials) throws IOException, TracLoginException { PostMethod post = new PostMethod(WebClientUtil.getRequestPath(repositoryUrl + LOGIN_URL)); post.setFollowRedirects(false); - NameValuePair[] data = { new NameValuePair("referer", ""), new NameValuePair("user", username), - new NameValuePair("password", password) }; + NameValuePair[] data = { new NameValuePair("referer", ""), new NameValuePair("user", credentials.getUserName()), + new NameValuePair("password", credentials.getPassword()) }; post.setRequestBody(data); try { int code = httpClient.executeMethod(post); @@ -170,12 +180,4 @@ public abstract class AbstractTracClient implements ITracClient { return null; } - public void setProxy(Proxy proxy) { - this.proxy = proxy; - } - - public Proxy getProxy() { - return proxy; - } - } diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/ITracClient.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/ITracClient.java index d7cedd215..8a80b720d 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/ITracClient.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/ITracClient.java @@ -9,7 +9,6 @@ package org.eclipse.mylyn.internal.trac.core; import java.io.InputStream; -import java.net.Proxy; import java.util.Date; import java.util.List; import java.util.Set; @@ -125,7 +124,7 @@ public interface ITracClient { * @throws TracException * thrown in case of a connection error */ - void validate() throws TracException; + void validate(IProgressMonitor callback) throws TracException; /** * Returns true, if the repository details are cached. If this method returns true, invoking @@ -184,8 +183,6 @@ public interface ITracClient { Set<Integer> getChangedTickets(Date since) throws TracException; - void setProxy(Proxy proxy); - Date getTicketLastChanged(Integer id) throws TracException; } diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracClientFactory.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracClientFactory.java index 09634eae9..572056b43 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracClientFactory.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracClientFactory.java @@ -9,24 +9,21 @@ package org.eclipse.mylyn.internal.trac.core; import java.net.MalformedURLException; -import java.net.Proxy; -import java.net.URL; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.mylyn.internal.trac.core.ITracClient.Version; +import org.eclipse.mylyn.web.core.AbstractWebLocation; /** * @author Steffen Pingel */ public class TracClientFactory { - public static ITracClient createClient(String location, Version version, String username, String password, - Proxy proxy) throws MalformedURLException { - URL url = new URL(location); - + public static ITracClient createClient(AbstractWebLocation location, Version version) { if (version == Version.TRAC_0_9) { - return new TracWebClient(url, version, username, password, proxy); + return new TracWebClient(location, version); } else if (version == Version.XML_RPC) { - return new TracXmlRpcClient(url, version, username, password, proxy); + return new TracXmlRpcClient(location, version); } throw new RuntimeException("Invalid repository version: " + version); @@ -39,17 +36,15 @@ public class TracClientFactory { * <p> * Order of the tried access types: XML-RPC, Trac 0.9 */ - public static Version probeClient(String location, String username, String password, Proxy proxy) - throws MalformedURLException, TracException { - URL url = new URL(location); + public static Version probeClient(AbstractWebLocation location) throws MalformedURLException, TracException { try { - ITracClient repository = new TracXmlRpcClient(url, Version.XML_RPC, username, password, proxy); - repository.validate(); + ITracClient repository = new TracXmlRpcClient(location, Version.XML_RPC); + repository.validate(new NullProgressMonitor()); return Version.XML_RPC; } catch (TracException e) { try { - ITracClient repository = new TracWebClient(url, Version.TRAC_0_9, username, password, proxy); - repository.validate(); + ITracClient repository = new TracWebClient(location, Version.TRAC_0_9); + repository.validate(new NullProgressMonitor()); return Version.TRAC_0_9; } catch (TracLoginException e2) { throw e; diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracClientManager.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracClientManager.java index baeeb9322..c4b55f8bf 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracClientManager.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracClientManager.java @@ -21,6 +21,8 @@ import java.util.Map; import org.eclipse.mylyn.internal.trac.core.ITracClient.Version; import org.eclipse.mylyn.tasks.core.ITaskRepositoryListener; import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.TaskRepositoryLocationFactory; +import org.eclipse.mylyn.web.core.AbstractWebLocation; /** * Caches {@link ITracClient} objects. @@ -35,8 +37,11 @@ public class TracClientManager implements ITaskRepositoryListener { private File cacheFile; - public TracClientManager(File cacheFile) { + private TaskRepositoryLocationFactory taskRepositoryLocationFactory; + + public TracClientManager(File cacheFile, TaskRepositoryLocationFactory taskRepositoryLocationFactory) { this.cacheFile = cacheFile; + this.taskRepositoryLocationFactory = taskRepositoryLocationFactory; readCache(); } @@ -44,9 +49,8 @@ public class TracClientManager implements ITaskRepositoryListener { public synchronized ITracClient getRepository(TaskRepository taskRepository) throws MalformedURLException { ITracClient repository = clientByUrl.get(taskRepository.getUrl()); if (repository == null) { - repository = TracClientFactory.createClient(taskRepository.getUrl(), - Version.fromVersion(taskRepository.getVersion()), taskRepository.getUserName(), - taskRepository.getPassword(), taskRepository.getProxy()); + AbstractWebLocation location = taskRepositoryLocationFactory.createWebLocation(taskRepository); + repository = TracClientFactory.createClient(location, Version.fromVersion(taskRepository.getVersion())); clientByUrl.put(taskRepository.getUrl(), repository); TracClientData data = clientDataByUrl.get(taskRepository.getUrl()); @@ -137,4 +141,12 @@ public class TracClientManager implements ITaskRepositoryListener { } } + public TaskRepositoryLocationFactory getTaskRepositoryLocationFactory() { + return taskRepositoryLocationFactory; + } + + public void setTaskRepositoryLocationFactory(TaskRepositoryLocationFactory taskRepositoryLocationFactory) { + this.taskRepositoryLocationFactory = taskRepositoryLocationFactory; + } + } diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracProxyAuthenticationException.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracProxyAuthenticationException.java new file mode 100644 index 000000000..2aace4e6a --- /dev/null +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracProxyAuthenticationException.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Mylyn project committers and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +package org.eclipse.mylyn.internal.trac.core; + +public class TracProxyAuthenticationException extends TracException { + + private static final long serialVersionUID = 305145749259511429L; + + public TracProxyAuthenticationException(String message) { + super(message); + } + + public TracProxyAuthenticationException() { + } + +} diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracRepositoryConnector.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracRepositoryConnector.java index 50b97c60f..5c943bd60 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracRepositoryConnector.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracRepositoryConnector.java @@ -36,6 +36,7 @@ import org.eclipse.mylyn.tasks.core.RepositoryStatus; import org.eclipse.mylyn.tasks.core.RepositoryTaskAttribute; import org.eclipse.mylyn.tasks.core.RepositoryTaskData; import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.TaskRepositoryLocationFactory; /** * @author Steffen Pingel @@ -50,6 +51,8 @@ public class TracRepositoryConnector extends AbstractRepositoryConnector { private TracAttachmentHandler attachmentHandler = new TracAttachmentHandler(this); + private TaskRepositoryLocationFactory taskRepositoryLocationFactory = new TaskRepositoryLocationFactory(); + public TracRepositoryConnector() { TracCorePlugin.getDefault().setConnector(this); } @@ -283,7 +286,7 @@ public class TracRepositoryConnector extends AbstractRepositoryConnector { if (TracCorePlugin.getDefault().getRepostioryAttributeCachePath() != null) { cacheFile = TracCorePlugin.getDefault().getRepostioryAttributeCachePath().toFile(); } - clientManager = new TracClientManager(cacheFile); + clientManager = new TracClientManager(cacheFile, taskRepositoryLocationFactory); } return clientManager; } @@ -417,4 +420,15 @@ public class TracRepositoryConnector extends AbstractRepositoryConnector { return ticket; } + public TaskRepositoryLocationFactory getTaskRepositoryLocationFactory() { + return taskRepositoryLocationFactory; + } + + public synchronized void setTaskRepositoryLocationFactory(TaskRepositoryLocationFactory taskRepositoryLocationFactory) { + this.taskRepositoryLocationFactory = taskRepositoryLocationFactory; + if (this.clientManager != null) { + clientManager.setTaskRepositoryLocationFactory(taskRepositoryLocationFactory); + } + } + }
\ No newline at end of file diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracWebClient.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracWebClient.java index c15f3707c..ca0a6350a 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracWebClient.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracWebClient.java @@ -15,8 +15,6 @@ import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.io.StringReader; import java.net.HttpURLConnection; -import java.net.Proxy; -import java.net.URL; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; @@ -28,8 +26,8 @@ import java.util.StringTokenizer; import javax.security.auth.login.LoginException; -import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; @@ -53,9 +51,12 @@ import org.eclipse.mylyn.internal.trac.core.model.TracTicket.Key; import org.eclipse.mylyn.internal.trac.core.util.TracUtils; import org.eclipse.mylyn.internal.trac.core.util.TracHttpClientTransportFactory.TracHttpException; import org.eclipse.mylyn.monitor.core.StatusHandler; +import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.web.core.AbstractWebLocation; import org.eclipse.mylyn.web.core.HtmlStreamTokenizer; import org.eclipse.mylyn.web.core.HtmlTag; import org.eclipse.mylyn.web.core.WebClientUtil; +import org.eclipse.mylyn.web.core.AbstractWebLocation.ResultType; import org.eclipse.mylyn.web.core.HtmlStreamTokenizer.Token; /** @@ -69,8 +70,8 @@ public class TracWebClient extends AbstractTracClient { private boolean authenticated; - public TracWebClient(URL url, Version version, String username, String password, Proxy proxy) { - super(url, version, username, password, proxy); + public TracWebClient(AbstractWebLocation location, Version version) { + super(location, version); httpClient = new HttpClient(); httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager()); @@ -78,8 +79,12 @@ public class TracWebClient extends AbstractTracClient { } private synchronized GetMethod connect(String serverURL) throws TracException { + return connect(serverURL, DEFAULT_MONITOR); + } + + private synchronized GetMethod connect(String serverURL, IProgressMonitor monitor) throws TracException { try { - return connectInternal(serverURL); + return connectInternal(serverURL, monitor); } catch (TracException e) { throw e; } catch (Exception e) { @@ -87,13 +92,17 @@ public class TracWebClient extends AbstractTracClient { } } - private GetMethod connectInternal(String serverURL) throws TracLoginException, IOException, TracHttpException { - WebClientUtil.setupHttpClient(httpClient, proxy, serverURL, null, null); + private GetMethod connectInternal(String serverURL, IProgressMonitor monitor) throws TracLoginException, + IOException, TracHttpException { + WebClientUtil.setupHttpClient(httpClient, USER_AGENT, location); for (int attempt = 0; attempt < 2; attempt++) { // force authentication - if (!authenticated && hasAuthenticationCredentials()) { - authenticate(); + if (!authenticated) { + UsernamePasswordCredentials credentials = location.getCredentials(TaskRepository.AUTH_DEFAULT); + if (credentialsValid(credentials)) { + authenticate(monitor); + } } GetMethod method = new GetMethod(WebClientUtil.getRequestPath(serverURL)); @@ -111,7 +120,7 @@ public class TracWebClient extends AbstractTracClient { // login or re-authenticate due to an expired session method.releaseConnection(); authenticated = false; - authenticate(); + authenticate(monitor); } else { throw new TracHttpException(code); } @@ -120,41 +129,63 @@ public class TracWebClient extends AbstractTracClient { throw new TracLoginException(); } - private void authenticate() throws TracLoginException, IOException { - if (!hasAuthenticationCredentials()) { - throw new TracLoginException(); - } + private void authenticate(IProgressMonitor monitor) throws TracLoginException, IOException { + while (true) { + UsernamePasswordCredentials credentials = location.getCredentials(TaskRepository.AUTH_DEFAULT); + if (!credentialsValid(credentials)) { + throw new TracLoginException(); + } - // try standard basic/digest authentication first - Credentials credentials = new UsernamePasswordCredentials(username, password); - httpClient.getState().setCredentials( - new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), credentials); + // try standard basic/digest authentication first + AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); + httpClient.getState().setCredentials(authScope, credentials); - GetMethod method = new GetMethod(WebClientUtil.getRequestPath(repositoryUrl + LOGIN_URL)); - method.setFollowRedirects(false); - int code; - try { - httpClient.getParams().setAuthenticationPreemptive(true); - code = httpClient.executeMethod(method); - if (code == HttpURLConnection.HTTP_UNAUTHORIZED || code == HttpURLConnection.HTTP_FORBIDDEN) { - throw new TracLoginException(); + GetMethod method = new GetMethod(WebClientUtil.getRequestPath(repositoryUrl + LOGIN_URL)); + method.setFollowRedirects(false); + int code; + try { + httpClient.getParams().setAuthenticationPreemptive(true); + code = httpClient.executeMethod(method); + if (needsReauthentication(code)) { + continue; + } + } finally { + method.releaseConnection(); + httpClient.getParams().setAuthenticationPreemptive(false); } - } finally { - method.releaseConnection(); - httpClient.getParams().setAuthenticationPreemptive(false); + + // the expected return code is a redirect, anything else is suspicious + if (code == HttpURLConnection.HTTP_OK) { + // try form-based authentication via AccountManagerPlugin as a + // fall-back + authenticateAccountManager(httpClient, credentials); + } + + validateAuthenticationState(httpClient); + + // success since no exception was thrown + authenticated = true; + break; + } + } + + private boolean needsReauthentication(int code) throws IOException, TracLoginException { + final String authenticationType; + if (code == HttpStatus.SC_UNAUTHORIZED || code == HttpStatus.SC_FORBIDDEN) { + authenticationType = TaskRepository.AUTH_DEFAULT; + } else if (code == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { + authenticationType = TaskRepository.AUTH_PROXY; + } else { + return false; } - // the expected return code is a redirect, anything else is suspicious - if (code == HttpURLConnection.HTTP_OK) { - // try form-based authentication via AccountManagerPlugin as a - // fall-back - authenticateAccountManager(httpClient); + if (location.requestCredentials(authenticationType, null) == ResultType.NOT_SUPPORTED) { + throw new TracLoginException(); } - validateAuthenticationState(httpClient); + WebClientUtil.setupHttpClient(httpClient, USER_AGENT, location); - // success since no exception was thrown - authenticated = true; + return true; } /** @@ -330,8 +361,8 @@ public class TracWebClient extends AbstractTracClient { return values; } - public void validate() throws TracException { - GetMethod method = connect(repositoryUrl + "/"); + public void validate(IProgressMonitor monitor) throws TracException { + GetMethod method = connect(repositoryUrl + "/", monitor); try { BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), ITracClient.CHARSET)); diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracXmlRpcClient.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracXmlRpcClient.java index 93b8c49b6..98b585942 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracXmlRpcClient.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/TracXmlRpcClient.java @@ -11,8 +11,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.Proxy; import java.net.URL; import java.util.ArrayList; import java.util.Collections; @@ -25,6 +23,8 @@ import java.util.Set; import java.util.TimeZone; import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; @@ -52,7 +52,10 @@ import org.eclipse.mylyn.internal.trac.core.util.TracHttpClientTransportFactory; import org.eclipse.mylyn.internal.trac.core.util.TracUtils; import org.eclipse.mylyn.internal.trac.core.util.TracHttpClientTransportFactory.TracHttpException; import org.eclipse.mylyn.monitor.core.StatusHandler; +import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.web.core.AbstractWebLocation; import org.eclipse.mylyn.web.core.WebClientUtil; +import org.eclipse.mylyn.web.core.AbstractWebLocation.ResultType; /** * Represents a Trac repository that is accessed through the Trac XmlRpcPlugin. @@ -90,39 +93,47 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli private boolean accountMangerAuthenticationFailed; - public TracXmlRpcClient(URL url, Version version, String username, String password, Proxy proxy) { - super(url, version, username, password, proxy); + private XmlRpcClientConfigImpl config; + + public TracXmlRpcClient(AbstractWebLocation location, Version version) { + super(location, version); } public synchronized XmlRpcClient getClient() throws TracException { - if (xmlrpc != null) { - return xmlrpc; + if (xmlrpc == null) { + config = new XmlRpcClientConfigImpl(); + config.setEncoding(ITracClient.CHARSET); + config.setTimeZone(TimeZone.getTimeZone(ITracClient.TIME_ZONE)); + config.setContentLengthOptional(false); + config.setConnectionTimeout(WebClientUtil.CONNNECT_TIMEOUT); + config.setReplyTimeout(WebClientUtil.SOCKET_TIMEOUT); + + xmlrpc = new XmlRpcClient(); + xmlrpc.setConfig(config); + + factory = new TracHttpClientTransportFactory(xmlrpc); + factory.setLocation(location); + xmlrpc.setTransportFactory(factory); + } + + // update configuration with latest values + UsernamePasswordCredentials credentials = location.getCredentials(TaskRepository.AUTH_DEFAULT); + if (credentialsValid(credentials)) { + config.setBasicUserName(credentials.getUserName()); + config.setBasicPassword(credentials.getPassword()); + } else { + config.setBasicUserName(null); + config.setBasicPassword(null); } - - XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); - config.setEncoding(ITracClient.CHARSET); - config.setBasicUserName(username); - config.setBasicPassword(password); - config.setServerURL(getXmlRpcUrl()); - config.setTimeZone(TimeZone.getTimeZone(ITracClient.TIME_ZONE)); - config.setContentLengthOptional(false); - config.setConnectionTimeout(WebClientUtil.CONNNECT_TIMEOUT); - config.setReplyTimeout(WebClientUtil.SOCKET_TIMEOUT); - - xmlrpc = new XmlRpcClient(); - xmlrpc.setConfig(config); - - factory = new TracHttpClientTransportFactory(xmlrpc); - factory.setProxy(proxy); - xmlrpc.setTransportFactory(factory); + config.setServerURL(getXmlRpcUrl(credentials)); return xmlrpc; } - private URL getXmlRpcUrl() throws TracException { + private URL getXmlRpcUrl(UsernamePasswordCredentials credentials) throws TracException { try { String location = repositoryUrl.toString(); - if (hasAuthenticationCredentials()) { + if (credentialsValid(credentials)) { location += LOGIN_URL; } location += XMLRPC_URL; @@ -134,9 +145,31 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli } private Object call(String method, Object... parameters) throws TracException { + while (true) { + getClient(); + + try { + return executeCall(method, parameters); + } catch (TracLoginException e) { + if (location.requestCredentials(TaskRepository.AUTH_DEFAULT, null) == ResultType.NOT_SUPPORTED) { + throw e; + } + } catch (TracPermissionDeniedException e) { + if (location.requestCredentials(TaskRepository.AUTH_DEFAULT, null) == ResultType.NOT_SUPPORTED) { + throw e; + } + } catch (TracProxyAuthenticationException e) { + if (location.requestCredentials(TaskRepository.AUTH_PROXY, null) == ResultType.NOT_SUPPORTED) { + throw e; + } + } + } + } + + private Object executeCall(String method, Object... parameters) throws TracException { try { // first attempt - return callInternal(method, parameters); + return executeCallInternal(method, parameters); } catch (TracPermissionDeniedException e) { if (accountMangerAuthenticationFailed) { // do not try again if this has failed in the past since it @@ -144,13 +177,18 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli throw e; } + UsernamePasswordCredentials credentials = location.getCredentials(TaskRepository.AUTH_DEFAULT); + if (!credentialsValid(credentials)) { + throw e; + } + // try form-based authentication via AccountManagerPlugin as a // fall-back HttpClient httpClient = new HttpClient(); httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); - WebClientUtil.setupHttpClient(httpClient, proxy, repositoryUrl.toString(), null, null); + WebClientUtil.setupHttpClient(httpClient, USER_AGENT, location); try { - authenticateAccountManager(httpClient); + authenticateAccountManager(httpClient, credentials); } catch (TracLoginException loginException) { // caused by wrong username or password throw loginException; @@ -172,19 +210,19 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli } // second attempt - return callInternal(method, parameters); + return executeCallInternal(method, parameters); } - private Object callInternal(String method, Object... parameters) throws TracException { - getClient(); - + private Object executeCallInternal(String method, Object... parameters) throws TracException { try { return xmlrpc.execute(method, parameters); } catch (TracHttpException e) { - if (e.code == HttpURLConnection.HTTP_UNAUTHORIZED) { + if (e.code == HttpStatus.SC_UNAUTHORIZED) { throw new TracLoginException(); - } else if (e.code == HttpURLConnection.HTTP_FORBIDDEN) { + } else if (e.code == HttpStatus.SC_FORBIDDEN) { throw new TracPermissionDeniedException(); + } else if (e.code == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { + throw new TracProxyAuthenticationException(); } else { throw new TracException(e); } @@ -234,7 +272,7 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli return ((Object[]) item)[0]; } - public void validate() throws TracException { + public void validate(IProgressMonitor monitor) throws TracException { try { Object[] result = (Object[]) call("system.getAPIVersion"); if (result.length >= 3) { @@ -264,7 +302,8 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli private void updateAPIVersion() throws TracException { if (epochAPIVersion == -1 || majorAPIVersion == -1 || minorAPIVersion == -1) { - validate(); + // TODO + validate(DEFAULT_MONITOR); } } @@ -669,25 +708,15 @@ public class TracXmlRpcClient extends AbstractTracClient implements ITracWikiCli return result; } - @Override - public void setProxy(Proxy proxy) { - super.setProxy(proxy); - - synchronized (this) { - if (factory != null) { - factory.setProxy(proxy); - } - } - } - public Date getTicketLastChanged(Integer id) throws TracException { Object[] result = (Object[]) call("ticket.get", id); return parseDate(result[2]); } public void validateWikiRpcApi() throws TracException { - if (((Integer) call("wiki.getRPCVersionSupported")) < 2) - validate(); + if (((Integer) call("wiki.getRPCVersionSupported")) < 2) { + validate(new NullProgressMonitor()); + } } public String wikiToHtml(String sourceText) throws TracException { diff --git a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/util/TracHttpClientTransportFactory.java b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/util/TracHttpClientTransportFactory.java index 00ad73145..1b0e7d9bd 100644 --- a/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/util/TracHttpClientTransportFactory.java +++ b/org.eclipse.mylyn.trac.core/src/org/eclipse/mylyn/internal/trac/core/util/TracHttpClientTransportFactory.java @@ -11,7 +11,6 @@ package org.eclipse.mylyn.internal.trac.core.util; import java.io.InputStream; import java.lang.reflect.Field; import java.net.HttpURLConnection; -import java.net.Proxy; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.HttpClient; @@ -25,6 +24,7 @@ import org.apache.xmlrpc.client.XmlRpcCommonsTransport; import org.apache.xmlrpc.client.XmlRpcHttpClientConfig; import org.apache.xmlrpc.client.XmlRpcTransport; import org.apache.xmlrpc.client.XmlRpcTransportFactory; +import org.eclipse.mylyn.web.core.AbstractWebLocation; import org.eclipse.mylyn.web.core.WebClientUtil; /** @@ -34,6 +34,8 @@ import org.eclipse.mylyn.web.core.WebClientUtil; */ public class TracHttpClientTransportFactory implements XmlRpcTransportFactory { + protected static final String USER_AGENT = "TracConnector, XmlRpcClient/3.0"; + public static class TracHttpException extends XmlRpcException { private static final long serialVersionUID = 9032521978140685830L; @@ -49,14 +51,14 @@ public class TracHttpClientTransportFactory implements XmlRpcTransportFactory { */ public static class TracHttpClientTransport extends XmlRpcCommonsTransport { - private Proxy proxy; + private AbstractWebLocation location; private Cookie[] cookies; - public TracHttpClientTransport(XmlRpcClient client, Proxy proxy, Cookie[] cookies) { + public TracHttpClientTransport(XmlRpcClient client, AbstractWebLocation location, Cookie[] cookies) { super(client); - this.proxy = proxy; + this.location = location; this.cookies = cookies; XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) client.getConfig(); @@ -125,7 +127,7 @@ public class TracHttpClientTransportFactory implements XmlRpcTransportFactory { XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) request.getConfig(); String url = config.getServerURL().toString(); - WebClientUtil.setupHttpClient(getHttpClient(), proxy, url, null, null); + WebClientUtil.setupHttpClient(getHttpClient(), USER_AGENT, location); if (cookies != null) { getHttpClient().getState().addCookies(cookies); } @@ -152,24 +154,16 @@ public class TracHttpClientTransportFactory implements XmlRpcTransportFactory { private XmlRpcClient client; - private Proxy proxy; - private Cookie[] cookies; + private AbstractWebLocation location; + public TracHttpClientTransportFactory(XmlRpcClient client) { this.client = client; } public XmlRpcTransport getTransport() { - return new TracHttpClientTransport(client, proxy, cookies); - } - - public Proxy getProxy() { - return proxy; - } - - public void setProxy(Proxy proxy) { - this.proxy = proxy; + return new TracHttpClientTransport(client, location, cookies); } public Cookie[] getCookies() { @@ -180,4 +174,12 @@ public class TracHttpClientTransportFactory implements XmlRpcTransportFactory { this.cookies = cookies; } + public void setLocation(AbstractWebLocation location) { + this.location = location; + } + + public AbstractWebLocation getLocation() { + return location; + } + } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AbstractTracClientRepositoryTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AbstractTracClientRepositoryTest.java index feefc54d9..c5dd03545 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AbstractTracClientRepositoryTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AbstractTracClientRepositoryTest.java @@ -52,12 +52,12 @@ public class AbstractTracClientRepositoryTest extends AbstractTracClientTest { // standard connect connect(url); - repository.validate(); + repository.validate(callback); // invalid url connect("http://non.existant/repository"); try { - repository.validate(); + repository.validate(callback); fail("Expected TracException"); } catch (TracException e) { } @@ -65,7 +65,7 @@ public class AbstractTracClientRepositoryTest extends AbstractTracClientTest { // invalid password connect(url, credentials.username, "wrongpassword"); try { - repository.validate(); + repository.validate(callback); fail("Expected TracLoginException"); } catch (TracLoginException e) { } @@ -73,7 +73,7 @@ public class AbstractTracClientRepositoryTest extends AbstractTracClientTest { // invalid username connect(url, "wrongusername", credentials.password); try { - repository.validate(); + repository.validate(callback); fail("Expected TracLoginException"); } catch (TracLoginException e) { } @@ -83,28 +83,10 @@ public class AbstractTracClientRepositoryTest extends AbstractTracClientTest { connect(TracTestConstants.TEST_TRAC_010_URL, "", "", new Proxy(Type.HTTP, new InetSocketAddress( "invalidhostname", 8080))); try { - repository.validate(); + repository.validate(callback); fail("Expected IOException"); } catch (TracException e) { } - - connect(TracTestConstants.TEST_TRAC_010_URL, "", "", null); - repository.setProxy(new Proxy(Type.HTTP, new InetSocketAddress("invalidhostname", 8080))); - try { - repository.validate(); - fail("Expected IOException"); - } catch (TracException e) { - } - - connect(TracTestConstants.TEST_TRAC_010_URL); - repository.validate(); - repository.setProxy(new Proxy(Type.HTTP, new InetSocketAddress("invalidhostname", 8080))); - try { - repository.validate(); - fail("Expected IOException"); - } catch (TracException e) { - } - } } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AbstractTracClientTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AbstractTracClientTest.java index e0bb661b1..dba622997 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AbstractTracClientTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AbstractTracClientTest.java @@ -12,12 +12,16 @@ import java.net.Proxy; import junit.framework.TestCase; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.mylyn.context.tests.support.TestUtil; import org.eclipse.mylyn.context.tests.support.TestUtil.Credentials; import org.eclipse.mylyn.context.tests.support.TestUtil.PrivilegeLevel; import org.eclipse.mylyn.internal.trac.core.ITracClient; import org.eclipse.mylyn.internal.trac.core.TracClientFactory; import org.eclipse.mylyn.internal.trac.core.ITracClient.Version; +import org.eclipse.mylyn.web.core.IProxyProvider; +import org.eclipse.mylyn.web.core.WebLocation; /** * Provides a base implementation for test cases that access trac repositories. @@ -38,6 +42,8 @@ public abstract class AbstractTracClientTest extends TestCase { private PrivilegeLevel level; + final IProgressMonitor callback = new NullProgressMonitor(); + public AbstractTracClientTest(Version version, PrivilegeLevel level) { this.version = version; this.level = level; @@ -84,12 +90,18 @@ public abstract class AbstractTracClientTest extends TestCase { return connect(url, username, password, proxy, version); } - public ITracClient connect(String url, String username, String password, Proxy proxy, Version version) + public ITracClient connect(String url, String username, String password, final Proxy proxy, Version version) throws Exception { this.repositoryUrl = url; this.username = username; this.password = password; - this.repository = TracClientFactory.createClient(url, version, username, password, proxy); + + WebLocation location = new WebLocation(url, username, password, new IProxyProvider() { + public Proxy getProxyForHost(String host, String proxyType) { + return proxy; + } + }); + this.repository = TracClientFactory.createClient(location, version); return this.repository; } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AllTracTests.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AllTracTests.java index 87afe2126..3d59ae227 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AllTracTests.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/AllTracTests.java @@ -11,7 +11,9 @@ package org.eclipse.mylyn.trac.tests; import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.mylyn.internal.trac.core.TracCorePlugin; import org.eclipse.mylyn.internal.trac.ui.TracHyperlinkUtilTest; +import org.eclipse.mylyn.tasks.core.TaskRepositoryLocationFactory; /** * @author Mik Kersten @@ -22,6 +24,8 @@ public class AllTracTests { public static Test suite() { // UrlConnectionUtil.initCommonsLoggingSettings(); + TracCorePlugin.getDefault().getConnector().setTaskRepositoryLocationFactory(new TaskRepositoryLocationFactory()); + TestSuite suite = new TestSuite("Test for org.eclipse.mylyn.trac.tests"); // $JUnit-BEGIN$ // suite.addTestSuite(TracXmlRpcTest.class); diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientFactoryTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientFactoryTest.java index 30e982b78..f9ffcc9d5 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientFactoryTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientFactoryTest.java @@ -8,20 +8,19 @@ package org.eclipse.mylyn.trac.tests; -import java.net.Proxy; - import junit.framework.TestCase; import org.eclipse.mylyn.context.tests.support.TestUtil; import org.eclipse.mylyn.context.tests.support.TestUtil.Credentials; import org.eclipse.mylyn.context.tests.support.TestUtil.PrivilegeLevel; import org.eclipse.mylyn.internal.trac.core.ITracClient; -import org.eclipse.mylyn.internal.trac.core.TracWebClient; import org.eclipse.mylyn.internal.trac.core.TracClientFactory; import org.eclipse.mylyn.internal.trac.core.TracException; import org.eclipse.mylyn.internal.trac.core.TracLoginException; +import org.eclipse.mylyn.internal.trac.core.TracWebClient; import org.eclipse.mylyn.internal.trac.core.TracXmlRpcClient; import org.eclipse.mylyn.internal.trac.core.ITracClient.Version; +import org.eclipse.mylyn.web.core.WebLocation; /** * @author Steffen Pingel @@ -29,25 +28,27 @@ import org.eclipse.mylyn.internal.trac.core.ITracClient.Version; public class TracClientFactoryTest extends TestCase { public void testCreateClient() throws Exception { - ITracClient client = TracClientFactory.createClient(TracTestConstants.TEST_TRAC_010_URL, Version.TRAC_0_9, - "user", "password", Proxy.NO_PROXY); + WebLocation location = new WebLocation(TracTestConstants.TEST_TRAC_010_URL, "user", "password"); + ITracClient client = TracClientFactory.createClient(location, Version.TRAC_0_9); assertTrue(client instanceof TracWebClient); - client = TracClientFactory.createClient(TracTestConstants.TEST_TRAC_010_SSL_URL, Version.TRAC_0_9, "user", - "password", Proxy.NO_PROXY); + + location = new WebLocation(TracTestConstants.TEST_TRAC_010_SSL_URL, "user", "password"); + client = TracClientFactory.createClient(location, Version.TRAC_0_9); assertTrue(client instanceof TracWebClient); - client = TracClientFactory.createClient(TracTestConstants.TEST_TRAC_010_URL, Version.XML_RPC, "user", - "password", Proxy.NO_PROXY); + location = new WebLocation(TracTestConstants.TEST_TRAC_010_URL, "user", "password"); + client = TracClientFactory.createClient(location, Version.XML_RPC); assertTrue(client instanceof TracXmlRpcClient); - client = TracClientFactory.createClient(TracTestConstants.TEST_TRAC_010_SSL_URL, Version.XML_RPC, "user", - "password", Proxy.NO_PROXY); + + location = new WebLocation(TracTestConstants.TEST_TRAC_010_SSL_URL, "user", "password"); + client = TracClientFactory.createClient(location, Version.XML_RPC); assertTrue(client instanceof TracXmlRpcClient); } public void testCreateClientNull() throws Exception { try { - TracClientFactory.createClient(TracTestConstants.TEST_TRAC_010_URL, null, "user", "password", - Proxy.NO_PROXY); + WebLocation location = new WebLocation(TracTestConstants.TEST_TRAC_010_URL, "user", "password"); + TracClientFactory.createClient(location, null); fail("Expected Exception"); } catch (Exception e) { } @@ -67,24 +68,28 @@ public class TracClientFactoryTest extends TestCase { protected void probeClient(String url, boolean xmlrpcInstalled) throws Exception { Credentials credentials = TestUtil.readCredentials(PrivilegeLevel.USER); - Version version = TracClientFactory.probeClient(url, credentials.username, credentials.password, Proxy.NO_PROXY); + WebLocation location = new WebLocation(url, credentials.username, credentials.password); + Version version = TracClientFactory.probeClient(location); if (xmlrpcInstalled) { assertEquals(Version.XML_RPC, version); } else { assertEquals(Version.TRAC_0_9, version); } - version = TracClientFactory.probeClient(url, "", "", Proxy.NO_PROXY); + location = new WebLocation(url, "", ""); + version = TracClientFactory.probeClient(location); assertEquals(Version.TRAC_0_9, version); try { - version = TracClientFactory.probeClient(url, "invaliduser", "password", Proxy.NO_PROXY); + location = new WebLocation(url, "invaliduser", "password"); + version = TracClientFactory.probeClient(location); fail("Expected TracLoginException, got " + version); } catch (TracLoginException e) { } try { - version = TracClientFactory.probeClient(url + "/nonexistant", "", "", Proxy.NO_PROXY); + location = new WebLocation(url + "/nonexistant", "", ""); + version = TracClientFactory.probeClient(location); fail("Expected TracException, got " + version); } catch (TracException e) { } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientManagerTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientManagerTest.java index ec29a3ca2..77ea698ec 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientManagerTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientManagerTest.java @@ -20,6 +20,7 @@ import org.eclipse.mylyn.internal.trac.core.TracCorePlugin; import org.eclipse.mylyn.internal.trac.core.ITracClient.Version; import org.eclipse.mylyn.internal.trac.core.model.TracMilestone; import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.TaskRepositoryLocationFactory; /** * @author Steffen Pingel @@ -31,7 +32,7 @@ public class TracClientManagerTest extends TestCase { TracTestConstants.TEST_TRAC_096_URL); taskRepository.setVersion(Version.TRAC_0_9.name()); - TracClientManager manager = new TracClientManager(null); + TracClientManager manager = new TracClientManager(null, new TaskRepositoryLocationFactory()); ITracClient client = manager.getRepository(taskRepository); assertNull(client.getMilestones()); @@ -47,7 +48,7 @@ public class TracClientManagerTest extends TestCase { File file = File.createTempFile("mylyn", null); file.deleteOnExit(); - TracClientManager manager = new TracClientManager(file); + TracClientManager manager = new TracClientManager(file, new TaskRepositoryLocationFactory()); ITracClient client = manager.getRepository(taskRepository); assertNull(client.getMilestones()); } @@ -60,7 +61,7 @@ public class TracClientManagerTest extends TestCase { File file = File.createTempFile("mylyn", null); file.deleteOnExit(); - TracClientManager manager = new TracClientManager(file); + TracClientManager manager = new TracClientManager(file, new TaskRepositoryLocationFactory()); ITracClient client = manager.getRepository(taskRepository); assertNull(client.getMilestones()); @@ -69,7 +70,7 @@ public class TracClientManagerTest extends TestCase { TracMilestone[] milestones = client.getMilestones(); manager.writeCache(); - manager = new TracClientManager(file); + manager = new TracClientManager(file, new TaskRepositoryLocationFactory()); assertEquals(Arrays.asList(milestones), Arrays.asList(client.getMilestones())); } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientProxyTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientProxyTest.java index b772fae50..ea1793247 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientProxyTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracClientProxyTest.java @@ -65,7 +65,7 @@ public class TracClientProxyTest extends AbstractTracClientTest { proxy = WebClientUtil.getProxy("localhost", proxyPort + "", "", ""); ITracClient client = connect(url, proxy); try { - client.validate(); + client.validate(callback); } catch (TracException e) { } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracRepositoryConnectorTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracRepositoryConnectorTest.java index 95de316ec..0948900a7 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracRepositoryConnectorTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracRepositoryConnectorTest.java @@ -43,6 +43,7 @@ import org.eclipse.mylyn.tasks.core.AbstractTask; import org.eclipse.mylyn.tasks.core.QueryHitCollector; import org.eclipse.mylyn.tasks.core.RepositoryTaskData; import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.TaskRepositoryLocationFactory; import org.eclipse.mylyn.tasks.core.TaskRepositoryManager; import org.eclipse.mylyn.tasks.ui.TaskFactory; import org.eclipse.mylyn.tasks.ui.TasksUiPlugin; @@ -68,6 +69,8 @@ public class TracRepositoryConnectorTest extends TestCase { protected void setUp() throws Exception { super.setUp(); + TracCorePlugin.getDefault().getConnector().setTaskRepositoryLocationFactory(new TaskRepositoryLocationFactory()); + manager = TasksUiPlugin.getRepositoryManager(); manager.clearRepositories(TasksUiPlugin.getDefault().getRepositoriesFilePath()); @@ -164,13 +167,17 @@ public class TracRepositoryConnectorTest extends TestCase { EditRepositoryWizard wizard = new EditRepositoryWizard(repository); Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); WizardDialog dialog = new WizardDialog(shell, wizard); - dialog.create(); + try { + dialog.create(); - ((TracRepositorySettingsPage) wizard.getSettingsPage()).setTracVersion(Version.XML_RPC); - assertTrue(wizard.performFinish()); + ((TracRepositorySettingsPage) wizard.getSettingsPage()).setTracVersion(Version.XML_RPC); + assertTrue(wizard.performFinish()); - client = connector.getClientManager().getRepository(repository); - assertEquals(Version.XML_RPC, client.getVersion()); + client = connector.getClientManager().getRepository(repository); + assertEquals(Version.XML_RPC, client.getVersion()); + } finally { + dialog.close(); + } } public void testPerformQueryXmlRpc011() { diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracTestConstants.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracTestConstants.java index 2be668746..293277558 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracTestConstants.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracTestConstants.java @@ -13,18 +13,20 @@ package org.eclipse.mylyn.trac.tests; */ public class TracTestConstants { - public static final String TEST_TRAC_096_URL = "http://mylyn.eclipse.org/trac096"; + public static final String SERVER = System.getProperty("mylyn.trac.server", "mylyn.eclipse.org"); + + public static final String TEST_TRAC_096_URL = "http://" + SERVER + "/trac096"; - public static final String TEST_TRAC_010_URL = "http://mylyn.eclipse.org/trac010"; + public static final String TEST_TRAC_010_URL = "http://" + SERVER + "/trac010"; - public static final String TEST_TRAC_010_SSL_URL = "https://mylyn.eclipse.org/trac010"; + public static final String TEST_TRAC_010_SSL_URL = "https://" + SERVER + "/trac010"; - public static final String TEST_TRAC_010_DIGEST_AUTH_URL = "http://mylyn.eclipse.org/trac010digest"; + public static final String TEST_TRAC_010_DIGEST_AUTH_URL = "http://" + SERVER + "/trac010digest"; - public static final String TEST_TRAC_010_FORM_AUTH_URL = "http://mylyn.eclipse.org/trac010formauth"; + public static final String TEST_TRAC_010_FORM_AUTH_URL = "http://" + SERVER + "/trac010formauth"; - public static final String TEST_TRAC_011_URL = "http://mylyn.eclipse.org/trac011"; + public static final String TEST_TRAC_011_URL = "http://" + SERVER + "/trac011"; - public static final String TEST_TRAC_INVALID_URL = "http://mylyn.eclipse.org/doesnotexist"; + public static final String TEST_TRAC_INVALID_URL = "http://" + SERVER + "/doesnotexist"; } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracWebClientTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracWebClientTest.java index d887d0c8d..8b1bd08e0 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracWebClientTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracWebClientTest.java @@ -40,7 +40,7 @@ public class TracWebClientTest extends AbstractTracClientRepositoryTest { public void testValidateAnyPage() throws Exception { connect("http://mylyn.eclipse.org/"); try { - repository.validate(); + repository.validate(callback); fail("Expected TracException"); } catch (TracException e) { } @@ -48,10 +48,10 @@ public class TracWebClientTest extends AbstractTracClientRepositoryTest { public void testValidateAnonymousLogin() throws Exception { connect(TracTestConstants.TEST_TRAC_010_URL, "", ""); - repository.validate(); + repository.validate(callback); connect(TracTestConstants.TEST_TRAC_096_URL, "", ""); - repository.validate(); + repository.validate(callback); } public void testUpdateAttributesAnonymous096() throws Exception { diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracXmlRpcClientTest.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracXmlRpcClientTest.java index 0f4ecd89c..30994feea 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracXmlRpcClientTest.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/TracXmlRpcClientTest.java @@ -40,8 +40,8 @@ public class TracXmlRpcClientTest extends AbstractTracClientRepositoryTest { public void testValidateFailNoAuth() throws Exception { connect(TracTestConstants.TEST_TRAC_010_URL, "", ""); try { - repository.validate(); - fail("Expected TracLoginException"); + repository.validate(callback); + fail("Expected TracPermissiongDeniedException"); } catch (TracPermissionDeniedException e) { } } diff --git a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/XmlRpcServer.java b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/XmlRpcServer.java index cdfcc0cdf..398cbefba 100644 --- a/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/XmlRpcServer.java +++ b/org.eclipse.mylyn.trac.tests/src/org/eclipse/mylyn/trac/tests/support/XmlRpcServer.java @@ -10,7 +10,6 @@ package org.eclipse.mylyn.trac.tests.support; import java.io.IOException; import java.net.MalformedURLException; -import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Hashtable; @@ -22,6 +21,7 @@ import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.eclipse.mylyn.internal.trac.core.TracXmlRpcClient; import org.eclipse.mylyn.internal.trac.core.ITracClient.Version; +import org.eclipse.mylyn.web.core.WebLocation; /** * @author Steffen Pingel @@ -291,7 +291,7 @@ public class XmlRpcServer { this.data = new TestData(); - this.repository = new TracXmlRpcClient(new URL(url), Version.XML_RPC, username, password, null); + this.repository = new TracXmlRpcClient(new WebLocation(url, username, password), Version.XML_RPC); this.client = repository.getClient(); } diff --git a/org.eclipse.mylyn.trac.ui/META-INF/MANIFEST.MF b/org.eclipse.mylyn.trac.ui/META-INF/MANIFEST.MF index 131150bea..98bcc1bba 100644 --- a/org.eclipse.mylyn.trac.ui/META-INF/MANIFEST.MF +++ b/org.eclipse.mylyn.trac.ui/META-INF/MANIFEST.MF @@ -17,7 +17,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.ui.editors, org.eclipse.jface.text, org.eclipse.core.resources, - org.eclipse.ui.workbench.texteditor + org.eclipse.ui.workbench.texteditor, + org.eclipse.mylyn.web.core Eclipse-LazyStart: true Export-Package: org.eclipse.mylyn.internal.trac.ui, org.eclipse.mylyn.internal.trac.ui.editor, diff --git a/org.eclipse.mylyn.trac.ui/src/org/eclipse/mylyn/internal/trac/ui/TracUiPlugin.java b/org.eclipse.mylyn.trac.ui/src/org/eclipse/mylyn/internal/trac/ui/TracUiPlugin.java index 975038837..3ca632f38 100644 --- a/org.eclipse.mylyn.trac.ui/src/org/eclipse/mylyn/internal/trac/ui/TracUiPlugin.java +++ b/org.eclipse.mylyn.trac.ui/src/org/eclipse/mylyn/internal/trac/ui/TracUiPlugin.java @@ -8,6 +8,7 @@ package org.eclipse.mylyn.internal.trac.ui; import org.eclipse.mylyn.internal.trac.core.TracCorePlugin; +import org.eclipse.mylyn.tasks.ui.TaskRepositoryLocationUiFactory; import org.eclipse.mylyn.tasks.ui.TasksUiPlugin; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; @@ -34,6 +35,7 @@ public class TracUiPlugin extends AbstractUIPlugin { public void start(BundleContext context) throws Exception { super.start(context); + TracCorePlugin.getDefault().getConnector().setTaskRepositoryLocationFactory(new TaskRepositoryLocationUiFactory()); TasksUiPlugin.getRepositoryManager().addListener(TracCorePlugin.getDefault().getConnector().getClientManager()); } diff --git a/org.eclipse.mylyn.trac.ui/src/org/eclipse/mylyn/internal/trac/ui/wizard/TracRepositorySettingsPage.java b/org.eclipse.mylyn.trac.ui/src/org/eclipse/mylyn/internal/trac/ui/wizard/TracRepositorySettingsPage.java index 15854b978..815ae5ce2 100644 --- a/org.eclipse.mylyn.trac.ui/src/org/eclipse/mylyn/internal/trac/ui/wizard/TracRepositorySettingsPage.java +++ b/org.eclipse.mylyn.trac.ui/src/org/eclipse/mylyn/internal/trac/ui/wizard/TracRepositorySettingsPage.java @@ -9,7 +9,6 @@ package org.eclipse.mylyn.internal.trac.ui.wizard; import java.net.MalformedURLException; -import java.net.Proxy; import java.net.URL; import org.eclipse.core.runtime.CoreException; @@ -25,8 +24,10 @@ import org.eclipse.mylyn.internal.trac.ui.TracUiPlugin; import org.eclipse.mylyn.tasks.core.RepositoryStatus; import org.eclipse.mylyn.tasks.core.RepositoryTemplate; import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.TaskRepositoryLocationFactory; import org.eclipse.mylyn.tasks.ui.AbstractRepositoryConnectorUi; import org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage; +import org.eclipse.mylyn.web.core.AbstractWebLocation; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; @@ -165,30 +166,25 @@ public class TracRepositorySettingsPage extends AbstractRepositorySettingsPage { // public for testing public class TracValidator extends Validator { - final String repositoryUrl; + private final String repositoryUrl; - final Version version; + private final TaskRepository taskRepository; - final String username; - - final String password; - - final Proxy proxy; + private final Version version; private Version result; - public TracValidator(TaskRepository repository, Version version) { - this.repositoryUrl = repository.getUrl(); - this.username = repository.getUserName(); - this.password = repository.getPassword(); - this.proxy = repository.getProxy(); + public TracValidator(TaskRepository taskRepository, Version version) { + this.repositoryUrl = taskRepository.getUrl(); + this.taskRepository = taskRepository; this.version = version; } @Override public void run(IProgressMonitor monitor) throws CoreException { try { - validate(); + //validate(Provider.of(monitor)); + validate(monitor); } catch (MalformedURLException e) { throw new CoreException(RepositoryStatus.createStatus(repositoryUrl, IStatus.ERROR, TracUiPlugin.PLUGIN_ID, INVALID_REPOSITORY_URL)); @@ -208,23 +204,23 @@ public class TracRepositorySettingsPage extends AbstractRepositorySettingsPage { } } - public void validate() throws MalformedURLException, TracException { + public void validate(IProgressMonitor monitor) throws MalformedURLException, TracException { + AbstractWebLocation location = new TaskRepositoryLocationFactory().createWebLocation(taskRepository); + if (version != null) { - ITracClient client = TracClientFactory.createClient(repositoryUrl, version, username, password, proxy); - client.validate(); + ITracClient client = TracClientFactory.createClient(location, version); + client.validate(monitor); } else { // probe version: XML-RPC access first, then web // access try { - ITracClient client = TracClientFactory.createClient(repositoryUrl, Version.XML_RPC, username, - password, proxy); - client.validate(); + ITracClient client = TracClientFactory.createClient(location, Version.XML_RPC); + client.validate(monitor); result = Version.XML_RPC; } catch (TracException e) { try { - ITracClient client = TracClientFactory.createClient(repositoryUrl, Version.TRAC_0_9, username, - password, proxy); - client.validate(); + ITracClient client = TracClientFactory.createClient(location, Version.TRAC_0_9); + client.validate(monitor); result = Version.TRAC_0_9; if (e instanceof TracPermissionDeniedException) { |