1
0
Fork 0

Merge pull request #76 from golovnin/attachments

Adds new API to add multiple attachments at once
master
Bob Carroll 2015-03-24 21:44:09 -07:00
commit 303973a639
2 changed files with 130 additions and 11 deletions

View File

@ -20,6 +20,7 @@
package net.rcarz.jiraclient;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.util.*;
@ -381,6 +382,57 @@ public class Issue extends Resource {
public List<Issue> issues = null;
}
public static final class NewAttachment {
private final String filename;
private final Object content;
public NewAttachment(File content) {
this(content.getName(), content);
}
public NewAttachment(String filename, File content) {
this.filename = requireFilename(filename);
this.content = requireContent(content);
}
public NewAttachment(String filename, InputStream content) {
this.filename = requireFilename(filename);
this.content = requireContent(content);
}
public NewAttachment(String filename, byte[] content) {
this.filename = requireFilename(filename);
this.content = requireContent(content);
}
String getFilename() {
return filename;
}
Object getContent() {
return content;
}
private static String requireFilename(String filename) {
if (filename == null) {
throw new NullPointerException("filename may not be null");
}
if (filename.length() == 0) {
throw new IllegalArgumentException("filename may not be empty");
}
return filename;
}
private static Object requireContent(Object content) {
if (content == null) {
throw new NullPointerException("content may not be null");
}
return content;
}
}
private String key = null;
private Map fields = null;
@ -570,7 +622,7 @@ public class Issue extends Resource {
*
* @param file java.io.File
*
* @throws JiraException when the comment creation fails
* @throws JiraException when the attachment creation fails
*/
public void addAttachment(File file) throws JiraException {
try {
@ -606,6 +658,27 @@ public class Issue extends Resource {
}
}
/**
* Adds an attachments to this issue.
*
* @param attachments the attachments to add
*
* @throws JiraException when the attachments creation fails
*/
public void addAttachments(NewAttachment... attachments) throws JiraException {
if (attachments == null) {
throw new NullPointerException("attachments may not be null");
}
if (attachments.length == 0) {
return;
}
try {
restclient.post(getRestUri(key) + "/attachments", attachments);
} catch (Exception ex) {
throw new JiraException("Failed add attachment to issue " + key, ex);
}
}
/**
* Adds a comment to this issue.
*

View File

@ -22,6 +22,7 @@ package net.rcarz.jiraclient;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
@ -48,7 +49,9 @@ import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
/**
* A simple REST client that speaks JSON.
@ -183,16 +186,43 @@ public class RestClient {
}
private JSON request(HttpEntityEnclosingRequestBase req, File file)
throws RestException, IOException {
if (file != null) {
File fileUpload = file;
req.setHeader("X-Atlassian-Token","nocheck");
MultipartEntity ent = new MultipartEntity();
ent.addPart("file", new FileBody(fileUpload));
req.setEntity(ent);
}
return request(req);
throws RestException, IOException {
if (file != null) {
File fileUpload = file;
req.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntity ent = new MultipartEntity();
ent.addPart("file", new FileBody(fileUpload));
req.setEntity(ent);
}
return request(req);
}
private JSON request(HttpEntityEnclosingRequestBase req, Issue.NewAttachment... attachments)
throws RestException, IOException {
if (attachments != null) {
req.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntity ent = new MultipartEntity();
for(Issue.NewAttachment attachment : attachments) {
String filename = attachment.getFilename();
Object content = attachment.getContent();
if (content instanceof byte[]) {
ent.addPart("file", new ByteArrayBody((byte[]) content, filename));
} else if (content instanceof InputStream) {
ent.addPart("file", new InputStreamBody((InputStream) content, filename));
} else if (content instanceof File) {
ent.addPart("file", new FileBody((File) content, filename));
} else if (content == null) {
throw new IllegalArgumentException("Missing content for the file " + filename);
} else {
throw new IllegalArgumentException(
"Expected file type byte[], java.io.InputStream or java.io.File but provided " +
content.getClass().getName() + " for the file " + filename);
}
}
req.setEntity(ent);
}
return request(req);
}
private JSON request(HttpEntityEnclosingRequestBase req, JSON payload)
throws RestException, IOException {
@ -354,7 +384,7 @@ public class RestClient {
/**
* Executes an HTTP POST with the given path and file payload.
*
* @param uri Full URI of the remote endpoint
* @param path Full URI of the remote endpoint
* @param file java.io.File
*
* @throws URISyntaxException
@ -365,6 +395,22 @@ public class RestClient {
return request(new HttpPost(buildURI(path)), file);
}
/**
* Executes an HTTP POST with the given path and file payloads.
*
* @param path Full URI of the remote endpoint
* @param attachments the name of the attachment
*
* @throws URISyntaxException
* @throws IOException
* @throws RestException
*/
public JSON post(String path, Issue.NewAttachment... attachments)
throws RestException, IOException, URISyntaxException
{
return request(new HttpPost(buildURI(path)), attachments);
}
/**
* Executes an HTTP PUT with the given URI and payload.
*