Treat Collaboration Server as a REST-based API

The IDK methods for Collaboration Server are terribly sparse – you can’t get calendar events, file sizes, or a whole bunch of other critical data that you may want if you were to actually embark on a mission to write a better UI for Collab (trust me, I have).  Sure you could try and use the woefully undocumented Collab API – I’ve shown you how to deploy the portal API in the past – but that’s a challenge in and of itself.

Instead, let’s look at an alternate approach:  use the Collab Server as a sort of REST API.  It’s not really, but the basic idea is that you use URLs in your code to directly call functionality in Collaboration Server to do certain tasks.  For example, say you want to add a Collaboration project to a page programmatically; there is no mechanism to do this through the IDK, and we have no idea how to use the API, but using a header tool, we find that through Project Explorer, it works with a simple URL: /collab/do/project/selector/add?commPage=true&projID=COLLABID.

So, it turns out we can do the same thing programmatically, by using Java’s network libraries to call that URL directly (setting the proper authenticationid).  The code after the jump shows an example of how to do this; we use this approach in Integryst’s Automater, which allows you to script a bunch of actions at a time (what good is automatically creating a collab project if you can’t add it to a community page you just created!?). 

Tweak away!

int collabID = ParamHelper.getIntParamValue(getInputs(),"collabid",-1);
int communityID = ParamHelper.getIntParamValue(getInputs(),"communityid",-1);
String wsapi = ParamHelper.getParamValue(getInputs(),"wsapi");

String response = "";"> Add Collab Project to Community. Params [collabID=" + collabID + ", communityID=" + communityID + "]");

// the IDK doesn't provide methods for attaching messages to documents, so we use HTTP methods to do that
// collab settings for direct connection
String host = ParamHelper.getParamValue(getInputs(),"");
String port = ParamHelper.getParamValue(getInputs(),"collab.port");
String user = ParamHelper.getParamValue(getInputs(),"collab.user");
String pass= ParamHelper.getParamValue(getInputs(),"collab.pass");

// these may not be needed...
String imageserver = ParamHelper.getParamValue(getInputs(),"collab.imageserverurl");
String gadgetid = ParamHelper.getParamValue(getInputs(),"collab.gadgetid");
String pageid = ParamHelper.getParamValue(getInputs(),"collab.pageid");
String userid = ParamHelper.getParamValue(getInputs(),"collab.userid");
String username = ParamHelper.getParamValue(getInputs(),"collab.username");

String addr = "http://" + host + ":" + port + "/collab/do/project/selector/add?commPage=true&projID=" + collabID;

LOG.debug("Attempting Collab Connection to " + addr);
URL url = new URL(addr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
String encoding = new sun.misc.BASE64Encoder().encode((user + ":" + pass).getBytes());
conn.setRequestProperty ("Authorization", "Basic " + encoding);
conn.setRequestProperty ("CSP-Aggregation-Mode", "Multiple");
conn.setRequestProperty ("CSP-Activity-Rights", "uri%u003A%u002F%u002Furi%u002Ecollaboration %u002Eplumtree%u002Ecom%u002Factivity%u002Fviewpresence,uri%u003A%u002F %u002Furi%u002Ecollaboration %u002Eplumtree%u002Ecom %u002Factivity%u002Fbulkupload,uri%u003A %u002F%u002Furi%u002Ecollaboration%u002Eplumtree%u002Ecom%u002Factivity %u002Fmanagecollab,uri%u003A%u002F%u002Furi %u002Ecollaboration %u002Eplumtree%u002Ecom%u002Factivity %u002Fmanagecollabprojects");
conn.setRequestProperty ("CSP-Gateway-Specific-Config",
"PT-User-Name=" + username + "," +
"PT-User-ID=" + userid + "," +
"PT-Community-ID=" + communityID + "," +
"PT-Gadget-ID=" + gadgetid + "," +
"PT-Gateway-Version=2.5," +
"PT-Content-Mode=1," +
"PT-Time-Zone=America%u002FNew%u005FYork," +
"PT-Imageserver-URI=" + imageserver + "/," +
"PT-User-Charset=UTF-8," +
"PT-Page-ID=" + pageid + "," +
"PT-Community-ACL=15," +
"PT-SOAP-API-URI=" + wsapi + "," +
"PT-Class-ID=43," +

conn.setRequestProperty ("USER-AGENT", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)");
conn.setRequestProperty ("CSP-Gateway-Type", "Plumtree");
conn.setRequestProperty ("CSP-User-Info", "FullName=" + username);
conn.setRequestProperty ("CSP-Protocol-Version", "1.3");
conn.setRequestProperty ("Accept-Language", "en-us");
conn.setRequestProperty ("CSP-Can-Set", "Gadget-User,User,Gadget-Realm,Realm");
conn.setRequestProperty ("Host", host + ":" + port);
conn.setRequestProperty ("ACCEPT", "*/*");
conn.setRequestProperty ("Accept-Encoding", "gzip");


// read the headers
for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
String headerValue = conn.getHeaderField(i);

if (headerName == null && headerValue == null) {
// No more headers
if (headerName == null)
LOG.debug("Response: " + headerValue);
LOG.debug("Response Header: " + headerName + ": " + headerValue);

InputStream in = conn.getInputStream();

// read the response
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String text = reader.readLine();
if (text != null)
LOG.debug("Response Text: " + text);

response += "Project " + collabID + " has been added to community : " + communityID + "

catch(Exception ex) {
response = "Exception adding project " + collabID + " to community " + communityID + ": " + ex.getMessage();
LOG.error(response, ex);

addOutput(new Param("collabid", "" + collabID, "output", null));
addOutput(new Param("communityid", "" + communityID, "output", null));"< Add Collab Project to Community: " + response);
return response;

Tags: , ,

One Response to “Treat Collaboration Server as a REST-based API”

  1. Omid says:

    Awesome idea! I wish actually that they had gotten a chance to build out a better REST API.

    The WebCenter team has been adding more rest functionality to the WebCenter framework and services. If you have seen the iPhone app in the iTunes market that is a great example of what you can build using just the REST APIs.


    ps. You should put that “conn.disconnect()” in a finally statement if you are using that in a long running process 🙂

Leave a Reply