summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Zarna2013-07-29 06:47:19 (EDT)
committerGerrit Code Review @ Eclipse.org2013-07-30 04:53:54 (EDT)
commit2419299d4e5288a55a9cc3fdfcc323f13e190bfa (patch)
treec959fd861de0baf8b2f5411eb18578440cc0ce39
parent997c4afd4aeb434a6b6e9aa00d907f98e379b808 (diff)
downloadorg.eclipse.mylyn.reviews-2419299d4e5288a55a9cc3fdfcc323f13e190bfa.zip
org.eclipse.mylyn.reviews-2419299d4e5288a55a9cc3fdfcc323f13e190bfa.tar.gz
org.eclipse.mylyn.reviews-2419299d4e5288a55a9cc3fdfcc323f13e190bfa.tar.bz2
395059: create method for sending GET REST requestsrefs/changes/15/14915/2
Bug: 395059 Change-Id: Ifbcdd5e5764abc2af2efdc23329711930c3ecbad 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/client/GerritClient.java70
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java42
2 files changed, 67 insertions, 45 deletions
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 a70529a..402bd0a 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
@@ -16,6 +16,7 @@ package org.eclipse.mylyn.internal.gerrit.core.client;
import java.io.IOException;
import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
@@ -260,7 +261,7 @@ public class GerritClient extends ReviewsClient {
});
} else {
final String uri = "/a/changes/" + id.getParentKey().get() + "/abandon"; //$NON-NLS-1$ //$NON-NLS-2$
- execute(uri, new AbandonInput(message), ChangeInfo.class, null/*no error handler*/, monitor);
+ executePostRestRequest(uri, new AbandonInput(message), ChangeInfo.class, null/*no error handler*/, monitor);
return getChangeDetail(id.getParentKey().get(), monitor);
}
}
@@ -491,7 +492,7 @@ public class GerritClient extends ReviewsClient {
});
} else {
final String uri = "/a/changes/" + id.getParentKey().get() + "/revisions/" + id.get() + "/review"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- execute(uri, new ReviewInput(message), ReviewInfo.class, null /*no error handler*/, monitor);
+ executePostRestRequest(uri, new ReviewInput(message), ReviewInfo.class, null /*no error handler*/, monitor);
}
}
@@ -684,7 +685,8 @@ public class GerritClient extends ReviewsClient {
} else {
final String uri = "/a/changes/" + id.getParentKey().get() + "/restore"; //$NON-NLS-1$ //$NON-NLS-2$
try {
- execute(uri, new RestoreInput(message), ChangeInfo.class, null/*no error handler*/, monitor);
+ executePostRestRequest(uri, new RestoreInput(message), ChangeInfo.class, null/*no error handler*/,
+ monitor);
} catch (GerritHttpException e) {
if (e.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
throw new GerritException("Not Found", e); //$NON-NLS-1$
@@ -725,7 +727,7 @@ public class GerritClient extends ReviewsClient {
private ChangeDetail submitRest(PatchSet.Id id, IProgressMonitor monitor) throws GerritException {
final String uri = "/a/changes/" + id.getParentKey().get() + "/revisions/" + id.get() + "/submit"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- execute(uri, new SubmitInput(true), SubmitInfo.class, new ErrorHandler() {
+ executePostRestRequest(uri, new SubmitInput(true), SubmitInfo.class, new ErrorHandler() {
@Override
public void handleError(HttpMethodBase method) throws GerritException {
if (method.getStatusCode() == HttpURLConnection.HTTP_FORBIDDEN
@@ -779,40 +781,14 @@ public class GerritClient extends ReviewsClient {
public List<GerritQueryResult> executeQueryRest(IProgressMonitor monitor, final String queryString)
throws GerritException {
- return execute(monitor, new Operation<List<GerritQueryResult>>() {
- @Override
- public void execute(IProgressMonitor monitor) throws GerritException {
- try {
- Request<List<GerritQueryResult>> request = new Request<List<GerritQueryResult>>() {
- @Override
- public HttpMethodBase createMethod() throws IOException {
- GetMethod method = new GetMethod(client.getUrl()
- + "/changes/?q=" + URLEncoder.encode(queryString, "UTF-8")); //$NON-NLS-1$ //$NON-NLS-2$
- method.setRequestHeader("Accept", "application/json"); //$NON-NLS-1$//$NON-NLS-2$
- return method;
- }
-
- @Override
- public List<GerritQueryResult> process(HttpMethodBase method) throws IOException {
- JSonSupport json = new JSonSupport();
- // Gerrit 2.5 prepends the output with bogus characters
- // see http://code.google.com/p/gerrit/issues/detail?id=1648
- String content = method.getResponseBodyAsString();
- if (content.startsWith(")]}'\n")) { //$NON-NLS-1$
- content = content.substring(5);
- }
- Type type = new TypeToken<List<GerritQueryResult>>() {
- }.getType();
- return json.getGson().fromJson(content, type);
- }
- };
- List<GerritQueryResult> result = client.execute(request, monitor);
- onSuccess(result);
- } catch (Exception e) {
- onFailure(e);
- }
- }
- });
+ try {
+ String uri = "/changes/?q=" + URLEncoder.encode(queryString, "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
+ TypeToken<List<GerritQueryResult>> queryResultListType = new TypeToken<List<GerritQueryResult>>() {
+ };
+ return executeGetRestRequest(uri, queryResultListType.getType(), monitor);
+ } catch (UnsupportedEncodingException e) {
+ throw new GerritException(e);
+ }
}
/**
@@ -937,8 +913,8 @@ public class GerritClient extends ReviewsClient {
}
}
- private <T> T execute(final String url, final Object input, final Type resultType, final ErrorHandler handler,
- IProgressMonitor monitor) throws GerritException {
+ private <T> T executePostRestRequest(final String url, final Object input, final Type resultType,
+ final ErrorHandler handler, IProgressMonitor monitor) throws GerritException {
return execute(monitor, new Operation<T>() {
@Override
public void execute(IProgressMonitor monitor) throws GerritException {
@@ -951,6 +927,20 @@ public class GerritClient extends ReviewsClient {
});
}
+ private <T> T executeGetRestRequest(final String url, final Type resultType, IProgressMonitor monitor)
+ throws GerritException {
+ return execute(monitor, new Operation<T>() {
+ @Override
+ public void execute(IProgressMonitor monitor) throws GerritException {
+ try {
+ setResult(client.<T> getRestRequest(url, resultType, monitor));
+ } catch (IOException e) {
+ throw new GerritException(e);
+ }
+ }
+ });
+ }
+
private <T> T executeOnce(IProgressMonitor monitor, Operation<T> operation) throws GerritException {
operation.execute(monitor);
if (operation.getException() instanceof GerritException) {
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 f645554..8577137 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
@@ -42,6 +42,7 @@ 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 org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.Request.HttpMethod;
import com.google.gerrit.common.auth.SignInMode;
import com.google.gerrit.common.auth.openid.DiscoveryResult;
@@ -60,6 +61,10 @@ public class GerritHttpClient {
public static abstract class Request<T> {
+ public enum HttpMethod {
+ POST, GET
+ }
+
public abstract HttpMethodBase createMethod() throws IOException;
public abstract T process(HttpMethodBase method) throws IOException;
@@ -106,6 +111,8 @@ public class GerritHttpClient {
private final JSonSupport json = new JSonSupport();
+ private final HttpMethod httpMethod;
+
private final String serviceUri;
private final Object input;
@@ -114,19 +121,32 @@ public class GerritHttpClient {
private final ErrorHandler errorHandler;
- public RestRequest(final String serviceUri, final Object input, Type resultType, ErrorHandler handler) {
+ public RestRequest(final HttpMethod httpMethod, final String serviceUri, final Object input, Type resultType,
+ ErrorHandler handler) {
+ this.httpMethod = httpMethod;
this.serviceUri = serviceUri;
this.input = input;
this.resultType = resultType;
this.errorHandler = handler;
}
+ @SuppressWarnings("null")
@Override
- public PostMethod createMethod() throws IOException {
- PostMethod method = new PostMethod(getUrl() + serviceUri);
+ public HttpMethodBase createMethod() throws IOException {
+ HttpMethodBase method = null;
+ if (httpMethod == HttpMethod.POST) {
+ method = createPostMethod();
+ } else if (httpMethod == HttpMethod.GET) {
+ method = new GetMethod(getUrl() + serviceUri);
+ }
method.setRequestHeader("Content-Type", "application/json; charset=utf-8"); //$NON-NLS-1$//$NON-NLS-2$
method.setRequestHeader("Accept", "application/json"); //$NON-NLS-1$//$NON-NLS-2$
+ return method;
+ }
+
+ private HttpMethodBase createPostMethod() throws IOException {
+ PostMethod method = new PostMethod(getUrl() + serviceUri);
String content = json.getGson().toJson(input);
RequestEntity requestEntity = new StringRequestEntity(content, "application/json", null); //$NON-NLS-1$
method.setRequestEntity(requestEntity);
@@ -215,11 +235,23 @@ public class GerritHttpClient {
public <T> T postRestRequest(final String serviceUri, final Object input, Type resultType, ErrorHandler handler,
IProgressMonitor monitor) throws IOException, GerritException {
- Assert.isNotNull(serviceUri, "REST Service URI must be not null."); //$NON-NLS-1$
Assert.isNotNull(input, "Input object must be not null."); //$NON-NLS-1$
+
+ return restRequest(HttpMethod.POST, serviceUri, input, resultType, handler, monitor);
+ }
+
+ public <T> T getRestRequest(final String serviceUri, Type resultType, IProgressMonitor monitor) throws IOException,
+ GerritException {
+ return restRequest(HttpMethod.GET, serviceUri, null, resultType, null, monitor);
+ }
+
+ private <T> T restRequest(final HttpMethod httpMethod, final String serviceUri, final Object input,
+ Type resultType, ErrorHandler handler, IProgressMonitor monitor) throws IOException, GerritException {
+ Assert.isNotNull(httpMethod, "HTTP Method must be not null."); //$NON-NLS-1$
+ Assert.isNotNull(serviceUri, "REST Service URI must be not null."); //$NON-NLS-1$
Assert.isNotNull(resultType, "Output type must be not null."); //$NON-NLS-1$
- return execute(new RestRequest<T>(serviceUri, input, resultType, handler), monitor);
+ return execute(new RestRequest<T>(httpMethod, serviceUri, input, resultType, handler), monitor);
}
public <T> T execute(Request<T> request, IProgressMonitor monitor) throws IOException, GerritException {