📄 jwmacontroller.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.*;import javax.servlet.*;import javax.servlet.http.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import org.apache.log4j.Logger;import org.apache.log4j.NDC;import dtw.webmail.util.*;import dtw.webmail.util.config.JwmaConfiguration;import dtw.webmail.util.config.PostOffice;import dtw.webmail.model.*;import dtw.webmail.plugin.RandomAppendPlugin;/** * Class extending the <tt>HttpServlet</tt> to implement * the main controller of jwma. * <p> * Please see the related documentation for more detailed * information on process flow and functionality. * * @author Dieter Wimberger * @version 0.9.7 07/02/2003 */public class JwmaController extends HttpServlet { //logging private static Logger log = Logger.getLogger(JwmaController.class); /** * Initializes the servlet when it is loaded by the * servlet engine. * <p> * This implementation "boots" jwma by starting up * the <tt>JwmaKernel</tt>. * * @param config the configuration as <tt>ServletConfig</tt> * * @throws ServletException if initialization fails. * * @see dtw.webmail.JwmaKernel */ public void init(ServletConfig config) throws ServletException { super.init(config); try { //kernel bootup JwmaKernel myKernel = JwmaKernel.getReference(); //check runtime environment String datadir = config.getInitParameter("datadir"); boolean absolutepaths = new Boolean(config.getInitParameter("absolutepaths")).booleanValue(); boolean status = new Boolean(config.getInitParameter("status")).booleanValue(); if (!absolutepaths) { datadir = config.getServletContext().getRealPath(datadir); } //set status myKernel.setJwmaStatusEnabled(status); //setup kernel with location myKernel.setup(datadir); log.info("Controller inited."); log.info("jwma ready."); } catch (Exception ex) { config.getServletContext().log(ex, "Initialization failed."); throw new UnavailableException(this, ex.getMessage()); } }//init /** * Handles the incoming requests. * <p> * This implementation ensures authenticated session * existence, retrieves the <tt>acton</tt> * and <tt>todo</tt> parameters, and dispatches all * valid actions to the target dispatchers. * <p> * The flow of the process is described in the related * documentation. * <p> * Application related errors/exceptions are handled * by forwarding the request to an error page, or the actual page * in case of an inlined error. * * @param req a reference to the actual <tt>HttpServletRequest</tt> * instance. * @param res a reference to the actual <tt>HttpServletResponse</tt> * instance. * * @throws ServletException if servlet related operations fail. * @throws IOException if i/o operations fail. */ public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try { //prepare NDC with client host name NDC.push(req.getRemoteHost()); //1. Handle session HttpSession websession = req.getSession(true); Object o = websession.getValue("jwma.session"); String acton = req.getParameter("acton"); String dome = req.getParameter("todo"); //2. update session information JwmaSession session = ((o == null)? new JwmaSession(websession):((JwmaSession) o)); session.setRequest(req); session.setResponse(res); session.setWebSession(websession); //redirect to login view if no valid websession if (!session.isValidWebSession()) { session.redirect(JwmaKernel.LOGIN_VIEW); return; } //dispatch actions try { if (acton == null || acton.equals("")) { throw new JwmaException("request.target.missing"); } else if (dome == null || dome.equals("")) { throw new JwmaException("request.action.missing"); } else if (acton.equals("session")) { doDispatchSessionActions(session, dome); } else { //ensure authentication & postoffice connection session.ensureAuthenticated(); session.ensurePostOfficeConnection(); if (acton.equals("folder")) { doDispatchFolderActions(session, dome); } else if (acton.equals("message")) { doDispatchMessageActions(session, dome); } else if (acton.equals("preferences")) { doDispatchPreferencesActions(session, dome); } else { throw new JwmaException("request.target.invalid"); } } } catch (JwmaException ex) { //log.debug("Exception handling routine",ex); boolean login = false; //pickup possible relogin String message = ex.getMessage(); if (!session.isAuthenticated() && message.equals("request.target.missing")) { login = true; } //log.debug("login="+login); //Log the problem if (!login) { //oneliner resolving of key to message in message = JwmaKernel.getReference().getLogMessage("jwma.usererror") + JwmaKernel.getReference().getErrorMessage(message); //log exception with description and trace if inlined exception //available, else with stacktrace. if (ex.hasException()) { log.error(message, ex.getException()); } else { log.error(message, ex); } //store error to be shown session.storeBean("jwma.error", ex); } //1. if not authenticated forward to login page if (!session.isAuthenticated()) { //ensure all errors are inline ex.setInlineError(true); session.redirect(JwmaKernel.LOGIN_VIEW); } else { //redirect last view with inlined error or //to error page. if (ex.isInlineError()) { session.redirectToActual(); } else { session.redirect(JwmaKernel.ERROR_VIEW); } } } } finally { NDC.pop(); } }//service /*** Dispatchers ***************************************************/ /** * Dispatches actions targeting a <tt>session</tt>. * * @param session a <tt>JwmaSession</tt> instance. * @param dome the task as <tt>String</tt>. * * @throws JwmaException if it fails to dispatch the request to a * method (i.e. invalid request), or the action method fails * to execute the task. */ private void doDispatchSessionActions(JwmaSession session, String dome) throws JwmaException { if (dome.equals("logout")) { session.ensureAuthenticated(); doLogout(session); } else if (dome.equals("login")) { String user = session.getRequestParameter("username"); String passwd = session.getRequestParameter("password"); String postoffice = session.getRequestParameter("postoffice"); //FIXME:catch invalid and trim the vars! doLogin(session, user, passwd, postoffice); } else if (dome.equals("redirect")) { session.ensureAuthenticated(); String view = session.getRequestParameter("view"); doRedirect(session, view); } else { throw new JwmaException("session.action.invalid"); } }//doDispatchSessionActions /** * Dispatches actions targeting a <tt>folder</tt>. * * @param session a <tt>JwmaSession</tt> instance. * @param dome the task as <tt>String</tt>. * * @throws JwmaException if it fails to dispatch the request to a * method (i.e. invalid request), or the action method fails * to execute the task. */ private void doDispatchFolderActions(JwmaSession session, String dome) throws JwmaException { if (dome.equals("display")) { String name = session.getRequestParameter("path"); if (name == null) { throw new JwmaException("folder.display.missingpath"); } else { doDisplayFolder(session, name); } } else if (dome.equals("sortmessages")) { String criteria = session.getRequestParameter("criteria"); if (criteria == null) { throw new JwmaException("folder.sortmessages.missingcriteria"); } else { doSortFolderMessages(session, toInt(criteria)); } } else if (dome.equals("move")) { String[] folders = session.getRequest().getParameterValues("paths"); String destination = session.getRequestParameter("destination"); if (folders == null || folders.length == 0) { throw new JwmaException("folder.move.missingpaths"); } else if (destination == null) { throw new JwmaException("folder.move.missingdestination"); } else { doMoveFolders(session, folders, destination); } } else if (dome.equals("create")) { String path = session.getRequestParameter("aname"); String type = session.getRequestParameter("type"); if (path == null) { throw new JwmaException("folder.create.missingname"); } else if (type == null) { throw new JwmaException("folder.create.missingtype"); } else { doCreateFolder(session, path, toInt(type)); } } else if (dome.equals("delete")) { String[] folders = session.getRequestParameters("paths"); if (folders == null || folders.length == 0) { throw new JwmaException("folder.delete.missingpaths"); } else { doDeleteFolders(session, folders); } } else if(dome.equals("display_subscribed")) { doDisplaySubscribedFolders(session); } else if(dome.equals("subscribe")) { String[] folders = session.getRequestParameters("paths"); if (folders == null || folders.length == 0) { throw new JwmaException("folder.subscribe.missingpaths"); } else { doSubscribeFolders(session, folders); } } else if(dome.equals("display_unsubscribed")) { doDisplayUnsubscribedFolders(session); } else if(dome.equals("unsubscribe")) { String[] folders = session.getRequestParameters("paths"); if (folders == null || folders.length == 0) { throw new JwmaException("folder.unsubscribe.missingpaths"); } else { doUnsubscribeFolders(session, folders); } } else { throw new JwmaException("folder.action.invalid"); } }//doDispatchFolderActions /** * Dispatches actions targeting a <tt>message</tt>. * * @param session a <tt>JwmaSession</tt> instance. * @param dome the task as <tt>String</tt>. * * @throws JwmaException if it fails to dispatch the request to a * method (i.e. invalid request), or the action method fails * to execute the task. */ private void doDispatchMessageActions(JwmaSession session, String dome) throws JwmaException { if (dome.equals("display")) { String number = session.getRequestParameter("number"); if (number == null) { throw new JwmaException("message.display.missingnumber"); } else { //check for "magic" numbers if (number.equals("next")) { doDisplayNextMessage(session); } else if (number.equals("prev")) { doDisplayPreviousMessage(session); } else { doDisplayMessage(session, toInt(number)); } } } else if (dome.equals("displaypart")) { String number = session.getRequestParameter("number"); if (number == null) { throw new JwmaException("message.displaypart.missingnumber"); } else { doDisplayMessagePart(session, toInt(number)); return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -