FileDocCategorySizeDatePackage
MailreaderSupport.javaAPI DocApache Struts 2.0.9 Apps17081Mon Jul 23 13:43:12 BST 2007mailreader2

MailreaderSupport

public class MailreaderSupport extends com.opensymphony.xwork2.ActionSupport implements org.apache.struts2.interceptor.ApplicationAware, org.apache.struts2.interceptor.SessionAware

Base Action for MailreaderSupport application.

Note that this class does NOT implement model driven because of the way the pre-existing model is designed. The MailReader DAO includes immutable fields that can only be set on construction, and some objects do not have a default construction. One approach would be to mirror all the DAO properties on the Actions. As an alternative, this implementations uses the DAO properties where possible, and uses local Action properties only as needed. To create new objects, a blank temporary object is constructed, and the page uses a mix of local Action properties and DAO properties. When the new object is to be saved, the local Action properties are used to create the object using the DAO factory methods, the input values are copied from the temporary object, and the new object is saved. It's kludge, but it avoids creating unnecessary local properties. Pick your poison.

Fields Summary
private Map
application

Field to store application context or its proxy.

The application context lasts for the life of the application. A reference to the database is stored in the application context at startup.

private Map
session

Field to store session context, or its proxy.

private String
task

Field to store workflow task.

The Task is used to track the state of the CRUD workflows. It can be set to Constant.CREATE, Constant.EDIT, or Constant.DELETE as needed.

private String
token

Field to store double-submit guard.

private String
host

Field to store Subscription host.

The host is an immutable property of the Subscrtion DAP object, so we need to store it locally until we are ready to create the Subscription.

private String
password

Field to store User password property.

The User DAO object password proerty is immutable, so we store it locally until we are ready to create the object.

private String
password2

Field to store the User password confirmation.

When a User object is created, we ask the client to enter the password twice, to help ensure the password is being typed correctly.

private String
username

Field to store User username.

The User DAO object password proerty is immutable, so we store it locally until we are ready to create the object.

protected Log
log

Log instance for this application.

Constructors Summary
Methods Summary
public java.lang.Stringcancel()
Return CANCEL so apropriate result can be selected.

return
"cancel" so apropriate result can be selected.

        return Constants.CANCEL;
    
protected voidcopySubscription(org.apache.struts.apps.mailreader.dao.Subscription source, org.apache.struts.apps.mailreader.dao.Subscription target)
Convenience method to copy Subscription properties.

      if ((source==null) || (target==null)) return;
      target.setAutoConnect(source.getAutoConnect());
      target.setPassword(source.getPassword());
      target.setType(source.getType());
      target.setUsername(source.getUsername());
    
public voidcopySubscription(java.lang.String host)

Create a new Subscription from the current Subscription object, making the new Subscription the current Subscription.

Usually, the "current" Subscription is a temporary object being used to capture input values.

param
host

        Subscription input = getSubscription();
        Subscription sub = createSubscription(host);
        if (null != sub) {
            copySubscription(input, sub);
            setSubscription(sub);
            setHost(sub.getHost());
        }
    
protected voidcopyUser(org.apache.struts.apps.mailreader.dao.User source, org.apache.struts.apps.mailreader.dao.User target)
Convenience method to copy User properties.

      if ((source==null) || (target==null)) return;
      target.setFromAddress(source.getFromAddress());
      target.setFullName(source.getFullName());
      target.setPassword(source.getPassword());
      target.setReplyToAddress(source.getReplyToAddress());
    
public voidcopyUser(java.lang.String _username, java.lang.String _password)

Use the current User object to create a new User object, and make the new User object the authenticated user.

The "current" User object is usually a temporary object being used to capture input.

param
_username User username
param
_password User password

        User input = getUser();
        input.setPassword(_password);
        User user = createUser(_username, _password);
        if (null != user) {
            copyUser(input,user);
            setUser(user);
        }
    
public voidcreateInputSubscription()

Provide a "temporary" User Subscription object that can be used to capture input values.

        Subscription sub = new MemorySubscription(getUser(), null);
        setSubscription(sub);
        setHost(sub.getHost());
    
public voidcreateInputUser()

        User user = new MemoryUser(null, null);
        setUser(user);
    
public org.apache.struts.apps.mailreader.dao.SubscriptioncreateSubscription(java.lang.String host)

Provide new User Subscription object for the given host, or null if the host is not unique.

param
host
return
New User Subscription object or null


        Subscription sub;

        sub = findSubscription(host);

        if (null != sub) {
            // FIXME - localization - "error.host.unique")
            addFieldError(Constants.HOST,"That hostname is already defined");
            return null;
        }

        return getUser().createSubscription(host);
    
public org.apache.struts.apps.mailreader.dao.UsercreateUser(java.lang.String username, java.lang.String password)

Verify input for creating a new user, create the user, and process the login.

return
A new User and empty Errors if create succeeds, or null and Errors if create fails


        UserDatabase database = getDatabase();
        User user;

        try {
            user = database.findUser(username);
         }

        catch (ExpiredPasswordException e) {
            user = getUser(); // Just so that it is not null
        }

        if (user != null) {
            this.addFieldError("username", "error.username.unique");
            return null;
        }

        return database.createUser(username);
    
public org.apache.struts.apps.mailreader.dao.SubscriptionfindSubscription(java.lang.String host)

Obtain User Subscription object for the given host, or return null if not found.

It would be possible for this code to throw a NullPointerException, but the ExceptionHandler in the xwork.xml will catch that for us.

return
The matching Subscription or null

        Subscription subscription;
        subscription = getUser().findSubscription(host);
        return subscription;
    
public org.apache.struts.apps.mailreader.dao.SubscriptionfindSubscription()

Obtain uSER Subscription for the local Host property.

Usually, the host property will be set from the client request, because it was embedded in a link to the Subcription action.

return
Subscription or null if not found

        return findSubscription(getHost());
    
public org.apache.struts.apps.mailreader.dao.UserfindUser(java.lang.String username, java.lang.String password)

Obtain User object from database, or return null if the credentials are not found or invalid.

param
username User username
param
password User password
return
User object or null if not found
throws
ExpiredPasswordException

        // FIXME: Stupid testing hack to compensate for inadequate DAO layer
        if (Constants.EXPIRED_PASSWORD_EXCEPTION.equals(username)) {
            throw new ExpiredPasswordException(Constants.EXPIRED_PASSWORD_EXCEPTION);
        }

        User user = getDatabase().findUser(username);
        if ((user != null) && !user.getPassword().equals(password)) {
            user = null;
        }
        if (user == null) {
            this.addFieldError(Constants.PASSWORD_MISMATCH_FIELD,
                    getText("error.password.mismatch"));
        }
        return user;
    
public java.util.MapgetApplication()

Provide application context.

        return application;
    
public org.apache.struts.apps.mailreader.dao.UserDatabasegetDatabase()

Provide reference to UserDatabase, or null if the database is not available.

return
a reference to the UserDatabase or null if the database is not available

        Object db = getApplication().get(Constants.DATABASE_KEY);
        if (db == null) {
            this.addActionError(getText("error.database.missing"));
        }
        return (UserDatabase) db;
    
public java.lang.StringgetHost()

Provide tSubscription host.

return
host property

        return host;
    
public java.lang.StringgetPassword()

Provide User password

return
Returns the password.



                
       
        return password;
    
public java.lang.StringgetPassword2()

Provide the User password confirmation.

return
Returns the confirmationpassword.



                  
       
        return password2;
    
public java.util.MapgetSession()

Provide session context.

return
session context

        return session;
    
public org.apache.struts.apps.mailreader.dao.SubscriptiongetSubscription()

Obtain the cached Subscription object, if any.

return
Cached Subscription object or null

        return (Subscription) getSession().get(Constants.SUBSCRIPTION_KEY);
    
public java.lang.StringgetSubscriptionHost()

Provide MailServer Host for current User Subscription.

return
MailServer Host for current User Subscription

        Subscription sub = getSubscription();
        if (null == sub) {
            return null;
        }
        return sub.getHost();
    
public java.lang.StringgetTask()

Provide worklow task.

return
Returns the task.



                
       
        return task;
    
public java.lang.StringgetToken()

Provide Token.

return
Returns the token.



               
       
        return token;
    
public org.apache.struts.apps.mailreader.dao.UsergetUser()

Provide reference to User object for authenticated user.

return
User object for authenticated user.

        return (User) getSession().get(Constants.USER_KEY);
    
public java.lang.StringgetUsername()

Provide User username.

return
Returns the User username.



                 
       
        return username;
    
public voidremoveSubscription()

Delete the current Subscription object from the database.

        getUser().removeSubscription(getSubscription());
        getSession().remove(Constants.SUBSCRIPTION_KEY);
    
public voidsaveUser()

Persist the User object, including subscriptions, to the database.

throws
java.lang.Exception on database error


                         
         
        try {
            getDatabase().save();
        } catch (Exception e) {
            String message = Constants.LOG_DATABASE_SAVE_ERROR + getUser()
                    .getUsername();
            log.error(message, e);
            throw new Exception(message, e);
        }
    
public voidsetApplication(java.util.Map value)

Store a new application context.

param
value A Map representing application state

        application = value;
    
public voidsetDatabase(org.apache.struts.apps.mailreader.dao.UserDatabase database)

Store a new reference to UserDatabase

param
database

        getApplication().put(Constants.DATABASE_KEY, database);
    
public voidsetHost(java.lang.String value)

Store new Subscription host.

param
value

        host = value;
    
public voidsetPassword(java.lang.String value)

Store new User Password

param
value The password to set.

        password = value;
    
public voidsetPassword2(java.lang.String value)

Store a new User password confirmation.

param
value The confirmation password to set.

        password2 = value;
    
public voidsetSession(java.util.Map value)

Store a new session context.

param
value A Map representing session state

        session = value;
    
public voidsetSubscription(org.apache.struts.apps.mailreader.dao.Subscription subscription)

Store new User Subscription.

param
subscription

        getSession().put(Constants.SUBSCRIPTION_KEY, subscription);
    
public voidsetTask(java.lang.String value)

Store new workflow task.

param
value The task to set.

        task =  value;
    
public voidsetToken(java.lang.String value)

Store new Token.

param
value The token to set.

        token =  value;
    
public voidsetUser(org.apache.struts.apps.mailreader.dao.User user)

Store new reference to User Object.

param
user User object for authenticated user

        getSession().put(Constants.USER_KEY, user);
    
public voidsetUsername(java.lang.String value)

Store new User username

param
value The username to set.

        username = value;