Adding comments using OSLC
Accepted answer
private void updateComments(List<String> comments, final ChangeRequest changeRequest) throws IOException {
for (final String comment : comments) {
ContentProducer cp = new ContentProducer() {
public void writeTo(OutputStream outstream) throws IOException {
Writer out = new OutputStreamWriter(outstream, HTTP.UTF_8);
out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
out.append("<rdf:RDF\n");
out.append("\t\txmlns:rtc_ext=\"http://jazz.net/xmlns/prod/jazz/rtc/ext/1.0/\"\n");
out.append("\t\txmlns:rtc_cm=\"http://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/\"\n");
out.append("\t\txmlns:oslc_cm=\"http://open-services.net/ns/cm#\"\n");
out.append("\t\txmlns:dcterms=\"http://purl.org/dc/terms/\"\n");
out.append("\t\txmlns:oslc_cmx=\"http://open-services.net/ns/cm-x#\"\n");
out.append("\t\txmlns:oslc=\"http://open-services.net/ns/core#\"\n");
out.append("\t\txmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n");
out.append("\t<rdf:Description \n");
out.append("\n\t\trdf:about=\"" + changeRequest.getAbout() + "\">\n");
out.append("\t<dcterms:description><![CDATA[" + comment + "]]> </dcterms:description>");
out.append("\t</rdf:Description>\n");
out.append("</rdf:RDF>\n");
out.flush();
}
};
HttpEntity entity = new EntityTemplate(cp);
HttpPost post = new HttpPost(changeRequest.getDiscussedBy() + "/oslc:comment");
post.addHeader("Accept", "application/rdf+xml");
post.addHeader("Content-type", "application/rdf+xml");
post.addHeader("OSLC-Core-Version", "2.0");
post.setEntity(entity);
// Call the PUT method against the Change Request URI
HttpResponse response = client.getHttpClient().execute(post);
response.getEntity().consumeContent();
}
}
Comments
7 other answers
Comments
1 vote
2 votes
After many hours, I finally looked at the ccm.log file. The response was saying my account did not have the required group memberships. The ccm.log clarified that my account did have the necessary group memberships, but that the request was blocked because it might have been malicious and said I had to use the JSESSION ID in a CRSF Prevent header. My tool was not retrieving any such JSESSIONID in the header, so I found you could get around the check by specifying User-Agent. HERE is how I got it to work in 7.0.2:
I've been following the link that Donald Nong provide: https://jazz.net/wiki/bin/view/Main/WorkItemAPIsForOSLCCM20#Adding_comments
I just appended "/oslc:comment" at the end according to the documentation.
Adding comments through the OSLC interface can be done like this:
/post one single comment to one work item@params {string} discussedByUri: a URI pointing to a work item discussion,can be taken e.g. from a 'oslc:discussedBy' field.@params {string} comment: a string that will be added to the work item comments section@returns {promise} a promise that indicates whether the creation was successful or not*/postComment: function(discussedByUri, comment) {if(typeof discussedByUri === "string" && discussedByUri.endsWith("/rtc_cm:comments") &&typeof comment === "string") {var commentObject = {"dcterms:description": comment};return XHR.oslcJsonPostRequest(discussedByUri + "/oslc:comment", commentObject);}},
oslcJsonPostRequest: function(url, data) {return this.xhr("POST", {url: url,headers: {"OSLC-Core-Version": "2.0","Accept": "application/json","Content-Type": "application/json"},postData: JSON.stringify(data),handleAs: "json"}, false);}
Hi Lucas,
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://[ServerName]/oslc/workitems/_q8RqoJoQEee2N-g-o1qd6A/rtc_cm:comments/oslc:comment" );request.Method = "POST";request.AllowAutoRedirect = true;request.KeepAlive = true;request.Timeout = 480000;request.Credentials = context.Credential;request.Headers.Add(context.AuthHeader);request.Headers.Add("OSLC-Core-Version", "2.0");request.Accept = "application/json";request.ContentType = "application/json";request.CookieContainer = context.Cookie;using (var streamWriter = new StreamWriter(request.GetRequestStream())){streamWriter.Write(json);streamWriter.Flush();}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Is possible to set the creation date? I'm setting it, but its not having effect. Comment on work item is being created, but not setting the date I sent:
For those reading here in 2021...I've still 6.0.2 and it works in this way: