📄 jwmacontactscontroller.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 org.apache.log4j.Logger;import org.apache.log4j.NDC;import dtw.webmail.util.*;import dtw.webmail.model.*;public class JwmaContactsController extends HttpServlet { //logging private static Logger log = Logger.getLogger(JwmaContactsController.class); /** * 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> * 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 { NDC.push(req.getRemoteHost()); //1. Handle session HttpSession websession = req.getSession(true); Object o = websession.getValue("jwma.session"); Object c = websession.getValue("jwma.contacts"); 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; } //3. prepare the contact database JwmaContactsImpl ctdb = ((c == null) ? null: ((JwmaContactsImpl) c)); //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 { //ensure authentication session.ensureAuthenticated(); //session.ensurePostOfficeConnection(); if (acton.equals("database")) { doDispatchDatabaseActions(session, dome, ctdb); } else if (acton.equals("group")) { doDispatchGroupActions(session, dome, ctdb); } else if (acton.equals("contact")) { doDispatchContactActions(session, dome, ctdb); } else { throw new JwmaException("request.target.invalid"); } } } catch (JwmaException ex) { //1. if not authenticated forward to login page if (!session.isAuthenticated()) { session.redirect(JwmaKernel.LOGIN_VIEW); } else { String message = ex.getMessage(); //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 session.storeBean("jwma.error", ex); //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>JwmaContactsImpl</tt>. * * @param session a <tt>JwmaSession</tt> instance. * @param dome the task as <tt>String</tt>. * @param ctdb a <tt>JwmaContactsImpl</tt> instance. * * @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 doDispatchDatabaseActions( JwmaSession session, String dome, JwmaContactsImpl ctdb) throws JwmaException { if (dome.equals("display")) { doDisplayDatabase(session, ctdb); } else { //ensure ctdb loaded if (ctdb == null) { throw new JwmaException("database.action.notloaded"); } if (dome.equals("store")) { doStoreDatabase(session, ctdb); } else if (dome.equals("export")) { //check type //do export in type } else if (dome.equals("setfilter")) { String filtertype = session.getRequestParameter("filtertype"); if (filtertype == null || filtertype.length() == 0) { throw new JwmaException("database.action.filtertypemissing"); } String filter = session.getRequestParameter("filter"); if (filter == null || filter.length() == 0) { throw new JwmaException("database.action.filtermissing"); } doSetContactsFilter(session, ctdb, filtertype, filter); } else { throw new JwmaException("database.action.invalid"); } } }//doDispatchDatabaseActions /** * Dispatches actions targeting a <tt>ContactGroup</tt>. * * @param session a <tt>JwmaSession</tt> instance. * @param dome the task as <tt>String</tt>. * @param ctdb a <tt>JwmaContactsImpl</tt> instance. * * @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 doDispatchGroupActions( JwmaSession session, String dome, JwmaContactsImpl ctdb) throws JwmaException { log.debug("Dispatching group action=" + dome); String[] gids = session.getRequestParameters("group.id"); if (dome.equals("display")) { if (gids == null || gids[0] == null || gids[0].equals("")) { throw new JwmaException("group.action.gidmissing"); } else { doDisplayGroup(session, ctdb, gids[0]); } } else if (dome.equals("edit")) { if (gids == null || gids[0] == null || gids[0].equals("")) { throw new JwmaException("group.action.gidmissing"); } else { doEditGroup(session, ctdb, gids[0]); } } else if (dome.equals("update")) { if (gids == null || gids[0] == null || gids[0].equals("")) { throw new JwmaException("group.action.gidmissing"); } doUpdateGroup(session, ctdb, gids[0]); } else if (dome.equals("create")) { doCreateGroup(session, ctdb); } else if (dome.equals("delete")) { if (gids == null || gids.length == 0) { throw new JwmaException("group.action.gidmissing"); } doDeleteGroups(session, ctdb, gids); } else if (dome.equals("removecontacts")) { if (gids == null || gids[0] == null || gids[0].equals("")) { throw new JwmaException("group.action.gidmissing"); } String[] cids = session.getRequestParameters("contact.member.id"); if (cids == null || cids.length == 0) { throw new JwmaException("group.action.cidmissing"); } doRemoveContacts(session, ctdb, gids[0], cids); } else if (dome.equals("addcontacts")) { if (gids == null || gids[0] == null || gids[0].equals("")) { throw new JwmaException("group.action.gidmissing"); } String[] cids = session.getRequestParameters("contact.nomember.id"); if (cids == null || cids.length == 0) { throw new JwmaException("group.action.cidmissing"); } doAddContacts(session, ctdb, gids[0], cids); } else { throw new JwmaException("group.action.invalid"); } }//doDispatchGroupActions /** * Dispatches actions targeting a <tt>Contact</tt>. * * @param session a <tt>JwmaSession</tt> instance. * @param dome the task as <tt>String</tt>. * @param ctdb a <tt>JwmaContactsImpl</tt> instance. * * @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 doDispatchContactActions( JwmaSession session, String dome, JwmaContactsImpl ctdb) throws JwmaException { if (dome.equals("create")) { doUpdateContact(session, ctdb, null); } else if (dome.equals("import")) { Object ct = session.getWebSession().getValue("jwma.contacts.import"); if (ct == null) { throw new JwmaException("contact.action.importmissing"); } doImportContact(session, ctdb, (JwmaContactImpl) ct); } else { String[] cids = session.getRequestParameters("contact.id"); if (cids == null || cids[0] == null || cids[0].equals("")) { throw new JwmaException("contact.action.idmissing"); } if (dome.equals("display")) { doDisplayContact(session, ctdb, cids[0]); } else if (dome.equals("edit")) { doEditContact(session, ctdb, cids[0]); } else if (dome.equals("update")) { doUpdateContact(session, ctdb, cids[0]); } else if (dome.equals("delete")) { doDeleteContacts(session, ctdb, cids); } else { throw new JwmaException("contact.action.invalid"); } } }//doDispatchContactActions /*** BEGIN database actions **********************************************************/ /** * Prepares the user's contact database and redirects him to the contacts * view. * * @param session a <tt>JwmaSession</tt> instance. * @param ctdb a <tt>JwmaContactsImpl</tt> instance. * * @throws JwmaException if it fails to execute properly. */ private void doDisplayDatabase(JwmaSession session, JwmaContactsImpl ctdb) throws JwmaException { session.redirect(JwmaKernel.CONTACTS_VIEW); return; }//doDisplayDatabase /** * Stores the user's contact database and redirects him to the contacts * view. * * @param session a <tt>JwmaSession</tt> instance. * @param ctdb a <tt>JwmaContactsImpl</tt> instance. * * @throws JwmaException if it fails to execute properly. */ private void doStoreDatabase(JwmaSession session, JwmaContactsImpl ctdb) throws JwmaException { JwmaKernel.getReference().getContactManagementPlugin() .saveContacts(ctdb); session.redirect(JwmaKernel.CONTACTS_VIEW); return; }//doStoreDatabase private void doSetContactsFilter(JwmaSession session, JwmaContactsImpl ctdb, String filtertype, String filter) throws JwmaException { //log.debug("Setting filter type="+filtertype+" value="+filter); if (filtertype.equals("alphabetic")) { if (filter.equals("none")) { ctdb.setContactFilter(null); //log.debug("Alphabetic filter reset."); } else { ctdb.setContactFilter(new LastnameStartsWithFilter(filter)); //log.debug("Alphabetic filter set to "+filter); } } else if (filtertype.equals("category")) { if (filter.equals("none")) { ctdb.setCategoryFilter(""); } else { ctdb.setCategoryFilter(filter); } } else { throw new JwmaException("database.setfilter.uknowntype"); } session.redirectToActual(); //redirect(JwmaKernel.CONTACTS_VIEW); return; }//doSetContactsFilter /*** END database actions ************************************************************/ /*** BEGIN group actions ************************************************************/ /** * Displays a contact group entry from the contact database. * * @param session a <tt>JwmaSession</tt> instance. * @param ctdb a <tt>JwmaContactsImpl</tt> instance. * @param cid a contact identifier as <tt>String</tt>. * * @throws JwmaException if it fails to execute properly. */ private void doDisplayGroup(JwmaSession session, JwmaContactsImpl ctdb, String cid) throws JwmaException { if (ctdb.containsContactGroup(cid)) { session.storeBean("jwma.contacts.group", ctdb.getContactGroup(cid)); } else { throw new JwmaException("group.action.invalidgroup", true); } session.redirect(JwmaKernel.CONTACTGROUP_VIEW); return; }//doDisplayGroup /** * Displays a contact entry from the contact database for editing. * * @param session a <tt>JwmaSession</tt> instance. * @param ctdb a <tt>JwmaContactsImpl</tt> instance. * @param cid a contact identifier as <tt>String</tt>. * * @throws JwmaException if it fails to execute properly. */ private void doEditGroup(JwmaSession session, JwmaContactsImpl ctdb, String cid) throws JwmaException { if (ctdb.containsContactGroup(cid)) { session.storeBean("jwma.contacts.group", ctdb.getContactGroup(cid)); } else { throw new JwmaException("group.action.invalidgroup", true); } session.redirect(JwmaKernel.CONTACTGROUP_EDIT_VIEW); return; }//doEditGroup /** * Updates a group entry in the contact database. * * @param session a <tt>JwmaSession</tt> instance. * @param ctdb a <tt>JwmaContactsImpl</tt> instance. * * @throws JwmaException if it fails to execute properly. */ private void doUpdateGroup(JwmaSession session, JwmaContactsImpl ctdb, String id) throws JwmaException { //check if id exists if (ctdb.containsContactGroup(id)) { String name = session.getRequestParameter("group.name"); String comments = session.getRequestParameter("group.comments"); //check if groupname already exists if (ctdb.containsContactGroupName(name) && !name.equals(ctdb.getContactGroup(id).getName())) { throw new JwmaException("group.name.duplicate"); } else { JwmaContactGroupImpl group = (JwmaContactGroupImpl)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -