diff options
author | Frank Becker | 2016-09-18 06:52:33 +0000 |
---|---|---|
committer | Frank Becker | 2016-09-25 10:04:35 +0000 |
commit | 79259588b16e50c54ad7307451c21604c352774e (patch) | |
tree | ac29775236d2ba766e365aaf31686f3abbc10e6e /connector-bugzilla-rest | |
parent | 384acbe8f6281410589c59861cbe366a839db62f (diff) | |
download | org.eclipse.mylyn.tasks-79259588b16e50c54ad7307451c21604c352774e.tar.gz org.eclipse.mylyn.tasks-79259588b16e50c54ad7307451c21604c352774e.tar.xz org.eclipse.mylyn.tasks-79259588b16e50c54ad7307451c21604c352774e.zip |
501376: refactor: Change class hierarchy for REST Bugzilla connector
Change-Id: I57cad3bf8fcf95c8fa501c97b0b150038765e5af
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=501376
Diffstat (limited to 'connector-bugzilla-rest')
21 files changed, 284 insertions, 459 deletions
diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/bugzilla/rest/core/tests/BugzillaRestClientTest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/bugzilla/rest/core/tests/BugzillaRestClientTest.java index bc05f4de3..0dab8d502 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/bugzilla/rest/core/tests/BugzillaRestClientTest.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/bugzilla/rest/core/tests/BugzillaRestClientTest.java @@ -54,7 +54,6 @@ import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestTaskAttachmentH import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestTaskSchema; import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestVersion; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Field; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Parameters; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Product; import org.eclipse.mylyn.internal.commons.core.operations.NullOperationMonitor; @@ -117,7 +116,6 @@ public class BugzillaRestClientTest { public void testGetVersion() throws Exception { BugzillaRestClient client = new BugzillaRestClient(actualFixture.location(), connector); assertNotNull(client.getClient()); - assertNull(client.getClient().getLoginToken()); BugzillaRestVersion version = client.getVersion(new NullOperationMonitor()); assertEquals("expeccted: " + actualFixture.getVersion() + " actual: " + version.toString(), actualFixture.getVersion(), version.toString()); @@ -127,14 +125,7 @@ public class BugzillaRestClientTest { public void testValidate() throws Exception { BugzillaRestClient client = new BugzillaRestClient(actualFixture.location(), connector); assertNotNull(client.getClient()); - assertNull(client.getClient().getLoginToken()); assertTrue(client.validate(new NullOperationMonitor())); - assertNotNull(client.getClient()); - LoginToken token = client.getClient().getLoginToken(); - assertNotNull(token); - assertEquals("2", token.getId()); - assertNotNull(token.getToken()); - assertTrue(token.getToken().length() > 0); } @Test @@ -145,11 +136,14 @@ public class BugzillaRestClientTest { location.setCredentialsStore(new InMemoryCredentialsStore()); location.setCredentials(AuthenticationType.REPOSITORY, new UserCredentials("wrong", "wrong")); thrown.expect(BugzillaRestException.class); - thrown.expectMessage("Authentication failed"); +// if (actualFixture.getVersion().compareTo("5.1") < 0) { + thrown.expectMessage("Unauthorized"); +// } else { +// thrown.expectMessage("Unexpected response from Bugzilla REST server"); +// } BugzillaRestClient client; client = new BugzillaRestClient(location, connector); assertNotNull(client.getClient()); - assertNull(client.getClient().getLoginToken()); client.validate(new NullOperationMonitor()); } @@ -164,8 +158,6 @@ public class BugzillaRestClientTest { thrown.expectMessage("Authentication requested without valid credentials"); BugzillaRestClient client; client = new BugzillaRestClient(location, connector); - assertNotNull(client.getClient()); - assertNull(client.getClient().getLoginToken()); client.validate(new NullOperationMonitor()); } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedGetRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedGetRequest.java deleted file mode 100644 index 34bbcd0d8..000000000 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedGetRequest.java +++ /dev/null @@ -1,107 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013 Frank Becker 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 - * - * Contributors: - * Frank Becker - initial API and implementation - *******************************************************************************/ - -package org.eclipse.mylyn.internal.bugzilla.rest.core; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.text.MessageFormat; - -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpRequestBase; -import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; -import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationException; -import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationRequest; -import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType; -import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials; -import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse; -import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - -public class BugzillaRestAuthenticatedGetRequest<T> extends BugzillaRestRequest<T> { - - private final String urlSuffix; - - private final TypeToken<?> responseType; - - public BugzillaRestAuthenticatedGetRequest(BugzillaRestHttpClient client, String urlSuffix, - TypeToken<?> responseType) { - super(client); - this.urlSuffix = urlSuffix; - this.responseType = responseType; - } - - @Override - protected void authenticate(IOperationMonitor monitor) throws IOException { - UserCredentials credentials = getClient().getLocation().getCredentials(AuthenticationType.REPOSITORY); - if (credentials == null) { - throw new IllegalStateException("Authentication requested without valid credentials"); //$NON-NLS-1$ - } - HttpRequestBase request = new HttpGet(baseUrl() + MessageFormat.format("/login?login={0}&password={1}", //$NON-NLS-1$ - new Object[] { credentials.getUserName(), credentials.getPassword() })); - request.setHeader(CONTENT_TYPE, TEXT_XML_CHARSET_UTF_8); - request.setHeader(ACCEPT, APPLICATION_JSON); - HttpResponse response = getClient().execute(request, monitor); - try { - if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { - getClient().setAuthenticated(false); - throw new AuthenticationException("Authentication failed", //$NON-NLS-1$ - new AuthenticationRequest<AuthenticationType<UserCredentials>>(getClient().getLocation(), - AuthenticationType.REPOSITORY)); - } else { - TypeToken<LoginToken> type = new TypeToken<LoginToken>() { - }; - InputStream is = response.getEntity().getContent(); - InputStreamReader in = new InputStreamReader(is); - LoginToken loginToken = new Gson().fromJson(in, type.getType()); - ((BugzillaRestHttpClient) getClient()).setLoginToken(loginToken); - getClient().setAuthenticated(true); - } - } finally { - HttpUtil.release(request, response, monitor); - } - } - - @Override - protected HttpRequestBase createHttpRequestBase(String url) { - HttpRequestBase request = new HttpGet(url); - request.setHeader(CONTENT_TYPE, TEXT_XML_CHARSET_UTF_8); - return request; - } - - @Override - protected T execute(IOperationMonitor monitor) throws IOException, BugzillaRestException { - if (needsAuthentication()) { - authenticate(monitor); - } - HttpRequestBase request = createHttpRequestBase(); - addHttpRequestEntities(request); - CommonHttpResponse response = execute(request, monitor); - return processAndRelease(response, monitor); - } - - @Override - protected T parseFromJson(InputStreamReader in) throws BugzillaRestException { - return new Gson().fromJson(in, responseType.getType()); - } - - @Override - protected String getUrlSuffix() { - return urlSuffix; - } - -} diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedPostRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedPostRequest.java deleted file mode 100644 index a8f683a42..000000000 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedPostRequest.java +++ /dev/null @@ -1,58 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 Frank Becker 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 - * - * Contributors: - * Frank Becker - initial API and implementation - *******************************************************************************/ - -package org.eclipse.mylyn.internal.bugzilla.rest.core; - -import java.io.IOException; -import java.io.InputStreamReader; - -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpRequestBase; -import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; -import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse; - -public abstract class BugzillaRestAuthenticatedPostRequest<T> extends BugzillaRestRequest<T> { - - public BugzillaRestAuthenticatedPostRequest(BugzillaRestHttpClient client) { - super(client); - } - - @Override - protected HttpRequestBase createHttpRequestBase(String url) { - HttpPost request = new HttpPost(url); - request.setHeader(CONTENT_TYPE, APPLICATION_JSON); - return request; - } - - @Override - protected T parseFromJson(InputStreamReader in) throws BugzillaRestException { - // ignore - return null; - } - - @Override - protected String createHttpRequestURL() { - String bugUrl = getUrlSuffix(); - return baseUrl() + bugUrl; - } - - @Override - protected T execute(IOperationMonitor monitor) throws IOException, BugzillaRestException { - if (needsAuthentication() && ((BugzillaRestHttpClient) getClient()).getLoginToken() == null) { - authenticate(monitor); - } - HttpRequestBase request = createHttpRequestBase(); - addHttpRequestEntities(request); - CommonHttpResponse response = execute(request, monitor); - return processAndRelease(response, monitor); - } - -}
\ No newline at end of file diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedPutRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedPutRequest.java deleted file mode 100644 index 25a6b5f86..000000000 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedPutRequest.java +++ /dev/null @@ -1,91 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 Frank Becker 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 - * - * Contributors: - * Frank Becker - initial API and implementation - *******************************************************************************/ - -package org.eclipse.mylyn.internal.bugzilla.rest.core; - -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.text.MessageFormat; - -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.methods.HttpRequestBase; -import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; -import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationException; -import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationRequest; -import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType; -import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials; -import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse; -import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; - -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - -public abstract class BugzillaRestAuthenticatedPutRequest<T> extends BugzillaRestRequest<T> { - - public BugzillaRestAuthenticatedPutRequest(BugzillaRestHttpClient client) { - super(client); - } - - @Override - protected void authenticate(IOperationMonitor monitor) throws IOException { - UserCredentials credentials = getClient().getLocation().getCredentials(AuthenticationType.REPOSITORY); - if (credentials == null) { - throw new IllegalStateException("Authentication requested without valid credentials"); - } - HttpRequestBase request = new HttpGet(baseUrl() + MessageFormat.format("/login?login={0}&password={1}", - new Object[] { credentials.getUserName(), credentials.getPassword() })); - request.setHeader(CONTENT_TYPE, TEXT_XML_CHARSET_UTF_8); - request.setHeader(ACCEPT, APPLICATION_JSON); - HttpResponse response = getClient().execute(request, monitor); - try { - if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { - getClient().setAuthenticated(false); - throw new AuthenticationException("Authentication failed", - new AuthenticationRequest<AuthenticationType<UserCredentials>>(getClient().getLocation(), - AuthenticationType.REPOSITORY)); - } else { - TypeToken<LoginToken> type = new TypeToken<LoginToken>() { - }; - InputStream is = response.getEntity().getContent(); - InputStreamReader in = new InputStreamReader(is); - LoginToken loginToken = new Gson().fromJson(in, type.getType()); - ((BugzillaRestHttpClient) getClient()).setLoginToken(loginToken); - getClient().setAuthenticated(true); - } - } finally { - HttpUtil.release(request, response, monitor); - } - } - - @Override - protected T execute(IOperationMonitor monitor) throws IOException, BugzillaRestException { - if (needsAuthentication() && ((BugzillaRestHttpClient) getClient()).getLoginToken() == null) { - authenticate(monitor); - } - HttpRequestBase request = createHttpRequestBase(); - addHttpRequestEntities(request); - CommonHttpResponse response = execute(request, monitor); - return processAndRelease(response, monitor); - } - - @Override - protected HttpRequestBase createHttpRequestBase(String url) { - HttpPut request = new HttpPut(url); - request.setHeader(CONTENT_TYPE, APPLICATION_JSON); - return request; - } - -}
\ No newline at end of file diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java index 93378f285..052b473ab 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java @@ -26,10 +26,11 @@ import org.eclipse.jdt.annotation.Nullable; import org.eclipse.mylyn.commons.core.StatusHandler; import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.BugzillaRestIdResult; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.ErrorResponse; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Field; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.FieldResponse; +import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Named; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.ParameterResponse; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Product; @@ -49,6 +50,7 @@ import org.eclipse.osgi.util.NLS; import com.google.common.base.Function; import com.google.common.base.Joiner; +import com.google.common.base.Strings; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -56,18 +58,18 @@ import com.google.gson.reflect.TypeToken; public class BugzillaRestClient { - private final BugzillaRestHttpClient client; + private final CommonHttpClient client; private final BugzillaRestConnector connector; public static final int MAX_RETRIEVED_PER_QUERY = 50; public BugzillaRestClient(RepositoryLocation location, BugzillaRestConnector connector) { - client = new BugzillaRestHttpClient(location); + client = new CommonHttpClient(location); this.connector = connector; } - public BugzillaRestHttpClient getClient() { + public CommonHttpClient getClient() { return client; } @@ -80,8 +82,11 @@ public class BugzillaRestClient { } public boolean validate(IOperationMonitor monitor) throws BugzillaRestException { - ErrorResponse validateResponse = new BugzillaRestValidateRequest(client).run(monitor); - return validateResponse.isError() && validateResponse.getCode() == 32614; + LoginToken validateResponse = new BugzillaRestLoginRequest(client).run(monitor); + if (validateResponse != null && !Strings.isNullOrEmpty(validateResponse.getId())) { + return true; + } + return false; } public BugzillaRestConfiguration getConfiguration(TaskRepository repository, IOperationMonitor monitor) { @@ -101,7 +106,7 @@ public class BugzillaRestClient { public <R extends RestResponse<E>, E extends Named> Map<String, E> retrieveItems(IOperationMonitor monitor, String path, TypeToken<?> typeToken) throws BugzillaRestException { - R response = new BugzillaRestAuthenticatedGetRequest<R>(client, path, typeToken).run(monitor); + R response = new BugzillaRestGetRequest<R>(client, path, typeToken).run(monitor); E[] members = response.getArray(); return Maps.uniqueIndex(Lists.newArrayList(members), new Function<E, String>() { public String apply(E input) { @@ -121,7 +126,7 @@ public class BugzillaRestClient { } public ParameterResponse getParameters(IOperationMonitor monitor) throws BugzillaRestException { - return new BugzillaRestAuthenticatedGetRequest<ParameterResponse>(client, "/parameters?", //$NON-NLS-1$ + return new BugzillaRestGetRequest<ParameterResponse>(client, "/parameters?", //$NON-NLS-1$ new TypeToken<ParameterResponse>() { }).run(monitor); } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetRequest.java new file mode 100644 index 000000000..a8a9a3d1c --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetRequest.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2013 Frank Becker 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 + * + * Contributors: + * Frank Becker - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.bugzilla.rest.core; + +import java.io.InputStreamReader; + +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpRequestBase; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +public class BugzillaRestGetRequest<T> extends BugzillaRestRequest<T> { + + private final TypeToken<?> responseType; + + public BugzillaRestGetRequest(CommonHttpClient client, String urlSuffix, TypeToken<?> responseType) { + super(client, urlSuffix, true); + this.responseType = responseType; + } + + public BugzillaRestGetRequest(CommonHttpClient client, String urlSuffix, TypeToken<?> responseType, + boolean authenticationRequired) { + super(client, urlSuffix, authenticationRequired); + this.responseType = responseType; + } + + @Override + protected HttpRequestBase createHttpRequestBase(String url) { + HttpRequestBase request = new HttpGet(url); + request.setHeader(CONTENT_TYPE, TEXT_XML_CHARSET_UTF_8); + return request; + } + + @Override + protected T parseFromJson(InputStreamReader in) throws BugzillaRestException { + return new Gson().fromJson(in, responseType.getType()); + } +} diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachmentData.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachmentData.java index b188da598..d99d2faa7 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachmentData.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachmentData.java @@ -20,6 +20,7 @@ import org.apache.commons.codec.binary.Base64InputStream; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import com.google.gson.GsonBuilder; @@ -31,10 +32,10 @@ import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.reflect.TypeToken; -public class BugzillaRestGetTaskAttachmentData extends BugzillaRestAuthenticatedGetRequest<InputStream> { +public class BugzillaRestGetTaskAttachmentData extends BugzillaRestGetRequest<InputStream> { private final TaskAttribute taskAttribute; - public BugzillaRestGetTaskAttachmentData(BugzillaRestHttpClient client, TaskAttribute taskAttribute) { + public BugzillaRestGetTaskAttachmentData(CommonHttpClient client, TaskAttribute taskAttribute) { super(client, "/bug/attachment/" + taskAttribute.getValue() + "?include_fields=data", null); //$NON-NLS-1$ //$NON-NLS-2$ this.taskAttribute = taskAttribute; } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachments.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachments.java index 7782f92a9..7afeeacd7 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachments.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachments.java @@ -24,6 +24,7 @@ import java.util.TimeZone; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.tasks.core.IRepositoryPerson; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.core.data.TaskData; @@ -36,10 +37,10 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; -public class BugzillaRestGetTaskAttachments extends BugzillaRestAuthenticatedGetRequest<ArrayList<TaskAttribute>> { +public class BugzillaRestGetTaskAttachments extends BugzillaRestGetRequest<ArrayList<TaskAttribute>> { private final TaskData taskData; - public BugzillaRestGetTaskAttachments(BugzillaRestHttpClient client, TaskData taskData) { + public BugzillaRestGetTaskAttachments(CommonHttpClient client, TaskData taskData) { super(client, "/bug/" + taskData.getTaskId() + "/attachment?exclude_fields=data", null); //$NON-NLS-1$ //$NON-NLS-2$ this.taskData = taskData; } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskComments.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskComments.java index 6e37fdb2b..821ad5fee 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskComments.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskComments.java @@ -24,6 +24,7 @@ import java.util.TimeZone; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.tasks.core.IRepositoryPerson; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper; @@ -37,10 +38,10 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; -public class BugzillaRestGetTaskComments extends BugzillaRestAuthenticatedGetRequest<ArrayList<TaskAttribute>> { +public class BugzillaRestGetTaskComments extends BugzillaRestGetRequest<ArrayList<TaskAttribute>> { private final TaskData taskData; - public BugzillaRestGetTaskComments(BugzillaRestHttpClient client, TaskData taskData) { + public BugzillaRestGetTaskComments(CommonHttpClient client, TaskData taskData) { super(client, "/bug/" + taskData.getTaskId() + "/comment?", null); //$NON-NLS-1$ //$NON-NLS-2$ this.taskData = taskData; } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskData.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskData.java index c65a69cb7..e08d4774d 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskData.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskData.java @@ -29,6 +29,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse; import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil; import org.eclipse.mylyn.tasks.core.TaskRepository; @@ -46,27 +47,20 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; -public class BugzillaRestGetTaskData extends BugzillaRestAuthenticatedGetRequest<List<TaskData>> { - private final String urlSuffix; +public class BugzillaRestGetTaskData extends BugzillaRestGetRequest<List<TaskData>> { private final TaskRepository taskRepository; private final BugzillaRestConnector connector; - public BugzillaRestGetTaskData(BugzillaRestHttpClient client, BugzillaRestConnector connector, String urlSuffix, + public BugzillaRestGetTaskData(CommonHttpClient client, BugzillaRestConnector connector, String urlSuffix, TaskRepository taskRepository) { - super(client, "", null); //$NON-NLS-1$ - this.urlSuffix = urlSuffix; + super(client, "/bug?" + urlSuffix, null); //$NON-NLS-1$ this.taskRepository = taskRepository; this.connector = connector; } @Override - protected String getUrlSuffix() { - return "/bug?" + urlSuffix; //$NON-NLS-1$ - } - - @Override protected List<TaskData> parseFromJson(InputStreamReader in) throws BugzillaRestException { TypeToken<List<TaskData>> type = new TypeToken<List<TaskData>>() { }; diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestHttpClient.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestLoginRequest.java index 86bc5109b..9bccc319b 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestHttpClient.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestLoginRequest.java @@ -11,27 +11,15 @@ package org.eclipse.mylyn.internal.bugzilla.rest.core; -import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation; import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; -public class BugzillaRestHttpClient extends CommonHttpClient { - private LoginToken loginToken = null; +import com.google.gson.reflect.TypeToken; - public BugzillaRestHttpClient(RepositoryLocation location) { - super(location); - } - - public LoginToken getLoginToken() { - return loginToken; - } - - public void setLoginToken(LoginToken loginToken) { - this.loginToken = loginToken; - } +public class BugzillaRestLoginRequest extends BugzillaRestGetRequest<LoginToken> { - @Override - public boolean needsAuthentication() { - return ((loginToken == null) || super.needsAuthentication()); + public BugzillaRestLoginRequest(CommonHttpClient client) { + super(client, "/login?", new TypeToken<LoginToken>() { //$NON-NLS-1$ + }); } } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewAttachment.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewAttachment.java index 16c9f5c8e..1eaa1c7e4 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewAttachment.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewAttachment.java @@ -15,7 +15,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; +import java.io.StringWriter; import java.net.HttpURLConnection; import java.util.List; @@ -31,10 +31,10 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse; import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.BugzillaRestIdsResult; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentSource; import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; @@ -42,8 +42,9 @@ import org.eclipse.osgi.util.NLS; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonWriter; -public class BugzillaRestPostNewAttachment extends BugzillaRestAuthenticatedPostRequest<BugzillaRestIdsResult> { +public class BugzillaRestPostNewAttachment extends BugzillaRestPostRequest<BugzillaRestIdsResult> { private final String bugReportID; private final String comment; @@ -52,27 +53,21 @@ public class BugzillaRestPostNewAttachment extends BugzillaRestAuthenticatedPost private final TaskAttribute attachmentAttribute; - public BugzillaRestPostNewAttachment(BugzillaRestHttpClient client, String bugReportID, String comment, + public BugzillaRestPostNewAttachment(CommonHttpClient client, String bugReportID, String comment, AbstractTaskAttachmentSource source, TaskAttribute attachmentAttribute, IOperationMonitor monitor) { - super(client); + super(client, "/bug/" + bugReportID + "/attachment"); //$NON-NLS-1$ //$NON-NLS-2$ this.bugReportID = bugReportID; this.comment = comment; this.source = source; this.attachmentAttribute = attachmentAttribute; } - @Override - protected String getUrlSuffix() { - return "/bug/" + bugReportID + "/attachment"; //$NON-NLS-1$ - } - List<NameValuePair> requestParameters; @Override protected void addHttpRequestEntities(HttpRequestBase request) throws BugzillaRestException { super.addHttpRequestEntities(request); - LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken(); String description = source.getDescription(); String contentType = source.getContentType(); String filename = source.getName(); @@ -128,15 +123,25 @@ public class BugzillaRestPostNewAttachment extends BugzillaRestAuthenticatedPost Base64 base64 = new Base64(); String dataBase64 = base64.encodeAsString(outb.toByteArray()); try { - String gsonString = "{\"Bugzilla_token\":\"" + token.getToken() + "\", \"ids\" : [ " + bugReportID + " ]," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - + " \"is_patch\" : " + Boolean.toString(isPatch) + "," + " \"summary\" : \"" + description + "\"," //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$ - + " \"content_type\" : \"" + contentType + "\"," + " \"data\" : \"" + dataBase64 + "\"," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$ - + " \"file_name\" : \"" + filename + "\"," + " \"is_private\" : false}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - StringEntity requestEntity = new StringEntity(gsonString); + StringWriter stringWriter = new StringWriter(); + JsonWriter out = new JsonWriter(stringWriter); + out.setLenient(true); + out.beginObject(); + addAuthenticationToGson(out, getClient().getLocation()); + out.name("ids").beginArray().value(Integer.parseInt(bugReportID)).endArray(); //$NON-NLS-1$ + out.name("is_patch").value(isPatch); //$NON-NLS-1$ + out.name("summary").value(description); //$NON-NLS-1$ + out.name("content_type").value(contentType); //$NON-NLS-1$ + out.name("data").value(dataBase64); //$NON-NLS-1$ + out.name("file_name").value(filename); //$NON-NLS-1$ + out.name("is_private").value(false); //$NON-NLS-1$ + out.endObject(); + out.close(); + StringEntity requestEntity = new StringEntity(stringWriter.toString()); ((HttpPost) request).setEntity(requestEntity); - } catch (UnsupportedEncodingException e) { + } catch (IOException e) { throw new BugzillaRestException( - "BugzillaRestPostNewAttachment.createHttpRequestBase could not create StringEntity", e); + "BugzillaRestPostNewAttachment.createHttpRequestBase could not create RequestEntity", e); } } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewTask.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewTask.java index 1b46a3514..c18e43ff5 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewTask.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewTask.java @@ -26,10 +26,11 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.StringEntity; import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; +import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse; import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.BugzillaRestIdResult; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.core.data.TaskData; import org.eclipse.osgi.util.NLS; @@ -42,33 +43,28 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; -public class BugzillaRestPostNewTask extends BugzillaRestAuthenticatedPostRequest<BugzillaRestIdResult> { +public class BugzillaRestPostNewTask extends BugzillaRestPostRequest<BugzillaRestIdResult> { TaskData taskData; - public BugzillaRestPostNewTask(BugzillaRestHttpClient client, TaskData taskData) { - super(client); + public BugzillaRestPostNewTask(CommonHttpClient client, TaskData taskData) { + super(client, "/bug"); //$NON-NLS-1$ this.taskData = taskData; } - @Override - protected String getUrlSuffix() { - return "/bug"; //$NON-NLS-1$ - } - List<NameValuePair> requestParameters; class TaskAttributeTypeAdapter extends TypeAdapter<TaskData> { - LoginToken token; + RepositoryLocation location; - public TaskAttributeTypeAdapter(LoginToken token) { + public TaskAttributeTypeAdapter(RepositoryLocation location) { super(); - this.token = token; + this.location = location; } @Override public void write(JsonWriter out, TaskData value) throws IOException { out.beginObject(); - out.name("Bugzilla_token").value(token.getToken()); //$NON-NLS-1$ + addAuthenticationToGson(out, location); for (Object element : taskData.getRoot().getAttributes().values()) { TaskAttribute taskAttribute = (TaskAttribute) element; String id = taskAttribute.getId(); @@ -76,7 +72,7 @@ public class BugzillaRestPostNewTask extends BugzillaRestAuthenticatedPostReques if (legalCreateAttributes.contains(id) || id.startsWith("cf_")) { //$NON-NLS-1$ id = BugzillaRestCreateTaskSchema.getFieldNameFromAttributeName(id); if (id.equals("status") //$NON-NLS-1$ - && (value != null && value.equals(TaskAttribute.PREFIX_OPERATION + "default"))) { + && (value != null && value.equals(TaskAttribute.PREFIX_OPERATION + "default"))) { //$NON-NLS-1$ continue; } else if (id.equals("cc")) { //$NON-NLS-1$ HashSet<String> setNew = new HashSet<String>( @@ -134,10 +130,11 @@ public class BugzillaRestPostNewTask extends BugzillaRestAuthenticatedPostReques @Override protected void addHttpRequestEntities(HttpRequestBase request) throws BugzillaRestException { super.addHttpRequestEntities(request); - LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken(); + try { // set form parameters - Gson gson = new GsonBuilder().registerTypeAdapter(TaskData.class, new TaskAttributeTypeAdapter(token)) + Gson gson = new GsonBuilder() + .registerTypeAdapter(TaskData.class, new TaskAttributeTypeAdapter(getClient().getLocation())) .create(); StringEntity requestEntity = new StringEntity(gson.toJson(taskData)); ((HttpPost) request).setEntity(requestEntity); diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostRequest.java new file mode 100644 index 000000000..97b2a5e62 --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostRequest.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2015 Frank Becker 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 + * + * Contributors: + * Frank Becker - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.bugzilla.rest.core; + +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpRequestBase; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; + +public abstract class BugzillaRestPostRequest<T> extends BugzillaRestRequest<T> { + + public BugzillaRestPostRequest(CommonHttpClient client, String urlSuffix) { + super(client, urlSuffix, false); + } + + @Override + protected HttpRequestBase createHttpRequestBase(String url) { + HttpPost request = new HttpPost(url); + request.setHeader(CONTENT_TYPE, APPLICATION_JSON); + return request; + } + +}
\ No newline at end of file diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPutRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPutRequest.java new file mode 100644 index 000000000..4699cb91f --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPutRequest.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2015 Frank Becker 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 + * + * Contributors: + * Frank Becker - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.bugzilla.rest.core; + +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; + +public abstract class BugzillaRestPutRequest<T> extends BugzillaRestRequest<T> { + + public BugzillaRestPutRequest(CommonHttpClient client, String urlSuffix, boolean authenticationRequired) { + super(client, urlSuffix, authenticationRequired); + } + + @Override + protected HttpRequestBase createHttpRequestBase(String url) { + HttpPut request = new HttpPut(url); + request.setHeader(CONTENT_TYPE, APPLICATION_JSON); + return request; + } + +}
\ No newline at end of file diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPutUpdateTask.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPutUpdateTask.java index 8fce3c2df..9e394ae61 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPutUpdateTask.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPutUpdateTask.java @@ -26,13 +26,15 @@ import org.apache.http.entity.StringEntity; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; +import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.PutUpdateResult; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.core.data.TaskData; import com.google.common.base.Function; import com.google.common.base.Joiner; +import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.gson.Gson; @@ -42,7 +44,7 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; -public class BugzillaRestPutUpdateTask extends BugzillaRestAuthenticatedPutRequest<PutUpdateResult> { +public class BugzillaRestPutUpdateTask extends BugzillaRestPutRequest<PutUpdateResult> { private final TaskData taskData; class OldAttributes { @@ -84,11 +86,11 @@ public class BugzillaRestPutUpdateTask extends BugzillaRestAuthenticatedPutReque .build(); class TaskAttributeTypeAdapter extends TypeAdapter<OldAttributes> { - LoginToken token; + RepositoryLocation location; - public TaskAttributeTypeAdapter(LoginToken token) { + public TaskAttributeTypeAdapter(RepositoryLocation location) { super(); - this.token = token; + this.location = location; } private final Function<String, String> function = new Function<String, String>() { @@ -102,7 +104,7 @@ public class BugzillaRestPutUpdateTask extends BugzillaRestAuthenticatedPutReque @Override public void write(JsonWriter out, OldAttributes oldValues) throws IOException { out.beginObject(); - out.name("Bugzilla_token").value(token.getToken()); //$NON-NLS-1$ + addAuthenticationToGson(out, location); for (TaskAttribute element : oldValues.oldAttributes) { TaskAttribute taskAttribute = taskData.getRoot().getAttribute(element.getId()); String id = taskAttribute.getId(); @@ -204,37 +206,25 @@ public class BugzillaRestPutUpdateTask extends BugzillaRestAuthenticatedPutReque } - public BugzillaRestPutUpdateTask(BugzillaRestHttpClient client, TaskData taskData, - Set<TaskAttribute> oldAttributes) { - super(client); + public BugzillaRestPutUpdateTask(CommonHttpClient client, TaskData taskData, Set<TaskAttribute> oldAttributes) { + super(client, "/bug/" + taskData.getTaskId(), false); //$NON-NLS-1$ this.taskData = taskData; this.oldAttributes = new OldAttributes(oldAttributes); } - @Override - protected String getUrlSuffix() { - return "/bug/" + taskData.getTaskId(); //$NON-NLS-1$ - } - List<NameValuePair> requestParameters; @Override - protected String createHttpRequestURL() { - String bugUrl = getUrlSuffix(); - return baseUrl() + bugUrl; - } - - @Override protected void addHttpRequestEntities(HttpRequestBase request) throws BugzillaRestException { super.addHttpRequestEntities(request); - LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken(); try { - Gson gson = new GsonBuilder().registerTypeAdapter(OldAttributes.class, new TaskAttributeTypeAdapter(token)) + Gson gson = new GsonBuilder() + .registerTypeAdapter(OldAttributes.class, new TaskAttributeTypeAdapter(getClient().getLocation())) .create(); StringEntity requestEntity = new StringEntity(gson.toJson(oldAttributes)); ((HttpPut) request).setEntity(requestEntity); } catch (UnsupportedEncodingException e) { - com.google.common.base.Throwables.propagate(new CoreException( + Throwables.propagate(new CoreException( new Status(IStatus.ERROR, BugzillaRestCore.ID_PLUGIN, "Can not build HttpRequest", e))); //$NON-NLS-1$ } } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestRequest.java index 812216cfe..c6cab9814 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestRequest.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestRequest.java @@ -11,24 +11,30 @@ package org.eclipse.mylyn.internal.bugzilla.rest.core; +import static com.google.common.base.Preconditions.checkState; + import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.text.MessageFormat; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpRequestBase; import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; +import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation; import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationException; import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationRequest; import org.eclipse.mylyn.commons.repositories.core.auth.AuthenticationType; import org.eclipse.mylyn.commons.repositories.core.auth.UserCredentials; +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpOperation; import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse; import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; import org.eclipse.osgi.util.NLS; +import com.google.gson.stream.JsonWriter; + public abstract class BugzillaRestRequest<T> extends CommonHttpOperation<T> { protected static final String ACCEPT = "Accept"; //$NON-NLS-1$ @@ -38,11 +44,22 @@ public abstract class BugzillaRestRequest<T> extends CommonHttpOperation<T> { protected static final String TEXT_XML_CHARSET_UTF_8 = "text/xml; charset=UTF-8"; //$NON-NLS-1$ - public BugzillaRestRequest(BugzillaRestHttpClient client) { + private final boolean authenticationRequired; + + private final String urlSuffix; + + public BugzillaRestRequest(CommonHttpClient client, String urlSuffix, boolean authenticationRequired) { super(client); + this.authenticationRequired = authenticationRequired; + this.urlSuffix = urlSuffix; } - protected abstract T execute(IOperationMonitor monitor) throws IOException, BugzillaRestException; + protected T execute(IOperationMonitor monitor) throws IOException, BugzillaRestException { + HttpRequestBase request = createHttpRequestBase(); + addHttpRequestEntities(request); + CommonHttpResponse response = execute(request, monitor); + return processAndRelease(response, monitor); + } protected abstract T parseFromJson(InputStreamReader in) throws BugzillaRestException; @@ -62,19 +79,20 @@ public abstract class BugzillaRestRequest<T> extends CommonHttpOperation<T> { } protected String getUrlSuffix() { - return ""; //$NON-NLS-1$ + return urlSuffix; } protected String createHttpRequestURL() { - String bugUrl = getUrlSuffix(); - LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken(); - if (token != null && bugUrl.length() > 0) { - if (!bugUrl.endsWith("?")) { //$NON-NLS-1$ - bugUrl += "&"; //$NON-NLS-1$ + String urlSuffix = getUrlSuffix(); + if (urlSuffix.length() > 0 && authenticationRequired) { + if (!urlSuffix.endsWith("?")) { //$NON-NLS-1$ + urlSuffix += "&"; //$NON-NLS-1$ } - bugUrl += "token=" + token.getToken(); //$NON-NLS-1$ + UserCredentials credentials = getCredentials(); + urlSuffix += MessageFormat.format("Bugzilla_login={0}&Bugzilla_password={1}", //$NON-NLS-1$ + new Object[] { credentials.getUserName(), credentials.getPassword() }); } - return baseUrl() + bugUrl; + return baseUrl() + urlSuffix; } protected void addHttpRequestEntities(HttpRequestBase request) throws BugzillaRestException { @@ -136,4 +154,25 @@ public abstract class BugzillaRestRequest<T> extends CommonHttpOperation<T> { } } + @Override + protected boolean needsAuthentication() { + return false; + } + + protected void addAuthenticationToGson(JsonWriter out, RepositoryLocation location) { + UserCredentials credentials = getCredentials(); + + try { + out.name("Bugzilla_login").value(credentials.getUserName()); //$NON-NLS-1$ + out.name("Bugzilla_password").value(credentials.getPassword()); //$NON-NLS-1$ + } catch (IOException e) { + throw new BugzillaRestRuntimeException("Authentication requested with IOException", e); //$NON-NLS-1$ + } + } + + protected UserCredentials getCredentials() { + UserCredentials credentials = getClient().getLocation().getCredentials(AuthenticationType.REPOSITORY); + checkState(credentials != null, "Authentication requested without valid credentials"); + return credentials; + } } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestRuntimeException.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestRuntimeException.java new file mode 100644 index 000000000..23c6cec32 --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestRuntimeException.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2016 Frank Becker 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 + * + * Contributors: + * Frank Becker - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.bugzilla.rest.core; + +public class BugzillaRestRuntimeException extends RuntimeException { + + private static final long serialVersionUID = 5028038807086982720L; + + public BugzillaRestRuntimeException() { + } + + public BugzillaRestRuntimeException(String message) { + super(message); + } + + public BugzillaRestRuntimeException(Throwable cause) { + super(cause.getMessage(), cause); + } + + public BugzillaRestRuntimeException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestUnauthenticatedGetRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestUnauthenticatedGetRequest.java index d9dfb9389..0bc3471ff 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestUnauthenticatedGetRequest.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestUnauthenticatedGetRequest.java @@ -11,24 +11,14 @@ package org.eclipse.mylyn.internal.bugzilla.rest.core; -import com.google.gson.reflect.TypeToken; - -public class BugzillaRestUnauthenticatedGetRequest<T> extends BugzillaRestAuthenticatedGetRequest<T> { +import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient; - public BugzillaRestUnauthenticatedGetRequest(BugzillaRestHttpClient client, String urlSuffix, - TypeToken<?> responseType) { - super(client, urlSuffix, responseType); - } +import com.google.gson.reflect.TypeToken; - @Override - protected boolean needsAuthentication() { - return false; - } +public class BugzillaRestUnauthenticatedGetRequest<T> extends BugzillaRestGetRequest<T> { - @Override - protected String createHttpRequestURL() { - String bugUrl = getUrlSuffix(); - return baseUrl() + bugUrl; + public BugzillaRestUnauthenticatedGetRequest(CommonHttpClient client, String urlSuffix, TypeToken<?> responseType) { + super(client, urlSuffix, responseType, false); } }
\ No newline at end of file diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestValidateRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestValidateRequest.java deleted file mode 100644 index de2fd3346..000000000 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestValidateRequest.java +++ /dev/null @@ -1,65 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2013 Frank Becker 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 - * - * Contributors: - * Frank Becker - initial API and implementation - *******************************************************************************/ - -package org.eclipse.mylyn.internal.bugzilla.rest.core; - -import java.io.IOException; -import java.io.InputStreamReader; - -import org.apache.http.HttpStatus; -import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; -import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse; -import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.ErrorResponse; - -import com.google.common.io.CharStreams; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - -public class BugzillaRestValidateRequest extends BugzillaRestAuthenticatedGetRequest<ErrorResponse> { - - public BugzillaRestValidateRequest(BugzillaRestHttpClient client) { - super(client, "", new TypeToken<ErrorResponse>() { - }); - } - - @Override - protected void doValidate(CommonHttpResponse response, IOperationMonitor monitor) - throws IOException, BugzillaRestException { - // since 4.5.5 we get an HttpStatus.SC_NOT_FOUND instead of an HttpStatus.SC_BAD_REQUEST - validate(response, response.getStatusCode() == HttpStatus.SC_NOT_FOUND - ? HttpStatus.SC_NOT_FOUND - : HttpStatus.SC_BAD_REQUEST, monitor); - } - - @Override - protected ErrorResponse parseFromJson(InputStreamReader in) throws BugzillaRestException { - String jsonString; - try { - jsonString = CharStreams.toString(in); - } catch (IOException e) { - throw new BugzillaRestException(e); - } - if (jsonString.startsWith("{\"result\":{")) { //$NON-NLS-1$ - jsonString = jsonString.substring(10, jsonString.length() - 1); - } - - TypeToken<ErrorResponse> a = new TypeToken<ErrorResponse>() { - }; - return new Gson().fromJson(jsonString, a.getType()); - } - - @Override - protected String createHttpRequestURL() { - String bugUrl = getUrlSuffix(); - return baseUrl() + bugUrl; - } - -} diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.tests/src/org/eclipse/mylyn/bugzilla/rest/tests/tck/ValidationTest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.tests/src/org/eclipse/mylyn/bugzilla/rest/tests/tck/ValidationTest.java index 961946dd5..af20fd482 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.tests/src/org/eclipse/mylyn/bugzilla/rest/tests/tck/ValidationTest.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.tests/src/org/eclipse/mylyn/bugzilla/rest/tests/tck/ValidationTest.java @@ -37,7 +37,7 @@ public class ValidationTest extends AbstractTckTest { @Test public void testInvalidateCredentials() throws CoreException { thrown.expect(CoreException.class); - thrown.expectMessage("Authentication failed"); + thrown.expectMessage("Unauthorized"); TaskRepository repository = fixture().createRepository(); org.eclipse.mylyn.commons.net.AuthenticationCredentials invalideCreds = new org.eclipse.mylyn.commons.net.AuthenticationCredentials( "invalidateCredentials", "invalidateCredentials"); |