getting portal user/username
I am working on trying to gather user information by calls from inside a portlet. After looking over some old threads, I am not getting very far. I am doing development in JSC Creator, if that helps. I was trying to get at the request information by accessing the RequestMap method. This is supposed to return a map of the request scope attributes for the current request. However, that did not seem to have any sort of information similar to a portlet request attribute (PortletRequest.USER_INFO). Any idea on how I can get this working?
I looked over the documenation provided here [http://docs.sun.com/source/816-6362-10/ch2.html ] in the provider API (PAPI), but that did not help me much. Thanks for your time.
[728 byte] By [
jbjonesjr] at [2007-11-26 8:13:12]

# 1
Take a quick look at the JSR 168 Portlet Spec, chapter 17 - User Information (pg 69-70). This explains how to retrieve user information using the Portlet API. In addition, you will need to deploy your portlet with a User Information Mapping File. The UserInfoFile maps logical user attribute name to physical ones in the LDAP repository.
Here is one from the portlet samples:
/opt/SUNWportal/samples/portlet/userInfoMapping.properties
preferredlocale=locale
preferredtimezone=timezone
You can add other mappings for attributes like cn, givenName, etc.
- Jim
# 2
Thanks for the response Jim.
I saw in an earlier post where you mentioned using pdeploy to deploy the user info mapping file, is this still the case with portal 7, or is there any easier way to do it.
Also, where can I find the JSR-168 spec (a quick search of docs.sun did not turn up anything. I was hoping you guys maintained a copy).
Also, I assume this is how the userInfo portlet is getting it's info (I notice that you are in fact pointing me to a properties file of the same name). This is the portlet used in the developer sample that displays info on the currently logged in user.
Thanks again for the QUICK response.
# 3
The command for Portal 7 is psadmin deploy-portlet.
You can also use the psconsole to deploy portlets. There are file-upload controls available for you to specify a user properties file.
You can get the Portlet Spec here:
http://jcp.org/aboutJava/communityprocess/final/jsr168/index.html
The User Info Portlet is actually a PAPI Provider and does not use the JSR 168 API at all.
- Jim
# 4
Hmm, possible bug in portal?
Using this code to get user information:
try{
PortletPreferences portletPreferences = request.getPreferences();
Map userInfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
Iterator it = userInfo.keySet().iterator();
while(it.hasNext()){
String t = it.next().toString();
keys+= t+userInfo.get(t)+":,;";
}
temp = userInfo.get(name).toString();
}catch(Exception e) {
temp = name+" is not a valid attribute. Full error: "+e.getMessage();
}
My portlet returns user information fine (note, there is lots of each debugging stuff in that fucntion, so ignore the obvious debugging text).
HOWEVER, after I do a simple database call, the user info is lost (I catch a null pointer exception). Is it possible that a database call could somehow destroy the portlet request object? I don't have anything of note in my logs, any idea what levels I could set to finest to maybe capture something?
Very odd that a page reload/render killed the object.
**Note, it also kills any user information available in other portlets on the same tab. ***
# 5
If you only need to get the username out and save it for later I use the following bits of code.
To get the username out
String username = MyPortletUtil.parseUsername(request.getRemoteUser());
MyPortletUtil is a simple class I wrote to get the user name out of a dn string. ie uid=authless,ou=People,dc=example,dc=sun,dc=com or other types of strings that can be returned by the request.getRemoteUser() function.
Then I stash it away in the globally accessible session since servlets and jsps can't see the portal session.
request.getPortletSession().setAttribute("userName", request.getRemoteUser(), PortletSession.APPLICATION_SCOPE);
Finally to get it back out again in my servlets or jsps I use
String userName = MyPortletUtil.parseUsername((String)request.getSession().getAttribute("userName"));
I hope that helps.
# 6
Ok, some more info in case anyone cares...
(Thanks for your solution psconsole, but I needed other ldap attributes as well).
I think the issue comes in from either my:
a) misunderstanding of the portet request scope, or
b) portal's/portlet mishandling of that scope.
The scope is present and working on my first page touch, but on subsequent touches (whether it be a button press, event action, etc) the portlet request object (and preferences included) are not there (return null). So my solution, grab the preferences on teh first page load (actually first session load), and store them. I wanted to get them each time a request was made for them (so you could edit the ldap in the background and get instant results), but will live with this for now.
# 7
try{
ExternalContext context = this.getExternalContext();
PortletRequest request = (PortletRequest) context.getRequest();
String username=request.getRemoteUser();
staticText5.setText("user is "+username);
}
catch(Exception e)
{
staticText5.setText("catch");
}
You'll get the cn of the user, which can be parsed to get the uid (User name) out of it!!!
# 8
seems reasonable but i think you have to specify APPLICATION_SCOPE on the getAttribute request:(String)request.getSession().getAttribute("userName", PortletSession.APPLICATION_SCOPE);