Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java17
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java8
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java68
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java6
-rw-r--r--org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/OpenIdAuthenticationTest.java12
5 files changed, 85 insertions, 26 deletions
diff --git a/org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java b/org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java
index d0178505a..42527e465 100644
--- a/org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java
+++ b/org.eclipse.mylyn.gerrit.core.tests/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClientTest.java
@@ -48,7 +48,7 @@ import com.google.gson.reflect.TypeToken;
/**
* Unit tests for {@link GerritHttpClient}.
- *
+ *
* @author Christian Trutz
*/
@RunWith(MockitoJUnitRunner.class)
@@ -57,7 +57,7 @@ public class GerritHttpClientTest {
private final int code;
private TestGerritHttpClient(AbstractWebLocation location, int code) {
- super(location);
+ super(location, GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
this.code = code;
}
@@ -89,7 +89,7 @@ public class GerritHttpClientTest {
*/
@Test(expected = AssertionFailedException.class)
public void constructorNull() {
- new GerritHttpClient((AbstractWebLocation) null);
+ new GerritHttpClient((AbstractWebLocation) null, GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
}
/**
@@ -98,7 +98,8 @@ public class GerritHttpClientTest {
*/
@Test(expected = AssertionFailedException.class)
public void postJsonRequestNullServiceUri() throws IOException, GerritException {
- GerritHttpClient gerritHttpClient = new GerritHttpClient(abstractWebLocation);
+ GerritHttpClient gerritHttpClient = new GerritHttpClient(abstractWebLocation,
+ GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
gerritHttpClient.postJsonRequest(null, new JsonEntity() {
@Override
public String getContent() {
@@ -113,7 +114,8 @@ public class GerritHttpClientTest {
*/
@Test(expected = AssertionFailedException.class)
public void postJsonRequestNullJsonEntity() throws IOException, GerritException {
- GerritHttpClient gerritHttpClient = new GerritHttpClient(abstractWebLocation);
+ GerritHttpClient gerritHttpClient = new GerritHttpClient(abstractWebLocation,
+ GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
gerritHttpClient.postJsonRequest("not null", null, progressMonitor); //$NON-NLS-1$
}
@@ -122,8 +124,9 @@ public class GerritHttpClientTest {
// given
final TypeToken<Byte[]> byteArrayType = new TypeToken<Byte[]>() {
};
- Request<byte[]> request = new GerritHttpClient(abstractWebLocation).new RestRequest<byte[]>(HttpMethod.GET,
- "serviceUri", null /*input*/, byteArrayType.getType(), null /*error handler*/); //$NON-NLS-1$
+ Request<byte[]> request = new GerritHttpClient(abstractWebLocation,
+ GerritCapabilities.MAXIMUM_SUPPORTED_VERSION).new RestRequest<byte[]>(HttpMethod.GET, "serviceUri", //$NON-NLS-1$
+ null /*input*/, byteArrayType.getType(), null /*error handler*/);
HttpMethodBase httpMethodBase = mock(HttpMethodBase.class);
byte[] binary = "binary".getBytes(); //$NON-NLS-1$
when(httpMethodBase.getResponseBody()).thenReturn(binary);
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 5169108f5..84311fe0b 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
@@ -208,7 +208,7 @@ public abstract class GerritClient extends ReviewsClient {
protected void initialize(AbstractWebLocation location, GerritConfiguration config,
GerritAuthenticationState authState, String xsrfKey, final GerritClientStateListener stateListener) {
this.stateListener = stateListener;
- this.client = new GerritHttpClient(location) {
+ this.client = new GerritHttpClient(location, version) {
@Override
protected void sessionChanged(Cookie cookie) {
GerritAuthenticationState authState = new GerritAuthenticationState();
@@ -297,7 +297,11 @@ public abstract class GerritClient extends ReviewsClient {
}, monitor);
if (gerritConfig == null) {
- throw new GerritException("Failed to obtain Gerrit configuration"); //$NON-NLS-1$
+ if (GerritVersion.isVersion2120OrLater(version)) {
+ gerritConfig = new GerritConfigX();
+ } else {
+ throw new GerritException("Failed to obtain Gerrit configuration"); //$NON-NLS-1$
+ }
}
return gerritConfig;
} catch (UnknownHostException cause) {
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 acb2b7d40..41b72357a 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
@@ -20,7 +20,10 @@ import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
+import java.util.Optional;
+import java.util.function.Supplier;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Credentials;
@@ -46,6 +49,7 @@ import org.eclipse.mylyn.commons.net.UnsupportedRequestException;
import org.eclipse.mylyn.commons.net.WebUtil;
import org.eclipse.mylyn.internal.gerrit.core.GerritConnector;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.Request.HttpMethod;
+import org.osgi.framework.Version;
import com.google.gerrit.common.auth.SignInMode;
import com.google.gerrit.common.auth.openid.DiscoveryResult;
@@ -228,10 +232,15 @@ public class GerritHttpClient {
private volatile boolean obtainedXsrfKey;
- public GerritHttpClient(AbstractWebLocation location) {
+ private static final String XSRF_TOKEN_COOKIE_NAME = "XSRF_TOKEN"; //$NON-NLS-1$
+
+ private final Version version;
+
+ public GerritHttpClient(AbstractWebLocation location, Version version) {
Assert.isNotNull(location, "Location must be not null."); //$NON-NLS-1$
this.location = location;
this.httpClient = new HttpClient(WebUtil.getConnectionManager());
+ this.version = version;
}
public synchronized int getId() {
@@ -331,7 +340,11 @@ public class GerritHttpClient {
if (obtainedXsrfKey) {
// required to authenticate against Gerrit 2.6+ REST endpoints
// harmless in previous versions
- method.setRequestHeader(X_GERRIT_AUTHORITY, xsrfKey);
+ if (GerritVersion.isVersion2120OrLater(version)) {
+ method.setRequestHeader(X_GERRIT_AUTHORITY, getXsrfKey());
+ } else {
+ method.setRequestHeader(X_GERRIT_AUTHORITY, xsrfKey);
+ }
}
try {
// Execute the method.
@@ -387,6 +400,14 @@ public class GerritHttpClient {
try {
code = execute(method, monitor);
if (code == HttpStatus.SC_OK) {
+ if (GerritVersion.isVersion2120OrLater(version)) {
+ try {
+ setXsrfCookie(XSRF_TOKEN_COOKIE_NAME);
+ } catch (Exception e) {
+ setXsrfKey(null);
+ }
+ }
+
InputStream in = WebUtil.getResponseBodyAsStream(method, monitor);
try {
GerritHtmlProcessor processor = new GerritHtmlProcessor();
@@ -460,8 +481,12 @@ public class GerritHttpClient {
throw new GerritLoginException();
}
- // Location: http://egit.eclipse.org/r/#SignInFailure,SIGN_IN,Session cookie not available
- validateAuthenticationState(httpClient);
+ if (GerritVersion.isVersion2120OrLater(version)) {
+ setXsrfCookie(LOGIN_COOKIE_NAME);
+ } else {
+ // Location: http://egit.eclipse.org/r/#SignInFailure,SIGN_IN,Session cookie not available
+ validateAuthenticationState(httpClient);
+ }
// success since no exception was thrown
break;
@@ -726,14 +751,11 @@ public class GerritHttpClient {
}
protected void validateAuthenticationState(HttpClient httpClient) throws GerritLoginException {
- Cookie[] cookies = httpClient.getState().getCookies();
- for (Cookie cookie : cookies) {
- if (LOGIN_COOKIE_NAME.equals(cookie.getName())) {
- setXsrfCookie(cookie);
- return;
- }
+ Optional<Cookie> cookie = findCookieWithName(LOGIN_COOKIE_NAME, httpClient);
+ if (cookie.isPresent()) {
+ setXsrfCookie(cookie.get());
+ return;
}
-
if (CoreUtil.TEST_MODE) {
System.err.println(" Authentication failed: " + httpClient.getState()); //$NON-NLS-1$
}
@@ -771,4 +793,28 @@ public class GerritHttpClient {
sessionChanged(xsrfCookie);
}
+ private void setXsrfCookie(String cookieName) throws GerritLoginException {
+ Optional<Cookie> cookie = findCookieWithName(cookieName, httpClient);
+ cookie.ifPresent(this::setXsrfCookie);
+ cookie.orElseThrow(() -> new GerritLoginException());
+ }
+
+ private Optional<Cookie> findCookieWithName(String cookieName, HttpClient httpClient) {
+ return Arrays.stream(httpClient.getState().getCookies())
+ .filter(c -> cookieName.equals(c.getName()))
+ .findFirst();
+ }
+
+ static <T extends Optional, X extends Throwable> T nonNullOrThrow(T val, Supplier<? extends X> exSupplier,
+ HttpClient httpClient) throws X {
+ if (val.isPresent()) {
+ return val;
+ } else {
+ if (CoreUtil.TEST_MODE) {
+ System.err.println(" Authentication failed: " + httpClient.getState()); //$NON-NLS-1$
+ }
+ throw exSupplier.get();
+ }
+ }
+
}
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 37fa7781f..22aa067b8 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
@@ -23,6 +23,8 @@ public class GerritVersion {
public static final Version VERSION_2_11_2 = new Version(2, 11, 2);
+ public static final Version VERSION_2_12_0 = new Version(2, 12, 0);
+
// e.g. 2.6 or 2.6.0
private static final Pattern MAJOR_MINOR_MICRO_VERSION_PATTERN = Pattern.compile("V?\\d+\\.\\d+(\\.\\d+)?"); //$NON-NLS-1$
@@ -59,4 +61,8 @@ public class GerritVersion {
return version.compareTo(VERSION_2_11_2) >= 0;
}
+ public static boolean isVersion2120OrLater(Version version) {
+ return version.compareTo(VERSION_2_12_0) >= 0;
+ }
+
}
diff --git a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/OpenIdAuthenticationTest.java b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/OpenIdAuthenticationTest.java
index 6a12a3b3b..4c64a9ac6 100644
--- a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/OpenIdAuthenticationTest.java
+++ b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/OpenIdAuthenticationTest.java
@@ -13,14 +13,13 @@ package org.eclipse.mylyn.gerrit.tests.core.client;
import java.io.IOException;
-import junit.framework.TestCase;
-
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.GetMethod;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.mylyn.commons.net.UnsupportedRequestException;
import org.eclipse.mylyn.commons.net.WebLocation;
import org.eclipse.mylyn.gerrit.tests.support.GerritFixture;
+import org.eclipse.mylyn.internal.gerrit.core.client.GerritCapabilities;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.Request;
import org.eclipse.mylyn.internal.gerrit.core.client.GerritLoginException;
@@ -28,6 +27,8 @@ import org.eclipse.mylyn.internal.gerrit.core.client.IOpenIdLocation;
import org.eclipse.mylyn.internal.gerrit.core.client.OpenIdAuthenticationRequest;
import org.eclipse.mylyn.internal.gerrit.core.client.OpenIdAuthenticationResponse;
+import junit.framework.TestCase;
+
/**
* @author Steffen Pingel
*/
@@ -56,11 +57,10 @@ public class OpenIdAuthenticationTest extends TestCase {
private static String PROVIDER_URL = "https://www.google.com/accounts/o8/id"; //$NON-NLS-1$
- StubRepositoryLocation location = new StubRepositoryLocation(GerritFixture.current()
- .repository()
- .getRepositoryUrl());
+ StubRepositoryLocation location = new StubRepositoryLocation(
+ GerritFixture.current().repository().getRepositoryUrl());
- GerritHttpClient client = new GerritHttpClient(location);
+ GerritHttpClient client = new GerritHttpClient(location, GerritCapabilities.MAXIMUM_SUPPORTED_VERSION);
public void testExecuteNullOpenIdProviderNullCredentials() throws Exception {
client.execute(createRequest(), null);

Back to the top