Commits

Jacek Jaroczynski committed 3b68281

rest (issue attachments)

  • Participants
  • Parent commits 35bd9de

Comments (0)

Files changed (7)

com.atlassian.connector.eclipse.jira.core/src/com/atlassian/connector/eclipse/internal/jira/core/JiraTaskAttachmentHandler.java

 
 package com.atlassian.connector.eclipse.internal.jira.core;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 
 import org.apache.commons.io.IOUtils;
 import org.eclipse.core.runtime.CoreException;
 		return true;
 	}
 
-	private void downloadAttachment(TaskRepository repository, ITask task, String attachmentId, OutputStream out,
+	private InputStream downloadAttachment(TaskRepository repository, ITask task, String attachmentId,
 			IProgressMonitor monitor) throws CoreException {
 		JiraClient client = JiraClientFactory.getDefault().getJiraClient(repository);
 		try {
 				throw new CoreException(new Status(IStatus.ERROR, JiraCorePlugin.ID_PLUGIN, "Attachment with id \"" //$NON-NLS-1$
 						+ attachmentId + "\" for JIRA issue \"" + task.getTaskKey() + "\" not found")); //$NON-NLS-1$ //$NON-NLS-2$
 			}
-			client.getAttachment(issue, jiraAttachment, out, monitor);
+			return client.getAttachment(issue, jiraAttachment, monitor);
 		} catch (JiraException e) {
 			throw new CoreException(JiraCorePlugin.toStatus(repository, e));
 		}
 		try {
 			monitor.beginTask(Messages.JiraTaskAttachmentHandler_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());
+//			ByteArrayOutputStream out = new ByteArrayOutputStream();
+			return downloadAttachment(repository, task, attachment.getAttachmentId(), monitor);
+//			downloadAttachment(repository, task, attachment.getAttachmentId(), out, monitor);
+//			return new ByteArrayInputStream(out.toByteArray());
 		} finally {
 			monitor.done();
 		}

com.atlassian.connector.eclipse.jira.core/src/com/atlassian/connector/eclipse/internal/jira/core/JiraTaskDataHandler.java

 			}
 			taskAttachment.setLength(attachment.getSize());
 			taskAttachment.setCreationDate(attachment.getCreated());
-			taskAttachment.setUrl(client.getBaseUrl() + "/secure/attachment/" + attachment.getId() + "/" //$NON-NLS-1$ //$NON-NLS-2$
-					+ attachment.getName());
+//			taskAttachment.setUrl(client.getBaseUrl() + "/secure/attachment/" + attachment.getId() + "/" //$NON-NLS-1$ //$NON-NLS-2$
+//					+ attachment.getName());
+			taskAttachment.setUrl(attachment.getContent().toString());
 			taskAttachment.applyTo(attribute);
 			i++;
 		}

com.atlassian.connector.eclipse.jira.core/src/com/atlassian/connector/eclipse/internal/jira/core/model/Attachment.java

 package com.atlassian.connector.eclipse.internal.jira.core.model;
 
 import java.io.Serializable;
+import java.net.URI;
 import java.util.Date;
 
 /**
 
 	private Date created;
 
+	private URI content;
+
+	public Attachment() {
+	}
+
 	public Attachment(String id, String name, long size, String author, Date created) {
 		this.id = id;
 		this.name = name;
 		return this.name;
 	}
 
+	public URI getContent() {
+		return content;
+	}
+
+	public void setContent(URI content) {
+		this.content = content;
+	}
+
 }

com.atlassian.connector.eclipse.jira.core/src/com/atlassian/connector/eclipse/internal/jira/core/service/JiraClient.java

 
 package com.atlassian.connector.eclipse.internal.jira.core.service;
 
-import java.io.OutputStream;
+import java.io.InputStream;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 			IProgressMonitor monitor) throws JiraException {
 		JiraCorePlugin.getMonitoring().logJob("addAttachment", null); //$NON-NLS-1$
 
-		restClient.attAttachment(jiraIssue.getKey(), content, filename);
+		restClient.addAttachment(jiraIssue.getKey(), content, filename);
 
 //		String[] encodedContents = new String[] { new String(new Base64().encode(content)) };
 //		String[] names = new String[] { filename };
 
 	}
 
-	public void getAttachment(JiraIssue jiraIssue, Attachment attachment, OutputStream out, IProgressMonitor monitor)
+	public InputStream getAttachment(JiraIssue jiraIssue, Attachment attachment, IProgressMonitor monitor)
 			throws JiraException {
 
-//		Issue issue = restClient.getIssueClient().getIssue(jiraIssue.getKey(), new NullProgressMonitor());
-//		
-//		issue.getAttachmentsUri().
-//		
-//		restClient.getIssueClient().getAttachment(new NullProgressMonitor(), attachmentUri)
+		JiraCorePlugin.getMonitoring().logJob("getAttachment", null); //$NON-NLS-1$
 
-		JiraCorePlugin.getMonitoring().logJob("getAttachment", null); //$NON-NLS-1$
-		webClient.retrieveFile(jiraIssue, attachment, out, monitor);
+		return restClient.getAttachment(attachment.getContent());
+
+//		webClient.retrieveFile(jiraIssue, attachment, out, monitor);
 	}
 
 	/**

com.atlassian.connector.eclipse.jira.core/src/com/atlassian/connector/eclipse/internal/jira/core/service/rest/JiraRestClientAdapter.java

 package com.atlassian.connector.eclipse.internal.jira.core.service.rest;
 
 import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 		return restClient.getIssueClient().getIssue(issueKey, new NullProgressMonitor());
 	}
 
-	public void attAttachment(String issueKey, byte[] content, String filename) {
+	public void addAttachment(String issueKey, byte[] content, String filename) {
 		restClient.getIssueClient().addAttachment(new NullProgressMonitor(), getIssue(issueKey).getAttachmentsUri(),
 				new ByteArrayInputStream(content), filename);
 	}
 
+	public InputStream getAttachment(URI attachmentUri) {
+		return restClient.getIssueClient().getAttachment(new NullProgressMonitor(), attachmentUri);
+	}
+
 	public Project[] getProjects() {
 		Iterable<BasicProject> allProjects = restClient.getProjectClient().getAllProjects(new NullProgressMonitor());
 

com.atlassian.connector.eclipse.jira.core/src/com/atlassian/connector/eclipse/internal/jira/core/service/rest/JiraRestConverter.java

 import org.eclipse.core.runtime.IProgressMonitor;
 import org.joda.time.DateTime;
 
+import com.atlassian.connector.eclipse.internal.jira.core.model.Attachment;
 import com.atlassian.connector.eclipse.internal.jira.core.model.Comment;
 import com.atlassian.connector.eclipse.internal.jira.core.model.Component;
 import com.atlassian.connector.eclipse.internal.jira.core.model.IssueLink;
 		}
 
 		jiraIssue.setIssueLinks(convertIssueLinks(issue.getIssueLinks()));
-
 		jiraIssue.setComments(convertComments(issue.getComments()));
 
+		jiraIssue.setAttachments(convertAttachments(issue.getAttachments()));
+
 		return jiraIssue;
 	}
 
+	private static Attachment[] convertAttachments(
+			Iterable<com.atlassian.jira.rest.client.domain.Attachment> attachments) {
+
+		List<Attachment> outAttachments = new ArrayList<Attachment>();
+
+		for (com.atlassian.jira.rest.client.domain.Attachment attachment : attachments) {
+			outAttachments.add(convert(attachment));
+		}
+
+		return outAttachments.toArray(new Attachment[outAttachments.size()]);
+	}
+
+	private static Attachment convert(com.atlassian.jira.rest.client.domain.Attachment attachment) {
+		Attachment outAttachment = new Attachment();
+
+		// TODO rest change to real id 
+		outAttachment.setId(attachment.getSelf().toString());
+		outAttachment.setAuthor(attachment.getAuthor().getDisplayName());
+		outAttachment.setCreated(attachment.getCreationDate().toDate());
+		outAttachment.setName(attachment.getFilename());
+		outAttachment.setSize(attachment.getSize());
+		outAttachment.setContent(attachment.getContentUri());
+
+		return outAttachment;
+	}
+
 	private static Comment[] convertComments(Iterable<com.atlassian.jira.rest.client.domain.Comment> comments) {
 		List<Comment> outComments = new ArrayList<Comment>();
 

com.atlassian.connector.eclipse.jira.tests/src/com/atlassian/connector/eclipse/jira/tests/util/MockJiraClient.java

 
 package com.atlassian.connector.eclipse.jira.tests.util;
 
-import java.io.OutputStream;
+import java.io.InputStream;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.mylyn.commons.net.WebLocation;
 	}
 
 	@Override
-	public void getAttachment(JiraIssue issue, Attachment attachment, OutputStream out, IProgressMonitor monitor)
+	public InputStream getAttachment(JiraIssue issue, Attachment attachment, IProgressMonitor monitor)
 			throws JiraException {
 		// ignore
+		return null;
 	}
 
 	@Override