1
0
Fork 0

Implement attatchment adding support.

master
Kyle Chaplin 2013-08-24 12:59:52 -05:00
parent bbf5b89042
commit b9129b2132
3 changed files with 60 additions and 7 deletions

10
pom.xml
View File

@ -38,13 +38,20 @@
</parent>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
@ -52,5 +59,6 @@
<classifier>jdk15</classifier>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -19,6 +19,7 @@
package net.rcarz.jiraclient;
import java.io.File;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
@ -422,6 +423,23 @@ public final class Issue extends Resource {
return (JSONArray)jo.get("transitions");
}
/**
* Adds an attachment to this issue.
*
* @param file java.io.File
*
* @throws JiraException when the comment creation fails
*/
public void addAttachment(File file) throws JiraException {
if(file != null){
try {
restclient.postFile(getRestUri(key) + "/attachments", file);
} catch (Exception ex) {
throw new JiraException("Failed add attachment to issue " + key, ex);
}
}
}
/**
* Adds a comment to this issue.

View File

@ -20,15 +20,17 @@
package net.rcarz.jiraclient;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.StringBuilder;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
@ -41,9 +43,8 @@ import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
/**
* A simple REST client that speaks JSON.
@ -157,6 +158,18 @@ public class RestClient {
return request(req);
}
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);
}
private JSON request(HttpEntityEnclosingRequestBase req, JSON payload)
throws RestException, IOException {
@ -279,6 +292,20 @@ public class RestClient {
return post(buildURI(path), payload);
}
/**
* Executes an HTTP POST with the given path and file payload.
*
* @param uri Full URI of the remote endpoint
* @param file java.io.File
*
* @throws URISyntaxException
* @throws IOException
* @throws RestException
*/
public JSON postFile(String path, File file) throws RestException, IOException, URISyntaxException{
return request(new HttpPost(buildURI(path)), file);
}
/**
* Executes an HTTP PUT with the given URI and payload.