📄 jwmasession.java
字号:
/*** * jwma Java WebMail * Copyright (c) 2000-2003 jwma team * * jwma is free software; you can distribute and use this source * under the terms of the BSD-style license received along with * the distribution. ***/package dtw.webmail;import java.io.*;import java.util.*;import java.text.MessageFormat;import javax.mail.*;import javax.mail.event.*;import javax.mail.internet.*;import javax.servlet.http.*;import org.apache.log4j.Logger;import dtw.webmail.model.*;import dtw.webmail.util.*;import dtw.webmail.util.config.JwmaConfiguration;import dtw.webmail.util.config.PostOffice;import dtw.webmail.util.config.MailTransportAgent;/** * Class that models the state of a jwma session. * <p> * Each instance stores the state and provides accessor and * mutator operations for retrieving and changing. * <p> * The reference to the instance itself is stored in the * <tt>HttpSession</tt> as session attribute. * <p> * Note that this class is actually the bridge between the web * session and the mail client session, and that instances have * the responsibility to manage the state of both. * * @author Dieter Wimberger * @version 0.9.7 07/02/2003 */public class JwmaSession implements Serializable { //logging private static Logger log = Logger.getLogger(JwmaSession.class); //instance attributes private boolean m_Authenticated; private boolean m_LanguageToggled = true; //http session state related private List m_BeanNames; transient private HttpServletRequest m_Request; transient private HttpServletResponse m_Response; transient private HttpSession m_WebSession; private String[] m_ViewHistory; //mail session state related transient private PostOffice m_PostOffice; transient private MailTransportAgent m_MTA; private boolean m_PostOfficeConnected; transient private Session m_MailSession; transient private JwmaStoreImpl m_JwmaStore; transient private JwmaTransportImpl m_JwmaTransport; transient private JwmaAuthenticator m_Authenticator; //utility attributes transient private JwmaConfiguration m_Configuration; transient private JwmaPreferencesImpl m_Preferences; transient private JwmaHtmlHelper m_HtmlHelper = new JwmaHtmlHelperImpl(); private String m_Username; private String m_LastLogin; private boolean m_NewUser = false; /** * Constructs a <tt>JwmaSession</tt> instance. * * @param session reference to the actual <tt>HttpSession</tt> instance. */ public JwmaSession(HttpSession session) { m_Configuration = JwmaKernel.getReference().getConfiguration(); m_MTA = m_Configuration.getMTA(); m_PostOffice = m_Configuration.getDefaultPostOffice(); m_Authenticated = false; m_WebSession = session; m_BeanNames = new ArrayList(15); m_ViewHistory = new String[2]; m_ViewHistory[0] = JwmaKernel.LOGIN_VIEW; storeDefaultBeans(); }//constructor /*** Web session related part ***********************************************************/ /** * Sets the reference to the actual <tt>HttpServletRequest</tt> * instance. * * @param request reference to the actual incoming * <tt>HttpServletRequest</tt> instance. */ public void setRequest(HttpServletRequest request) { m_Request = request; }//setRequest /** * Returns the reference to the actual <tt>HttpServletRequest</tt> * instance. * * @return reference to the actual <tt>HttpServletRequest</tt> instance. */ public HttpServletRequest getRequest() { return m_Request; }//getRequest /** * Sets the reference to the actual <tt>HttpServletResponse</tt> * instance. * * @param request reference to the actual <tt>HttpServletResponse</tt> * instance. */ public void setResponse(HttpServletResponse response) { m_Response = response; }//setResponse /** * Returns the reference to the actual <tt>HttpServletResponse</tt> * instance. * * @return reference to the actual <tt>HttpServletResponse</tt> * instance. */ public HttpServletResponse getResponse() { return m_Response; }//getResponse /** * Returns the reference to the actual <tt>HttpSession</tt> * instance. * * @return reference to the actual <tt>HttpSession</tt> instance. */ public HttpSession getWebSession() { return m_WebSession; }//getWebSession /** * Tests if the associated web session is valid. * This is the case if it is not new, and if it has * not been invalidated. * * @return true if valid, false otherwise. */ public boolean isValidWebSession() { try { if (m_WebSession == null || m_WebSession.isNew()) { return false; } } catch (IllegalStateException illex) { log.error("isValidWebSession()",illex); return false; } return true; }//isValidWebSession /** * Sets the reference to the actual <tt>HttpSession</tt> * instance. * * @param reference to the actual <tt>HttpSession</tt> instance. */ public void setWebSession(HttpSession websession) { m_WebSession = websession; }//setWebSession private String getRequestAddress() { return getRequest().getRemoteHost(); }//getRequestAddress /** * Returns the value of a request parameter from this * session's <tt>HttpServletRequest</tt>. * This is a convenience method that trims the Strings. * * @return the value of the request parameter or null * if it doesn't have a value. */ public String getRequestParameter(String name) { String value = getRequest().getParameter(name); if (value != null && value.length() > 0) { return value.trim(); } else { return null; } }//getRequestParameter /** * Returns the values of a request parameter from this * session's <tt>HttpServletRequest</tt>. * This is a convenience method that trims the Strings. * * @return the value of the request parameters or null * if it doesn't have any value. */ public String[] getRequestParameters(String name) { String[] value = getRequest().getParameterValues(name); if (value != null && value.length > 0) { for (int i = 0; i < value.length; i++) { value[i] = value[i].trim(); } } return value; }//getRequestParameter /** * Redirects the the actual <tt>HttpServletRequest</tt> * to an abstract view. * <p> * The <tt>String</tt> parameter passed in <b>has to be</b> * one of the defined abstract view constants. * * @param view representing one of the constants defining an abstract * view. * * @see dtw.webmail.JwmaKernel#LOGIN_VIEW * @see dtw.webmail.JwmaKernel#ERROR_VIEW * @see dtw.webmail.JwmaKernel#LOGOUT_VIEW * @see dtw.webmail.JwmaKernel#FOLDER_VIEW * @see dtw.webmail.JwmaKernel#MESSAGE_VIEW * @see dtw.webmail.JwmaKernel#PREFERENCES_VIEW * @see dtw.webmail.JwmaKernel#COMPOSE_VIEW * @see dtw.webmail.JwmaKernel#FIRSTTIME_VIEW * @see dtw.webmail.JwmaKernel#CONTACTS_VIEW */ public void redirect(String view) { //update history m_ViewHistory[1] = m_ViewHistory[0]; m_ViewHistory[0] = view; //retrieve URL from Kernel view = JwmaKernel.getReference().getViewUrl(view); try { m_Response.sendRedirect(m_Response.encodeRedirectUrl(view)); } catch (IOException ex) { log.error(JwmaKernel.getReference().getLogMessage("jwma.session.redirectfailed"), ex); } }//redirect /** * Redirects the the actual <tt>HttpServletRequest</tt> * to the last view. * <p> * The last view is traced by a simple history mechanism * implemented in the <tt>redirect()</tt> method. */ public void redirectToLast() { redirect(m_ViewHistory[1]); }//redirectToLast /** * Redirects the the actual <tt>HttpServletRequest</tt> * to the present view. * <p> * The actual view is traced by a simple history mechanism * implemented in the <tt>redirect()</tt> method. * This method helps to acquire an updated view of the actual view. */ public void redirectToActual() { redirect(m_ViewHistory[0]); }//redirectToLast /** * Stores a reference to a bean(<i>model</i>) in the * actual <tt>HttpSession</tt>. * <p> * All stored beans are traced, so that they can be properly * removed. * * @param name the unique identifier of bean to be stored as * session attribute. * @param bean the reference to the bean to be stored as session * attribute. */ public void storeBean(String name, Object bean) { m_BeanNames.add(name); m_WebSession.putValue(name, bean); }//storeBean /** * Stores the default beans of any session. * This is done directly, to bypass the mechanism * that traces the other stored beans. * On cleanup, the default beans remain referenceable * within the session. */ private void storeDefaultBeans() { //the session bean m_WebSession.putValue("jwma.session", this); //the HtmlHelper bean m_WebSession.putValue("jwma.htmlhelper", m_HtmlHelper); //a reference to the i18n classloader m_WebSession.putValue("jwma.resourceloader", JwmaKernel.getReference().getResourceClassLoader()); }//storeDefaultBeans /** * Returns a reference to a bean(<i>model</i>) in the * actual <tt>HttpSession</tt>. * <p> * If no bean with the given name/identifyer is stored * in the actual session, then it returns null. * * @param name the unique identifier of bean to be stored as * session attribute. * @return the reference to the bean stored as session * attribute or null. */ public Object retrieveBean(String name) { return m_WebSession.getValue(name); }//retrieveBean /** * Removes a reference to a bean (<i>model</i>) in the * actual <tt>HttpSession</tt>. * * @param name the unique identifier of bean to be stored as * session attribute. */ public void removeBean(String name) { m_WebSession.removeValue(name); }//removeBean /** * Removes all traced bean references from the * actual <tt>HttpSession</tt>. */ private void removeBeans() { try { for (int i = 0; i < m_BeanNames.size(); i++) { m_WebSession.removeValue((String) m_BeanNames.get(i)); } m_BeanNames.clear(); } catch (IllegalStateException illex) { return; } catch (Exception ex) { log.error(JwmaKernel.getReference().getLogMessage("jwma.session.beanreferences"), ex); } }//removeBeans /** * Fetches the last login from the user's * preferences (to have it available for the * rest of the session) and then overwrites * the value in the preferences. */ private void setLastLogin() { //hold the last one for this session m_LastLogin = m_Preferences.getLastLogin(); //overwrite the persisted one m_Preferences.setLastLogin( StringUtil.getFormattedDate() + " from " + getRequest().getRemoteHost() ); }//setLastLogin /** * Returns a <tt>String</tt> representing the * user's last login date and originating host. * * @return the last login with date and originating * host as <tt>String</tt>. */ public String getLastLogin() { return m_LastLogin; }//getLastLogin /*** End web session related part *************************************************/ /*** Mail session related part ****************************************************/ /** * Initializes the mail session and prepares the store wrapper instance. * * @param password completing the login for the post office of * the jwma user. * @throws JwmaException when something goes wrong while opening or preparing * the mail session. */ private Store initMailSession(String password) throws JwmaException { log.debug("initMailSession()"); //prepare for formatted log message Object[] args = {m_Username + "@" + m_PostOffice.getAddress(), getRequestAddress()}; try { //JwmaSettings settings = JwmaKernel.getReference().getSettings(); //log.debug("Retrieved Settings from Kernel"); //prepare authenticator m_Authenticator = new JwmaAuthenticator(m_Username, password); log.debug("Prepared Authenticator"); //get session instance m_MailSession = Session.getInstance(System.getProperties(), m_Authenticator); log.debug("Retrieved Session instance."); //setup store and connect to it Store store = m_MailSession.getStore(m_PostOffice.getProtocol()); log.debug("Retrieved store instance"); store.connect( m_PostOffice.getAddress(), m_PostOffice.getPort(), m_Username, password ); log.debug("connected to store"); m_PostOfficeConnected = true; //1. setup MTA transport Transport transport = null; if (m_MTA.isAuthenticated()) { transport = m_MailSession.getTransport( new URLName( m_MTA.getProtocol(), m_MTA.getAddress(), m_MTA.getPort(), null, m_Username, password ) ); log.debug("Prepared authenticated transport."); } else { transport = m_MailSession.getTransport( new URLName( m_MTA.getProtocol(), m_MTA.getAddress(), m_MTA.getPort(), null, null, null ) ); log.debug("Prepared non authenticated transport."); } //create JwmaTransport wrapper instance m_JwmaTransport =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -