A preference is modified by using
setValue
. This normally occurs at the personalized layer and therefore affects only the current user. WebSphere Portal uses two special custom modes from the set of predefined custom modes in the Java Portlet Specification to allow setting up the more general preference levels:
- The
edit_defaults
custom portlet mode is used to work directly on the shared preferences. In this case the personalized preferences level is not available.
- Similarly, the
config
mode is used to read and modify the administrator level of preferences.
- The deployment descriptor level of preferences can only change when the portlet is redeployed with a modified portlet.xml. It cannot be modified by portlet code.
I built a sample portlet to demonstrate how you can use the
edit_defaults
and
config
mode supported by WebSphere Portal Server.
package com.webspherenotes.portlet.jsr286;
import java.io.IOException;
import java.util.ArrayList;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
public class WPSCustomPortletMode extends GenericPortlet{
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering CustomPortletMode.doView()");
response.setContentType("text/html");
response.getWriter().println("User Name "
+ request.getPreferences().getValue("userName", "Not Set"));
System.out.println("Exiting CustomPortletMode.doView()");
}
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("Entering CustomPortletMode.processAction()");
String userName = request.getParameter("userName");
PortletPreferences preference = request.getPreferences();
preference.setValue("userName", userName);
preference.store();
System.out.println("Entering CustomPortletMode.processAction()");
}
protected void doEdit(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering CustomPortletMode.doEdit()");
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Exiting CustomPortletMode.doEdit()");
}
protected void doEditDefaults(RenderRequest request, RenderResponse response)
throws PortletException, IOException{
System.out.println("Entering CustomPortletMode.doEditDefaults()");
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Exiting CustomPortletMode.doEdit()");
System.out.println("Entering CustomPortletMode.doEditDefaults()");
}
protected void doConfig(RenderRequest request, RenderResponse response)
throws PortletException, IOException{
System.out.println("Entering CustomPortletMode.doConfig()");
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);
System.out.println("Exiting CustomPortletMode.doConfig()");
}
PortletMode configMode = new PortletMode("config");
PortletMode editDefaultsMode = new PortletMode("edit_defaults");
protected void doDispatch(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering CustomPortletMode.doDispatch");
System.out.println("Requested portlet mode " + request.getPortletMode());
if(request.getPortletMode().equals(configMode)){
System.out.println("Request for config mode");
doConfig(request, response);
}else if(request.getPortletMode().equals(editDefaultsMode)){
System.out.println("Request for edit_defaults mode");
doEditDefaults(request, response);
}else{
super.doDispatch(request, response);
}
System.out.println("Exiting CustomPortletMode.doDispatch");
}
}
The WPSCustomPortletMode.java overrides
doDispatch()
and forwards control to corresponding custom modes. I am forwarding control to same index.jsp in
edit
,
edit_defaults
and
config
mode and all three of them allow user to submit a value for
userName
that i am storing in preference in
processAction()
method. Depending on the mode this value will get stored at different preference level.
This is how the portlet.xml file for WPCustomPortletMode looks like
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd">
<portlet>
<portlet-name>WPSCustomPortletMode</portlet-name>
<display-name>WPS Custom Portlet Mode Portlet</display-name>
<portlet-class>com.webspherenotes.portlet.jsr286.WPSCustomPortletMode</portlet-class>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
<portlet-mode>config</portlet-mode>
<portlet-mode>edit_defaults</portlet-mode>
</supports>
<portlet-info>
<title>WPS Custom Portlet Mode Portlet</title>
<short-title>WPS Custom Portlet Mode Portlet</short-title>
<keywords>WPS Custom Portlet Mode Portlet</keywords>
</portlet-info>
</portlet>
<custom-portlet-mode>
<description>Shared Settings mode</description>
<portlet-mode>edit_defaults</portlet-mode>
</custom-portlet-mode>
<custom-portlet-mode>
<description>Administrative mode</description>
<portlet-mode>config</portlet-mode>
</custom-portlet-mode>
</portlet-app>
I am defining two
custom-portlet-modes
here and for both of them the value of
portal-managed
equal to true, which is default value to indicate that these custom portlet modes are managed by WebSphere Portal