Accessing User Information from JSR-168 compliant portlet

If you want to access users information such as first name and last name which is normally stored in coroporate directory in JSR-168 compliant way then use these steps
First add these entries in

<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>

After that you should be able to access those two attributes in your java code by using code like this

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

At deployment time the portal can map the requested user profile attributes to the supported profile attributes or the portal can ignore the requested attributes.

No comments: