如何阅读缺陷上的附件内容

时间:2013-05-14 19:03:52

标签: java rest attachment rally

使用 Rally Java Rest API ,获取AttachmentContent对象后,如何实际获取保存内容的字节数?

1 个答案:

答案 0 :(得分:0)

您可能会发现以下示例对您想要执行的操作很有用。这是一个用户故事而不是缺陷,但过程对于缺陷来说是相同的。

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.DeleteRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.DeleteResponse;
import com.rallydev.rest.response.GetResponse;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import com.rallydev.rest.util.Ref;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.codec.binary.Base64;
public class RestExample_DownloadAttachment {

public static void main(String[] args) throws URISyntaxException, IOException {

    // Create and configure a new instance of RallyRestApi
    // Connection parameters
    String rallyURL = "https://rally1.rallydev.com";
    String wsapiVersion = "1.43";
    String applicationName = "RestExample_DownloadAttachment";

    // Credentials
    String userName = "user@company.com";
    String userPassword = "topsecret";

    RallyRestApi restApi = new RallyRestApi(
    new URI(rallyURL),
    userName,
    userPassword
    );
    restApi.setWsapiVersion(wsapiVersion);
    restApi.setApplicationName(applicationName);       

    // Workspace and Project Settings
    String myWorkspace = "My Workspace";
    String myProject = "My Project";

    // FormattedID of Existing Test Case to Query
    String existStoryFormattedID = "US43";       

    // Get reference to Workspace of interest
    QueryRequest workspaceRequest = new QueryRequest("Workspace");
    workspaceRequest.setFetch(new Fetch("Name", "Owner", "Projects"));
    workspaceRequest.setQueryFilter(new QueryFilter("Name", "=", myWorkspace));
    QueryResponse workspaceQueryResponse = restApi.query(workspaceRequest);
    String workspaceRef = workspaceQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString();

    // Get reference to Project of interest
    QueryRequest projectRequest = new QueryRequest("Project");
    projectRequest.setFetch(new Fetch("Name", "Owner", "Projects"));
    projectRequest.setQueryFilter(new QueryFilter("Name", "=", myProject));
    QueryResponse projectQueryResponse = restApi.query(projectRequest);
    String projectRef = projectQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString();      

    // Query for existing User Story
    System.out.println("Querying for User Story: " + existStoryFormattedID);

    QueryRequest  existUserStoryRequest = new QueryRequest("HierarchicalRequirement");
    existUserStoryRequest.setFetch(new Fetch("FormattedID","Name","Attachments"));
    existUserStoryRequest.setQueryFilter(new QueryFilter("FormattedID", "=", existStoryFormattedID));
    QueryResponse userStoryQueryResponse = restApi.query(existUserStoryRequest);
    JsonObject existUserStoryJsonObject = userStoryQueryResponse.getResults().get(0).getAsJsonObject();
    String existUserStoryRef = userStoryQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString();
    JsonArray attachmentsJsonArray = existUserStoryJsonObject.getAsJsonArray("Attachments");

    // Take first attachment
    JsonObject attachmentObject = attachmentsJsonArray.get(0).getAsJsonObject();
    String attachmentRef = attachmentObject.get("_ref").toString();

    // Read attachment from Ref
    System.out.println("Reading First Attachment: " + attachmentRef);

    GetRequest attachmentRequest = new GetRequest(attachmentRef);
    attachmentRequest.setFetch(new Fetch("Name","Content"));
    GetResponse attachmentResponse = restApi.get(attachmentRequest);

    // AttachmentContent object
    JsonObject attachmentContentObject = attachmentResponse.getObject().get("Content").getAsJsonObject();
    String attachmentContentRef = attachmentContentObject.get("_ref").toString();

    // Read Content from Ref
    System.out.println("Reading Attachment Content: " + attachmentRef);

    GetRequest contentRequest = new GetRequest(attachmentContentRef);
    contentRequest.setFetch(new Fetch("Content"));
    GetResponse contentResponse = restApi.get(contentRequest);      

    // Read Content String of AttachmentContent
    String attachmentContentBase64String = contentResponse.getObject().get("Content").getAsString();

    // Grab attachment name
    String attachmentName = attachmentResponse.getObject().get("Name").getAsString();        

    // Decode base64 string into bytes
    byte[] imageBytes = Base64.decodeBase64(attachmentContentBase64String);

    // Image output
    String imageFilePath = "/Users/username/Desktop/";
    String fullImageFile = imageFilePath + attachmentName;

    // Write output file  
    System.out.println("Writing attachment to file: " + attachmentName);
    try {        

    OutputStream imageOutputStream = new FileOutputStream(fullImageFile);
    imageOutputStream.write(imageBytes);
    imageOutputStream.flush();
    imageOutputStream.close();

    } catch (Exception e) {
        System.out.println("Exception occurred while write image file ");
        e.printStackTrace();            
    }

    finally {
    //Release all resources
    restApi.close();
    }                
}
}