如何阅读缺陷上的附件内容 [英] How do I read the attachment content on a Defect

查看:69
本文介绍了如何阅读缺陷上的附件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the Rally Java Rest API, after I get the AttachmentContent object, how do I actually get the bytes which hold the content?

推荐答案

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

You may find the following example to be useful for what you are wanting to do. It's for a User Story rather than a Defect but the process would be identical for a Defect.

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();
    }                
}
}

这篇关于如何阅读缺陷上的附件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆