Showing posts with label pumaspi. Show all posts
Showing posts with label pumaspi. Show all posts

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