diff options
author | Frank Becker | 2015-05-10 06:26:30 +0000 |
---|---|---|
committer | Frank Becker | 2015-06-12 03:14:02 +0000 |
commit | 286b3deb95fc0b32d2c7319ec1909c2a8fc1e385 (patch) | |
tree | ec6830cd3fa34867b207cd43e55e6e50a6d80fde | |
parent | 8440f0cb25a6b5a1072d692aa29c790e08b53041 (diff) | |
download | org.eclipse.mylyn.tasks-286b3deb95fc0b32d2c7319ec1909c2a8fc1e385.tar.gz org.eclipse.mylyn.tasks-286b3deb95fc0b32d2c7319ec1909c2a8fc1e385.tar.xz org.eclipse.mylyn.tasks-286b3deb95fc0b32d2c7319ec1909c2a8fc1e385.zip |
22722: 414360: add create Tasks for the REST API [I9c54ef32]
Change-Id: I9c54ef328d85febebae9a9f299494f8385ad77cc
Task-Url: https://git.eclipse.org/r/#/c/22722/
10 files changed, 551 insertions, 12 deletions
diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestClientTest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestClientTest.java index 051423f66..3116ca755 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestClientTest.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/src/org/eclipse/mylyn/internal/bugzilla/rest/core/tests/BugzillaRestClientTest.java @@ -15,6 +15,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.IOException; import java.util.ArrayList; @@ -33,6 +34,7 @@ import org.eclipse.mylyn.commons.sdk.util.Junit4TestFixtureRunner.FixtureDefinit import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestClient; import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestConfiguration; import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestConnector; +import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestCreateTaskSchema; import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestException; import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestVersion; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Field; @@ -42,11 +44,16 @@ import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Product; import org.eclipse.mylyn.internal.bugzilla.rest.test.support.BugzillaRestTestFixture; import org.eclipse.mylyn.internal.commons.core.operations.NullOperationMonitor; import org.eclipse.mylyn.internal.commons.repositories.core.InMemoryCredentialsStore; +import org.eclipse.mylyn.tasks.core.RepositoryResponse; +import org.eclipse.mylyn.tasks.core.RepositoryResponse; +import org.eclipse.mylyn.tasks.core.RepositoryResponse.ResponseKind; +import org.eclipse.mylyn.tasks.core.TaskMapping; import org.eclipse.mylyn.tasks.core.TaskMapping; import org.eclipse.mylyn.tasks.core.TaskRepository; import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler; import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper; import org.eclipse.mylyn.tasks.core.data.TaskData; +import org.eclipse.mylyn.tasks.core.data.TaskMapper; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -87,7 +94,7 @@ public class BugzillaRestClientTest { @Test public void testGetVersion() throws Exception { - BugzillaRestClient client = new BugzillaRestClient(actualFixture.location()); + BugzillaRestClient client = new BugzillaRestClient(actualFixture.location(), connector); assertNotNull(client.getClient()); assertNull(client.getClient().getLoginToken()); BugzillaRestVersion version = client.getVersion(new NullOperationMonitor()); @@ -97,7 +104,7 @@ public class BugzillaRestClientTest { @Test public void testValidate() throws Exception { - BugzillaRestClient client = new BugzillaRestClient(actualFixture.location()); + BugzillaRestClient client = new BugzillaRestClient(actualFixture.location(), connector); assertNotNull(client.getClient()); assertNull(client.getClient().getLoginToken()); assertTrue(client.validate(new NullOperationMonitor())); @@ -119,7 +126,7 @@ public class BugzillaRestClientTest { thrown.expect(BugzillaRestException.class); thrown.expectMessage("Authentication failed"); BugzillaRestClient client; - client = new BugzillaRestClient(location); + client = new BugzillaRestClient(location, connector); assertNotNull(client.getClient()); assertNull(client.getClient().getLoginToken()); client.validate(new NullOperationMonitor()); @@ -135,7 +142,7 @@ public class BugzillaRestClientTest { thrown.expect(IllegalStateException.class); thrown.expectMessage("Authentication requested without valid credentials"); BugzillaRestClient client; - client = new BugzillaRestClient(location); + client = new BugzillaRestClient(location, connector); assertNotNull(client.getClient()); assertNull(client.getClient().getLoginToken()); client.validate(new NullOperationMonitor()); @@ -223,4 +230,166 @@ public class BugzillaRestClientTest { taskData.getRoot().toString()); } + @Test + public void testPostTaskDataWithoutProduct() throws Exception { + final TaskMapping taskMappingInit = new TaskMapping() { + @Override + public String getSummary() { + return "The Summary"; + } + + @Override + public String getDescription() { + return "The Description"; + } + + }; + AbstractTaskDataHandler taskDataHandler = connector.getTaskDataHandler(); + TaskAttributeMapper mapper = taskDataHandler.getAttributeMapper(actualFixture.repository()); + TaskData taskData = new TaskData(mapper, actualFixture.repository().getConnectorKind(), + actualFixture.repository().getRepositoryUrl(), ""); + taskDataHandler.initializeTaskData(actualFixture.repository(), taskData, taskMappingInit, null); + taskData.getRoot().getAttribute("cf_dropdown").setValue("one"); + try { + connector.getClient(actualFixture.repository()).postTaskData(taskData, null); + fail("BugzillaRestException expected, never reach this!"); + } catch (BugzillaRestException e) { + String url = actualFixture.getRepositoryUrl(); + assertEquals("You must select/enter a product. (status: Bad Request from " + + url.substring(url.lastIndexOf('/')) + "/rest.cgi/bug)", e.getMessage()); + } + } + + @Test + public void testPostTaskDataWithoutMilestone() throws Exception { + final TaskMapping taskMappingInit = new TaskMapping() { + @Override + public String getSummary() { + return "The Summary"; + } + + @Override + public String getDescription() { + return "The Description"; + } + + @Override + public String getProduct() { + return "ManualTest"; + } + + @Override + public String getComponent() { + return "ManualC1"; + } + + @Override + public String getVersion() { + return "R1"; + } + }; + AbstractTaskDataHandler taskDataHandler = connector.getTaskDataHandler(); + TaskAttributeMapper mapper = taskDataHandler.getAttributeMapper(actualFixture.repository()); + TaskData taskData = new TaskData(mapper, actualFixture.repository().getConnectorKind(), + actualFixture.repository().getRepositoryUrl(), ""); + taskDataHandler.initializeTaskData(actualFixture.repository(), taskData, taskMappingInit, null); + taskData.getRoot().getAttribute("cf_dropdown").setValue("one"); + try { + connector.getClient(actualFixture.repository()).postTaskData(taskData, null); + fail("never reach this!"); + } catch (BugzillaRestException e) { + String url = actualFixture.getRepositoryUrl(); + assertEquals("You must select/enter a milestone. (status: Bad Request from " + + url.substring(url.lastIndexOf('/')) + "/rest.cgi/bug)", e.getMessage()); + } + } + + @Test + public void testPostTaskData() throws Exception { + final TaskMapping taskMappingInit = new TaskMapping() { + @Override + public String getSummary() { + return "The Summary"; + } + + @Override + public String getDescription() { + return "The Description"; + } + + @Override + public String getProduct() { + return "ManualTest"; + } + + @Override + public String getComponent() { + return "ManualC1"; + } + + @Override + public String getVersion() { + return "R1"; + } + }; + AbstractTaskDataHandler taskDataHandler = connector.getTaskDataHandler(); + TaskAttributeMapper mapper = taskDataHandler.getAttributeMapper(actualFixture.repository()); + TaskData taskData = new TaskData(mapper, actualFixture.repository().getConnectorKind(), + actualFixture.repository().getRepositoryUrl(), ""); + taskDataHandler.initializeTaskData(actualFixture.repository(), taskData, taskMappingInit, null); + taskData.getRoot().getAttribute("cf_dropdown").setValue("one"); + taskData.getRoot() + .getAttribute(BugzillaRestCreateTaskSchema.getDefault().TARGET_MILESTONE.getKey()) + .setValue("M2"); + RepositoryResponse xx = connector.getClient(actualFixture.repository()).postTaskData(taskData, null); + assertEquals(ResponseKind.TASK_CREATED, xx.getReposonseKind()); + } + + @Test + public void testPostTaskDataFromTaskdata() throws Exception { + final TaskMapping taskMappingInit = new TaskMapping() { + @Override + public String getSummary() { + return "The Summary"; + } + + @Override + public String getDescription() { + return "The Description"; + } + + @Override + public String getProduct() { + return "ManualTest"; + } + + @Override + public String getComponent() { + return "ManualC1"; + } + + @Override + public String getVersion() { + return "R1"; + } + }; + AbstractTaskDataHandler taskDataHandler = connector.getTaskDataHandler(); + TaskAttributeMapper mapper = taskDataHandler.getAttributeMapper(actualFixture.repository()); + TaskData taskData = new TaskData(mapper, actualFixture.repository().getConnectorKind(), + actualFixture.repository().getRepositoryUrl(), ""); + taskDataHandler.initializeTaskData(actualFixture.repository(), taskData, taskMappingInit, null); + taskData.getRoot().getAttribute("cf_dropdown").setValue("one"); + taskData.getRoot() + .getAttribute(BugzillaRestCreateTaskSchema.getDefault().TARGET_MILESTONE.getKey()) + .setValue("M2"); + + TaskData taskDataSubmit = new TaskData(mapper, actualFixture.repository().getConnectorKind(), + actualFixture.repository().getRepositoryUrl(), ""); + taskDataHandler.initializeTaskData(actualFixture.repository(), taskDataSubmit, null, null); + TaskMapper mapper1 = new TaskMapper(taskData); + connector.getTaskMapping(taskDataSubmit).merge(mapper1); + + RepositoryResponse xx = connector.getClient(actualFixture.repository()).postTaskData(taskDataSubmit, null); + assertEquals(ResponseKind.TASK_CREATED, xx.getReposonseKind()); + } } 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 new file mode 100644 index 000000000..5f6cf45da --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAuthenticatedPostRequest.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * 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 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; +import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.LoginToken; + +public abstract class BugzillaRestAuthenticatedPostRequest<T> extends BugzillaRestRequest<T> { + + public BugzillaRestAuthenticatedPostRequest(BugzillaRestHttpClient client) { + super(client); + } + + @Override + protected HttpRequestBase createHttpRequestBase() throws IOException { + String bugUrl = getUrlSuffix(); + LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken(); + if (token != null && bugUrl.length() > 0) { + if (bugUrl.endsWith("?")) { //$NON-NLS-1$ + bugUrl += ("token=" + token.getToken()); //$NON-NLS-1$ + } else { + bugUrl += ("&token=" + token.getToken()); //$NON-NLS-1$ + } + } + + HttpPost request = new HttpPost(baseUrl() + bugUrl); + request.setHeader(CONTENT_TYPE, APPLICATION_JSON); + request.setHeader(ACCEPT, APPLICATION_JSON); + return request; + } + + @Override + protected T execute(IOperationMonitor monitor) throws IOException, BugzillaRestException { + if (needsAuthentication() && ((BugzillaRestHttpClient) getClient()).getLoginToken() == null) { + authenticate(monitor); + } + HttpRequestBase request = createHttpRequestBase(); + 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/BugzillaRestClient.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestClient.java index d118e5020..617a758ca 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 @@ -18,6 +18,7 @@ import org.eclipse.core.runtime.Status; 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.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; @@ -27,7 +28,10 @@ import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.Product; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.ProductResponse; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.RestResponse; import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.VersionResponse; +import org.eclipse.mylyn.tasks.core.RepositoryResponse; +import org.eclipse.mylyn.tasks.core.RepositoryResponse.ResponseKind; import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.data.TaskData; import com.google.common.base.Function; import com.google.common.collect.Lists; @@ -38,8 +42,13 @@ public class BugzillaRestClient { private final BugzillaRestHttpClient client; - public BugzillaRestClient(RepositoryLocation location) { + private final BugzillaRestConnector connector; + + public static final int MAX_RETRIEVED_PER_QUERY = 50; + + public BugzillaRestClient(RepositoryLocation location, BugzillaRestConnector connector) { client = new BugzillaRestHttpClient(location); + this.connector = connector; } public BugzillaRestHttpClient getClient() { @@ -101,4 +110,13 @@ public class BugzillaRestClient { }).run(monitor); } + public RepositoryResponse postTaskData(TaskData taskData, IOperationMonitor monitor) throws BugzillaRestException { + if (taskData.isNew()) { + BugzillaRestIdResult result = new BugzillaRestPostNewTask(client, taskData).run(monitor); + return new RepositoryResponse(ResponseKind.TASK_CREATED, result.getId()); + } else { + throw new UnsupportedOperationException(); + } + } + } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestConnector.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestConnector.java index 7fb1e1e2b..602fb58b6 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestConnector.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestConnector.java @@ -35,6 +35,7 @@ import org.eclipse.mylyn.tasks.core.RepositoryInfo; import org.eclipse.mylyn.tasks.core.RepositoryVersion; import org.eclipse.mylyn.tasks.core.TaskRepository; import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler; +import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper; import org.eclipse.mylyn.tasks.core.data.TaskData; import org.eclipse.mylyn.tasks.core.data.TaskDataCollector; import org.eclipse.mylyn.tasks.core.sync.ISynchronizationSession; @@ -195,8 +196,11 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { @Override public TaskData getTaskData(TaskRepository repository, String taskIdOrKey, IProgressMonitor monitor) throws CoreException { - // ignore - return null; + // only to not generate NPE in SubmitTaskJob.createTask() + // we replace this with the next review + AbstractTaskDataHandler taskDataHandler = getTaskDataHandler(); + TaskAttributeMapper mapper = taskDataHandler.getAttributeMapper(repository); + return new TaskData(mapper, repository.getConnectorKind(), repository.getRepositoryUrl(), taskIdOrKey); } @Override @@ -248,7 +252,7 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { UserCredentials credentials = new UserCredentials(credentials1.getUserName(), credentials1.getPassword(), null, true); location.setCredentials(AuthenticationType.REPOSITORY, credentials); - BugzillaRestClient client = new BugzillaRestClient(location); + BugzillaRestClient client = new BugzillaRestClient(location, this); return client; } diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestCreateTaskSchema.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestCreateTaskSchema.java index 2e83dda91..b12c00e0d 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestCreateTaskSchema.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestCreateTaskSchema.java @@ -15,6 +15,8 @@ import org.eclipse.mylyn.tasks.core.data.AbstractTaskSchema; import org.eclipse.mylyn.tasks.core.data.DefaultTaskSchema; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; +import com.google.common.collect.ImmutableMap; + public class BugzillaRestCreateTaskSchema extends AbstractTaskSchema { private static final BugzillaRestCreateTaskSchema instance = new BugzillaRestCreateTaskSchema(); @@ -23,6 +25,28 @@ public class BugzillaRestCreateTaskSchema extends AbstractTaskSchema { return instance; } + private static ImmutableMap<String, String> attribute2FieldMapper = new ImmutableMap.Builder() + .put(getDefault().SUMMARY.getKey(), "summary") + .put(getDefault().DESCRIPTION.getKey(), "description") + .put(getDefault().OPERATION.getKey(), "status") + .put(getDefault().PRODUCT.getKey(), "product") + .put(getDefault().COMPONENT.getKey(), "component") + .put(getDefault().CC.getKey(), "cc") + .put(getDefault().SEVERITY.getKey(), "severity") + .put(getDefault().PRIORITY.getKey(), "priority") + .put(getDefault().ASSIGNED_TO.getKey(), "assigned_to") + .put(getDefault().OS.getKey(), "op_sys") + .put(getDefault().VERSION.getKey(), "version") + .build(); + + public static String getFieldNameFromAttributeName(String attributeName) { + String result = attribute2FieldMapper.get(attributeName); + if (result == null) { + result = attributeName; + } + return result; + } + private final DefaultTaskSchema parent = DefaultTaskSchema.getInstance(); public final Field PRODUCT = inheritFrom(parent.PRODUCT).addFlags(Flag.REQUIRED).create(); diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGsonUtil.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGsonUtil.java new file mode 100644 index 000000000..9d1b8f57b --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGsonUtil.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * 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; + +public class BugzillaRestGsonUtil { + public static String convertString2GSonString(String str) { + str = str.replace("\"", "\\\"").replace("\n", "\\\n"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$//$NON-NLS-4$ + StringBuffer ostr = new StringBuffer(); + for (int i = 0; i < str.length(); i++) { + char ch = str.charAt(i); + if ((ch >= 0x0020) && (ch <= 0x007e)) { + ostr.append(ch); + } else { + ostr.append("\\u"); //$NON-NLS-1$ + String hex = Integer.toHexString(str.charAt(i) & 0xFFFF); + for (int j = 0; j < 4 - hex.length(); j++) { + ostr.append("0"); //$NON-NLS-1$ + } + ostr.append(hex.toLowerCase()); + } + } + return (new String(ostr)); + } + +}
\ 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/BugzillaRestPostNewTask.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewTask.java new file mode 100644 index 000000000..f8b6766b9 --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestPostNewTask.java @@ -0,0 +1,193 @@ +/******************************************************************************* + * 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.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.util.List; + +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; +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.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; + +import com.google.common.collect.ImmutableList; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.reflect.TypeToken; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +public class BugzillaRestPostNewTask extends BugzillaRestAuthenticatedPostRequest<BugzillaRestIdResult> { + TaskData taskData; + + public BugzillaRestPostNewTask(BugzillaRestHttpClient client, TaskData taskData) { + super(client); + this.taskData = taskData; + } + + @Override + protected String getUrlSuffix() { + return "/bug"; //$NON-NLS-1$ + } + + List<NameValuePair> requestParameters; + + class TaskAttributeTypeAdapter extends TypeAdapter<TaskData> { + LoginToken token; + + public TaskAttributeTypeAdapter(LoginToken token) { + super(); + this.token = token; + } + + @Override + public void write(JsonWriter out, TaskData value) throws IOException { + out.beginObject(); + out.name("Bugzilla_token").value(token.getToken()); //$NON-NLS-1$ + for (Object element : taskData.getRoot().getAttributes().values()) { + TaskAttribute taskAttribute = (TaskAttribute) element; + String id = taskAttribute.getId(); + String value1 = BugzillaRestGsonUtil.convertString2GSonString(taskAttribute.getValue()); + if (legalCreateAttributes.contains(id) || id.startsWith("cf_")) { //$NON-NLS-1$ + id = BugzillaRestCreateTaskSchema.getFieldNameFromAttributeName(id); + if (id.equals("status")) { //$NON-NLS-1$ + if (value != null && value.equals(TaskAttribute.PREFIX_OPERATION + "default")) { //$NON-NLS-1$ + continue; + } + } + if (taskAttribute.getMetaData().getType() != null + && taskAttribute.getMetaData().getType().equals(TaskAttribute.TYPE_MULTI_SELECT)) { + List<String> values = taskAttribute.getValues(); + int ii = 0; + value1 = ""; //$NON-NLS-1$ + for (String string : values) { + string = BugzillaRestGsonUtil.convertString2GSonString(string); + value1 += ((ii++ == 0 ? "" : ",") + string); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + out.name(id).value(value1); + if (id.equals("description")) { //$NON-NLS-1$ + TaskAttribute descriptionpri = taskAttribute + .getAttribute(BugzillaRestCreateTaskSchema.getDefault().COMMENT_IS_PRIVATE.getKey()); + Boolean descriptionprivalue = (descriptionpri != null) + ? (descriptionpri.getValue().equals("1")) //$NON-NLS-1$ + : false; + out.name("comment_is_private").value(Boolean.toString(descriptionprivalue)); //$NON-NLS-1$ + } + } + } + out.endObject(); + } + + @Override + public TaskData read(JsonReader in) throws IOException { + // TODO Auto-generated method stub + return null; + } + + } + + @Override + protected HttpRequestBase createHttpRequestBase() throws IOException { + String bugUrl = getUrlSuffix(); + LoginToken token = ((BugzillaRestHttpClient) getClient()).getLoginToken(); + + HttpPost request = new HttpPost(baseUrl() + bugUrl); + request.setHeader(CONTENT_TYPE, APPLICATION_JSON); + request.setHeader(ACCEPT, APPLICATION_JSON); + try { + // set form parameters + Gson gson = new GsonBuilder().registerTypeAdapter(TaskData.class, new TaskAttributeTypeAdapter(token)) + .create(); + StringEntity requestEntity = new StringEntity(gson.toJson(taskData)); + request.setEntity(requestEntity); + } catch (UnsupportedEncodingException e) { + throw new IOException("could not build REST String", e); + } + return request; + } + + @Override + protected BugzillaRestIdResult parseFromJson(InputStreamReader in) { + TypeToken<BugzillaRestIdResult> type = new TypeToken<BugzillaRestIdResult>() { + }; + return new Gson().fromJson(in, type.getType()); + } + + protected BugzillaRestStatus parseErrorFromJson(InputStreamReader in) { + TypeToken<BugzillaRestStatus> type = new TypeToken<BugzillaRestStatus>() { + }; + return new Gson().fromJson(in, type.getType()); + } + + @Override + protected void doValidate(CommonHttpResponse response, IOperationMonitor monitor) + throws IOException, BugzillaRestException { + int statusCode = response.getStatusCode(); + if (statusCode != HttpURLConnection.HTTP_BAD_REQUEST && statusCode != HttpURLConnection.HTTP_OK) { + if (statusCode == HttpStatus.SC_NOT_FOUND) { + throw new BugzillaRestResourceNotFoundException( + NLS.bind("Requested resource ''{0}'' does not exist", response.getRequestPath())); + } + throw new BugzillaRestException(NLS.bind("Unexpected response from Bugzilla REST server for ''{0}'': {1}", + response.getRequestPath(), HttpUtil.getStatusText(statusCode))); + } + + } + + ImmutableList<String> legalCreateAttributes = new ImmutableList.Builder<String>() + .add(BugzillaRestCreateTaskSchema.getDefault().PRODUCT.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().COMPONENT.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().SUMMARY.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().VERSION.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().DESCRIPTION.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().OS.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().PLATFORM.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().PRIORITY.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().SEVERITY.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().ALIAS.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().ASSIGNED_TO.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().QA_CONTACT.getKey()) + .add(BugzillaRestCreateTaskSchema.getDefault().TARGET_MILESTONE.getKey()) + .add(TaskAttribute.OPERATION) + .build(); + + @Override + protected BugzillaRestIdResult doProcess(CommonHttpResponse response, IOperationMonitor monitor) + throws IOException, BugzillaRestException { + InputStream is = response.getResponseEntityAsStream(); + InputStreamReader in = new InputStreamReader(is); + switch (response.getStatusCode()) { + case HttpURLConnection.HTTP_OK: + return parseFromJson(in); + default: + BugzillaRestStatus status = parseErrorFromJson(in); + throw new BugzillaRestException( + NLS.bind("{2} (status: {1} from {0})", new String[] { response.getRequestPath(), + HttpUtil.getStatusText(response.getStatusCode()), status.getMessage() })); + } + } +}
\ 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/BugzillaRestRequest.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestRequest.java index 292474e3e..1755a7b85 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 @@ -45,7 +45,7 @@ public abstract class BugzillaRestRequest<T> extends CommonHttpOperation<T> { protected abstract T parseFromJson(InputStreamReader in) throws BugzillaRestException; - protected abstract HttpRequestBase createHttpRequestBase(); + protected abstract HttpRequestBase createHttpRequestBase() throws IOException; protected String baseUrl() { String url = getClient().getLocation().getUrl(); @@ -56,7 +56,7 @@ public abstract class BugzillaRestRequest<T> extends CommonHttpOperation<T> { } protected String getUrlSuffix() { - return ""; + return ""; //$NON-NLS-1$ } public T run(IOperationMonitor monitor) throws BugzillaRestException { diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestTaskDataHandler.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestTaskDataHandler.java index 99769e47d..963166342 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestTaskDataHandler.java +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestTaskDataHandler.java @@ -15,6 +15,11 @@ import java.util.Set; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; +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.core.operations.OperationUtil; +import org.eclipse.mylyn.commons.net.Policy; import org.eclipse.mylyn.tasks.core.ITaskMapping; import org.eclipse.mylyn.tasks.core.RepositoryResponse; import org.eclipse.mylyn.tasks.core.TaskRepository; @@ -33,8 +38,20 @@ public class BugzillaRestTaskDataHandler extends AbstractTaskDataHandler { @Override public RepositoryResponse postTaskData(TaskRepository repository, TaskData taskData, Set<TaskAttribute> oldAttributes, IProgressMonitor monitor) throws CoreException { - // ignore - return null; + monitor = Policy.monitorFor(monitor); + try { + monitor.beginTask("Submitting_task", IProgressMonitor.UNKNOWN); + BugzillaRestClient client = connector.getClient(repository); + try { + IOperationMonitor progress = OperationUtil.convert(monitor, "post taskdata", 3); + return client.postTaskData(taskData, progress); + } catch (BugzillaRestException e) { + throw new CoreException(new Status(IStatus.ERROR, BugzillaRestCore.ID_PLUGIN, 2, + "Error post taskdata.\n\n" + e.getMessage(), e)); + } + } finally { + monitor.done(); + } } @Override diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/response/data/BugzillaRestIdResult.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/response/data/BugzillaRestIdResult.java new file mode 100644 index 000000000..86d6a24bd --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/response/data/BugzillaRestIdResult.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * 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.response.data; + +public class BugzillaRestIdResult { + private String id; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } +}
\ No newline at end of file |