summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Zarna2013-07-09 11:45:20 (EDT)
committer Tomasz Zarna2013-07-12 11:06:52 (EDT)
commitc6bf420f62652c220f15858b625b8f8666c47311 (patch)
treeafe72cccec7d30f0a82c739114cfbe2676dc90e2
parentafd9a826ca6875b52eabe889b01b17137e25c562 (diff)
downloadorg.eclipse.mylyn.reviews-c6bf420f62652c220f15858b625b8f8666c47311.zip
org.eclipse.mylyn.reviews-c6bf420f62652c220f15858b625b8f8666c47311.tar.gz
org.eclipse.mylyn.reviews-c6bf420f62652c220f15858b625b8f8666c47311.tar.bz2
395059: fix 2.6 auth: use xGerritAuth if available and try POST to loginrefs/changes/91/14191/8
Bug: 395059 Change-Id: Ib8452cbd82187e3e55bb1855718b446a906f349a Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=395059 Signed-off-by: Tomasz Zarna <tomasz.zarna@tasktop.com>
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/GerritConnector.java6
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java106
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHtmlProcessor.java18
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java78
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritService.java16
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java6
6 files changed, 144 insertions, 86 deletions
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/GerritConnector.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/GerritConnector.java
index f3ae0aa..cf7c323 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/GerritConnector.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/GerritConnector.java
@@ -1,5 +1,5 @@
/*********************************************************************
- * Copyright (c) 2010 Sony Ericsson/ST Ericsson and others.
+ * Copyright (c) 2010, 2013 Sony Ericsson/ST Ericsson 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
@@ -108,6 +108,10 @@ public class GerritConnector extends ReviewsConnector {
public static final String KEY_REPOSITORY_OPEN_ID_PROVIDER = CONNECTOR_KIND + ".openId.provider"; //$NON-NLS-1$
+ public static final String GERRIT_RPC_URI = "/gerrit/rpc/"; //$NON-NLS-1$
+
+ public static final String GERRIT_260_RPC_URI = "/gerrit_ui/rpc/"; //$NON-NLS-1$
+
private final GerritTaskDataHandler taskDataHandler = new GerritTaskDataHandler(this);
private TaskRepositoryLocationFactory taskRepositoryLocationFactory = new TaskRepositoryLocationFactory();
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java
index 0042441..71af1c4 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java
@@ -96,8 +96,6 @@ public class GerritClient extends ReviewsClient {
private static final Pattern GERRIT_VERSION_PATTERN = Pattern.compile("Powered by Gerrit Code Review (.+)</p>"); //$NON-NLS-1$
- private static final Version GERRIT_VERSION_2_6 = new Version(2, 6, 0);
-
private abstract class Operation<T> implements AsyncCallback<T> {
private Throwable exception;
@@ -283,7 +281,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<PatchLineComment>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getPatchDetailService().saveDraft(comment, this);
+ getPatchDetailService(monitor).saveDraft(comment, this);
}
});
}
@@ -294,7 +292,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ChangeDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeManageService().abandonChange(id, message, this);
+ getChangeManageService(monitor).abandonChange(id, message, this);
}
});
}
@@ -307,7 +305,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ChangeDetailX>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeDetailService().changeDetailX(id, this);
+ getChangeDetailService(monitor).changeDetailX(id, this);
}
});
}
@@ -344,7 +342,7 @@ public class GerritClient extends ReviewsClient {
AccountDiffPreference diffPreference = execute(monitor, new Operation<AccountDiffPreference>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getAccountService().myDiffPreferences(this);
+ getAccountService(monitor).myDiffPreferences(this);
}
});
@@ -365,12 +363,7 @@ public class GerritClient extends ReviewsClient {
// getSystemInfoService().contributorAgreements(this);
// }
// });
- account = execute(monitor, new Operation<Account>() {
- @Override
- public void execute(IProgressMonitor monitor) throws GerritException {
- getAccountService().myAccount(this);
- }
- });
+ account = getAccount(monitor);
} else {
// XXX should run some more meaningful validation as anonymous, for now any call is good to validate the URL etc.
executeQuery(monitor, "status:open"); //$NON-NLS-1$
@@ -387,7 +380,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<PatchScript>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getPatchDetailService().patchScript(key, leftId, rightId, diffPrefs, this);
+ getPatchDetailService(monitor).patchScript(key, leftId, rightId, diffPrefs, this);
}
});
}
@@ -410,7 +403,8 @@ public class GerritClient extends ReviewsClient {
result = execute(monitor, new Operation<PatchSetDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeDetailService().patchSetDetail2(idBase, idTarget, createAccountDiffPreference(), this);
+ getChangeDetailService(monitor).patchSetDetail2(idBase, idTarget, createAccountDiffPreference(),
+ this);
}
});
} catch (GerritException e) {
@@ -420,7 +414,7 @@ public class GerritClient extends ReviewsClient {
result = execute(monitor, new Operation<PatchSetDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeDetailService().patchSetDetail(idTarget, this);
+ getChangeDetailService(monitor).patchSetDetail(idTarget, this);
}
});
} else {
@@ -433,8 +427,8 @@ public class GerritClient extends ReviewsClient {
result = execute(monitor, new Operation<PatchSetDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeDetailService().patchSetDetail(idBase, idTarget, createAccountDiffPreference(),
- this);
+ getChangeDetailService(monitor).patchSetDetail(idBase, idTarget,
+ createAccountDiffPreference(), this);
}
});
} else {
@@ -456,7 +450,7 @@ public class GerritClient extends ReviewsClient {
new Operation<org.eclipse.mylyn.internal.gerrit.core.client.compat.PatchSetPublishDetailX>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeDetailService().patchSetPublishDetailX(id, this);
+ getChangeDetailService(monitor).patchSetPublishDetailX(id, this);
}
});
return publishDetail;
@@ -518,7 +512,7 @@ public class GerritClient extends ReviewsClient {
execute(monitor, new Operation<VoidResult>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getPatchDetailService().publishComments(id, message, approvals, this);
+ getPatchDetailService(monitor).publishComments(id, message, approvals, this);
}
});
}
@@ -530,7 +524,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ReviewerResult>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getPatchDetailService().addReviewers(id, reviewers, this);
+ getPatchDetailService(monitor).addReviewers(id, reviewers, this);
}
});
} catch (GerritException e) {
@@ -540,7 +534,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ReviewerResult>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getPatchDetailService().addReviewers(id, reviewers, false, this);
+ getPatchDetailService(monitor).addReviewers(id, reviewers, false, this);
}
});
} else {
@@ -575,7 +569,7 @@ public class GerritClient extends ReviewsClient {
AccountDashboardInfo ad = execute(monitor, new Operation<AccountDashboardInfo>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeListService().forAccount(account.getId(), this);
+ getChangeListService(monitor).forAccount(account.getId(), this);
}
});
@@ -597,7 +591,7 @@ public class GerritClient extends ReviewsClient {
private boolean hasJsonRpcApi(IProgressMonitor monitor) throws GerritException {
Version version = getCachedVersion(monitor);
- return version.compareTo(GERRIT_VERSION_2_6) < 0;
+ return !GerritVersion.isVersion26OrLater(version);
}
/**
@@ -680,7 +674,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ChangeDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeManageService().publish(id, this);
+ getChangeManageService(monitor).publish(id, this);
}
});
}
@@ -690,7 +684,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ChangeDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeManageService().rebaseChange(id, this);
+ getChangeManageService(monitor).rebaseChange(id, this);
}
});
}
@@ -701,7 +695,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ChangeDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeManageService().restoreChange(id, message, this);
+ getChangeManageService(monitor).restoreChange(id, message, this);
}
});
}
@@ -712,7 +706,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ChangeDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeManageService().revertChange(id, message, this);
+ getChangeManageService(monitor).revertChange(id, message, this);
}
});
}
@@ -723,7 +717,7 @@ public class GerritClient extends ReviewsClient {
return execute(monitor, new Operation<ChangeDetail>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeManageService().submit(id, this);
+ getChangeManageService(monitor).submit(id, this);
}
});
}
@@ -735,7 +729,7 @@ public class GerritClient extends ReviewsClient {
SingleListChangeInfo sl = execute(monitor, new Operation<SingleListChangeInfo>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getChangeListService().allQueryNext(queryString, "z", -1, this); //$NON-NLS-1$
+ getChangeListService(monitor).allQueryNext(queryString, "z", -1, this); //$NON-NLS-1$
}
});
return convert(sl.getChanges());
@@ -814,12 +808,7 @@ public class GerritClient extends ReviewsClient {
return myAcount;
}
}
- Account account = execute(monitor, new Operation<Account>() {
- @Override
- public void execute(IProgressMonitor monitor) throws GerritException {
- getAccountService().myAccount(this);
- }
- });
+ Account account = executeAccount(monitor);
synchronized (this) {
myAcount = account;
@@ -827,24 +816,33 @@ public class GerritClient extends ReviewsClient {
return myAcount;
}
- private AccountService getAccountService() {
- return getService(AccountService.class);
+ public Account executeAccount(IProgressMonitor monitor) throws GerritException {
+ return execute(monitor, new Operation<Account>() {
+ @Override
+ public void execute(IProgressMonitor monitor) throws GerritException {
+ getAccountService(monitor).myAccount(this);
+ }
+ });
}
- private ChangeDetailService getChangeDetailService() {
- return getService(ChangeDetailService.class);
+ private AccountService getAccountService(IProgressMonitor monitor) {
+ return getService(AccountService.class, monitor);
}
- private ChangeListService getChangeListService() {
- return getService(ChangeListService.class);
+ private ChangeDetailService getChangeDetailService(IProgressMonitor monitor) {
+ return getService(ChangeDetailService.class, monitor);
}
- private ChangeManageService getChangeManageService() {
- return getService(ChangeManageService.class);
+ private ChangeListService getChangeListService(IProgressMonitor monitor) {
+ return getService(ChangeListService.class, monitor);
}
- private PatchDetailService getPatchDetailService() {
- return getService(PatchDetailService.class);
+ private ChangeManageService getChangeManageService(IProgressMonitor monitor) {
+ return getService(ChangeManageService.class, monitor);
+ }
+
+ private PatchDetailService getPatchDetailService(IProgressMonitor monitor) {
+ return getService(PatchDetailService.class, monitor);
}
private List<Project> getVisibleProjects(IProgressMonitor monitor, GerritConfig gerritConfig)
@@ -854,7 +852,7 @@ public class GerritClient extends ReviewsClient {
List<ProjectDetailX> projectDetails = execute(monitor, new Operation<List<ProjectDetailX>>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getProjectAdminService().visibleProjectDetails(this);
+ getProjectAdminService(monitor).visibleProjectDetails(this);
}
});
for (ProjectDetailX projectDetail : projectDetails) {
@@ -868,7 +866,7 @@ public class GerritClient extends ReviewsClient {
List<Project> projects = execute(monitor, new Operation<List<Project>>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
- getProjectAdminService().visibleProjects(this);
+ getProjectAdminService(monitor).visibleProjects(this);
}
});
for (Project project : projects) {
@@ -885,8 +883,8 @@ public class GerritClient extends ReviewsClient {
return result;
}
- private ProjectAdminService getProjectAdminService() {
- return getService(ProjectAdminService.class);
+ private ProjectAdminService getProjectAdminService(IProgressMonitor monitor) {
+ return getService(ProjectAdminService.class, monitor);
}
public boolean isAnonymous() {
@@ -932,10 +930,16 @@ public class GerritClient extends ReviewsClient {
return operation.getResult();
}
- protected synchronized <T extends RemoteJsonService> T getService(Class<T> clazz) {
+ protected synchronized <T extends RemoteJsonService> T getService(Class<T> clazz, IProgressMonitor monitor) {
+ Version version = Version.emptyVersion;
+ try {
+ version = getCachedVersion(monitor);
+ } catch (GerritException e) {
+ // ignore, continue with emptyVersion
+ }
RemoteJsonService service = serviceByClass.get(clazz);
if (service == null) {
- service = GerritService.create(clazz, client);
+ service = GerritService.create(clazz, client, version);
serviceByClass.put(clazz, service);
}
return clazz.cast(service);
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHtmlProcessor.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHtmlProcessor.java
index eb6052a..bd0bf02 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHtmlProcessor.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHtmlProcessor.java
@@ -60,6 +60,11 @@ public class GerritHtmlProcessor {
private String xsrfKey;
+ /**
+ * Introduced in Gerrit 2.6 as a replacement for {@link #xsrfKey}.
+ */
+ private String xGerritAuth;
+
public GerritConfigX getConfig() {
return config;
}
@@ -68,6 +73,10 @@ public class GerritHtmlProcessor {
return xsrfKey;
}
+ public String getXGerritAuth() {
+ return xGerritAuth;
+ }
+
public void parse(InputStream in, String charset) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, charset));
HtmlStreamTokenizer tokenizer = new HtmlStreamTokenizer(reader, null);
@@ -94,6 +103,7 @@ public class GerritHtmlProcessor {
private void parse(String text) {
String configPrefix = "var gerrit_hostpagedata={\"config\":"; //$NON-NLS-1$
String configXsrfToken = "hostpagedata.xsrfToken=\""; //$NON-NLS-1$
+ String configXGerritAuth = "hostpagedata.xGerritAuth=\""; //$NON-NLS-1$
String[] tokens = text.split(";gerrit_"); //$NON-NLS-1$
for (String token : tokens) {
if (token.startsWith(configPrefix)) {
@@ -101,12 +111,16 @@ public class GerritHtmlProcessor {
// remove closing }
token = token.substring(0, token.length() - 1);
this.config = gerritConfigFromString(token);
- }
- if (token.startsWith(configXsrfToken)) {
+ } else if (token.startsWith(configXsrfToken)) {
token = token.substring(configXsrfToken.length());
// remove closing "
token = token.substring(0, token.length() - 1);
this.xsrfKey = token;
+ } else if (token.startsWith(configXGerritAuth)) {
+ token = token.substring(configXGerritAuth.length());
+ // remove closing "
+ token = token.substring(0, token.length() - 1);
+ this.xGerritAuth = token;
}
}
}
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
index 7064a1d..8bb20e3 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
@@ -40,6 +40,7 @@ import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.commons.net.UnsupportedRequestException;
import org.eclipse.mylyn.commons.net.WebUtil;
+import org.eclipse.mylyn.internal.gerrit.core.GerritConnector;
import com.google.gerrit.common.auth.SignInMode;
import com.google.gerrit.common.auth.openid.DiscoveryResult;
@@ -99,7 +100,7 @@ public class GerritHttpClient {
}
- private static final Object LOGIN_COOKIE_NAME = "GerritAccount"; //$NON-NLS-1$
+ private static final String LOGIN_COOKIE_NAME = "GerritAccount"; //$NON-NLS-1$
private static final String LOGIN_URL = "/login/mine"; //$NON-NLS-1$
@@ -228,7 +229,12 @@ public class GerritHttpClient {
try {
GerritHtmlProcessor processor = new GerritHtmlProcessor();
processor.parse(in, method.getResponseCharSet());
- setXsrfKey(processor.getXsrfKey());
+ String xGerritAuth = processor.getXGerritAuth();
+ if (xGerritAuth != null) {
+ setXsrfKey(xGerritAuth);
+ } else {
+ setXsrfKey(processor.getXsrfKey());
+ }
} finally {
in.close();
}
@@ -325,7 +331,7 @@ public class GerritHttpClient {
};
OpenIdAuthenticationResponse openIdResponse = null;
- JsonRequest jsonRequest = new JsonRequest("/gerrit/rpc/OpenIdService", entity);
+ JsonRequest jsonRequest = new JsonRequest(GerritConnector.GERRIT_RPC_URI + "OpenIdService", entity); //$NON-NLS-1$
PostMethod method = jsonRequest.createMethod();
try {
int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);
@@ -341,7 +347,7 @@ public class GerritHttpClient {
OpenIdAuthenticationRequest authenticationRequest = new OpenIdAuthenticationRequest(
result.providerUrl, result.providerArgs, returnUrl);
authenticationRequest.setAlternateUrl(location.getUrl());
- authenticationRequest.setCookie("GerritAccount");
+ authenticationRequest.setCookie("GerritAccount"); //$NON-NLS-1$
authenticationRequest.setCookieUrl(location.getUrl());
try {
openIdResponse = ((IOpenIdLocation) location).requestAuthentication(authenticationRequest,
@@ -351,7 +357,7 @@ public class GerritHttpClient {
}
}
} else {
- throw new GerritException("Invalid OpenID provider");
+ throw new GerritException("Invalid OpenID provider"); //$NON-NLS-1$
}
}
if (openIdResponse == null) {
@@ -363,8 +369,8 @@ public class GerritHttpClient {
if (openIdResponse.getCookieValue() != null) {
URL url = new URL(location.getUrl());
- boolean isSecure = "https".equals(url.getProtocol());
- setXsrfCookie(new Cookie(url.getHost(), "GerritAccount", openIdResponse.getCookieValue(), url.getPath(),
+ boolean isSecure = "https".equals(url.getProtocol()); //$NON-NLS-1$
+ setXsrfCookie(new Cookie(url.getHost(), "GerritAccount", openIdResponse.getCookieValue(), url.getPath(), //$NON-NLS-1$
null, isSecure));
return HttpStatus.SC_TEMPORARY_REDIRECT;
} else {
@@ -394,7 +400,7 @@ public class GerritHttpClient {
args.add(credentials.getUserName());
args.add(credentials.getPassword());
- final String request = json.createRequest(getId(), null, "authenticate", args);
+ final String request = json.createRequest(getId(), null, "authenticate", args); //$NON-NLS-1$
JsonEntity entity = new JsonEntity() {
@Override
public String getContent() {
@@ -402,7 +408,7 @@ public class GerritHttpClient {
}
};
- JsonRequest jsonRequest = new JsonRequest("/gerrit/rpc/UserPassAuthService", entity);
+ JsonRequest jsonRequest = new JsonRequest(GerritConnector.GERRIT_RPC_URI + "UserPassAuthService", entity); //$NON-NLS-1$
PostMethod method = jsonRequest.createMethod();
try {
int code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);
@@ -473,30 +479,44 @@ public class GerritHttpClient {
Credentials httpCredentials = WebUtil.getHttpClientCredentials(credentials, WebUtil.getHost(repositoryUrl));
httpClient.getState().setCredentials(authScope, httpCredentials);
- GetMethod method = new GetMethod(WebUtil.getRequestPath(repositoryUrl + LOGIN_URL));
- method.setFollowRedirects(false);
- int code;
- try {
- code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);
- if (needsReauthentication(code, monitor)) {
- return -1;
- }
-
- if (code == HttpStatus.SC_MOVED_TEMPORARILY) {
- Header locationHeader = method.getResponseHeader("Location");
- if (locationHeader != null) {
- if (locationHeader.getValue().endsWith("SignInFailure,SIGN_IN,Session cookie not available.")) {
- // try different authentication method
- return HttpStatus.SC_NOT_FOUND;
+ HttpMethodBase[] methods = getFormAuthMethods(repositoryUrl, credentials);
+ for (HttpMethodBase method : methods) {
+ int code;
+ try {
+ code = WebUtil.execute(httpClient, hostConfiguration, method, monitor);
+ if (code == HttpStatus.SC_METHOD_NOT_ALLOWED) {
+ continue; // try next http method
+ } else if (needsReauthentication(code, monitor)) {
+ return -1;
+ } else if (code == HttpStatus.SC_MOVED_TEMPORARILY) {
+ Header locationHeader = method.getResponseHeader("Location"); //$NON-NLS-1$
+ if (locationHeader != null) {
+ if (locationHeader.getValue().endsWith("SignInFailure,SIGN_IN,Session cookie not available.")) { //$NON-NLS-1$
+ // try different authentication method
+ return HttpStatus.SC_NOT_FOUND;
+ }
}
+ } else if (code != HttpStatus.SC_NOT_FOUND) {
+ throw new GerritHttpException(code);
}
- } else if (code != HttpStatus.SC_NOT_FOUND) {
- throw new GerritHttpException(code);
+ return code;
+ } finally {
+ WebUtil.releaseConnection(method, monitor);
}
- return code;
- } finally {
- WebUtil.releaseConnection(method, monitor);
}
+ return -1;
+ }
+
+ private HttpMethodBase[] getFormAuthMethods(String repositoryUrl, AuthenticationCredentials credentials) {
+ PostMethod post = new PostMethod(WebUtil.getRequestPath(repositoryUrl + LOGIN_URL));
+ post.setParameter("username", credentials.getUserName()); //$NON-NLS-1$
+ post.setParameter("password", credentials.getPassword()); //$NON-NLS-1$
+ post.setFollowRedirects(false);
+
+ GetMethod get = new GetMethod(WebUtil.getRequestPath(repositoryUrl + LOGIN_URL));
+ get.setFollowRedirects(false);
+
+ return new HttpMethodBase[] { post, get };
}
private synchronized boolean needsAuthentication() {
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritService.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritService.java
index dd7286b..b775d0a 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritService.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritService.java
@@ -1,5 +1,5 @@
/*********************************************************************
- * Copyright (c) 2011 Tasktop Technologies and others.
+ * Copyright (c) 2011, 2013 Tasktop Technologies 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
@@ -19,7 +19,9 @@ import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.mylyn.internal.gerrit.core.GerritConnector;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.JsonEntity;
+import org.osgi.framework.Version;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwtjsonrpc.client.RemoteJsonService;
@@ -53,12 +55,20 @@ public class GerritService implements InvocationHandler {
}
- public static <T extends RemoteJsonService> T create(Class<T> serviceClass, GerritHttpClient gerritHttpClient) {
- InvocationHandler handler = new GerritService(gerritHttpClient, "/gerrit/rpc/" + serviceClass.getSimpleName()); //$NON-NLS-1$
+ public static <T extends RemoteJsonService> T create(Class<T> serviceClass, GerritHttpClient gerritHttpClient,
+ Version version) {
+ String uri = getGerritRpcUri(version);
+ InvocationHandler handler = new GerritService(gerritHttpClient, uri + serviceClass.getSimpleName());
return serviceClass.cast(Proxy.newProxyInstance(GerritService.class.getClassLoader(),
new Class<?>[] { serviceClass }, handler));
}
+ private static String getGerritRpcUri(Version version) {
+ return GerritVersion.isVersion26OrLater(version)
+ ? GerritConnector.GERRIT_260_RPC_URI
+ : GerritConnector.GERRIT_RPC_URI;
+ }
+
private final String uri;
protected GerritHttpClient client;
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java
index 7ddb5a3..7453120 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java
@@ -21,6 +21,8 @@ import org.osgi.framework.Version;
public class GerritVersion extends Version {
+ private static final Version VERSION_2_6_0 = new Version(2, 6, 0);
+
// e.g. 2.6 or 2.6.0
private static final Pattern MAJOR_MINOR_MICRO_VERSION_PATTERN = Pattern.compile("\\d+\\.\\d+(\\.\\d+)?"); //$NON-NLS-1$
@@ -53,4 +55,8 @@ public class GerritVersion extends Version {
}
throw new IllegalArgumentException("Unrecognized version pattern : " + version); //$NON-NLS-1$
}
+
+ public static boolean isVersion26OrLater(Version version) {
+ return version.compareTo(VERSION_2_6_0) >= 0;
+ }
}