diff options
author | Frank Becker | 2013-01-27 18:56:31 +0000 |
---|---|---|
committer | Steffen Pingel | 2013-04-28 00:18:04 +0000 |
commit | c8191bd67f7137f0e8403f6a4e6206fafc53fcab (patch) | |
tree | bca67fa88a3cd331231201cf2c74d8fbe0d1e8ea /org.eclipse.mylyn.bugzilla.core | |
parent | cd9abb53f4c6021f16268c24a739e0b6d832c5ee (diff) | |
download | org.eclipse.mylyn.tasks-c8191bd67f7137f0e8403f6a4e6206fafc53fcab.tar.gz org.eclipse.mylyn.tasks-c8191bd67f7137f0e8403f6a4e6206fafc53fcab.tar.xz org.eclipse.mylyn.tasks-c8191bd67f7137f0e8403f6a4e6206fafc53fcab.zip |
395029: Bugzilla should support streaming for attachments
Change-Id: I9f80f423f926b36a69d0af15f4e92902ba78aaaa
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=395029
Diffstat (limited to 'org.eclipse.mylyn.bugzilla.core')
2 files changed, 48 insertions, 43 deletions
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 d11bc1f39..06647e699 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 @@ -21,6 +21,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; +import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.Proxy; @@ -98,6 +99,7 @@ import org.eclipse.mylyn.tasks.core.data.TaskDataCollector; * @author Mik Kersten * @author Rob Elves * @author Steffen Pingel + * @author Frank Becker */ public class BugzillaClient { @@ -508,7 +510,7 @@ public class BugzillaClient { if (!loggedIn) { InputStream input = getResponseStream(postMethod, monitor); try { - parseHtmlError(input); + throw new CoreException(parseHtmlError(input)); } finally { input.close(); } @@ -637,13 +639,12 @@ public class BugzillaClient { } } // because html is not a valid config content type it is save to get the response here - parseHtmlError(getResponseStream(postMethod, monitor)); + throw new CoreException(parseHtmlError(getResponseStream(postMethod, monitor))); } finally { if (postMethod != null) { WebUtil.releaseConnection(postMethod, monitor); } } - return false; } protected RepositoryQueryResultsFactory getQueryResultsFactory(InputStream stream) { @@ -796,8 +797,7 @@ public class BugzillaClient { } if (loggedIn) { - parseHtmlError(stream); - return null; + throw new CoreException(parseHtmlError(stream)); } } finally { stream.close(); @@ -812,27 +812,32 @@ public class BugzillaClient { return null; } - public void getAttachmentData(String attachmentId, OutputStream out, IProgressMonitor monitor) throws IOException, + public InputStream getAttachmentData(String attachmentId, IProgressMonitor monitor) throws IOException, CoreException { String url = repositoryUrl + IBugzillaConstants.URL_GET_ATTACHMENT_DOWNLOAD + attachmentId; - GetMethod method = getConnect(url, monitor);//getConnectGzip(url, monitor); + GetMethod method = getConnect(url, monitor); + Status status = null; try { if (method.getStatusCode() == HttpStatus.SC_OK) { - //copy the response - InputStream instream = method.getResponseBodyAsStream(); - if (instream != null) { - byte[] buffer = new byte[4096]; - int len; - while ((len = instream.read(buffer)) > 0) { - out.write(buffer, 0, len); - } + Header contentDisposition = method.getResponseHeader("Content-disposition"); //$NON-NLS-1$ + if (contentDisposition == null) { + status = parseHtmlError(method.getResponseBodyAsStream()); + } else { + //copy the response + return method.getResponseBodyAsStream(); } } else { - parseHtmlError(method.getResponseBodyAsStream()); + status = parseHtmlError(method.getResponseBodyAsStream()); } + } catch (Exception e) { + status = new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, "Unable to retrieve attachment", e); //$NON-NLS-1$ } finally { - WebUtil.releaseConnection(method, monitor); + if (status != null) { + WebUtil.releaseConnection(method, monitor); + throw new CoreException(status); + } } + throw new CoreException(status); } private String getCharacterEncoding() { @@ -1863,10 +1868,24 @@ public class BugzillaClient { /** * Utility method for determining what potential error has occurred from a bugzilla html reponse page */ - private BugzillaRepositoryResponse parseHtmlError(InputStream inputStream) throws IOException, CoreException { + private Status parseHtmlError(InputStream inputStream) { - BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, getCharacterEncoding())); - return parseRepositoryResponse(null, in); + try { + BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, getCharacterEncoding())); + parseRepositoryResponse(null, in); + return new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, "No Exception from parseHtmlError"); //$NON-NLS-1$ + } catch (CoreException e) { + if (e.getStatus() instanceof BugzillaStatus || e.getStatus() instanceof RepositoryStatus) { + return (Status) e.getStatus(); + } else { + return new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, + "No Exception from parseHtmlError, Status is not from expected Type"); //$NON-NLS-1$ + } + } catch (UnsupportedEncodingException e1) { + return new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, "UnsupportedEncodingException:", e1); //$NON-NLS-1$ + } catch (IOException e) { + return new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, "IOException:", e); //$NON-NLS-1$ + } } private BugzillaRepositoryResponse parsePostResponse(String taskId, InputStream inputStream) throws IOException, @@ -2186,8 +2205,7 @@ public class BugzillaClient { if (!parseable) { // because html is not a valid config content type it is save to get the response here - parseHtmlError(getResponseStream(method, monitor)); - break; + throw new CoreException(parseHtmlError(getResponseStream(method, monitor))); } } catch (CoreException c) { if (c.getStatus().getCode() == RepositoryStatus.ERROR_REPOSITORY_LOGIN && authenticationAttempt < 1) { diff --git a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaTaskAttachmentHandler.java b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaTaskAttachmentHandler.java index 8c18538b2..aa3f4182c 100644 --- a/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaTaskAttachmentHandler.java +++ b/org.eclipse.mylyn.bugzilla.core/src/org/eclipse/mylyn/internal/bugzilla/core/BugzillaTaskAttachmentHandler.java @@ -7,15 +7,13 @@ * * Contributors: * Tasktop Technologies - initial API and implementation + * Frank Becker - bug# 395029 *******************************************************************************/ package org.eclipse.mylyn.internal.bugzilla.core; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; @@ -31,6 +29,7 @@ import org.eclipse.mylyn.tasks.core.data.TaskAttribute; /** * @author Rob Elves + * @author Frank Becker * @since 3.0 */ public class BugzillaTaskAttachmentHandler extends AbstractTaskAttachmentHandler { @@ -56,12 +55,15 @@ public class BugzillaTaskAttachmentHandler extends AbstractTaskAttachmentHandler @Override public InputStream getContent(TaskRepository repository, ITask task, TaskAttribute attachmentAttribute, IProgressMonitor monitor) throws CoreException { + BugzillaClient client; try { monitor.beginTask(Messages.BugzillaTaskAttachmentHandler_Getting_attachment, IProgressMonitor.UNKNOWN); TaskAttachmentMapper attachment = TaskAttachmentMapper.createFrom(attachmentAttribute); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - downloadAttachment(repository, task, attachment.getAttachmentId(), out, monitor); - return new ByteArrayInputStream(out.toByteArray()); + client = connector.getClientManager().getClient(repository, monitor); + return client.getAttachmentData(attachment.getAttachmentId(), monitor); + } catch (IOException e) { + throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, + "Unable to retrieve attachment", e)); //$NON-NLS-1$ } finally { monitor.done(); } @@ -82,20 +84,5 @@ public class BugzillaTaskAttachmentHandler extends AbstractTaskAttachmentHandler } finally { monitor.done(); } - - } - - private void downloadAttachment(TaskRepository repository, ITask task, String attachmentId, OutputStream out, - IProgressMonitor monitor) throws CoreException { - BugzillaClient client; - try { - client = connector.getClientManager().getClient(repository, - new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN)); - client.getAttachmentData(attachmentId, out, monitor); - } catch (IOException e) { - throw new CoreException(new Status(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, - "Unable to retrieve attachment", e)); //$NON-NLS-1$ - } } - } |