Sample PUMA COde

If you want to use PUMA in IBM WebSphere Portal Server Version 6.0 then you might be interested in this sample code

public class PUMAPortlet extends javax.portlet.GenericPortlet {
private PumaHome pumaHome = null;
public void init() throws PortletException{
super.init();
PortletServiceHome portletServiceHome =null;

try {
Context context = new InitialContext();
portletServiceHome = (PortletServiceHome)
context.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");
if(portletServiceHome != null){
pumaHome =(PumaHome)portletServiceHome.getPortletService(PumaHome.class);
}
} catch (NamingException e) {
e.printStackTrace();
}
}


public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
response.setContentType(request.getResponseContentType());
demoCurrentUser(request, response);
demoFindingUserByAttribute(request, response);
demoFindUserByDn(request,response);
}

public void demoCurrentUser(RenderRequest portletRequest, RenderResponse portletResponse){

try {
PumaProfile pumaProfile = pumaHome.getProfile(portletRequest);
User user = pumaProfile.getCurrentUser();
List attributeList = new ArrayList();
attributeList.add("sn");
attributeList.add("givenName");
attributeList.add("uid");
attributeList.add("preferredLanguage");

Map userAttributeMap = pumaProfile.getAttributes(user, attributeList);
PrintWriter out = portletResponse.getWriter();

out.println("DN of current User " + pumaProfile.getIdentifier(user) + "");
out.println("Attributes Values for current user
" );
Iterator userAttributeIt = userAttributeMap.keySet().iterator();
while(userAttributeIt.hasNext()){
String attributeName = (String)userAttributeIt.next();
Object attributeValue = userAttributeMap.get(attributeName);
out.println(attributeName + " = " + attributeValue);
out.println("
");
}
out.println("
");

System.out.println("Inside demoCurrentUser " + pumaProfile.getIdentifier(user));

} catch (PumaAttributeException e) {
e.printStackTrace();
} catch (PumaSystemException e) {
e.printStackTrace();
} catch (PumaModelException e) {
e.printStackTrace();
} catch (PumaMissingAccessRightsException e) {
e.printStackTrace();
} catch (PumaException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void demoFindingUserByAttribute(PortletRequest portletRequest, RenderResponse portletResponse){
try {
PumaLocator pumaLocator = pumaHome.getLocator(portletRequest);
PumaProfile pumaProfile = pumaHome.getProfile(portletRequest);
List userList = pumaLocator.findUsersByAttribute("uid", "sunilpatil");
List attributeList = new ArrayList();
attributeList.add("sn");
attributeList.add("givenName");
attributeList.add("uid");
attributeList.add("preferredLanguage");

User firstUser = (User)userList.get(0);
Map userAttributeMap = pumaProfile.getAttributes(firstUser, attributeList);
PrintWriter out = portletResponse.getWriter();
out.println("Attributes Values for uid=sunilpatil user
" );
Iterator userAttributeIt = userAttributeMap.keySet().iterator();
while(userAttributeIt.hasNext()){
String attributeName = (String)userAttributeIt.next();
Object attributeValue = userAttributeMap.get(attributeName);
out.println(attributeName + " = " + attributeValue);
out.println("
");
}


out.println("
");
} catch (PumaSystemException e) {
e.printStackTrace();
} catch (PumaModelException e) {
e.printStackTrace();
} catch (PumaMissingAccessRightsException e) {
e.printStackTrace();
} catch (PumaAttributeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void demoFindUserByDn(RenderRequest renderRequest, RenderResponse renderResponse){
try {
PumaLocator pumaLocator = pumaHome.getLocator(renderRequest);
PumaProfile pumaProfile = pumaHome.getProfile(renderRequest);
User user = pumaLocator.findUserByIdentifier("uid=wasadmin,o=default organization");
List attributeList = new ArrayList();
attributeList.add("sn");
attributeList.add("givenName");
attributeList.add("uid");
attributeList.add("preferredLanguage");


Map userAttributeMap = pumaProfile.getAttributes(user, attributeList);
PrintWriter out = renderResponse.getWriter();
out.println("Attributes Values for 'uid=wasadmin,o=default organization' user
" );
Iterator userAttributeIt = userAttributeMap.keySet().iterator();
while(userAttributeIt.hasNext()){
String attributeName = (String)userAttributeIt.next();
Object attributeValue = userAttributeMap.get(attributeName);
out.println(attributeName + " = " + attributeValue);
out.println("
");
}


out.println("
");
} catch (PumaSystemException e) {
e.printStackTrace();
} catch (PumaModelException e) {
e.printStackTrace();
} catch (PumaMissingAccessRightsException e) {
e.printStackTrace();
} catch (PumaAttributeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

It has three methods first demonstrates how to get list of attributes for current users, second is how to search user with value of particular attribute equal to something and third is how to find user based on DN

11 comments:

Anonymous said...

java.lang.ClassCastException: com.ibm.wps.services.portletserviceregistry.home.PortletServiceHomeImpl incompatible with com.ibm.portal.portlet.service.PortletServiceHome

error at line psh = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");

Anonymous said...

Running into the same issue. Anyone have any update. Please help!

Anonymous said...

remove the jar in the lib folder of your war file

Anonymous said...

Remove the jsr168.jar file in ur lib file this causes the exception to occure

test said...

Working fine..........Thanks

Anonymous said...

Removing puma library wp.user.api.jar from lib folder of project worked for me.
Just add this library as external jar.

Environment: WPS8.0

Thanks!!

Sree Harsha Pothireddy said...

In WPS 8.5 I had to add 2 jars (choose as external jars in the build path setup) public_spi.jar and wp.user.api.jar.

Like above comments these jars should not be part of the WEB-INF/lib folder otherwise you will see the below error

java.lang.ClassCastException: com.ibm.wps.services.portletserviceregistry.home.PortletServiceHomeImpl incompatible with com.ibm.portal.portlet.service.PortletServiceHome

Sample Code in the doView() method

//Required imports

//Added below imports for PUMA API calls
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.um.PumaProfile;
import com.ibm.portal.um.User;
import com.ibm.portal.um.exceptions.PumaException;
import com.ibm.portal.um.portletservice.PumaHome;



//private variables

private PortletServiceHome portletServiceHome;
String uid = null;


//this is the init method, u initialize the puma service here

public void init(PortletConfig portletConfig) throws PortletException{
super.init(portletConfig);
try {
if (portletServiceHome == null) {
Context ctx = new javax.naming.InitialContext();
portletServiceHome = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");
}
} catch (NamingException e) {
System.out.println("-- init() > NamingException caught attempting to retrieve the PumaHome service: " + e.getMessage());
e.printStackTrace();
}
}



System.out.println("Testing the rerieve createUser but first PUMA APIS ");
try {
PumaHome pumaHome = (PumaHome) portletServiceHome.getPortletService(PumaHome.class);
PumaProfile pumaProfile = pumaHome.getProfile(request);
User user = pumaProfile.getCurrentUser();
user = pumaProfile.getCurrentUser();
List userAttrs = new ArrayList();
userAttrs.add("uid");
Map userMap = pumaProfile.getAttributes(user, userAttrs);
uid = (String) userMap.get("uid");
System.out.println("The User ID is " + uid);
} catch (PumaException e) {
e.printStackTrace();
}

Sree Harsha Pothireddy said...

Incase of JSP



<%
com.ibm.portal.puma.User portalUser= (com.ibm.portal.puma.User)request.getAttribute(com.ibm.portal.RequestConstants.REQUEST_USER_OBJECT);
String loggedInUser = (String) portalUser.get("uid");
System.out.println("userID =>" + loggedInUser);
request.setAttribute("userID", loggedInUser);
%>

Sree Harsha Pothireddy said...

In WPS 8.5 I had to add 2 jars (choose as external jars in the build path setup) public_spi.jar and wp.user.api.jar.

Like above comments these jars should not be part of the WEB-INF/lib folder otherwise you will see the below error

java.lang.ClassCastException: com.ibm.wps.services.portletserviceregistry.home.PortletServiceHomeImpl incompatible with com.ibm.portal.portlet.service.PortletServiceHome

Sample Code in the doView() method

//Required imports

//Added below imports for PUMA API calls
import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.um.PumaProfile;
import com.ibm.portal.um.User;
import com.ibm.portal.um.exceptions.PumaException;
import com.ibm.portal.um.portletservice.PumaHome;



//private variables

private PortletServiceHome portletServiceHome;
String uid = null;


//this is the init method, u initialize the puma service here

public void init(PortletConfig portletConfig) throws PortletException{
super.init(portletConfig);
try {
if (portletServiceHome == null) {
Context ctx = new javax.naming.InitialContext();
portletServiceHome = (PortletServiceHome) ctx.lookup("portletservice/com.ibm.portal.um.portletservice.PumaHome");
}
} catch (NamingException e) {
System.out.println("-- init() > NamingException caught attempting to retrieve the PumaHome service: " + e.getMessage());
e.printStackTrace();
}
}



System.out.println("Testing the rerieve createUser but first PUMA APIS ");
try {
PumaHome pumaHome = (PumaHome) portletServiceHome.getPortletService(PumaHome.class);
PumaProfile pumaProfile = pumaHome.getProfile(request);
User user = pumaProfile.getCurrentUser();
user = pumaProfile.getCurrentUser();
List userAttrs = new ArrayList();
userAttrs.add("uid");
Map userMap = pumaProfile.getAttributes(user, userAttrs);
uid = (String) userMap.get("uid");
System.out.println("The User ID is " + uid);
} catch (PumaException e) {
e.printStackTrace();
}
A

Anonymous said...

@Thoughts Thank you, you saved my day. AGAIN IBM wasn't able (or willing) to help on the IBM forum (first Google search result entry about this ClassCastException problem)!

Xclusiveoffer said...

Thank you for such a great Information.......& Get extra discount on all Puma Products at Best Online Shopping Site In India