Client Side User Prorile Manipulation

I built a sample application for testing how to work with the User Profile on the client side. You can download the sample application from here

The sample application allows you to get values of user profile attributes and set the value for user profile attribute, It seems that either there is some problem with the setting user profile attribute server side code or the it requires some additional settings because i could not get it working.



The business logic of the user profile application is implemented in the userprofile.jsp like this.


<%@page
language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%><%@taglib
uri="http://java.sun.com/portlet" prefix="portlet"%><%@taglib
uri="http://www.ibm.com/xmlns/prod/websphere/portal/v6.1/portlet-client-model"
prefix="portlet-client-model"%><%@taglib
uri="http://java.sun.com/portlet" prefix="portletx"%><portlet-client-model:init>
<portlet-client-model:require module="ibm.portal.xml.*" />
<portlet-client-model:require module="ibm.portal.portlet.*" />
</portlet-client-model:init>
<portlet:defineObjects />
<script language="javascript">
function printUserProfile(){
var _portletWindow = new ibm.portal.portlet.PortletWindow("<%=portletWindowID%>");
_portletWindow.getUserProfile(printUserProfileCallback);
}
function printUserProfileCallback(portletWindow, status, userProfile){
if (status==ibm.portal.portlet.PortletWindow.STATUS_OK) {
var attributeList = ["cn","uid", "mobile","displayName","seeAlso","postalAddress","telephoneNumber","jpegPhoto","departmentNumber","viewIdentifiers","secretary","businessCategory","l","homePostalAddress","c","partyRoles","sn","pager","groups","businessAddress","carLinces","ibm-jobTitle","st","description","homeAddress","initials","postalCode","roomNumbr","title","stateOrProvinceName","givenName","children","street","manager","facsimileTelephoneNumber","countryName","localityName","uid","cn" ];
var userProfileStr = "";
for( var i = 0; i < attributeList.length; i++){
userProfileStr = userProfileStr + attributeList[i] + " - " + userProfile.getAttribute(attributeList[i]) +"<br/>";
}

dojo.byId("userPref").innerHTML = userProfileStr;
}else{
console.error("Error in getting user profile");
}
}
function setProfileAttribute(portletWindow, status, userProfile){
console.log("Entering setProfileAttribute()");
if (status==ibm.portal.portlet.PortletWindow.STATUS_OK) {
var profName = portletWindow.getAttribute("profName");
var profValue = portletWindow.getAttribute("profValue");
console.log("Setting User profile " + profName + " " +profValue);
userProfile.setAttribute(profName,profValue);
portletWindow.setUserProfile(userProfile,printUserProfileCallback);
}else{
alert("Error in loading user profile");
}
console.log("Exiting setProfileAttribute()");
}
function setUserProfileAttribute(){
console.log("Entering setUserProfileAttribute()");
console.log("Attribute Name " + dojo.byId("profName").value);
console.log("Attribute Value " + dojo.byId("profValue").value);
var _portletWindow = new ibm.portal.portlet.PortletWindow("<%=portletWindowID%>");
_portletWindow.setAttribute("profName",dojo.byId("profName").value );
_portletWindow.setAttribute("profValue",dojo.byId("profValue").value );
_portletWindow.getUserProfile(setProfileAttribute);

console.log("Exiting changePreference()");
return false;
}
printUserProfile();
</script>
<portlet:defineObjects />
<form method="post" onsubmit="setUserProfileAttribute">
<table>
<tr>
<td>Profile Attribute Name</td>
<td><input type="text" name="profName" id="profName" /></td>
</tr>
<tr>
<td>Profile Attribute Value</td>
<td><input type="text" name="profValue" id="profValue" /></td>
</tr>
<tr>
<td><input type="button" onclick="setUserProfileAttribute()" value="Set Profile Attribute" /></td>
<td><input type="button" onclick="printUserProfile()" value="Get Profile Attribute" /></td>

</tr>
</table>
</form>
<h4>User Profile</h4>
<div id="userPref">
</div


Getting the user profile attributes

You can get values for the user profile attributes by clicking on the "Get Profile Attribute" button, when you do that the control will go to printUserProfile(), this method will create object of PortletWindow class and then call its getUserProfile() method, passing it printUserProfileCallback as name of the call back function, the getUserProfile(), makes a call to get ATOM feed for the User Profile which looks something like this



Once this ATOM feed is retrieved, the printUserProfileCallback, method gets called with userProfile as third object. Inside the method first check the status of the request if it was OK then go through list of attribute names, get values for each one of them and set the resultant string as value of userPref div attribute.

These are the methods supported by UserProfile


Setting the user profile attribute

You can set value for UserProfile attribute by entering some value for "Profile Attribute Name" and "Profile Attribute Value" and clicking on "Set Profile Attribute", when you do that the setUserProfileAttribute() method gets called. This method creates object of PortletWindow and then calls its getUserProfile() method, in the call back method it is setting user profile attribute by calling userProfile.setAttribute(profName,profValue), which modifies the ATOM feed but in order to persist these changes you will have to call portletWindow.setUserProfile(userProfile,printUserProfileCallback), at that point the client side API will submit the ATOM feed to the server.

In my locale environment i do have WPS 6.1.5 environment with default File System Repository when i tried setting any user profile attribute i am getting this error

[1/12/10 22:34:12:909 PST] 00000058 WebApp E [Servlet Error]-[PROTECTED_HANDLER_47555912]: java.lang.NullPointerException
at com.ibm.portal.rest.userprofile.UserProfileFeedRequestProxy.getPathInfo(UserProfileFeedRequestProxy.java:116)
at com.ibm.portal.rest.userprofile.pathelements.PathElementStateMachine.handle(PathElementStateMachine.java:146)
at com.ibm.portal.rest.userprofile.pathelements.PathElementStateMachine.handleRequest(PathElementStateMachine.java:133)
at com.ibm.portal.rest.userprofile.UserProfileDataSource.calculate(UserProfileDataSource.java:137)
at com.ibm.portal.rest.userprofile.UserProfileDataSink.parse(UserProfileDataSink.java:129)
at com.ibm.wps.resolver.data.ByteXmlDataSink.read(ByteXmlDataSink.java:159)
at com.ibm.wps.resolver.servlet.ContentHandlerUpload.handleDownloadAndDispose(ContentHandlerUpload.java:482)
at com.ibm.wps.resolver.servlet.ContentHandlerUpload.handleUpload(ContentHandlerUpload.java:636)
at com.ibm.wps.resolver.servlet.ContentHandlerUpload.doFilter(ContentHandlerUpload.java:200)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerGzip.internalDoFilter(ContentHandlerGzip.java:413)
at com.ibm.wps.resolver.servlet.ContentHandlerGzip.doFilter(ContentHandlerGzip.java:248)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerByteRange.doFilter(ContentHandlerByteRange.java:98)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerPortal.doFilter(ContentHandlerPortal.java:211)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerOptions.doFilter(ContentHandlerOptions.java:107)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerBouncer.doFilter(ContentHandlerBouncer.java:238)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerDecoder.doFilter(ContentHandlerDecoder.java:132)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerVirtualPortal.doFilter(ContentHandlerVirtualPortal.java:198)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerCleanup.doFilter(ContentHandlerCleanup.java:648)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.ContentHandlerLogin.doFilter(ContentHandlerLogin.java:89)
at com.ibm.wps.resolver.servlet.AbstractFilter.doFilter(AbstractFilter.java:93)
at com.ibm.wps.resolver.servlet.FilterChainElement.doFilter(FilterChainElement.java:57)
at com.ibm.wps.resolver.servlet.FilterChainServlet.service(FilterChainServlet.java:342)
at com.ibm.wps.resolver.servlet.AbstractServlet.service(AbstractServlet.java:530)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1146)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1087)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:145)
at com.ibm.wps.state.filter.StateCleanup.doFilter(StateCleanup.java:94)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
at com.ibm.wps.engine.ExtendedLocaleFilter.doFilter(ExtendedLocaleFilter.java:113)
at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:87)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:837)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:680)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:588)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:524)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:90)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:751)
at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1478)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:125)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:196)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:751)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:881)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1497)