diff options
author | Frank Becker | 2016-01-16 09:04:39 +0000 |
---|---|---|
committer | Frank Becker | 2016-01-19 21:16:11 +0000 |
commit | 85b14d73a4da164a9bd9bf6cf65c692c9d395fe1 (patch) | |
tree | 0f67c342e49efdaf5125fdc0fe59583b3b1270a6 | |
parent | b8c3dc881367e5895382a0c6a70ae462f357cf4f (diff) | |
download | org.eclipse.mylyn.tasks-85b14d73a4da164a9bd9bf6cf65c692c9d395fe1.tar.gz org.eclipse.mylyn.tasks-85b14d73a4da164a9bd9bf6cf65c692c9d395fe1.tar.xz org.eclipse.mylyn.tasks-85b14d73a4da164a9bd9bf6cf65c692c9d395fe1.zip |
414360: add getAttachments for the REST API
Change-Id: I97c3f6ba47718609638c26e86029564f59d03f69
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=414360
7 files changed, 456 insertions, 16 deletions
diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/META-INF/MANIFEST.MF b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/META-INF/MANIFEST.MF index 4ac07355c..7a4e6150d 100644 --- a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/META-INF/MANIFEST.MF +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core.tests/META-INF/MANIFEST.MF @@ -10,7 +10,7 @@ Require-Bundle: org.junit;bundle-version="4.8.2", org.hamcrest;bundle-version="[1.0.0,1.2.0)", org.hamcrest.library;bundle-version="[1.0.0,1.2.0)", org.eclipse.mylyn.commons.sdk.util, - com.google.guava;bundle-version="10.0.1", + com.google.guava;bundle-version="15.0.0", org.apache.commons.io Export-Package: org.eclipse.mylyn.internal.bugzilla.rest.core;x-internal:=true, org.eclipse.mylyn.internal.bugzilla.rest.core.tests;x-internal:=true, diff --git a/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAttachmentMapper.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAttachmentMapper.java new file mode 100644 index 000000000..1a8cb3070 --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestAttachmentMapper.java @@ -0,0 +1,167 @@ +/******************************************************************************* + * 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; + +import java.util.Date; + +import org.eclipse.core.runtime.Assert; +import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper; +import org.eclipse.mylyn.tasks.core.data.TaskAttribute; +import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper; +import org.eclipse.mylyn.tasks.core.data.TaskAttributeMetaData; +import org.eclipse.mylyn.tasks.core.data.TaskData; + +public class BugzillaRestAttachmentMapper extends TaskAttachmentMapper { + private Date deltaDate; + + @Override + public void applyTo(TaskAttribute taskAttribute) { + Assert.isNotNull(taskAttribute); + TaskData taskData = taskAttribute.getTaskData(); + TaskAttributeMapper mapper = taskData.getAttributeMapper(); + taskAttribute.getMetaData().defaults().setType(TaskAttribute.TYPE_ATTACHMENT); + if (getAttachmentId() != null) { + mapper.setValue(taskAttribute, getAttachmentId()); + } + if (getAuthor() != null && getAuthor().getPersonId() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_AUTHOR); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_PERSON); + defaults.setLabel("Attachment Author:"); + mapper.setRepositoryPerson(child, getAuthor()); + } + if (getContentType() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_CONTENT_TYPE); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_SHORT_TEXT); + defaults.setLabel("Content Type:"); + mapper.setValue(child, getContentType()); + } + if (getCreationDate() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_DATE); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_DATE); + defaults.setLabel("Creation Date:"); + mapper.setDateValue(child, getCreationDate()); + } + if (getDescription() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_DESCRIPTION); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_SHORT_TEXT); + defaults.setLabel("Description:"); + mapper.setValue(child, getDescription()); + } + if (getFileName() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_FILENAME); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_SHORT_TEXT); + defaults.setLabel("Filename:"); + mapper.setValue(child, getFileName()); + } + if (isDeprecated() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_IS_DEPRECATED); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_BOOLEAN); + defaults.setLabel("obsolete"); + mapper.setBooleanValue(child, isDeprecated()); + } + if (isPatch() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_IS_PATCH); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_BOOLEAN); + defaults.setLabel("patch"); + mapper.setBooleanValue(child, isPatch()); + } + if (getLength() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_SIZE); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_SHORT_TEXT); + defaults.setLabel("Size in Bytes:"); + mapper.setLongValue(child, getLength()); + } + if (getUrl() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute(TaskAttribute.ATTACHMENT_URL); + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_URL); + defaults.setLabel("URL:"); + mapper.setValue(child, getUrl()); + } + if (getDeltaDate() != null) { + TaskAttribute child = taskAttribute.createMappedAttribute("delta_ts"); //$NON-NLS-1$ + TaskAttributeMetaData defaults = child.getMetaData().defaults(); + defaults.setType(TaskAttribute.TYPE_DATE); + defaults.setLabel("Modification Date"); + mapper.setDateValue(child, getDeltaDate()); + } + } + + public static BugzillaRestAttachmentMapper createFrom(TaskAttribute taskAttribute) { + Assert.isNotNull(taskAttribute); + TaskAttributeMapper mapper = taskAttribute.getTaskData().getAttributeMapper(); + BugzillaRestAttachmentMapper attachment = new BugzillaRestAttachmentMapper(); + attachment.setAttachmentId(mapper.getValue(taskAttribute)); + TaskAttribute child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_AUTHOR); + if (child != null) { + attachment.setAuthor(mapper.getRepositoryPerson(child)); + } + child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_CONTENT_TYPE); + if (child != null) { + attachment.setContentType(mapper.getValue(child)); + } + child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_DATE); + if (child != null) { + attachment.setCreationDate(mapper.getDateValue(child)); + } + child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_DESCRIPTION); + if (child != null) { + attachment.setDescription(mapper.getValue(child)); + } + child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_FILENAME); + if (child != null) { + attachment.setFileName(mapper.getValue(child)); + } + child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_IS_DEPRECATED); + if (child != null) { + attachment.setDeprecated(mapper.getBooleanValue(child)); + } + child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_IS_PATCH); + if (child != null) { + attachment.setPatch(mapper.getBooleanValue(child)); + } + child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_SIZE); + if (child != null) { + Long value = mapper.getLongValue(child); + if (value != null) { + attachment.setLength(value); + } + } + child = taskAttribute.getMappedAttribute(TaskAttribute.ATTACHMENT_URL); + if (child != null) { + attachment.setUrl(mapper.getValue(child)); + } + child = taskAttribute.getMappedAttribute("delta_ts"); //$NON-NLS-1$ + if (child != null) { + attachment.setDeltaDate(mapper.getDateValue(child)); + } + + return attachment; + } + + public Date getDeltaDate() { + return deltaDate; + } + + public void setDeltaDate(Date deltaDate) { + this.deltaDate = deltaDate; + } + +} 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 26e06bcaf..347699b9f 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 @@ -11,6 +11,7 @@ package org.eclipse.mylyn.internal.bugzilla.rest.core; +import java.io.InputStream; import java.util.List; import java.util.Map; import java.util.Set; @@ -20,6 +21,8 @@ import org.eclipse.core.runtime.ISafeRunnable; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.SafeRunner; import org.eclipse.core.runtime.Status; +import org.eclipse.jdt.annotation.NonNull; +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; @@ -133,7 +136,7 @@ public class BugzillaRestClient { } } - private final Function<String, String> function = new Function<String, String>() { + private final Function<String, String> removeLeadingZero = new Function<String, String>() { @Override public String apply(String input) { @@ -153,7 +156,7 @@ public class BugzillaRestClient { throw new BugzillaRestException(e1); } - Iterable<String> taskIdsTemp = Iterables.transform(taskIds, function); + Iterable<String> taskIdsTemp = Iterables.transform(taskIds, removeLeadingZero); Iterable<List<String>> partitions = Iterables.partition(taskIdsTemp, MAX_RETRIEVED_PER_QUERY); for (List<String> list : partitions) { Joiner joiner = Joiner.on(",id=").skipNulls(); //$NON-NLS-1$ @@ -164,12 +167,11 @@ public class BugzillaRestClient { .run(monitor); for (TaskData taskData : taskDataArray) { new BugzillaRestGetTaskComments(getClient(), taskData).run(monitor); + new BugzillaRestGetTaskAttachments(getClient(), taskData).run(monitor); config.updateProductOptions(taskData); config.addValidOperations(taskData); collector.accept(taskData); } - } catch (BugzillaRestException e) { - throw e; } catch (RuntimeException e) { // if the Throwable was warped in a RuntimeException in // BugzillaRestGetTaskData.JSonTaskDataDeserializer.deserialize() @@ -211,4 +213,9 @@ public class BugzillaRestClient { return Status.OK_STATUS; } + public InputStream getAttachmentData(@NonNull TaskAttribute attachmentAttribute, + @Nullable IOperationMonitor monitor) throws BugzillaRestException { + return new BugzillaRestGetTaskAttachmentData(client, attachmentAttribute).run(monitor); + } + } 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 8dc2b3836..becd6cea4 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 @@ -20,6 +20,7 @@ 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.jdt.annotation.Nullable; import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; import org.eclipse.mylyn.commons.core.operations.OperationUtil; import org.eclipse.mylyn.commons.net.AuthenticationCredentials; @@ -36,6 +37,7 @@ import org.eclipse.mylyn.tasks.core.ITaskMapping; 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.AbstractTaskAttachmentHandler; import org.eclipse.mylyn.tasks.core.data.AbstractTaskDataHandler; import org.eclipse.mylyn.tasks.core.data.TaskAttribute; import org.eclipse.mylyn.tasks.core.data.TaskData; @@ -60,16 +62,18 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { private static final ThreadLocal<IOperationMonitor> context = new ThreadLocal<IOperationMonitor>(); + private BugzillaRestTaskAttachmentHandler attachmentHandler; + private boolean ignoredProperty(String propertyName) { if (propertyName.equals(RepositoryLocation.PROPERTY_LABEL) || propertyName.equals(TaskRepository.OFFLINE) || propertyName.equals(IRepositoryConstants.PROPERTY_ENCODING) || propertyName.equals(TaskRepository.PROXY_HOSTNAME) || propertyName.equals(TaskRepository.PROXY_PORT) - || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.savePassword") - || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.usedefault") - || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.savePassword") - || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.username") - || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.password") - || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.enabled")) { + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.savePassword") //$NON-NLS-1$ + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.usedefault") //$NON-NLS-1$ + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.savePassword") //$NON-NLS-1$ + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.username") //$NON-NLS-1$ + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.password") //$NON-NLS-1$ + || propertyName.equals("org.eclipse.mylyn.tasklist.repositories.proxy.enabled")) { //$NON-NLS-1$ return true; } return false; @@ -92,7 +96,7 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { @Override public void propertyChange(PropertyChangeEvent evt) { if (ignoredProperty(evt.getPropertyName()) - || evt.getPropertyName().equals("org.eclipse.mylyn.tasklist.repositories.password")) { + || evt.getPropertyName().equals("org.eclipse.mylyn.tasklist.repositories.password")) { //$NON-NLS-1$ return; } TaskRepository taskRepository = (TaskRepository) evt.getSource(); @@ -120,6 +124,7 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { public BugzillaRestConnector(Duration refreshAfterWriteDuration) { super(); + this.attachmentHandler = new BugzillaRestTaskAttachmentHandler(this); configurationCache = createCacheBuilder(CONFIGURATION_CACHE_EXPIRE_DURATION, refreshAfterWriteDuration) .build(new CacheLoader<RepositoryKey, Optional<BugzillaRestConfiguration>>() { @@ -192,7 +197,7 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { if (taskUrl == null) { return null; } - int index = taskUrl.indexOf("/rest.cgi/"); + int index = taskUrl.indexOf("/rest.cgi/"); //$NON-NLS-1$ return index == -1 ? null : taskUrl.substring(0, index); } @@ -210,7 +215,7 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { @Override public String getTaskUrl(String repositoryUrl, String taskIdOrKey) { - return repositoryUrl + "/rest.cgi/bug/" + taskIdOrKey; + return repositoryUrl + "/rest.cgi/bug/" + taskIdOrKey; //$NON-NLS-1$ } @Override @@ -226,7 +231,7 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { try { monitor.beginTask("performQuery", IProgressMonitor.UNKNOWN); BugzillaRestClient client = getClient(repository); - IOperationMonitor progress = OperationUtil.convert(monitor, "performQuery", 3); + IOperationMonitor progress = OperationUtil.convert(monitor, "performQuery", 3); //$NON-NLS-1$ return client.performQuery(repository, query, collector, progress); } catch (CoreException e) { return new Status(IStatus.ERROR, BugzillaRestCore.ID_PLUGIN, IStatus.INFO, @@ -249,7 +254,7 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { @Override public void updateTaskFromTaskData(TaskRepository taskRepository, ITask task, TaskData taskData) { - task.setUrl(taskData.getRepositoryUrl() + "/rest.cgi/bug/" + taskData.getTaskId()); + task.setUrl(taskData.getRepositoryUrl() + "/rest.cgi/bug/" + taskData.getTaskId()); //$NON-NLS-1$ } @Override @@ -361,4 +366,10 @@ public class BugzillaRestConnector extends AbstractRepositoryConnector { }; } + @Override + @Nullable + public AbstractTaskAttachmentHandler getTaskAttachmentHandler() { + return attachmentHandler; + } + } 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 new file mode 100644 index 000000000..b188da598 --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachmentData.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * 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; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Type; + +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.tasks.core.data.TaskAttribute; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.reflect.TypeToken; + +public class BugzillaRestGetTaskAttachmentData extends BugzillaRestAuthenticatedGetRequest<InputStream> { + private final TaskAttribute taskAttribute; + + public BugzillaRestGetTaskAttachmentData(BugzillaRestHttpClient client, TaskAttribute taskAttribute) { + super(client, "/bug/attachment/" + taskAttribute.getValue() + "?include_fields=data", null); //$NON-NLS-1$ //$NON-NLS-2$ + this.taskAttribute = taskAttribute; + } + + @Override + protected InputStream parseFromJson(InputStreamReader in) { + TypeToken<InputStream> type = new TypeToken<InputStream>() { + }; + return new GsonBuilder().registerTypeAdapter(type.getType(), new JSonTaskDataDeserializer()) + .create() + .fromJson(in, type.getType()); + } + + private class JSonTaskDataDeserializer implements JsonDeserializer<InputStream> { + + @Override + public InputStream deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + + JsonElement attachments = ((JsonObject) json.getAsJsonObject().get("attachments")) //$NON-NLS-1$ + .get(taskAttribute.getValue()); + JsonPrimitive attachment = attachments.getAsJsonObject().get("data").getAsJsonPrimitive(); //$NON-NLS-1$ + if (attachment == null) { + throw com.google.common.base.Throwables.propagate(new CoreException( + new Status(IStatus.ERROR, BugzillaRestCore.ID_PLUGIN, "Can not get Attachment Data"))); //$NON-NLS-1$ + } + InputStream is = new ByteArrayInputStream(attachment.getAsString().getBytes()); + + return new Base64InputStream(is); + } + } + +} 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 new file mode 100644 index 000000000..7782f92a9 --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestGetTaskAttachments.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * 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; + +import java.io.InputStreamReader; +import java.lang.reflect.Type; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Locale; +import java.util.Map.Entry; +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.tasks.core.IRepositoryPerson; +import org.eclipse.mylyn.tasks.core.data.TaskAttribute; +import org.eclipse.mylyn.tasks.core.data.TaskData; + +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.reflect.TypeToken; + +public class BugzillaRestGetTaskAttachments extends BugzillaRestAuthenticatedGetRequest<ArrayList<TaskAttribute>> { + private final TaskData taskData; + + public BugzillaRestGetTaskAttachments(BugzillaRestHttpClient client, TaskData taskData) { + super(client, "/bug/" + taskData.getTaskId() + "/attachment?exclude_fields=data", null); //$NON-NLS-1$ //$NON-NLS-2$ + this.taskData = taskData; + } + + @Override + protected ArrayList<TaskAttribute> parseFromJson(InputStreamReader in) { + TypeToken<ArrayList<TaskAttribute>> type = new TypeToken<ArrayList<TaskAttribute>>() { + }; + return new GsonBuilder().registerTypeAdapter(type.getType(), new JSonTaskDataDeserializer()) + .create() + .fromJson(in, type.getType()); + } + + BugzillaRestTaskSchema taskSchema = BugzillaRestTaskSchema.getDefault(); + + private class JSonTaskDataDeserializer implements JsonDeserializer<ArrayList<TaskAttribute>> { + + @Override + public ArrayList<TaskAttribute> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + ArrayList<TaskAttribute> response = new ArrayList<TaskAttribute>(); + + for (Entry<String, JsonElement> bugEntry : ((JsonObject) json.getAsJsonObject().get("bugs")).entrySet()) { //$NON-NLS-1$ + for (JsonElement jsonElement : bugEntry.getValue().getAsJsonArray()) { + JsonObject attachmentObject = (JsonObject) jsonElement; + String id = attachmentObject.get("id").getAsString(); //$NON-NLS-1$ + String creator = attachmentObject.get("creator").getAsString(); //$NON-NLS-1$ + Long size = attachmentObject.get("size").getAsLong(); //$NON-NLS-1$ + TaskAttribute attachmentAttribute = taskData.getRoot() + .createAttribute(TaskAttribute.PREFIX_ATTACHMENT + id); + BugzillaRestAttachmentMapper attachmentMapper = BugzillaRestAttachmentMapper + .createFrom(attachmentAttribute); + attachmentMapper.setAttachmentId(id); + + IRepositoryPerson author = taskData.getAttributeMapper().getTaskRepository().createPerson(creator); + author.setName(creator); + attachmentMapper.setAuthor(author); + attachmentMapper.setLength(size != null ? size : -1L); + try { + SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); //$NON-NLS-1$ + iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$ + Date tempDate = iso8601Format.parse(attachmentObject.get("creation_time").getAsString()); //$NON-NLS-1$ + attachmentMapper.setCreationDate(tempDate); + tempDate = iso8601Format.parse(attachmentObject.get("last_change_time").getAsString()); //$NON-NLS-1$ + attachmentMapper.setDeltaDate(tempDate); + } catch (ParseException e) { + com.google.common.base.Throwables.propagate(new CoreException(new Status(IStatus.ERROR, + BugzillaRestCore.ID_PLUGIN, + "Can not parse Date (" + attachmentObject.get("creation_time").getAsString() + ")"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + attachmentMapper.setContentType(attachmentObject.get("content_type").getAsString()); //$NON-NLS-1$ + attachmentMapper.setDeprecated(attachmentObject.get("is_obsolete") //$NON-NLS-1$ + .getAsString() + .equals(Integer.valueOf(1))); + attachmentMapper.setDescription(attachmentObject.get("summary").getAsString()); //$NON-NLS-1$ + attachmentMapper.setFileName(attachmentObject.get("file_name").getAsString()); //$NON-NLS-1$ + attachmentMapper + .setPatch(attachmentObject.get("is_patch").getAsString().equals(Integer.valueOf(1))); //$NON-NLS-1$ + attachmentMapper.applyTo(attachmentAttribute); + } + } + return response; + } + + } + +}
\ 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/BugzillaRestTaskAttachmentHandler.java b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestTaskAttachmentHandler.java new file mode 100644 index 000000000..42de36be5 --- /dev/null +++ b/connector-bugzilla-rest/org.eclipse.mylyn.bugzilla.rest.core/src/org/eclipse/mylyn/internal/bugzilla/rest/core/BugzillaRestTaskAttachmentHandler.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * 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; + +import java.io.InputStream; + +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.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.mylyn.commons.core.operations.IOperationMonitor; +import org.eclipse.mylyn.commons.core.operations.IOperationMonitor.OperationFlag; +import org.eclipse.mylyn.commons.core.operations.OperationUtil; +import org.eclipse.mylyn.commons.net.Policy; +import org.eclipse.mylyn.tasks.core.ITask; +import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler; +import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentSource; +import org.eclipse.mylyn.tasks.core.data.TaskAttribute; + +public class BugzillaRestTaskAttachmentHandler extends AbstractTaskAttachmentHandler { + private final BugzillaRestConnector connector; + + public BugzillaRestTaskAttachmentHandler(BugzillaRestConnector connector) { + this.connector = connector; + } + + @Override + public boolean canGetContent(TaskRepository repository, ITask task) { + return true; + } + + @Override + public boolean canPostContent(TaskRepository repository, ITask task) { + return false; + } + + @Override + public InputStream getContent(@NonNull TaskRepository repository, @NonNull ITask task, + @NonNull TaskAttribute attachmentAttribute, @Nullable IProgressMonitor monitor) throws CoreException { + + try { + monitor = Policy.monitorFor(monitor); + monitor.beginTask("getAttachment Data", IProgressMonitor.UNKNOWN); //$NON-NLS-1$ + BugzillaRestClient client = connector.getClient(repository); + try { + IOperationMonitor progress = OperationUtil.convert(monitor, "get Attachment Data", 3); //$NON-NLS-1$ + progress.addFlag(OperationFlag.BACKGROUND); + return client.getAttachmentData(attachmentAttribute, progress); + } catch (BugzillaRestException e) { + throw new CoreException(new Status(IStatus.ERROR, BugzillaRestCore.ID_PLUGIN, 2, + "Error get attachment data.\n\n" + e.getMessage(), e)); //$NON-NLS-1$ + } + } finally { + monitor.done(); + } + } + + @Override + public void postContent(@NonNull TaskRepository repository, @NonNull ITask task, + @NonNull AbstractTaskAttachmentSource source, @Nullable String comment, + @Nullable TaskAttribute attachmentAttribute, @Nullable IProgressMonitor monitor) throws CoreException { + } + +} |