Hello User Portlet

The next step in your portlet example is to make the world more specific. That is, instead of writing out a static string, the greeting is personalized according to the user settings.


package org.apache.jetspeed.tutorial.portal.portlets;

public class HelloUserPortlet extends AbstractInstancePortlet
{
    public ConcreteElement getContent(RunData runData)
    {
        StringBuffer text =new StringBuffer();

        text.append("Hello ");

        String name = runData.getUser().getFirstName();

        if (name == null)
            name ="Anonymous";

        text.append (name);
        text.append ("!");
        return (new StringElement(text.toString()));
    }
}
    

With this example, you will be using the classes inherited from another Apache project: Turbine. Turbine is a web application framework based on the Java Servlet API. Jetspeed was written with Turbine as its framework, and also upon the Java Servlet API. Lets take a closer look at the interfaces supplied by Turbine: RunData and User.

The RunData interface provides you with access to the servlets request state and other session context information. Examples are security (access control), Turbine actions, cookies, servlet parameters (parsed), and the current user, to name a few. The current user is accessible via the getUser() or getJetspeedUser() methods as shown in the example above.

With the User object, you can then access typical user information, and also store temporary values for your session (getTemp(), setTemp()). Heres as summary of the standard user information provided:

User Property/Methods Description
AccessCounter Keep track of how many times the user has accessed the portal
Confirmed Returns the confirmation value used during automatic account sign up.
CreateDate The creation date of this account.
Email The email address of the user.
FirstName The first name of the user.
LastAccessDate The last access time stamp for the user.
LastLogin The last login time stamp for the user.
LastName The last name of the user.
Password The password of the user.
Perm Jetspeed stores a hash table of name/value pairs in a binary column in the database.
Temp A temporary hash table of values for the user stored in the session. These values go away after user logs off or after the session times out.
UserName The user name (primary credential) of the user.
hasLoggedIn Indicates if the user is currently logged on.
incrementAccessCounter Increment the users access counter.
isConfirmed Indicates if this user has yet been confirmed.
removeTemp Remove a name/value pair from the temporary hash table.
Jetspeed User Property Description
Disabled An account can be disabled with this property
UserId The user id
isNew Boolean indicating if the user has been stored yet

Remember that the set methods of the properties may not store the changes to the database immediately. There is a setting in the JetspeedResources.properties file that must be set to true to automatically save all modifications to the user on logout.

# automatically save user state on logout
automatic.logout.save = false

Otherwise, you will need to explicitly save the user with the User Management service. Here is an example of disabling the current user.


JetspeedUser user = user = rundata.getJetspeedUser();
user.setDisabled(true);
JetspeedUserManagement.saveUser(user);

Jetspeed actually extends the RunData interface as JetspeedRunData. You can safely cast RunData to JetspeedRunData from anywhere within Jetspeed. The extended class gives you access to some important Jetspeed request state such as the Profile and the Capability Map.

To summarize, you can always get the current user from the RunData. RunData is used all over Jetspeed as a common mechanism for passing request information.