Client profile information (CC/PP) in portlets

For each incoming request, the portal tries to determine the client that issued the request. It is important to determine the client in order to know its capabilities and respond properly. The portal database contains a repository of client profiles, providing all relevant information about the supported clients. The Portal uses JSR 188, which provides a standard API named "CC/PP" for accessing client profiles. The Java Portlet Specification allows portlets to access the client profile through a request attribute. The Getting Started With Composite Capabilities/Preference Profiles and JSR 188 has more information about CC/PP

I wanted to learn how the CC/PP works so i built this sample portlet, which queries list of all the profile attributes and then prints the attributes and there values in table format like this. You can download the sample code from here



This is source code of the portlet


package com.webspherenotes.misc;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.ccpp.Attribute;
import javax.ccpp.Component;
import javax.ccpp.Profile;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public class CPIPortlet extends GenericPortlet{
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering CPIPortlet.doView()");
response.setContentType("text/html");
Profile clientProfile =
(Profile) request.getAttribute(PortletRequest.CCPP_PROFILE);
System.out.println("Profile Object " + clientProfile);
HashMap attributeMap = new HashMap();
if(clientProfile != null){
processProfile(clientProfile );
Iterator attributeIt = clientProfile.getAttributes().iterator();
while(attributeIt.hasNext()){
Attribute attribute = attributeIt.next();
if(attribute != null){

System.out.println(attribute.getName() + " = " + attribute.getValue());
attributeMap.put(attribute.getName(), attribute.getValue());
}
}
}
System.out.println("Attribute Map " + attributeMap);
request.setAttribute("clientProfileAttributeMap", attributeMap);
getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Entering CPIPortlet.doView()");
}
public void processProfile(Profile profile) {
System.out.println("Entering processProfile");
Set comps = profile.getComponents();
for(Iterator i = comps.iterator(); i.hasNext(); ) {
Component comp = (Component) i.next();
System.out.println("Component: " + comp.getName());
Set attrs = comp.getAttributes();
for(Iterator j = attrs.iterator(); j.hasNext(); ) {
Attribute attr = (Attribute) j.next();
Object value = attr.getValue();
System.out.println("\tAttribute: " + attr.getName() +
" = " + attr.getValue());
}
}
System.out.println("Entering processProfile");
}
}


In the doView() method of the CPIPorltet i am reading the PortletRequest.CCPP_PROFILE attribute from the PortletRequest and then i am using the Profile attribute to first find out list of the attributes and then generating table of this data using JSP

No comments: