Sample: Retrieving names from the Profiles directory 

PeopleLike.java is a Java™ program that displays all the people in the Connections Profiles directory whose name matches the given input. It demonstrates how to read an Atom feed that does not require authentication from the Profiles server, and how to process the returned content using XPath.


Requirements

To run the PeopleLike.java program, have the following software and information:


Installing the sample

To install the sample...

  1. Download the PeopleLike.java file by right-clicking the following link.

      PeopleLike.java

      Save the link to a local directory, such as c:\samples. Name the file, PeopleLike.java.

      Note: The file name is case-sensitive.

  2. Compile the program. For example, from a command prompt, enter the following command:

      C:\sample>\jdk150\bin\javac PeopleLike.java

Running the sample

To run the sample, use the following command:

java PeopleLike <server-address> <input-name>


Example

The following example queries the Profiles directory for users with the name, daryn.

C:\sample>java PeopleLike connections.mycompany.com/profiles daryn
Contacting: http://connections.mycompany.com/profiles/atom/search.do?name=daryn
Response:   HTTP/1.1 200 OK
 
profiles where name=daryn
Showing first 1 names of 1 found:
Samantha Daryn


Source

The following Java code is the source of the PeopleLike.java program:

/**
 * Sample client for IBM Connections 1.0.2 Profiles Applications Programming 
 * Interface
 * 
 * Displays all people in the Profiles directory whose first name or last name  
 * starts with the input string given.
 * 
 * Shows how to read an Atom feed that does not require authentication from the 
 * Profiles server, and process the contents using XPath.
 * 
 * Sample invocation:
 * 
 * java PeopleLike connections.mycompany.com/profiles Daryn
 * 
 */
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
 
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
 
public class PeopleLike {
 
    public static String path = "atom/search.do";
    public static String param = "?name=";
    
    /**
     * main -- for command line invocation
     * 
     * @param args server-address name-to-search
     */
    public static void main(String [] args) {
 
        if (args.length == 0) {
            System.out.println("Missing Profiles server address");
            return;
        }
        if (args.length == 1) {
            System.out.println("Missing name to search for");
            return;
        }
        
        try {
            String server = args [0];
            if (!server.endsWith("/")) server += "/";
            server = "http://"+ server + path + param + \
             URLEncoder.encode(args [1], "UTF-8");
            System.out.println("Contacting: "+ server);
            URL profiles_url = new URL(server);
            
            // Open the URL: throws exception if not found
            HttpURLConnection profiles_conn = \
             (HttpURLConnection)profiles_url.openConnection();
            profiles_conn.connect();
            System.out.println("Response: HTTP/1.1 "_
            +profiles_conn.getResponseCode()+" "+profiles_conn.getResponseMessage());
 
            // Process the Atom feed in the response content
            readResponse(profiles_url.openStream(), args [0]);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * Use XPath to process the Profiles Atom feed document
     * 
     * @param is
     * @param base
     */
    static void readResponse(InputStream is, String base ) {
        try {
            DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
            docbf.setNamespaceAware(true);
            DocumentBuilder docbuilder = docbf.newDocumentBuilder();            
            Document feed = docbuilder.parse(is, base);
            
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            PeopleLike pl = new PeopleLike();
            xpath.setNamespaceContext(pl.new AtomNamespaceContext());
            
            // Count the Atom Entries in the feed
            int count = Integer.parseInt(xpath.evaluate("count(//a:entry)", feed));
            // Opensearch extension tells us how many profiles are in this feed 
                // and any subsequent feed pages
            int totalResults = \
             Integer.parseInt(xpath.evaluate("/a:feed/os:totalResults/text()",feed));
            // Title of the feed
            System.out.println("\n"+ xpath.evaluate("/a:feed/a:title/text()", feed));
            System.out.println("Showing first "+ count +" names of "\
             + totalResults +" found:");
            
            // Title of each Profile Entry is the person's name
            Object result = \
                 xpath.evaluate("//a:entry/a:title/text()", feed, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println(nodes.item(i).getNodeValue()); 
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * Defines XML Namespace contexts used by IBM Connections Profiles
     */
    public class AtomNamespaceContext implements NamespaceContext {
 
        public String getNamespaceURI(String prefix) {
            if (prefix == null) throw new NullPointerException("Null prefix");
            else if ("a".equals(prefix)) return "http://www.w3.org/2005/Atom";
            else if ("app".equals(prefix)) return "http://www.w3.org/2007/app";
            else if ("os".equals(prefix)) return "http://a9.com/-/spec/opensearch/1.1/";
            else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;
            return XMLConstants.NULL_NS_URI;
        }
 
        // This method isn't necessary for XPath processing.
        public String getPrefix(String uri) {
            throw new UnsupportedOperationException();
        }
 
        // This method isn't necessary for XPath processing either.
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    }
    
}


Parent topic

Samples

+

Search Tips   |   Advanced Search