ibm websphere portal blog,websphere portal, portal blog, ibm blog,websphere blog,websphere tips and tricks, websphere interview questions, portal interview questions, portal faqs

How to acceass Users profile Info in Portlets i.e.PUMA API | IBM WebSphere Portal Blog

Pages

Sunday, October 30, 2016

How to acceass Users profile Info in Portlets i.e.PUMA API


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

UseCase1:
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”);


UseCase 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);
}