Previous |
Next
This section demonstrates how to use the IBM Connections APIs in different programming languages to perform the task of adding a bookmark. IBM Connections makes available all of its functionality in an easy to use REST-style API. Therefore, understanding how the API works in one module can be applied to all of the modules. Keeping this in mind, this topic focuses on creating a bookmark in the Bookmarks application using application code that is written in multiple languages.
The REST-style API that IBM Connections provides receives an HTTP request and returns an Atom document. Therefore, creating a bookmark using the API means assembling the correct HTTP request. For more details about how to assemble the Atom API request URL, refer to the
IBM Connections 3.0.1 Product Documentation
.
Note that the IBM Connections Bookmarks application was called Dogear in previous versions. However, the Boomarks API still references the term
dogear in the API urls.
An API call is simply an HTTP call using a specific format.The following code snippet provides a sample Atom document that creates an entry in the Bookmarks application:
POST /dogear/api/app?email=user@company HTTP/1.1
Host: www.company.com
Content-Type: application/atom+xml
Authorization: Basic RG9uIFA1aXhvdAAAAm9jaW5hAAR1
<?xml version="1.0" encoding="utf-8">
<entry xmlns="http://www.w3.org/2005/Atom">
<author><name>Author</name></author>
<title>IBM Connections wiki</title>
<content type="html"><![CDATA[IBM Connections wiki>]]>
</content>
<category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="bookmark" />
<category term="wiki" />
<category term="Connections" />
<link href="http://www-10.lotus.com/ldd/lcwiki.nsf" />
</entry>
The next example creates a bookmark to the IBM Connections wiki and tags it with the terms Connections and wiki. In addition, the title is set to "IBM Connections wiki" and description to "IBM Connections wiki bookmarked from Java".
Note: We use Apache Abdera in some of these code samples. Apache Abdera is an open source implementation of the Atom Syndication Format and Atom Publishing Protocol. Abdera provides a Java development toolkit that is used to quickly develop Atom clients. For more details about the Apache Abdera project, refer to the
Apache Abdera project site
.
Using API in Java (Core Java):
// Create a secure connection to the server
SocketFactory factory = SSLSocketFactory.getDefault();
Socket socket = factory.createSocket("servername", 443);
OutputStreamWriter out=new OutputStreamWriter(socket.getOutputStream(), "UTF8");
// Create the request and encode the username and password
out.write("POST /dogear/api/app?email=userEmail HTTP/1.1\r\n");
out.write("HOST: servername\r\n");
String encoding = new String(Base64.encodeBase64("user:pass".getBytes()));
out.write("Authorization: Basic " + encoding + "\r\n");
String data = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<entry xmlns=\"http://www.w3.org/2005/Atom\">" +
"<title>IBM Connections wiki</title>" +
"<content type=\"html\"><![CDATA[IBM Connections wiki bookmarked from Java]]></content>" +
"<category scheme=\"http://www.ibm.com/xmlns/prod/sn/type\" term=\"bookmark\" />" +
"<category term=\"wiki\" />" +
"<category term=\"Connections\" />" +
"<link href=\"http://www-10.lotus.com/ldd/lcwiki.nsf\" />" +
"</entry>";
out.write("Content-Length: " + data.length() + "\r\n");
out.write("Content-Type: application/atom+xml\r\n");
out.write("\r\n");
out.write(data);
out.flush();
// Process the response
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
Using API in Java using helper classes (Abdera):
Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);
client.addCredentials("https://servername", null, null, new UsernamePasswordCredentials("user","password"));
ClientResponse resp = client.get("https://servername/dogear/api/app");
Document<Service> service_doc = resp.getDocument();
Service service = service_doc.getRoot();
Collection collection = service.getCollection("My Bookmarks", "Entries");
String coll_uri = collection.getResolvedHref().toASCIIString();
Entry entry = abdera.newEntry();
entry.addLink("http://www-10.lotus.com/ldd/lcwiki.nsf"); // URL being bookmarked
entry.setTitle("IBM Connections Product Documentation");
entry.setContentAsHtml("IBM Connections wiki bookmarked from Java");
entry.addCategory("wiki");
entry.addCategory("Connections");
entry.addCategory("http://www.ibm.com/xmlns/prod/sn/type","bookmark",null);
// Mark private
entry.addCategory("http://www.ibm.com/xmlns/prod/sn/flags","private",null);
resp = client.post(coll_uri, entry);
switch(resp.getType()) {
case SUCCESS:
String location = resp.getLocation().toASCIIString();
System.out.println("New entry created at: " + location);
break;
default:
System.out.println("Error: " + resp.getStatusText());
}
Using API in Perl:
request LWP::UserAgent
use LWP:Debug qw(+ -conns);
my $user = "user@company.com";
my $pw = "password";
my $server = "dogear.tap.ibm.com";
my $url = "http://".$server."/api/app";
my $port = "443";
# Create a user object
my $ua = LWP::UserAgent->new;
$ua->agent("MyBookmark/0.1");
$ua->credentials($server.':'.$port,'Dogear',$user,$pw);
push @{$ua->requests_redirectableA},'POST';
# Create a request
my $req = HTTP:Request->new(POST => $url);
use XML:Generator ':pretty';
# Build XML document
my $gen = XML:Generator->new(':pretty',namespace => ["http://www.w3.org/2005/atom"]);
$content = sprintf$gen=>xml($gen->entry($gen->author("Author"),
$gen->title("IBM Connections wiki"),
$gen->content({type=>'html'},"IBM Connections wiki bookmared from Perl.");
$gen->category({scheme=>"http://www.ibm.com/xmlns/prod/sn/type",term=>"bookmark"}),
$gen->category({term=>"wiki"});
$gen->category({term=>"Connections"});
$gen->link({href=>"http://www-10.lotus.com/ldd/lcwiki.nsf"})
)
);
# Set request content
$req->content_type('application/atom+xml');
$req->content($content);
# Pass request to the user agent and get a response
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_sucess) {
print "Bookmark posted.\n";
}
else {
print $res->status_line, "\n";
}
Using API in VBScript
url = "https://dogear.tap.ibm.com/api/app"
entry = "<?xml version="1.0" encoding="utf-8">" &_
"<entry xmlns="http://www.w3.org/2005/Atom">" &_
"<author><name>Author</name></author>" &_
"<title>IBM Connections wiki</title>" &_
"<content type="html"><![CDATA[IBM Connections wiki bookmarked from VBScript>]]>" &_
"</content>" &_
"<category scheme="http://www.ibm.com/xmlns/prod/sn/type" term="bookmark" />" &_
"<category term="wiki" />" &_
"<category term="Connections" />" &_
"<link href="http://www-10.lotus.com/ldd/lcwiki.nsf" />" &_
"</entry>"
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.open "POST", url, False, "johndoe@ibm.com", "password")
objHTTP.setRequestHeader "Content-Type", "application/atom+xml"
objHTTP.send entry
Document.Write objHTTP.statusText
Set objHTTP = Nothing
Parent topic:
9.0 Using the API