There are two ways you can access the user info in portlets,
1. Using PUMA API provided by websphere portal
2. By stetting the user attributes in portlet.xml
Approach 1:
a Sample code to access the PUMA through portlet service
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.um.*;
import com.ibm.portal.um.exceptions.PumaException;
import com.ibm.portal.puma.User;
Context ctx=null;
Object homeObj=null;
User user=null;
try {
ctx = new InitialContext();
homeObj=ctx.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");
PortletServiceHome serviceHome=(PortletServiceHome)homeObj;
PumaHome pHome =(PumaHome) serviceHome.getPortletService(PumaHome.class);
PumaProfile pProf=pHome.getProfile();
user=(User)pProf.getCurrentUser();
} catch (NamingException e) {
} catch (PumaException e) {
}
request.setAttribute("user", user.getObjectID());
b Accessing user attributes
//after you got the pumaHome like above
List attributeList=new ArrayList();
attributeList.add(“sn”);
attributeList.add(“givenName”);
attributeList.add(“uid”);
attributeList.add(“preferredLanguage”);
UserProfile profile=pumaHome.getProfile(portletRequest);
User user=profile.getCurrentUser();
Map attributesMap=profile.getAttributes(user,attributeList);
PrintWriter out=response.getWriter();
out.write(“Distinguished name is”+profile.getUserIdentifier(user));
for(Iterator itr=attributesMap.getKeySet().Iterator(); itr.hasNext();){
String attributeName=(String)itr.next();
String attributeValue=(String)attributesMap.get(attributeName);
}
c Using PUMA Locator
i To find user by attributes
PumaLocator locator= pumaHome.getLocator(portletRequest);
PumaProfile profile= pumaHome.getProfile(portletRequest);
List userList=locator.findUserByAttribute(“uid”, “sivavaka”);
User user=(User)userList[0];
Map attributesMap=profile.getAttributes(user,attributeList);//use above attributeList
ii To find user by distinguished name
PumaLocator locator= pumaHome.getLocator(portletRequest);
PumaProfile profile= pumaHome.getProfile(portletRequest);
List userList=locator.findUserByIdentifier(“uid=wasadmin,o=default”);
Approach 2:
Portal can access USER INFO in jsr168 portlet
a Add following elements in the portlet.xml
<user-attribute>
<description xml:lang="en">User Given Name</description>
<name>user.name.given</name>
</user-attribute>
<user-attribute>
<description xml:lang="en">User Last name</description>
<name>user.name.family</name>
</user-attribute>
b Write the following in portlet
Map userInfo=(Map)request.getAttribute(PortletRequest.USER_INFO);
if(userInfo != null){
String givenName = (String)userInfo.get("user.name.given");
String lastName =(String)userInfo.get("user.name.family");
response.getWriter().println("Hello " + givenName +" " + lastName);
}
Hi Shiva.. I'm getting the below error. Can you please help.
ReplyDeletecannot access com.ibm.portal.Identifiable
[ERROR] class file for com.ibm.portal.Identifiable not found
Looks the error u mentioned is compile time error and this can happen if you dont proper server runtime added in your class path.
Deleteif not provide little more details
Siva do you need to have all the LDAP attributes mapped in WAS in order to get the attributes through PUMA
ReplyDeleteits not mandatory , if puma doesn't find the attribute there will additional ldap call happens (we map the attributes to avoid this call)
ReplyDeleteAwesome
ReplyDeleteAwesome
ReplyDeleteSiva, any article that shows how to map the attributes in VMM ...
ReplyDelete