Simple Test Case
Let's take a simple Hello World test case.
2) Copy and paste the following into the text editor:
4) Copy the helloworld.jsp file to the following location on your Portal server:
7) Still in manage pages, on the right-hand side of the "helloworldtest" page, click on the Pencil icon for "Edit Page Layout"
8) Click the + sign to add portlet, search for "Welcome". Click the checkbox next to the"Welcome to WebSphere Portal", and click OK to add it to the page. Click Done to complete the procedure.
Hello World! gives us an introduction to invoking code with the JSP. Let's try something more interesting ... say using the PUMA API to get all users and groups.
1) Create a new page named pumaapitest
2) Create a new file named pumaapitest.jsp with following code and upload to the Portal server in same folder:
<%@ page import="com.ibm.portal.um.*" %>
<%@ page import="javax.naming.*"%>
<%@ page import="java.util.*"%>
final String BR = "<br><br>";
out.print("Starting to gathering info"); out.print(BR);
Context ctx = null;
PumaHome pumaHome = null;
PumaProfile pumaProfile = null;
PumaProfile pumaProfileForContext = null;
PumaLocator pumaLocator = null;
PumaController pumaController = null;
ctx = new InitialContext();
pumaHome = (PumaHome)ctx.lookup(PumaHome.JNDI_NAME);
pumaProfile = pumaHome.getProfile();
pumaController = pumaHome.getController();
pumaLocator = pumaHome.getLocator();
List userList = new ArrayList();
List myList = new ArrayList();
userList = pumaLocator.findUsersByAttribute("uid","*");
if (userList.size() == 0)
out.print("No users found from search!"); out.print(BR);
out.print("Found a total number of users " + userList.size()); out.print(BR);
for (int i = 0; i < userList.size(); i++)
out.print("User number " + (i+1) + " has the following attributes " + pumaProfile.getAttributes((User)userList.get(i), myList)); out.print("<br>");
List groupList = new ArrayList();
myList = new ArrayList();
groupList = pumaLocator.findGroupsByAttribute("cn","*");
if (groupList.size() == 0)
out.print("No groups found from search!"); out.print(BR);
out.print("Found a number of groups " + groupList.size()); out.print(BR);
for (int i = 0; i < groupList.size(); i++)
out.print("Group number " + (i+1) + " has the following attributes " + pumaProfile.getAttributes((Group)groupList.get(i), myList)); out.print(BR);
out.print("Exception occured!!! " + e); out.print(BR);
3) Add the "Welcome" portlet to the page, configure the portlet to point to /jsp/pumaapitest.jsp
4) Observe results and notes from author of this blog entry:
- My test Portal only has the
default / out of the box user and group. In a larger Portal connected
to an LDAP, this sample could would query for uid=* and cn=* for users
and groups respectively. On the sample code, formatting could probably
use some improvements, as could error catching and handling, but, the
proof of concept was implemented fairly quickly.
- Also, should your code fail to compile (missing semicolons for instance), you will observe an HTTP 500 error on the page with debug output of where the compile error is in the code. Upload the corrected version of the code, reload the page, no more HTTP 500.
- In a production system, it is NOT recommended to use the Blurb portlet for this purpose. However, for testing purposes, this offers a quicker means to potentially try out some new options within the Portal that would have otherwise been difficult to allocate the time to code via a portlet. Future blog postings will cover some more advanced uses of this debugging technique, including pulling information from the portlet request, dumping browser cookies, session information, rememberme, etc. Stay tuned!