import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.axis.encoding.ser.Base64Serializer;
import org.swift.common.soap.jira.JiraSoapService;
import org.swift.common.soap.jira.JiraSoapServiceServiceLocator;
import org.swift.common.soap.jira.RemoteAttachment;
import org.swift.common.soap.jira.RemoteIssue;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class ExampleAttachmentDownloadClient {
private final JiraSoapServiceServiceLocator fJiraSoapServiceGetter = new JiraSoapServiceServiceLocator();
private JiraSoapService fJiraSoapService = null;
private String fToken = null;
private String fServerURL;
private String fUser;
private String fPass;
/**
* Example client using the SOAP interface, download an attachment
*
* @author Andy Brook
*/
public static void main(String[] args) throws Exception {
if (args.length == 4) {
ExampleAttachmentDownloadClient ex = new ExampleAttachmentDownloadClient(args[0], args[1], args[2]);
System.out.println("Connected ok.");
JiraSoapService soapy = ex.getJiraSOAPService();
String token = ex.getToken();
RemoteIssue iss = soapy.getIssue(token, args[3]);
ex.getAttachment(iss, new File("test"));
System.out.println("Completed.");
} else {
System.err.println("Usage: [server] [userid] [password] [issue-key]");
System.exit(-1);
}
}
public ExampleAttachmentDownloadClient(String server, String user, String pass) throws Exception { // yea, in a constructor
try {
fServerURL = server;
String endPoint = "/rpc/soap/jirasoapservice-v2";
fJiraSoapServiceGetter.setJirasoapserviceV2EndpointAddress(fServerURL + endPoint);
fJiraSoapServiceGetter.setMaintainSession(true);
fJiraSoapService = fJiraSoapServiceGetter.getJirasoapserviceV2();
fToken = fJiraSoapService.login(user, pass);
fUser = user;
fPass = pass;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
public String getToken() {
return fToken;
}
public JiraSoapService getJiraSOAPService() {
return fJiraSoapService;
}
/**
* http://docs.atlassian.com/software/jira/docs/api/rpc-jira-plugin/latest/com/atlassian/jira/rpc/soap/JiraSoapService.html
*
* @param iss
* @param resource
*/
private void getAttachment(RemoteIssue iss, File resource) throws Exception {
byte[] buff = null;
try {
RemoteAttachment[] atts = fJiraSoapService.getAttachmentsFromIssue(fToken, iss.getKey());
RemoteAttachment ra1 = atts[0];
String id = ra1.getId();
String name = ra1.getFilename();
URL u = new URL(fServerURL + "/secure/attachment/" + id + "/" + name);
URLConnection urlc = u.openConnection();
String userData = fUser + ":" + fPass;
String encodedUserPass = Base64.encode(userData.getBytes());
urlc.addRequestProperty("Authorization", encodedUserPass);
int length = urlc.getContentLength();
InputStream is = urlc.getInputStream();
buff = new byte[length];
int read;
int offset = 0;
do {
read = is.read(buff, offset, buff.length - offset);
if (read > 0) {
offset += read;
}
} while (read < buff.length);
} catch (Exception e) {
System.err.println(e.getLocalizedMessage());
}
return;
}
}