Sun Java System Portal Server 7.1 Developer's Guide

Developing the Class File

This section includes:

GenericPortlet Class

This section includes:

Methods to Override

When extending from the GenericPortlet abstract class, the following methods should be overridden:

public void init(PortletConfig config)

The container calls this method once when the portlet object is created.

protected void doView(RenderRequest req, RenderResponse resp)

The render method calls this method when the portlet is in VIEW mode. The GenericPortlet class’ default implementation only throws an exception.

protected void doEdit(RenderRequest req, RenderResponse resp)

The render method calls this method when the portlet is in EDIT mode. The GenericPortlet class’ default implementation only throws an exception.

protected void doHelp(RenderRequest req, RenderResponse resp)

The render method calls this method when the portlet is in HELP mode. The GenericPortlet class’ default implementation only throws an exception.

public void processAction(ActionRequest req, ActionResponse resp)

The GenericPortlet class includes a default implementation that only throws an exception.

public void destroy()

The container calls this method on the portlet when the portlet is being taken out of service.

Render Request Processing

The request from the client is encapsulated in a RenderRequest object. The portlet uses this object to access information about the request that was received from the client by the Portal Server. Typically, the current portlet mode and request parameters included in the incoming request is the information that is accessed. The portlet’s response is encapsulated in a RenderResponse object. Portlet use this object to set the content type of the response and send their portlet content to the portlet container.

Each request object is valid only within the scope of a particular processAction() or render() method call.

Action Request Processing

Action URL requests from the client are encapsulated in an ActionRequest object. The portlet uses ActionRequest objects to gain access to the details of the incoming HTTP request from the client. The portlet’s response to the action request is encapsulated in the ActionResponse object. The portlet uses this object to set the new portlet mode (typically the VIEW mode) and send information to the subsequent render request after action request has been proceessed.

Portlet Preferences

The portlet specification mandates that portlet container provide persistent store for portlet preferences. Portlets can only access their preferences during requests (render or action). Portlet preferences are encapsulated by the PortletPreferences interface.

Portlets can update their preferences within the processing of the processAction() method. An exception is thrown if the portlet attempts to update preferences during a render request.

User preferences are stored and accessed through a PortletPreferences object. A handle to this object can be obtained by using both RenderRequest.getPreferences() and ActionRequest.getPreferences(), depending from which method the attempt to access the preferences is made. An individual preferences is called using getValue() or getValues(), for single string value or multiple string values respectively. The methods setValue() and setValues() modifies the preference, but no changes are stored in the datastore until the method store() is called. All user preferences must be defined in the portlet deployment descriptor.

Preferences defined at the organization will affect the user’s preference only if the user’s preference has not been defined and only until the user modifies the individual preference. Once preferences have been modified, it will retain its value, even if blank, until the reset() method is called on the preference.

LDAP attributes can only be accessed through the portlet implementation of a user info Map. All attributes needed within the portlet must be defined in the portlet deployment descriptor. The attribute map of all such defined attributes can be retrieved using either ActionRequest.getAttribute(PortletRequest.USER_INFO) or RenderRequest.getAttribute(PortletRequest.USER_INFO). Each attribute can then be retrieved using Map.get("Attribute Name").

Only user attributes defined in the deployment descriptor can be read. The user info map returned is unmodifiable.

Example Portlet Class File

Following example contains the class file for the sample PrefPortlet.


Example 14–1 PrefPortlet.java File


package examples;

import javax.portlet.GenericPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.RenderRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletURL;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.WindowState;
import java.io.IOException;
import java.io.PrintWriter;

public class PrefPortlet extends GenericPortlet {
    public void processAction(ActionRequest request, 
		ActionResponse response) 
		throws PortletException {
        // process the salutation set by the user 
			in the edit mode.
        String salutation = request.getParameter("SALUTATION");
        try {
            PortletPreferences pref = request.getPreferences();
            pref.setValue("salutation", salutation);
            pref.store();
        } catch (Exception e) {
            throw new PortletException(e.getMessage());
        }
        // return the user back to the view mode and normal state
        response.setPortletMode(PortletMode.VIEW);
        response.setWindowState(WindowState.NORMAL);
    }

    public void doView(RenderRequest request,RenderResponse response) 
		throws PortletException,IOException {
        // displays the salutation stored in the preference.
        PortletPreferences pref = request.getPreferences();
        String salutation = pref.getValue("salutation","");
        response.setContentType(request.getResponseContentType());
        PrintWriter writer = response.getWriter();
        writer.write("Hello " + salutation);
    }

    public void doEdit(RenderRequest request,RenderResponse response)
		throws PortletException,IOException {
        PortletURL actionURL = response.createActionURL();
        response.setContentType(request.getResponseContentType());
        PrintWriter writer = response.getWriter();
        writer.print("<form method=\"post\" action=\"" 
			+ actionURL.toString());
        writer.println("\">");
			writer.println("<center><p>Salutation: <input type=
			\"text\" name=\"salutation\"></p>");
			writer.println("<input type=\"submit\" value=\"Submit\">");
			writer.println("</form>");
    }

    public void doHelp(RenderRequest request, RenderResponse response) 
		throws PortletException {
        response.setContentType(request.getResponseContentType());
        try {
            response.setContentType(request.getResponseContentType());
            PrintWriter writer = response.getWriter();
            writer.write("Pref Portlet Help<p><p>");
        } catch (IOException e) {
            throw new PortletException("PrefPortlet.doHelp exception", e);
        }
    }
}