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

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

Related

ContentModel cannot be resolved to a variable

I'm having this error :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
ContentModel cannot be resolved to a variable
at test2CMIS.Test.main(Test.java:39)"
And I dont understand from where it comes, here is my code :
public class Test {
public static void main(String[] args){
Test atest = new Test();
Session session = atest.iniSession();
AuthenticationService authenticationService=null;
PersonService personService = null;
if (authenticationService.authenticationExists("test") == false)
{
authenticationService.createAuthentication("test", "changeMe".toCharArray());
PropertyMap ppOne = new PropertyMap(4);
ppOne.put(ContentModel.PROP_USERNAME, "test");
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
ppOne.put(ContentModel.PROP_EMAIL, "test"+"#example.com");
personService.createPerson(ppOne);
}
}
I did import the : import org.alfresco.model.ContentModel; and a lot of others librarys for my code.
Thx for help.
The code I'm using and I left some things that I tried too in comments so you can see what things I have done:
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.alfresco.service.cmr.security.*;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import java.util.Iterator;
import org.alfresco.repo.jscript.People;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.PropertyMap;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException;
import org.apache.chemistry.opencmis.client.util.FileUtils;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
public class Test {
public static void main(String[] args){
Test atest = new Test();
Session session = atest.iniSession();
AuthenticationService authenticationService=new AuthenticationServiceImpl();
PersonService personService = new PersonServiceImpl();
HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>();
properties.put(ContentModel.PROP_USERNAME, "test");
properties.put(ContentModel.PROP_FIRSTNAME, "test");
properties.put(ContentModel.PROP_LASTNAME, "qsdqsd");
properties.put(ContentModel.PROP_EMAIL, "wshAlors#gmail.com");
properties.put(ContentModel.PROP_ENABLED, Boolean.valueOf(true));
properties.put(ContentModel.PROP_ACCOUNT_LOCKED, Boolean.valueOf(false));
personService.createPerson(properties);
authenticationService.createAuthentication("test", "changeme".toCharArray());
authenticationService.setAuthenticationEnabled("test", true);
authenticationService.getAuthenticationEnabled("Admin");
//String testAuthen = authenticationService.getCurrentTicket();
//System.out.println(testAuthen);
//QName username = QName.createQName("test");
//Map<QName,Serializable> propertiesUser = new HashMap<QName,Serializable>();
//propertiesUser.put(ContentModel.PROP_USERNAME,username);
//propertiesUser.put(ContentModel.PROP_FIRSTNAME,"test");
//propertiesUser.put(ContentModel.PROP_LASTNAME,"test");
//propertiesUser.put(ContentModel.PROP_EMAIL, "test#example.com");
//propertiesUser.put(ContentModel.PROP_PASSWORD,"0000");
//personService.createPerson(propertiesUser);
//if (authenticationService.authenticationExists("test") == false)
//{
// authenticationService.createAuthentication("test", "changeMe".toCharArray());
// PropertyMap ppOne = new PropertyMap(4);
// ppOne.put(ContentModel.PROP_USERNAME, "test");
// ppOne.put(ContentModel.PROP_FIRSTNAME, "test");
// ppOne.put(ContentModel.PROP_LASTNAME, "test");
// ppOne.put(ContentModel.PROP_EMAIL, "test#example.com");
//ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
// personService.createPerson(ppOne);
//}
}
public Session iniSession() {
Session session;
SessionFactoryImpl sf = SessionFactoryImpl.newInstance();
Map<String, String> parameters = new HashMap<String, String>();
Scanner reader = new Scanner(System.in);
System.out.println("Enter your logging : ");
String log = reader.nextLine();
System.out.println("Enter your password : ");
String pass = reader.nextLine();
parameters.put(SessionParameter.USER, log);
parameters.put(SessionParameter.PASSWORD, pass);
parameters.put(SessionParameter.BROWSER_URL, "http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser");
parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());
parameters.put(SessionParameter.REPOSITORY_ID, "-default-");
try{
session = sf.createSession(parameters);
}catch(CmisUnauthorizedException cue){
session = null;
System.out.println("Wrong logging OR password !");
}
return session;
}
You are writing a runnable class which is not running the same process as Alfresco. In that sense, your class is running "remotely".
Because your class is running remotely to Alfresco, you are correct in using CMIS. But CMIS will only allow you to perform Create, Read, Update, and Delete (CRUD) functions against documents and folders in Alfresco. CMIS does not know how to create users or groups.
Your class will not be able to instantiate the AuthenticationService or PersonService. Those are part of the Alfresco Foundation API which only works when you are running in the same process as Alfresco, such as in an Action, a Behavior, or a Java-backed web script. In those cases, you will use Spring Dependency Injection to inject those services into your Java class. You would then put your class in a JAR that gets deployed into the Alfresco web application and loaded by the same classloader as Alfresco's.
If you want to create users remotely you should consider using the Alfresco REST API. Your runnable class can then use an HTTP client to invoke REST calls to create people and groups.
Thanks you for everything ! Thanks to you and researches I found out how to do it ! For the others who wonder how to do I'll post how I did and what site I used to understand it !
So you just need to manipulate JSON with Java because your alfresco people page (127.0.0.1:8080/alfresco/service/api/people) returns a JSON object, and you'll be able to create, delete, search... users! Thx again !
Sites :
https://api-explorer.alfresco.com/api-explorer/#/people
http://crunchify.com/json-manipulation-in-java-examples/
The code :
This is for creating an user :
public User createUser(String firstN, String lastN, String email, String pass, String authTicket) throws Exception{
try{
String url = "http://127.0.0.1:8080/alfresco/service/api/people?alf_ticket="+authTicket;
HttpClient httpclient = new HttpClient();
PostMethod mPost = new PostMethod(url);
//JSONObject obj = new JSONObject();
//JSONArray people = obj.getJSONArray("people");
JSONObject newUser = new JSONObject();
newUser.put("userName", firstN.toLowerCase().charAt(0)+lastN.toLowerCase());
newUser.put("enabled",true);
newUser.put("firstName",firstN);
newUser.put("lastName", lastN);
newUser.put("email", email);
newUser.put("quota",-1);
newUser.put("emailFreedDisable",false);
newUser.put("isDeleted",false);
newUser.put("isAdminAuthority",false);
newUser.put("password", pass);
//people.put(newUser);
//Response response = PostRequest(newUser.toString()));
StringRequestEntity requestEntity = new StringRequestEntity(
newUser.toString(),
"application/json",
"UTF-8");
mPost.setRequestEntity(requestEntity);
int statusCode2 = httpclient.executeMethod(mPost);
mPost.releaseConnection();
}catch(Exception e){
System.err.println("[ERROR] "+e);
}
return new User(firstN, lastN);
}
And if you want to get all the users you have on alfresco :
public ArrayList<User> getAllUsers(String authTicket)
{
ArrayList<User> allUsers = new ArrayList<>();
String lastName, firstName;
try{
String url = "http://127.0.0.1:8080/alfresco/service/api/people?alf_ticket="+authTicket;
HttpClient httpclient = new HttpClient();
GetMethod mPost = new GetMethod(url);
int statusCode1 = httpclient.executeMethod(mPost);
System.out.println("statusLine >>> "+statusCode1+"....."
+"\n status line \n"
+mPost.getStatusLine()+"\nbody \n"+mPost.getResponseBodyAsString());
JSONObject obj = new JSONObject(mPost.getResponseBodyAsString());
JSONArray people = obj.getJSONArray("people");
int n = people.length();
for(int i =0 ; i < n ; i++)
{
JSONObject peoples = people.getJSONObject(i);
User u = new User(peoples.getString("firstName"), peoples.getString("lastName"));
if (!allUsers.contains(u)){
allUsers.add(u);
}
}
}catch(Exception e){
System.err.println("[ERROR] "+e);
}
return(allUsers);
}

Create Page In Confluence Via Rest API using Java

Does anyone have any examples on how to create a page/wiki entry in Confluence using Confluence's RESTful API? I'm trying to write something in Java that can do this.
Thank you in advance...
Thank you, I already checked the documentation online but I couldn't find any examples THAT USE JAVA in the Confluence REST API. That's why I posted on here.
Regardless, I think I figured it out:
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* Creates a Confluence wiki page via the RESTul API
* using an HTTP Post command.
*/
public class ConfluenceRestApi2CreateEntry {
//private static final String BASE_URL = "http://localhost:1990/confluence";
private static final String BASE_URL = "https://<context>.atlassian.net/wiki";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static final String ENCODING = "utf-8";
public static String createContentRestUrl()throws UnsupportedEncodingException
{
return String.format("%s/rest/api/content/?&os_authType=basic&os_username=%s&os_password=%s", BASE_URL, URLEncoder.encode(USERNAME, ENCODING), URLEncoder.encode(PASSWORD, ENCODING));
}
public static void main(final String[] args) throws Exception
{
String wikiPageTitle = "My Awesome Page";
String wikiPage = "<h1>Things That Are Awesome</h1><ul><li>Birds</li><li>Mammals</li><li>Decapods</li></ul>";
String wikiSpace = "JOUR";
String labelToAdd = "awesome_stuff";
int parentPageId = 9994250;
JSONObject newPage = defineConfluencePage(wikiPageTitle,
wikiPage,
wikiSpace,
labelToAdd,
parentPageId);
createConfluencePageViaPost(newPage);
}
public static void createConfluencePageViaPost(JSONObject newPage) throws Exception
{
HttpClient client = new DefaultHttpClient();
// Send update request
HttpEntity pageEntity = null;
try
{
//2016-12-18 - StirlingCrow: Left off here. Was finally able to get the post command to work
//I can begin testing adding more data to the value stuff (see above)
HttpPost postPageRequest = new HttpPost(createContentRestUrl());
StringEntity entity = new StringEntity(newPage.toString(), ContentType.APPLICATION_JSON);
postPageRequest.setEntity(entity);
HttpResponse postPageResponse = client.execute(postPageRequest);
pageEntity = postPageResponse.getEntity();
System.out.println("Push Page Request returned " + postPageResponse.getStatusLine().toString());
System.out.println("");
System.out.println(IOUtils.toString(pageEntity.getContent()));
}
finally
{
EntityUtils.consume(pageEntity);
}
}
public static JSONObject defineConfluencePage(String pageTitle,
String wikiEntryText,
String pageSpace,
String label,
int parentPageId) throws JSONException
{
//This would be the command in Python (similar to the example
//in the Confluence example:
//
//curl -u <username>:<password> -X POST -H 'Content-Type: application/json' -d'{
// "type":"page",
// "title":"My Awesome Page",
// "ancestors":[{"id":9994246}],
// "space":{"key":"JOUR"},
// "body":
// {"storage":
// {"value":"<h1>Things That Are Awesome</h1><ul><li>Birds</li><li>Mammals</li><li>Decapods</li></ul>",
// "representation":"storage"}
// },
// "metadata":
// {"labels":[
// {"prefix":"global",
// "name":"journal"},
// {"prefix":"global",
// "name":"awesome_stuff"}
// ]
// }
// }'
// http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool
JSONObject newPage = new JSONObject();
// "type":"page",
// "title":"My Awesome Page"
newPage.put("type","page");
newPage.put("title", pageTitle);
// "ancestors":[{"id":9994246}],
JSONObject parentPage = new JSONObject();
parentPage.put("id",parentPageId);
JSONArray parentPageArray = new JSONArray();
parentPageArray.put(parentPage);
newPage.put("ancestors", parentPageArray);
// "space":{"key":"JOUR"},
JSONObject spaceOb = new JSONObject();
spaceOb.put("key",pageSpace);
newPage.put("space", spaceOb);
// "body":
// {"storage":
// {"value":"<p><h1>Things That Are Awesome</h1><ul><li>Birds</li><li>Mammals</li><li>Decapods</li></ul></p>",
// "representation":"storage"}
// },
JSONObject jsonObjects = new JSONObject();
jsonObjects.put("value", wikiEntryText);
jsonObjects.put("representation","storage");
JSONObject storageObject = new JSONObject();
storageObject.put("storage", jsonObjects);
newPage.put("body", storageObject);
//LABELS
// "metadata":
// {"labels":[
// {"prefix":"global",
// "name":"journal"},
// {"prefix":"global",
// "name":"awesome_stuff"}
// ]
// }
JSONObject prefixJsonObject1 = new JSONObject();
prefixJsonObject1.put("prefix","global");
prefixJsonObject1.put("name","journal");
JSONObject prefixJsonObject2 = new JSONObject();
prefixJsonObject2.put("prefix","global");
prefixJsonObject2.put("name",label);
JSONArray prefixArray = new JSONArray();
prefixArray.put(prefixJsonObject1);
prefixArray.put(prefixJsonObject2);
JSONObject labelsObject = new JSONObject();
labelsObject.put("labels", prefixArray);
newPage.put("metadata",labelsObject);
return newPage;
}
}
Here's a project in GitHub that I created that also has an example of simply pulling wiki page entry using Java:
https://github.com/stirlingcrow/Confluence-AccessRestApiWithJava
What about using the official REST client?
https://mvnrepository.com/artifact/com.atlassian.confluence/confluence-rest-client
I'm not able to find any documentation on how to use it, tho.

500 Error - Uploading An Audio File Using AcrCloud RESTful api

I upload an audio file to an audio & video bucket, called demo, using the AcrCloud RESTful services. I am getting a 500 Internal Server Error. This indicates that my signature is correct (I was getting a 422 when the signature was incorrect). The part that I suspect is incorrect is the construction of the multipart post request
My Code:
import com.xperiel.common.logging.Loggers;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpMediaType;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.MultipartContent;
import com.google.api.client.http.MultipartContent.Part;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.BaseEncoding;
import com.google.common.io.CharStreams;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class TestAcrCloudSignature {
private static final String ACCESS_KEY = "xxxx"; // confidential
private static final String SECRET_KEY = "yyyy"; // confidential
private static final String URL = "https://api.acrcloud.com/v1/audios";
private static HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();
private static final Logger logger = Loggers.getLogger();
public static void main(String [] args) {
String filePath = "/Users/serena/Desktop/ArcCloudMusic/Fernando.m4a";
String httpMethod = HttpMethod.POST.toString();
String httpUri = "/v1/audios";
String signatureVersion = "1";
long timestamp = System.currentTimeMillis();
String stringToSign = getStringToSign(httpMethod, httpUri, signatureVersion, timestamp);
String signature = getSignature(stringToSign);
logger.log(Level.INFO, "Timestamp:\t" + timestamp);
HttpResponse response = null;
try {
ImmutableMap<String, String> params = ImmutableMap.of(
"title", "fernando",
"audio_id", "1",
"bucket_name", "demo",
"data_type", "audio");
byte[] audio = getAudioFileTo(filePath);
String strResponse = sendMultiPartPostRequest(
"",
params,
ImmutableMap.of("audio-file", new Pair<>("Fernando.m4a", audio)),
signatureVersion,
signature,
timestamp);
logger.log(Level.INFO, "RESPONSE:" + strResponse);
} catch (Exception e) {
logger.log(Level.WARNING, "Response: " + response);
logger.log(Level.WARNING, "Exception: " + e.getMessage());
e.printStackTrace();
}
}
private static String getStringToSign(String method, String httpUri, String signatureVersion, long timestamp) {
String stringToSign = method+"\n"+httpUri+"\n"+ACCESS_KEY+"\n"+signatureVersion+"\n"+timestamp;
logger.log(Level.INFO, "String to Sign:\t" + stringToSign);
return stringToSign;
}
private static String getSignature(String stringToSign) {
String signature = BaseEncoding.base64().encode(hmacSha1(stringToSign));
logger.log(Level.INFO, "Signature:\t" + signature);
return signature;
}
private static byte[] hmacSha1(String toSign) {
try {
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(new SecretKeySpec(SECRET_KEY.getBytes(), "HmacSHA1"));
return mac.doFinal(toSign.getBytes());
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException(e);
}
}
private enum HttpMethod {
GET, POST, PUT, DELETE,
}
private static byte[] getAudioFileTo(String filePath){
File file = new File(filePath);
byte[] buffer = null;
try {
InputStream fis = new FileInputStream(file);
buffer = new byte[(int) file.length()];
fis.read(buffer, 0, buffer.length);
fis.close();
} catch (IOException e) {
logger.log(Level.WARNING, "IOException: " + e.getMessage());
}
return buffer;
}
private static String sendMultiPartPostRequest(
String path,
ImmutableMap<String, String> parameters,
ImmutableMap<String, Pair<String, byte[]>> blobData,
String signatureVersion,
String signature,
long timestamp) {
try {
MultipartContent multipartContent = new MultipartContent();
multipartContent.setMediaType(new HttpMediaType("multipart/form-data"));
multipartContent.setBoundary("--------------------------0e94e468d6023641");
for (Entry<String, String> currentParameter : parameters.entrySet()) {
HttpHeaders headers = new HttpHeaders();
headers.clear();
headers.setAcceptEncoding(null);
headers.set("Content-Disposition", "form-data; name=\"" + currentParameter.getKey() + '\"');
HttpContent content = new ByteArrayContent(null, currentParameter.getValue().getBytes());
Part part = new Part(content);
part.setHeaders(headers);
multipartContent.addPart(part);
}
for (Entry<String, Pair<String, byte[]>> current : blobData.entrySet()) {
ByteArrayContent currentContent = new ByteArrayContent("application/octet-stream", current.getValue().second);
HttpHeaders headers = new HttpHeaders();
headers.clear();
headers.setAcceptEncoding(null);
headers.set("Content-Disposition", "form-data; name=\"" + current.getKey() + "\"; filename=\"" + current.getValue().first + '\"');
headers.setContentType("application/octet-stream");
multipartContent.addPart(new Part(headers, currentContent));
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
multipartContent.writeTo(out);
HttpResponse response = requestFactory
.buildPostRequest(new GenericUrl(URL + path), multipartContent)
.setHeaders(new HttpHeaders()
.set("access-key", ACCESS_KEY)
.set("signature-version", signatureVersion)
.set("signature", signature)
.set("timestamp", timestamp))
.execute();
String responseString = CharStreams.toString(new InputStreamReader(response.getContent()));
return responseString;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static class Pair<A, B> {
final A first;
final B second;
Pair(A first, B second) {
this.first = first;
this.second = second;
}
}
}
The error message I am getting from AcrCloud is:
500
{"name":"Internal Server Error","message":"There was an error at the server.","code":0,"status":500}
I am able to upload an audio file using this cUrl command:
Command: $ curl -H "access-key: xxxx" -H "signature-version: 1" -H
"timestamp: 1439958502089" -H "signature:
Nom6oajEzon260F2WzLpK3PE9e0=" -F "title=fernando" -F "audio_id=100" -F
"bucket_name=demo" -F "data_type=audio" -F
"audio_file=#/Users/serena/Desktop/ArcCloudMusic/Fernando.m4a"
https://api.acrcloud.com/v1/audios
Does anyone have any tips on how to debug this? Or has anyone had success using this service programmatically with Java? Or can someone show me how to print the contents of the HttpPOST request?
UPDATE I have also tried using their java example on GITHUB found here:
https://github.com/acrcloud/webapi_example/blob/master/RESTful%20service/UploadAudios.java
I get the same 500 error
UPDATE I no longer get the 500 error when I run their code. I fiddled with the apache jar versions and now I can successfully use the java code found on git hub. For record, The version that I used that work with their github code is apache-http-codec-1.10, apache-http-client-4.5, apache-http-core-4.4.1, apache-http-mime-4.5. When i used apache-http-core-4.5 it did not work.
UPDATE I have written a file that prints out the signatures generated by the java code on github reference above, and my own code. The signatures match so I am convinced that issue in the way I am constructing the multipart post request. I have also written the contents of both post requests to file and the headers contain different information in a few spots.
Thanks Serena for your patience, our team is doing a detailed analysis on the code and the apache jars now. Hopefully will have an update soon.
For now, if anyone who has the same problems, please use the following jars as mentioned in https://github.com/acrcloud/webapi_example/blob/master/RESTful%20service/UploadAudios.java
// import commons-codec-<version>.jar, download from http://commons.apache.org/proper/commons-codec/download_codec.cgi
import org.apache.commons.codec.binary.Base64;
// import HttpClient, download from http://hc.apache.org/downloads.cgi
/**
*
* commons-codec-1.1*.jar
* commons-logging-1.*.jar
* httpclient-4.*.jar
* httpcore-4.4.1.jar
* httpmime-4.*.jar
*
* */

HTTP/1.1 401 SSO Redirection issue in Rally

Am trying to update testcase results in Rally Tool by using the below code:
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.testng.annotations.Test;
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.GetRequest;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.GetResponse;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import com.rallydev.rest.util.Ref;
public class RallyStatusUpdate {
#Test
public void f() throws IOException, URISyntaxException {
// Create and configure a new instance of RallyRestApi
RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"),"user#company.com", "123");
restApi.setWsapiVersion("2.0.1");
restApi.setApplicationName("Add Test Case Result");
//Query User
QueryRequest userRequest = new QueryRequest("user");
userRequest.setFetch(new Fetch("user", "Subscription", "DisplayName"));
userRequest.setQueryFilter(new QueryFilter("user", "=", "user#company.com"));
QueryResponse userQueryResponse = restApi.query(userRequest);
JsonArray userQueryResults = userQueryResponse.getResults();
JsonElement userQueryElement = userQueryResults.get(0);
JsonObject userQueryObject = userQueryElement.getAsJsonObject();
String userRef = userQueryObject.get("_ref").getAsString();
// Query for Test Case to which we want to add results
QueryRequest testCaseRequest = new QueryRequest("TC_001_Automation_Sample_Test");
testCaseRequest.setFetch(new Fetch("FormattedID","TC_001_Automation_Sample_Test"));
testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "TC5238"));
QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);
JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
String testCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString();
try{
//Add a Test Case Result
System.out.println("Creating Test Case Result...");
JsonObject newTestCaseResult = new JsonObject();
newTestCaseResult.addProperty("Verdict", "Pass");
newTestCaseResult.addProperty("Date", "2015-07-16T18:00:00.000Z");
newTestCaseResult.addProperty("Notes", "Automated Selenium Test Runs");
newTestCaseResult.addProperty("Build", "2015.07.16.0020101");
newTestCaseResult.addProperty("Tester", userRef);
newTestCaseResult.addProperty("TestCase", testCaseRef);
CreateRequest createRequest = new CreateRequest("testcaseresult", newTestCaseResult);
CreateResponse createResponse = restApi.create(createRequest);
if(createResponse.wasSuccessful()){
// println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
//Read Test Case
String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Test Case Result %s...", ref));
GetRequest getRequest = new GetRequest(ref);
getRequest.setFetch(new Fetch("Date", "Verdict"));
GetResponse getResponse = restApi.get(getRequest);
JsonObject obj = getResponse.getObject();
//println(String.format("Read Test Case Result. Date = %s, Verdict = %s", obj.get("Date").getAsString(), obj.get("Verdict").getAsString()));
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error occurred creating Test Case: ");
for (int i=0; i<createErrors.length;i++) {
System.out.println(createErrors[i]);
}
}
}
finally{
restApi.close();
}
}
}
however I am getting "HTTP/1.1 401 SSO Redirection !" error.
As SO answers pointed 401 error maybe due to authentication issue, I changed with different logins/environment it did not work.
If your organization uses SSO to access Rally, using an API Key is a good way to connect to CA Agile Central via the Java SDK, without the need to do the SAML handshaking. API Key details are included in this article:
http://www.ca.com/us/services-support/ca-support/ca-support-online/knowledge-base-articles.TEC01000001898.html?intcmp=searchresultclick&resultnum=2
Also, you should set your WSAPI version string to "v2.0" as there's no "2.0.1"

How to access fields of user story's Owner with Java Rest API?

We are using the Java Rest API to export data.
For the HierarchicalRequirement object, how do we access the email address for the Owner?
Owner is a reference to a User object, and this object's fields need to be included in the fetch, for exaple:
storyRequest.setFetch(new Fetch("Name","Owner","UserName", "EmailAddress"));
Here is the full code:
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class aRESTstories {
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://rally1.rallydev.com";
String username = "user#co.com";
String password = "secret";
String projectRef = "/project/2222";
String workspaceRef = "/workspace/1111";
String applicationName = "RESTExampleStoriesChildren";
RallyRestApi restApi = new RallyRestApi(
new URI(host),
username,
password);
restApi.setApplicationName(applicationName);
System.out.println(restApi.getWsapiVersion()); //v.2.0 by default when using 2.0.2 jar
QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement");
storyRequest.setFetch(new Fetch("Name","Owner","UserName", "EmailAddress"));
storyRequest.setLimit(1000);
storyRequest.setScopedDown(false);
storyRequest.setScopedUp(false);
storyRequest.setWorkspace(workspaceRef);
storyRequest.setProject(projectRef);
storyRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "US16"));
QueryResponse storyQueryResponse = restApi.query(storyRequest);
JsonObject storyJsonObject = storyQueryResponse.getResults().get(0).getAsJsonObject();
System.out.println("Name: " + storyJsonObject.get("Name"));
JsonObject userObject = storyJsonObject.get("Owner").getAsJsonObject().getAsJsonObject();
System.out.println(userObject.get("UserName"));
System.out.println(userObject.get("EmailAddress"));
}
}

Categories