Previous |
Next
One of the most commonly used services that IBM Connections API provides is retrieving a person's profile. This topic describes a sample application that retrieves a person's profile using the Profiles API.
The REST-style API that IBM Connections provides receives an HTTP request and returns an Atom document. Therefore, to retrieve a person's information, the correct HTTP request URL must be constructed. For example, the following URL returns an Atom document that contains the profile information of person with the e-mail address of PBrown@demoibm.com:
http://connections.demoibm.com/profiles/atom/profile.do?email=PBrown@demoibm.com
For more details about how to assemble the Atom API request URL, refer to
IBM Connections Product Documentation
. Sending the Profiles API request through a web browser or other client that can send HTTP requests, a resulting Atom document is returned.
The following example shows a Profiles Atom document:
Using this Atom document, profile information can be extracted and processed. So, the next step is to parse the Atom document to get the profiles attributes. Since the profile Atom document is an XML document, there are many ways to parse the document.
Note: In the remaining sections of this topic, the Apache Abdera library is used in the sample code. Apache Abdera is an open source implementation of the Atom Syndication Format and Atom Publishing Protocol. It provides a Java development toolkit that is used to help develop Atom clients. For more details about the Apache Abdera project, refer to the
Apache Abdera project site
The following code example shows how to retrieve a person's profile and extract attributes using traditional XML DOM processing:
Abdera abdera = new Abdera();
Parser parser = abdera.getParser();
URL api_url = new URL("http://connections.demoibm.com/profiles/atom/profile.do?email=PBrown@demoibm.com");
Document<Element> document = parser.parse(api_url.openStream());
Feed feed = (Feed)document.getRoot();
// Navigating the content element to get the profile of the person
List<Entry> entries = feed.getEntries();
Entry entry = entries.get(0);
// Name
System.out.println("Name: " + entry.getTitle());
Element content = entry.getContentElement();
List<Element> divElements = content.getFirstChild().getFirstChild().getElements();
for (Element divElement : divElements) {
if ("org".equalsIgnoreCase(divElement.getAttributeValue("class"))) {
List<Element> spanElements = divElement.getElements();
for (Element spanElement : spanElements) {
// Organization
if ("organization-unit".equalsIgnoreCase(spanElement.getAttributeValue("class"))) {
System.out.println("Organization: " + spanElement.getText());
}
}
}
// Title
if ("title".equalsIgnoreCase(divElement.getAttributeValue("class"))) {
System.out.println("Title: " + divElement.getText());
}
if ("tel".equalsIgnoreCase(divElement.getAttributeValue("class"))) {
List<Element> spanElements = divElement.getElements();
for (Element spanElement : spanElements) {
// Telephone number
if ("value".equalsIgnoreCase(spanElement.getAttributeValue("class"))) {
System.out.println("Telephone: " + spanElement.getText());
}
}
}
}
The following code example shows how to retrieve a person's profile and extract attributes using XPath:
Abdera abdera = new Abdera();
Parser parser = abdera.getParser();
URL api_url = new URL("http://connections.demoibm.com/profiles/atom/profile.do?email=PBrown@demoibm.com");
Document<Element> document = parser.parse(api_url.openStream());
Feed feed = (Feed)document.getRoot();
List<Entry> entries = feed.getEntries();
Entry entry = entries.get(0);
Element content = entry.getContentElement();
// Using XPath functions to get the profile of the person
XPath xpath = abdera.getXPath();
System.out.println("Name: " + xpath.valueOf("a:entry/a:title", feed));
System.out.println("Organization: " + xpath.valueOf("//xhtml:div[@class='org']/xhtml:span[@class='organization-unit']", feed));
System.out.println("Title: " + xpath.valueOf("//xhtml:div[@class='title']", feed));
System.out.println("Telephone: " + xpath.valueOf("//xhtml:div[@class='tel']/xhtml:span[@class='value']", content));
Parent topic:
9.0 Using the API