Background
In a recent project I did the requirement was to use the REST API to add and update forum topics and replies and to have the option to add an attachment to this topic or reply. The IBM Connections Infocenter only supplies little information on how to achieve this; no working examples. Also in the Connections forum there were only some hints and tips on how to achieve this functionality but no real code examples.
Scope
This article is scoped to the server side code that was used to invoke the REST API in order to upload a file into Connections. The client code needed to select a file and start an upload is out of scope for this article.
Prerequisites to the code
Main package dependencies are:
- httpclient-4.3.3.jar
- httpcore-4.3.2.jar
- httpmime-4.3.3.jar
The code
String url = "protocol://hostname:port/forums/ajax/createTopic";
String boundary = "somedashes" + Long.toHexString(System.currentTimeMillis());
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "multipart/form-data; boundary="+boundary);
## Get the cookies from the request and convert these to a string
httpPost.addHeader("Cookie", "All cookies from request as string, or ltpa cookie as string"));
## always add the nonce key
httpPost.addHeader("X-Update-Nonce", “nonce key”);
MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create().setBoundary(boundary);
mpBuilder.addTextBody("communityId", "unique id of the community", ContentType.MULTIPART_FORM_DATA);
## parentId = forum id
mpBuilder.addTextBody("parentId", "unique id of the forum", ContentType.MULTIPART_FORM_DATA);
mpBuilder.addTextBody("name", "Topic title", ContentType.MULTIPART_FORM_DATA);
mpBuilder.addTextBody("description", "Forum description", ContentType.MULTIPART_FORM_DATA);
## choose whether to mark the topic as a question
mpBuilder.addTextBody("markAsQuestion", "false", ContentType.MULTIPART_FORM_DATA);
mpBuilder.addTextBody("tags", "space seperated string", ContentType.MULTIPART_FORM_DATA);
## attachment is an inputstream containing the attached file
mpBuilder.addBinaryBody("attachments", attachment, ContentType.APPLICATION_OCTET_STREAM, "attachment name");
httpPost.setEntity(mpBuilder.build());
try {
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
## the response contains the new created topic
String response = EntityUtils.toString(entity);
} finally {
httpResponse.close();
}
} catch (Exception e) {
exception handling
} finally {
try {
httpClient.close();
} catch (Exception e2) {
}
}
Additions to the code
LTPA token is used for authentication but you can of course also use Basic Authentication.
In the code a header is created to hold the so called nonce key. This key is needed as an extra check to validate the upload. A detailed explanation of the nonce key and how to retrieve it can be found by clicking on the URL below:
https://www-10.lotus.com/ldd/lcwiki.nsf/xpAPIViewer.xsp?lookupName=IBM+Connections+5.0+API+Documentation#action=openDocument&res_title=Getting_a_cryptographic_key_ic50&content=apicontent
In order not to store the uploaded file in the JVM which can cause memory issues, it is best to use an input stream for the file upload.