Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormkersten2007-06-05 01:20:37 +0000
committermkersten2007-06-05 01:20:37 +0000
commit77b03f518715908344326384e6f585a84cf3d243 (patch)
tree7ffaa26a88bca40928cfa1bf8791dd7f4ecae50a /org.eclipse.mylyn.bugzilla.core
parent8a2bb7d31f69655928880ac393b1d39cdd3aac84 (diff)
downloadorg.eclipse.mylyn.tasks-77b03f518715908344326384e6f585a84cf3d243.tar.gz
org.eclipse.mylyn.tasks-77b03f518715908344326384e6f585a84cf3d243.tar.xz
org.eclipse.mylyn.tasks-77b03f518715908344326384e6f585a84cf3d243.zip
NEW - bug 186070: [api] make IAttachmentHandler API stream-based
https://bugs.eclipse.org/bugs/show_bug.cgi?id=186070
Diffstat (limited to 'org.eclipse.mylyn.bugzilla.core')
-rw-r--r--org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/AttachmentPartSource.java43
-rw-r--r--org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaAttachmentHandler.java56
-rw-r--r--org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java31
3 files changed, 67 insertions, 63 deletions
diff --git a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/AttachmentPartSource.java b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/AttachmentPartSource.java
new file mode 100644
index 000000000..ec36bff5e
--- /dev/null
+++ b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/AttachmentPartSource.java
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2004 - 2006 University Of British Columbia 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:
+ * University Of British Columbia - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylar.internal.bugzilla.core;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.httpclient.methods.multipart.PartSource;
+import org.eclipse.mylar.tasks.core.ITaskAttachment;
+
+/**
+ * @author Steffen Pingel
+ */
+public class AttachmentPartSource implements PartSource {
+
+ private ITaskAttachment attachment;
+
+ public AttachmentPartSource(ITaskAttachment attachment) {
+ this.attachment = attachment;
+ }
+
+ public InputStream createInputStream() throws IOException {
+ return attachment.createInputStream();
+ }
+
+ public String getFileName() {
+ return attachment.getFilename();
+ }
+
+ public long getLength() {
+ return attachment.getLength();
+ }
+
+}
diff --git a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaAttachmentHandler.java b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaAttachmentHandler.java
index 58fc7934d..bf97e5e1f 100644
--- a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaAttachmentHandler.java
+++ b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaAttachmentHandler.java
@@ -11,28 +11,24 @@
package org.eclipse.mylar.internal.bugzilla.core;
-import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.OutputStream;
+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.mylar.core.MylarStatusHandler;
+import org.eclipse.mylar.tasks.core.AbstractAttachmentHandler;
import org.eclipse.mylar.tasks.core.AbstractRepositoryTask;
-import org.eclipse.mylar.tasks.core.IAttachmentHandler;
import org.eclipse.mylar.tasks.core.IMylarStatusConstants;
+import org.eclipse.mylar.tasks.core.ITaskAttachment;
import org.eclipse.mylar.tasks.core.RepositoryAttachment;
-import org.eclipse.mylar.tasks.core.RepositoryTaskAttribute;
import org.eclipse.mylar.tasks.core.TaskRepository;
/**
* @author Mik Kersten
* @author Rob Elves
*/
-public class BugzillaAttachmentHandler implements IAttachmentHandler {
+public class BugzillaAttachmentHandler extends AbstractAttachmentHandler {
private BugzillaRepositoryConnector connector;
@@ -40,55 +36,22 @@ public class BugzillaAttachmentHandler implements IAttachmentHandler {
this.connector = connector;
}
- public byte[] getAttachmentData(TaskRepository repository, RepositoryAttachment attachment) throws CoreException {
+ public InputStream getAttachmentAsStream(TaskRepository repository, RepositoryAttachment attachment, IProgressMonitor monitor) throws CoreException {
try {
BugzillaClient client = connector.getClientManager().getClient(repository);
- byte[] data = client.getAttachmentData(attachment.getId());
- return data;
+ return client.getAttachmentData(attachment.getId());
} catch (IOException e) {
throw new CoreException(new BugzillaStatus(Status.ERROR, BugzillaCorePlugin.PLUGIN_ID,
IMylarStatusConstants.IO_ERROR, repository.getUrl(), e));
}
}
- public void downloadAttachment(TaskRepository repository, RepositoryAttachment attachment, File file, IProgressMonitor monitor)
- throws CoreException {
- if (repository == null || attachment == null || file == null) {
- MylarStatusHandler.log("Unable to download. Null argument.", this);
- throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.PLUGIN_ID, IStatus.ERROR,
- "Unable to download attachment", null));
- }
- String filename = attachment.getAttributeValue(RepositoryTaskAttribute.ATTACHMENT_FILENAME);
- if (filename == null) {
- throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.PLUGIN_ID, IStatus.ERROR,
- "Attachment download from " + repository.getUrl() + " failed, missing attachment filename.", null));
- }
-
- try {
- BugzillaClient client = connector.getClientManager().getClient(repository);
- byte[] data = client.getAttachmentData("" + attachment.getId());
- writeData(file, data);
- } catch (Exception e) {
- throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.PLUGIN_ID, 0,
- "Attachment download from " + repository.getUrl() + " failed.", e));
- }
- }
-
- private void writeData(File file, byte[] data) throws IOException {
- OutputStream out = new FileOutputStream(file);
- try {
- out.write(data);
- } finally {
- out.close();
- }
- }
-
- public void uploadAttachment(TaskRepository repository, AbstractRepositoryTask task, String comment,
- String description, File file, String contentType, boolean isPatch, IProgressMonitor monitor) throws CoreException {
+ public void uploadAttachment(TaskRepository repository, AbstractRepositoryTask task, ITaskAttachment attachment,
+ String comment, IProgressMonitor monitor) throws CoreException {
try {
String bugId = task.getTaskId();
BugzillaClient client = connector.getClientManager().getClient(repository);
- client.postAttachment(bugId, comment, description, file, contentType, isPatch);
+ client.postAttachment(bugId, comment, attachment);
} catch (IOException e) {
throw new CoreException(new BugzillaStatus(Status.ERROR, BugzillaCorePlugin.PLUGIN_ID,
IMylarStatusConstants.IO_ERROR, repository.getUrl(), e));
@@ -110,5 +73,6 @@ public class BugzillaAttachmentHandler implements IAttachmentHandler {
public void updateAttachment(TaskRepository repository, RepositoryAttachment attachment) throws CoreException {
// implement
}
+
}
diff --git a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java
index 4b37740d6..3c17f1a3f 100644
--- a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java
+++ b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaClient.java
@@ -11,8 +11,8 @@
package org.eclipse.mylar.internal.bugzilla.core;
import java.io.BufferedReader;
-import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy;
@@ -57,6 +57,7 @@ import org.eclipse.mylar.internal.bugzilla.core.history.BugzillaTaskHistoryParse
import org.eclipse.mylar.internal.bugzilla.core.history.TaskHistory;
import org.eclipse.mylar.tasks.core.AbstractRepositoryQuery;
import org.eclipse.mylar.tasks.core.IMylarStatusConstants;
+import org.eclipse.mylar.tasks.core.ITaskAttachment;
import org.eclipse.mylar.tasks.core.QueryHitCollector;
import org.eclipse.mylar.tasks.core.RepositoryOperation;
import org.eclipse.mylar.tasks.core.RepositoryStatus;
@@ -545,22 +546,18 @@ public class BugzillaClient {
}
}
- public byte[] getAttachmentData(String attachmentId) throws IOException, CoreException {
- GetMethod method = null;
+ public InputStream getAttachmentData(String attachmentId) throws IOException, CoreException {
+ String url = repositoryUrl + IBugzillaConstants.URL_GET_ATTACHMENT_DOWNLOAD + attachmentId;
+ GetMethod method = getConnect(url);
try {
- String url = repositoryUrl + IBugzillaConstants.URL_GET_ATTACHMENT_DOWNLOAD + attachmentId;
- method = getConnect(url);
- return method.getResponseBody();
-
- } finally {
- if (method != null) {
- method.releaseConnection();
- }
+ return method.getResponseBodyAsStream();
+ } catch (IOException e) {
+ method.releaseConnection();
+ throw e;
}
}
- public void postAttachment(String bugReportID, String comment, String description, File sourceFile,
- String contentType, boolean isPatch) throws HttpException, IOException, CoreException {
+ public void postAttachment(String bugReportID, String comment, ITaskAttachment attachment) throws HttpException, IOException, CoreException {
WebClientUtil.setupHttpClient(httpClient, proxy, repositoryUrl.toString(), htAuthUser, htAuthPass);
if (!authenticated && hasAuthenticationCredentials()) {
authenticate();
@@ -580,15 +577,15 @@ public class BugzillaClient {
parts.add(new StringPart(IBugzillaConstants.POST_INPUT_BUGZILLA_LOGIN, username, characterEncoding));
parts.add(new StringPart(IBugzillaConstants.POST_INPUT_BUGZILLA_PASSWORD, password, characterEncoding));
parts.add(new StringPart(IBugzillaConstants.POST_INPUT_BUGID, bugReportID, characterEncoding));
- parts.add(new StringPart(IBugzillaConstants.POST_INPUT_DESCRIPTION, description, characterEncoding));
+ parts.add(new StringPart(IBugzillaConstants.POST_INPUT_DESCRIPTION, attachment.getDescription(), characterEncoding));
parts.add(new StringPart(IBugzillaConstants.POST_INPUT_COMMENT, comment, characterEncoding));
- parts.add(new FilePart(IBugzillaConstants.POST_INPUT_DATA, sourceFile));
+ parts.add(new FilePart(IBugzillaConstants.POST_INPUT_DATA, new AttachmentPartSource(attachment)));
- if (isPatch) {
+ if (attachment.isPatch()) {
parts.add(new StringPart(ATTRIBUTE_ISPATCH, VALUE_ISPATCH));
} else {
parts.add(new StringPart(ATTRIBUTE_CONTENTTYPEMETHOD, VALUE_CONTENTTYPEMETHOD_MANUAL));
- parts.add(new StringPart(ATTRIBUTE_CONTENTTYPEENTRY, contentType));
+ parts.add(new StringPart(ATTRIBUTE_CONTENTTYPEENTRY, attachment.getContentType()));
}
postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[1]), postMethod.getParams()));

Back to the top