Package

Source Code of ExampleAttachmentUploadClient

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import sun.misc.BASE64Encoder;

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;

public class ExampleAttachmentUploadClient {
    private JiraSoapServiceServiceLocator fJiraSoapServiceGetter = new JiraSoapServiceServiceLocator();
    private JiraSoapService fJiraSoapService = null;
    private String fToken = null;

    /**
     * Example client using the SOAP interface, upload an attachment
     *
     * @author Andy Brook
     */
    public static void main(String[] args) throws Exception {
        if (args.length == 5) {
            ExampleAttachmentUploadClient ex = new ExampleAttachmentUploadClient(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.addAttachment(iss, new File(args[4]));

            System.out.println("Completed.");
        } else {
            System.err.println("Usage: [server] [userid] [password] [issue-key] [filename]");
            System.exit(-1);
        }

    }

    /**
     * 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 addAttachment(RemoteIssue iss, File resource) throws Exception {
        RemoteAttachment[] atts = fJiraSoapService.getAttachmentsFromIssue(fToken, iss.getKey());
        String[] filenames = new String[] {resource.getName()};
        InputStream is = new FileInputStream(resource);
        byte[] buff = new byte[(int) resource.length()];

        int read;
        int offset = 0;
        do {
            read = is.read(buff, offset, buff.length - offset);
            if (read > 0) {
                offset += read;
            }
        } while (read < buff.length);

        byte[][] data = new byte[][] {buff};

        // jira 3.x
        //fJiraSoapService.addAttachmentsToIssue(fToken, iss.getKey(), filenames, data);

        // Jira 4.
        String base64encodedFileData=new BASE64Encoder().encode(buff); String[] encodedData= new String[] { base64encodedFileData };
        fJiraSoapService.addBase64EncodedAttachmentsToIssue(fToken, iss.getKey(), filenames, encodedData);
    }

    public ExampleAttachmentUploadClient(String server, String user, String pass) throws Exception { // yea, in a constructor
        try {
            String endPoint = "/rpc/soap/jirasoapservice-v2";
            fJiraSoapServiceGetter.setJirasoapserviceV2EndpointAddress(server + endPoint);
            fJiraSoapServiceGetter.setMaintainSession(true);
            fJiraSoapService = fJiraSoapServiceGetter.getJirasoapserviceV2();
            fToken = fJiraSoapService.login(user, pass);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public String getToken() {
        return fToken;
    }

    public JiraSoapService getJiraSOAPService() {
        return fJiraSoapService;
    }
}
TOP

Related Classes of ExampleAttachmentUploadClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.