java - How do I read the attachment content on a Defect -


using rally java rest api, after attachmentcontent object, how bytes hold content?

you may find following example useful wanting do. it's user story rather defect process identical 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 , configure 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 , project settings     string myworkspace = "my workspace";     string myproject = "my project";      // formattedid of existing test case query     string existstoryformattedid = "us43";             // reference 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();      // reference 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 existing user story     system.out.println("querying 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 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 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 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 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();                 }      {     //release resources     restapi.close();     }                 } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -