Source Code Cross Referenced for JwmaController.java in  » Web-Mail » Jwma » dtw » webmail » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Mail » Jwma » dtw.webmail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /***
0002:         * jwma Java WebMail
0003:         * Copyright (c) 2000-2003 jwma team
0004:         *
0005:         * jwma is free software; you can distribute and use this source
0006:         * under the terms of the BSD-style license received along with
0007:         * the distribution.
0008:         ***/package dtw.webmail;
0009:
0010:        import java.io.*;
0011:        import java.util.*;
0012:        import java.text.*;
0013:
0014:        import javax.servlet.*;
0015:        import javax.servlet.http.*;
0016:        import javax.mail.*;
0017:        import javax.mail.internet.*;
0018:        import javax.activation.*;
0019:
0020:        import org.apache.log4j.Logger;
0021:        import org.apache.log4j.NDC;
0022:
0023:        import dtw.webmail.util.*;
0024:        import dtw.webmail.util.config.JwmaConfiguration;
0025:        import dtw.webmail.util.config.PostOffice;
0026:        import dtw.webmail.model.*;
0027:        import dtw.webmail.plugin.RandomAppendPlugin;
0028:
0029:        /**
0030:         * Class extending the <tt>HttpServlet</tt> to implement
0031:         * the main controller of jwma.
0032:         * <p>
0033:         * Please see the related documentation for more detailed
0034:         * information on process flow and functionality.
0035:         *
0036:         * @author Dieter Wimberger
0037:         * @version 0.9.7 07/02/2003
0038:         */
0039:        public class JwmaController extends HttpServlet {
0040:
0041:            //logging
0042:            private static Logger log = Logger.getLogger(JwmaController.class);
0043:
0044:            /**
0045:             * Initializes the servlet when it is loaded by the
0046:             * servlet engine.
0047:             * <p>
0048:             * This implementation "boots" jwma by starting up
0049:             * the <tt>JwmaKernel</tt>.
0050:             *
0051:             * @param config the configuration as <tt>ServletConfig</tt>
0052:             *
0053:             * @throws ServletException if initialization fails.
0054:             *
0055:             * @see dtw.webmail.JwmaKernel
0056:             */
0057:            public void init(ServletConfig config) throws ServletException {
0058:
0059:                super .init(config);
0060:
0061:                try {
0062:                    //kernel bootup
0063:                    JwmaKernel myKernel = JwmaKernel.getReference();
0064:
0065:                    //check runtime environment
0066:                    String datadir = config.getInitParameter("datadir");
0067:                    boolean absolutepaths = new Boolean(config
0068:                            .getInitParameter("absolutepaths")).booleanValue();
0069:                    boolean status = new Boolean(config
0070:                            .getInitParameter("status")).booleanValue();
0071:
0072:                    if (!absolutepaths) {
0073:                        datadir = config.getServletContext().getRealPath(
0074:                                datadir);
0075:                    }
0076:
0077:                    //set status
0078:                    myKernel.setJwmaStatusEnabled(status);
0079:                    //setup kernel with location
0080:                    myKernel.setup(datadir);
0081:
0082:                    log.info("Controller inited.");
0083:                    log.info("jwma ready.");
0084:
0085:                } catch (Exception ex) {
0086:                    config.getServletContext()
0087:                            .log(ex, "Initialization failed.");
0088:                    throw new UnavailableException(this , ex.getMessage());
0089:                }
0090:            }//init
0091:
0092:            /**
0093:             * Handles the incoming requests.
0094:             * <p>
0095:             * This implementation ensures authenticated session
0096:             * existence, retrieves the <tt>acton</tt>
0097:             * and <tt>todo</tt> parameters, and dispatches all
0098:             * valid actions to the target dispatchers.
0099:             * <p>
0100:             * The flow of the process is described in the related
0101:             * documentation.
0102:             * <p>
0103:             * Application related errors/exceptions are handled
0104:             * by forwarding the request to an error page, or the actual page
0105:             * in case of an inlined error.
0106:             *
0107:             * @param req a reference to the actual <tt>HttpServletRequest</tt>
0108:             *        instance.
0109:             * @param res a reference to the actual <tt>HttpServletResponse</tt>
0110:             *        instance.
0111:             *
0112:             * @throws ServletException if servlet related operations fail.
0113:             * @throws IOException if i/o operations fail.
0114:             */
0115:            public void service(HttpServletRequest req, HttpServletResponse res)
0116:                    throws ServletException, IOException {
0117:
0118:                try {
0119:                    //prepare NDC with client host name
0120:                    NDC.push(req.getRemoteHost());
0121:
0122:                    //1. Handle session
0123:                    HttpSession websession = req.getSession(true);
0124:                    Object o = websession.getValue("jwma.session");
0125:                    String acton = req.getParameter("acton");
0126:                    String dome = req.getParameter("todo");
0127:
0128:                    //2. update session information
0129:                    JwmaSession session = ((o == null) ? new JwmaSession(
0130:                            websession) : ((JwmaSession) o));
0131:
0132:                    session.setRequest(req);
0133:                    session.setResponse(res);
0134:                    session.setWebSession(websession);
0135:
0136:                    //redirect to login view if no valid websession
0137:                    if (!session.isValidWebSession()) {
0138:                        session.redirect(JwmaKernel.LOGIN_VIEW);
0139:                        return;
0140:                    }
0141:
0142:                    //dispatch actions
0143:                    try {
0144:                        if (acton == null || acton.equals("")) {
0145:                            throw new JwmaException("request.target.missing");
0146:                        } else if (dome == null || dome.equals("")) {
0147:                            throw new JwmaException("request.action.missing");
0148:                        } else if (acton.equals("session")) {
0149:                            doDispatchSessionActions(session, dome);
0150:                        } else {
0151:                            //ensure authentication & postoffice connection
0152:                            session.ensureAuthenticated();
0153:                            session.ensurePostOfficeConnection();
0154:
0155:                            if (acton.equals("folder")) {
0156:                                doDispatchFolderActions(session, dome);
0157:                            } else if (acton.equals("message")) {
0158:                                doDispatchMessageActions(session, dome);
0159:                            } else if (acton.equals("preferences")) {
0160:                                doDispatchPreferencesActions(session, dome);
0161:                            } else {
0162:                                throw new JwmaException(
0163:                                        "request.target.invalid");
0164:                            }
0165:                        }
0166:                    } catch (JwmaException ex) {
0167:                        //log.debug("Exception handling routine",ex);
0168:                        boolean login = false;
0169:
0170:                        //pickup possible relogin
0171:                        String message = ex.getMessage();
0172:                        if (!session.isAuthenticated()
0173:                                && message.equals("request.target.missing")) {
0174:                            login = true;
0175:                        }
0176:                        //log.debug("login="+login);
0177:                        //Log the problem
0178:                        if (!login) {
0179:                            //oneliner resolving of key to message in
0180:                            message = JwmaKernel.getReference().getLogMessage(
0181:                                    "jwma.usererror")
0182:                                    + JwmaKernel.getReference()
0183:                                            .getErrorMessage(message);
0184:
0185:                            //log exception with description and trace if inlined exception
0186:                            //available, else with stacktrace.
0187:                            if (ex.hasException()) {
0188:                                log.error(message, ex.getException());
0189:                            } else {
0190:                                log.error(message, ex);
0191:                            }
0192:                            //store error to be shown
0193:                            session.storeBean("jwma.error", ex);
0194:                        }
0195:
0196:                        //1. if not authenticated forward to login page
0197:                        if (!session.isAuthenticated()) {
0198:                            //ensure all errors are inline
0199:                            ex.setInlineError(true);
0200:                            session.redirect(JwmaKernel.LOGIN_VIEW);
0201:                        } else {
0202:                            //redirect last view with inlined error or
0203:                            //to error page.
0204:                            if (ex.isInlineError()) {
0205:                                session.redirectToActual();
0206:                            } else {
0207:                                session.redirect(JwmaKernel.ERROR_VIEW);
0208:                            }
0209:                        }
0210:                    }
0211:                } finally {
0212:                    NDC.pop();
0213:                }
0214:            }//service
0215:
0216:            /*** Dispatchers ***************************************************/
0217:
0218:            /**
0219:             * Dispatches actions targeting a <tt>session</tt>.
0220:             *
0221:             * @param session a <tt>JwmaSession</tt> instance.
0222:             * @param dome the task as <tt>String</tt>.
0223:             *
0224:             * @throws JwmaException if it fails to dispatch the request to a
0225:             *         method (i.e. invalid request), or the action method fails
0226:             *         to execute the task.
0227:             */
0228:            private void doDispatchSessionActions(JwmaSession session,
0229:                    String dome) throws JwmaException {
0230:
0231:                if (dome.equals("logout")) {
0232:                    session.ensureAuthenticated();
0233:                    doLogout(session);
0234:                } else if (dome.equals("login")) {
0235:                    String user = session.getRequestParameter("username");
0236:                    String passwd = session.getRequestParameter("password");
0237:                    String postoffice = session
0238:                            .getRequestParameter("postoffice");
0239:                    //FIXME:catch invalid and trim the vars!
0240:
0241:                    doLogin(session, user, passwd, postoffice);
0242:                } else if (dome.equals("redirect")) {
0243:                    session.ensureAuthenticated();
0244:                    String view = session.getRequestParameter("view");
0245:                    doRedirect(session, view);
0246:                } else {
0247:                    throw new JwmaException("session.action.invalid");
0248:                }
0249:            }//doDispatchSessionActions
0250:
0251:            /**
0252:             * Dispatches actions targeting a <tt>folder</tt>.
0253:             *
0254:             * @param session a <tt>JwmaSession</tt> instance.
0255:             * @param dome the task as <tt>String</tt>.
0256:             *
0257:             * @throws JwmaException if it fails to dispatch the request to a
0258:             *         method (i.e. invalid request), or the action method fails
0259:             *         to execute the task.
0260:             */
0261:            private void doDispatchFolderActions(JwmaSession session,
0262:                    String dome) throws JwmaException {
0263:
0264:                if (dome.equals("display")) {
0265:                    String name = session.getRequestParameter("path");
0266:                    if (name == null) {
0267:                        throw new JwmaException("folder.display.missingpath");
0268:                    } else {
0269:                        doDisplayFolder(session, name);
0270:                    }
0271:                } else if (dome.equals("sortmessages")) {
0272:                    String criteria = session.getRequestParameter("criteria");
0273:                    if (criteria == null) {
0274:                        throw new JwmaException(
0275:                                "folder.sortmessages.missingcriteria");
0276:                    } else {
0277:                        doSortFolderMessages(session, toInt(criteria));
0278:                    }
0279:                } else if (dome.equals("move")) {
0280:                    String[] folders = session.getRequest().getParameterValues(
0281:                            "paths");
0282:                    String destination = session
0283:                            .getRequestParameter("destination");
0284:
0285:                    if (folders == null || folders.length == 0) {
0286:                        throw new JwmaException("folder.move.missingpaths");
0287:                    } else if (destination == null) {
0288:                        throw new JwmaException(
0289:                                "folder.move.missingdestination");
0290:                    } else {
0291:                        doMoveFolders(session, folders, destination);
0292:                    }
0293:                } else if (dome.equals("create")) {
0294:                    String path = session.getRequestParameter("aname");
0295:                    String type = session.getRequestParameter("type");
0296:                    if (path == null) {
0297:                        throw new JwmaException("folder.create.missingname");
0298:                    } else if (type == null) {
0299:                        throw new JwmaException("folder.create.missingtype");
0300:                    } else {
0301:                        doCreateFolder(session, path, toInt(type));
0302:                    }
0303:                } else if (dome.equals("delete")) {
0304:                    String[] folders = session.getRequestParameters("paths");
0305:                    if (folders == null || folders.length == 0) {
0306:                        throw new JwmaException("folder.delete.missingpaths");
0307:                    } else {
0308:                        doDeleteFolders(session, folders);
0309:                    }
0310:                } else if (dome.equals("display_subscribed")) {
0311:                    doDisplaySubscribedFolders(session);
0312:                } else if (dome.equals("subscribe")) {
0313:                    String[] folders = session.getRequestParameters("paths");
0314:                    if (folders == null || folders.length == 0) {
0315:                        throw new JwmaException("folder.subscribe.missingpaths");
0316:                    } else {
0317:                        doSubscribeFolders(session, folders);
0318:                    }
0319:                } else if (dome.equals("display_unsubscribed")) {
0320:                    doDisplayUnsubscribedFolders(session);
0321:                } else if (dome.equals("unsubscribe")) {
0322:                    String[] folders = session.getRequestParameters("paths");
0323:                    if (folders == null || folders.length == 0) {
0324:                        throw new JwmaException(
0325:                                "folder.unsubscribe.missingpaths");
0326:                    } else {
0327:                        doUnsubscribeFolders(session, folders);
0328:                    }
0329:                } else {
0330:                    throw new JwmaException("folder.action.invalid");
0331:                }
0332:            }//doDispatchFolderActions
0333:
0334:            /**
0335:             * Dispatches actions targeting a <tt>message</tt>.
0336:             *
0337:             * @param session a <tt>JwmaSession</tt> instance.
0338:             * @param dome the task as <tt>String</tt>.
0339:             *
0340:             * @throws JwmaException if it fails to dispatch the request to a
0341:             *         method (i.e. invalid request), or the action method fails
0342:             *         to execute the task.
0343:             */
0344:            private void doDispatchMessageActions(JwmaSession session,
0345:                    String dome) throws JwmaException {
0346:
0347:                if (dome.equals("display")) {
0348:                    String number = session.getRequestParameter("number");
0349:                    if (number == null) {
0350:                        throw new JwmaException("message.display.missingnumber");
0351:                    } else {
0352:                        //check for "magic" numbers
0353:                        if (number.equals("next")) {
0354:                            doDisplayNextMessage(session);
0355:                        } else if (number.equals("prev")) {
0356:                            doDisplayPreviousMessage(session);
0357:                        } else {
0358:                            doDisplayMessage(session, toInt(number));
0359:                        }
0360:                    }
0361:                } else if (dome.equals("displaypart")) {
0362:                    String number = session.getRequestParameter("number");
0363:                    if (number == null) {
0364:                        throw new JwmaException(
0365:                                "message.displaypart.missingnumber");
0366:                    } else {
0367:                        doDisplayMessagePart(session, toInt(number));
0368:                        return;
0369:                    }
0370:                } else if (dome.equals("move")) {
0371:                    String[] messages = session.getRequest()
0372:                            .getParameterValues("numbers");
0373:                    String destination = session
0374:                            .getRequestParameter("destination");
0375:
0376:                    if (messages == null || messages.length == 0) {
0377:                        throw new JwmaException("message.move.missingnumbers");
0378:                    } else if (destination == null) {
0379:                        throw new JwmaException(
0380:                                "message.move.missingdestination");
0381:                    } else {
0382:                        //check if it's about the active message
0383:                        if (messages[0].trim().equals("actual")) {
0384:                            doMoveActualMessage(session, destination);
0385:                        } else {
0386:                            doMoveMessages(session, destination,
0387:                                    toInts(messages));
0388:                        }
0389:                    }
0390:                } else if (dome.equals("compose")) {
0391:                    String to = session.getRequestParameter("to");
0392:                    if (to == null) {
0393:                        to = "";
0394:                    }
0395:                    boolean reply = new Boolean(session
0396:                            .getRequestParameter("reply")).booleanValue();
0397:                    boolean toall = new Boolean(session
0398:                            .getRequestParameter("toall")).booleanValue();
0399:                    boolean togglequote = new Boolean(session
0400:                            .getRequestParameter("togglequote")).booleanValue();
0401:                    //see if its a forward
0402:                    boolean forward = new Boolean(session
0403:                            .getRequestParameter("forward")).booleanValue();
0404:                    //and if attachments should be forwarded
0405:                    boolean attfwd = new Boolean(session
0406:                            .getRequestParameter("withattachments"))
0407:                            .booleanValue();
0408:
0409:                    doComposeMessage(session, to, forward, reply, toall,
0410:                            togglequote, attfwd);
0411:
0412:                    return;
0413:                } else if (dome.equals("composedraft")) {
0414:                    String number = session.getRequestParameter("number");
0415:                    if (number == null) {
0416:                        throw new JwmaException("message.draft.missingnumber");
0417:                    }
0418:                    doComposeDraft(session, toInt(number));
0419:                } else if (dome.equals("delete")) {
0420:                    String[] messages = session.getRequest()
0421:                            .getParameterValues("numbers");
0422:                    if (messages == null || messages.length == 0) {
0423:                        throw new JwmaException("message.delete.missingnumbers");
0424:                    } else {
0425:                        //check if it's about the active message
0426:                        if (messages[0].trim().equals("actual")) {
0427:                            doDeleteActualMessage(session);
0428:                        } else {
0429:                            doDeleteMessages(session, toInts(messages));
0430:                        }
0431:                    }
0432:                } else {
0433:                    throw new JwmaException("message.action.invalid");
0434:                }
0435:            }//doDispatchMessageActions
0436:
0437:            /**
0438:             * Dispatches actions targeting <tt>preferences</tt>.
0439:             *
0440:             * @param session a <tt>JwmaSession</tt> instance.
0441:             * @param dome the task as <tt>String</tt>.
0442:             *
0443:             * @throws JwmaException if it fails to dispatch the request to a
0444:             *         method (i.e. invalid request), or the action method fails
0445:             *         to execute the task.
0446:             */
0447:            private void doDispatchPreferencesActions(JwmaSession session,
0448:                    String dome) throws JwmaException {
0449:
0450:                if (dome.equals("update")) {
0451:                    doUpdatePreferences(session);
0452:                } else if (dome.equals("addmailid")) {
0453:                    doAddMailIdentity(session);
0454:                } else if (dome.equals("removemailid")) {
0455:                    //retrieve the uid
0456:                    String uid = session.getRequestParameter("mid.uid");
0457:                    if (uid == null) {
0458:                        throw new JwmaException("preferences.mailid.missinguid");
0459:                    } else {
0460:                        doRemoveMailIdentity(session, uid);
0461:                        return;
0462:                    }
0463:                } else if (dome.equals("updatemailid")) {
0464:                    //retrieve the uid
0465:                    String uid = session.getRequestParameter("mid.uid");
0466:                    if (uid == null) {
0467:                        throw new JwmaException("preferences.mailid.missinguid");
0468:                    } else {
0469:                        doUpdateMailIdentity(session, uid);
0470:                        return;
0471:                    }
0472:                } else {
0473:                    throw new JwmaException("preferences.action.invalid");
0474:                }
0475:            }//doDispatchPreferencesActions
0476:
0477:            /*** End Dispatchers *************************************************/
0478:
0479:            /*** Session Actions  ************************************************/
0480:
0481:            /**
0482:             * Handles a user login to the jwma system.
0483:             * <p>
0484:             * <ul>
0485:             * <li>Checks if a user account exists.</li>
0486:             * <li>Handles account creation according to settings.</li>
0487:             * <li>Initializes session with the post office.</li>
0488:             * <li>Ensures jwma specific folders are there (trash, archives).</li>
0489:             * <li>Redirects the user to the folder view.</li>
0490:             * <ul>
0491:             *
0492:             * @param session a <tt>JwmaSession</tt> instance.
0493:             * @param username of the user as <tt>String</tt>.
0494:             * @param password of the user as <tt>String</tt>.
0495:             * @param post office the name of a post office as <tt>String</tt>.
0496:             *
0497:             * @throws JwmaException if it fails to execute properly.
0498:             */
0499:            private void doLogin(JwmaSession session, String user,
0500:                    String passwd, String postoffice) throws JwmaException {
0501:                log.debug("doLogin() called");
0502:                boolean storelogin = new Boolean(session
0503:                        .getRequestParameter("remember")).booleanValue();
0504:                JwmaKernel kernel = JwmaKernel.getReference();
0505:                JwmaConfiguration config = kernel.getConfiguration();
0506:                boolean newAccount = false;
0507:
0508:                //set post office host, if not allowed or invalid,
0509:                //it will be the system's set default postoffice host
0510:                session.setPostOffice(config.getPostOfficeByName(postoffice));
0511:                log.debug("PostOffice set to: "
0512:                        + session.getPostOffice().getName());
0513:
0514:                //get the assembled id, session has no user yet
0515:                String identity = session.getUserIdentity(user);
0516:                //check if user exists (i.e. preferences exist for the user)
0517:                if (!kernel.getPrefsPersistencePlugin().isPersistent(identity)) {
0518:                    //get settings to decide strategy
0519:                    if (config.isAccountCreationEnabled()) {
0520:                        newAccount = true;
0521:                    } else {
0522:                        //Forward to set view for account creation or
0523:                        //for telling how it is done
0524:                        session.redirect(JwmaKernel.ACCOUNT_CREATION_VIEW);
0525:                    }
0526:                }
0527:                log.debug("identity=" + identity + ":prefsExist="
0528:                        + (!newAccount));
0529:
0530:                //2. user exists for jwma -> authenticate user
0531:                session.authenticate(user, passwd, newAccount);
0532:                log.debug("Authenticated!");
0533:                //we have now a created store
0534:                JwmaStoreImpl store = session.getJwmaStore();
0535:                log.debug("Have Store!");
0536:                //store storeinfo bean
0537:                session.storeBean("jwma.storeinfo", (JwmaStoreInfo) store);
0538:                //store inboxinfo bean
0539:                session.storeBean("jwma.inboxinfo", store.getInboxInfo());
0540:                //store trashinfo bean
0541:                session.storeBean("jwma.trashinfo", store.getTrashInfo());
0542:                //store actual folder
0543:                session.storeBean("jwma.folder", store.getActualFolder());
0544:
0545:                //load contacts
0546:                if (kernel.getContactManagementPlugin().isPersistent(
0547:                        session.getPreferences().getContactDatabaseID())) {
0548:                    log.debug("Loading contact database.");
0549:                    session.storeBean("jwma.contacts", kernel
0550:                            .getContactManagementPlugin().loadContacts(
0551:                                    session.getPreferences()
0552:                                            .getContactDatabaseID()));
0553:                    log.debug("Loaded user's contact database.");
0554:                } else {
0555:                    log.debug("Creating contact database.");
0556:                    JwmaContactsImpl contacts = kernel
0557:                            .getContactManagementPlugin().createContacts();
0558:                    session.getPreferences().setContactDatabaseID(
0559:                            contacts.getUID());
0560:                    session.storeBean("jwma.contacts", contacts);
0561:                }
0562:                //add cookie if
0563:                if (storelogin) {
0564:                    doSetCookie(session);
0565:                }
0566:                //redirect to folder view
0567:                session.redirect(JwmaKernel.FOLDER_VIEW);
0568:
0569:            }//doLogin
0570:
0571:            /**
0572:             * Logs a user out of a jwma system, ending the session.
0573:             *
0574:             * @param session a <tt>JwmaSession</tt> instance.
0575:             *
0576:             * @throws JwmaException if it fails to execute properly.
0577:             */
0578:            private void doLogout(JwmaSession session) throws JwmaException {
0579:
0580:                try {
0581:                    //perform cleanup of store
0582:                    session.getJwmaStore().cleanup();
0583:                    //ensure saving contactsdb
0584:                    log.debug("Saving contacts database.");
0585:                    JwmaKernel.getReference().getContactManagementPlugin()
0586:                            .saveContacts(
0587:                                    (JwmaContactsImpl) session.getWebSession()
0588:                                            .getValue("jwma.contacts"));
0589:                    //end session
0590:                } catch (JwmaException ex) {
0591:                    throw new JwmaException("session.logout.failed")
0592:                            .setException(ex);
0593:                } finally {
0594:                    session.end();
0595:                    session.redirect(JwmaKernel.LOGOUT_VIEW);
0596:                }
0597:            }//doLogout
0598:
0599:            /**
0600:             * Redirects a request to a given & possible view,
0601:             * to the last view or to the main view otherwise
0602:             * (defined by the root folder view).
0603:             * <i><b>Note:</b> the model instances are updated!</i>
0604:             *
0605:             * @param session a <tt>JwmaSession</tt> instance.
0606:             * @param view the view as <tt>String</tt>.
0607:             *
0608:             * @throws JwmaException if it fails to execute properly.
0609:             */
0610:            private void doRedirect(JwmaSession session, String view) {
0611:                try {
0612:                    if (view != null) {
0613:                        //clean whitespace
0614:                        view.trim();
0615:                        if (view.equals("last")) {
0616:                            session.redirectToLast();
0617:                            return;
0618:                        } else if (view.equals("actual")) {
0619:                            session.redirectToActual();
0620:                            return;
0621:                        } else if (view.equals("folder")) {
0622:                            //dispatch to folder display, invoke update
0623:                            session.getJwmaStore().getActualFolder().update(
0624:                                    session.getJwmaStore());
0625:                            session.redirect(JwmaKernel.FOLDER_VIEW);
0626:                            return;
0627:                        } else if (view.equals("message")) {
0628:                            session.storeBean("jwma.message", session
0629:                                    .getJwmaStore().getActualFolder()
0630:                                    .getActualMessage());
0631:                            session.redirect(JwmaKernel.MESSAGE_VIEW);
0632:                        }
0633:                    }
0634:                    //if fall through here display root folder
0635:                    session.storeBean("jwma.folder", session.getJwmaStore()
0636:                            .resetToRootFolder());
0637:                    session.redirect(JwmaKernel.FOLDER_VIEW);
0638:                    return;
0639:                } catch (Exception ex) {
0640:                    log.error("Failed to redirect.", ex);
0641:                    //JwmaKernel.getReference().debugLog().writeStackTrace(ex);
0642:
0643:                }
0644:            }//doRedirect
0645:
0646:            private void doSetCookie(JwmaSession session) throws JwmaException {
0647:
0648:                HttpServletResponse res = session.getResponse();
0649:                StringBuffer cracker = new StringBuffer("");
0650:                cracker.append(session.getUsername()).append(".").append(
0651:                        MD5.hash(session.getPostOffice().getName()));
0652:                Cookie polly = new Cookie("jwmalogin", cracker.toString());
0653:                polly.setPath("/webmail");
0654:                polly.setMaxAge(COOKIE_EXPIRES);
0655:                res.addCookie(polly);
0656:
0657:            }//doSetCookie
0658:
0659:            /*** End Session Actions  ***********************************************/
0660:
0661:            /*** Folder Actions  ****************************************************/
0662:
0663:            /**
0664:             * Prepares a given folder for display and redirects to the
0665:             * folder view.
0666:             *
0667:             * @param session a <tt>JwmaSession</tt> instance.
0668:             * @param foldername the full path of a valid folder of the
0669:             *        actual store.
0670:             *
0671:             * @throws JwmaException if it fails to execute properly.
0672:             */
0673:            private void doDisplayFolder(JwmaSession session, String foldername)
0674:                    throws JwmaException {
0675:
0676:                JwmaStoreImpl store = session.getJwmaStore();
0677:                //1. check folder existence
0678:                if (!store.checkFolderExistence(foldername)) {
0679:                    throw new JwmaException("folder.display.existence");
0680:                }
0681:                //2. store new actual folder, creation delegated to store
0682:                session.storeBean("jwma.folder", store
0683:                        .setActualFolder(foldername));
0684:
0685:                //redirect to folder view
0686:                session.redirect(JwmaKernel.FOLDER_VIEW);
0687:            }//doDisplayFolder
0688:
0689:            /**
0690:             * Sorts the messages in the actual folder according to
0691:             * the given criteria and redirects to the folder view.
0692:             *
0693:             * @param session a <tt>JwmaSession</tt> instance.
0694:             * @param criteria a valid criteria for sorting the messages
0695:             *        in the actual folder.
0696:             *
0697:             * @throws JwmaException if it fails to execute properly.
0698:             */
0699:            private void doSortFolderMessages(JwmaSession session, int criteria)
0700:                    throws JwmaException {
0701:
0702:                //1. get actual folder reference
0703:                JwmaFolderImpl folder = session.getJwmaStore()
0704:                        .getActualFolder();
0705:                //2. ensure it is a folder holding messages
0706:                if (folder.hasMessages()) {
0707:                    //3. get and sort the message info list
0708:                    folder.getMessageInfoList().sort(criteria);
0709:                }
0710:                //4. remember set sort criteria
0711:                session.getPreferences().setMessageSortCriteria(criteria);
0712:
0713:                session.redirect(JwmaKernel.FOLDER_VIEW);
0714:                return;
0715:            }//doSortFolderMessages
0716:
0717:            /**
0718:             * Deletes the given folders and redirects to the folder view.
0719:             *
0720:             * @param session a <tt>JwmaSession</tt> instance.
0721:             * @param foldernames an array of strings; each <tt>String</tt>
0722:             *        representing the full path of a valid folder of the
0723:             *        actual store.
0724:             *
0725:             * @throws JwmaException if it fails to execute properly.
0726:             */
0727:            private void doDeleteFolders(JwmaSession session,
0728:                    String[] foldernames) throws JwmaException {
0729:
0730:                //delete Folders by path
0731:                session.getJwmaStore().deleteFolders(foldernames);
0732:
0733:                //redirect to folder view
0734:                session.redirect(JwmaKernel.FOLDER_VIEW);
0735:            }//doDeleteFolders
0736:
0737:            /**
0738:             * Creates a folder with a given name and a given type and redirects
0739:             * to the folder view.
0740:             *
0741:             * @param session a <tt>JwmaSession</tt> instance.
0742:             * @param foldername the full path of a possible new folder of the
0743:             *        actual store.
0744:             * @param type the type of the folder to be created as <tt>int</tt>.
0745:             *
0746:             * @throws JwmaException if it fails to execute properly.
0747:             */
0748:            private void doCreateFolder(JwmaSession session, String foldername,
0749:                    int type) throws JwmaException {
0750:
0751:                //create the folder
0752:                session.getJwmaStore().createFolder(foldername, type);
0753:
0754:                //redirect to folder view
0755:                session.redirect(JwmaKernel.FOLDER_VIEW);
0756:
0757:            }//doCreateFolder
0758:
0759:            /**
0760:             * Moves the given folders to a given destination folder
0761:             * and redirects to the folder view.
0762:             *
0763:             * @param session a <tt>JwmaSession</tt> instance.
0764:             * @param foldernames an array of strings; each <tt>String</tt>
0765:             *        representing the full path of a valid folder of the
0766:             *        actual store.
0767:             * @param destination the full path of a valid folder of the
0768:             *        actual store.
0769:             *
0770:             * @throws JwmaException if it fails to execute properly.
0771:             */
0772:            private void doMoveFolders(JwmaSession session,
0773:                    String[] foldernames, String destname) throws JwmaException {
0774:
0775:                //move the folders
0776:                session.getJwmaStore().moveFolders(foldernames, destname);
0777:                //redirect to folder view
0778:                session.redirect(JwmaKernel.FOLDER_VIEW);
0779:            }//doMoveFolders
0780:
0781:            private void doDisplaySubscribedFolders(JwmaSession session) {
0782:                session.redirect(JwmaKernel.SUBSCRIBED_VIEW);
0783:            }//doDisplaySubscribedFolders
0784:
0785:            private void doDisplayUnsubscribedFolders(JwmaSession session) {
0786:                session.redirect(JwmaKernel.UNSUBSCRIBED_VIEW);
0787:            }//doDisplayUnsubscribedFolders
0788:
0789:            private void doUnsubscribeFolders(JwmaSession session,
0790:                    String[] foldernames) throws JwmaException {
0791:
0792:                //Unsubscribe the folders
0793:                session.getJwmaStore().updateFolderSubscription(foldernames,
0794:                        false);
0795:                //redirect to subscription view
0796:                session.redirect(JwmaKernel.SUBSCRIBED_VIEW);
0797:            }//doUnsubscribeFolders
0798:
0799:            private void doSubscribeFolders(JwmaSession session,
0800:                    String[] foldernames) throws JwmaException {
0801:
0802:                //Subscribe the folders
0803:                session.getJwmaStore().updateFolderSubscription(foldernames,
0804:                        true);
0805:                //redirect to subscription view
0806:                session.redirect(JwmaKernel.UNSUBSCRIBED_VIEW);
0807:            }//doUnsubscribeFolders
0808:
0809:            /*** End Folder Actions ***********************************************************/
0810:
0811:            /*** Message Actions *****************************************************/
0812:
0813:            /**
0814:             * Prepares a given message for display and redirects to the
0815:             * message view.
0816:             *
0817:             * @param session a <tt>JwmaSession</tt> instance.
0818:             * @param number of the message to be displayed as <tt>int</tt>.
0819:             *
0820:             * @throws JwmaException if it fails to execute properly.
0821:             */
0822:            private void doDisplayMessage(JwmaSession session, int number)
0823:                    throws JwmaException {
0824:
0825:                JwmaFolderImpl folder = session.getJwmaStore()
0826:                        .getActualFolder();
0827:                //1. check folder existence
0828:                if (!folder.checkMessageExistence(number)) {
0829:                    throw new JwmaException("message.display.existence");
0830:                }
0831:                //store newly created bean
0832:                session
0833:                        .storeBean("jwma.message", folder
0834:                                .getJwmaMessage(number));
0835:
0836:                //redirect to message view
0837:                session.redirect(JwmaKernel.MESSAGE_VIEW);
0838:
0839:            }//doDisplayMessage
0840:
0841:            /**
0842:             * Displays the next message in the actual folder.
0843:             * If there is no message left, it redirects to the folder view.
0844:             *
0845:             * @param session a <tt>JwmaSession</tt> instance.
0846:             *
0847:             * @throws JwmaException if it fails to execute properly.
0848:             */
0849:            private void doDisplayNextMessage(JwmaSession session)
0850:                    throws JwmaException {
0851:
0852:                int next = session.getJwmaStore().getActualFolder()
0853:                        .getNextMessageNumber();
0854:                //if (next != -1) {
0855:                if (next >= 1) {
0856:                    doDisplayMessage(session, next);
0857:                } else {
0858:                    //FIXME: folder view?
0859:                    session.redirect(JwmaKernel.FOLDER_VIEW);
0860:                }
0861:            }//doDisplayNextMessage
0862:
0863:            /**
0864:             * Displays the previous message in the actual folder.
0865:             * If there is no previous message left, it redirects
0866:             * to the folder view.
0867:             *
0868:             * @param session a <tt>JwmaSession</tt> instance.
0869:             *
0870:             * @throws JwmaException if it fails to execute properly.
0871:             */
0872:            private void doDisplayPreviousMessage(JwmaSession session)
0873:                    throws JwmaException {
0874:
0875:                int previous = session.getJwmaStore().getActualFolder()
0876:                        .getPreviousMessageNumber();
0877:                //if (previous != -1) {
0878:                if (previous >= 1) {
0879:                    doDisplayMessage(session, previous);
0880:                } else {
0881:                    //FIXME: folder view?
0882:                    session.redirect(JwmaKernel.FOLDER_VIEW);
0883:                }
0884:            }//doDisplayPreviousMessage
0885:
0886:            /**
0887:             * Deletes the active message and redirects either
0888:             * to the next mail (if exists) or to the folder view.
0889:             *
0890:             * @param session a <tt>JwmaSession</tt> instance.
0891:             *
0892:             * @throws JwmaException if it fails to execute properly.
0893:             */
0894:            private void doDeleteActualMessage(JwmaSession session)
0895:                    throws JwmaException {
0896:
0897:                int next = session.getJwmaStore().getActualFolder()
0898:                        .deleteActualMessage();
0899:                //if (next != -1) {
0900:                if (next >= 1) {
0901:                    doDisplayMessage(session, next);
0902:                } else {
0903:                    //FIXME: folder view?
0904:                    session.redirect(JwmaKernel.FOLDER_VIEW);
0905:                }
0906:            }//doDeleteActiveMessage
0907:
0908:            /**
0909:             * Deletes the given messages and
0910:             * redirects to the folder view.
0911:             *
0912:             * @param session a <tt>JwmaSession</tt> instance.
0913:             * @param numbers the messages to be deleted as <tt>int[]</tt>.
0914:             *
0915:             * @throws JwmaException if it fails to execute properly.
0916:             */
0917:            private void doDeleteMessages(JwmaSession session, int[] numbers)
0918:                    throws JwmaException {
0919:
0920:                //do the job
0921:                session.getJwmaStore().getActualFolder()
0922:                        .deleteMessages(numbers);
0923:
0924:                //redirect to
0925:                //FIXME: really folder view?
0926:                session.redirect(JwmaKernel.FOLDER_VIEW);
0927:
0928:            }//doDeleteMessages
0929:
0930:            /**
0931:             * Moves the active message to a given destination and
0932:             * redirects either to the next message or to the folder view.
0933:             *
0934:             * @param session a <tt>JwmaSession</tt> instance.
0935:             * @param destination the full path of a valid folder of the
0936:             *        actual store as <tt>String</tt>.
0937:             *
0938:             * @throws JwmaException if it fails to execute properly.
0939:             */
0940:            private void doMoveActualMessage(JwmaSession session,
0941:                    String destination) throws JwmaException {
0942:
0943:                int next = session.getJwmaStore().getActualFolder()
0944:                        .moveActualMessage(destination);
0945:                //if (next != -1) {
0946:                if (next >= 1) {
0947:                    doDisplayMessage(session, next);
0948:                } else {
0949:                    //FIXME: folder view?
0950:                    session.redirect(JwmaKernel.FOLDER_VIEW);
0951:                }
0952:            }//doMoveActiveMessage
0953:
0954:            /**
0955:             * Moves the given messages and
0956:             * redirects to the folder view.
0957:             *
0958:             * @param session a <tt>JwmaSession</tt> instance.
0959:             * @param numbers the messages to be moved as <tt>int[]</tt>.
0960:             * @param destination the full path of a valid folder of the
0961:             *        actual store as <tt>String</tt>.
0962:             *
0963:             * @throws JwmaException if it fails to execute properly.
0964:             */
0965:            private void doMoveMessages(JwmaSession session,
0966:                    String destination, int[] numbers) throws JwmaException {
0967:
0968:                //do the job
0969:                session.getJwmaStore().getActualFolder().moveMessages(numbers,
0970:                        destination);
0971:                //redirect to
0972:                //FIXME: really folder view?
0973:                session.redirect(JwmaKernel.FOLDER_VIEW);
0974:            }//doMoveMessages
0975:
0976:            /**
0977:             * Prepares a message model instance for
0978:             * a message to be composed and redirects to
0979:             * the compose view.
0980:             *
0981:             * @param session a <tt>JwmaSession</tt> instance.
0982:             * @param reply flags whether it is a reply or not.
0983:             * @param toall flags whether it is a reply to all senders
0984:             *        or not.
0985:             * @param togglequote flags whether auto-quoting will be toggled for this message.
0986:             * @param attfwd toggles whether attachments will be forwarded.
0987:             *
0988:             * @throws JwmaException if it fails to execute properly.
0989:             */
0990:            private void doComposeMessage(JwmaSession session, String to,
0991:                    boolean forward, boolean reply, boolean toall,
0992:                    boolean togglequote, boolean attfwd) throws JwmaException {
0993:
0994:                JwmaFolderImpl folder = session.getJwmaStore()
0995:                        .getActualFolder();
0996:                JwmaComposeMessage message = null;
0997:                JwmaDisplayMessage actualmsg = null;
0998:
0999:                if (reply || forward) {
1000:                    actualmsg = (JwmaDisplayMessage) folder.getActualMessage();
1001:                }
1002:                if (reply) {
1003:                    message = JwmaComposeMessage.createReply(actualmsg, toall,
1004:                            session.getPreferences(), togglequote);
1005:                } else if (forward) {
1006:                    message = JwmaComposeMessage.createForward(session
1007:                            .getMailSession(), actualmsg, to, session
1008:                            .getPreferences(), togglequote, attfwd);
1009:                } else {
1010:                    message = JwmaComposeMessage
1011:                            .createJwmaComposeMessage(session.getMailSession());
1012:                    if (to != null) {
1013:                        try {
1014:                            message.setTo(to);
1015:                        } catch (MessagingException mex) {
1016:                            //handle probably?
1017:                        }
1018:                    }
1019:                }
1020:
1021:                //store compose message bean
1022:                session.storeBean("jwma.composemessage", message);
1023:
1024:                //redirect to compose view
1025:                session.redirect(JwmaKernel.COMPOSE_VIEW);
1026:            }//doComposeMessage
1027:
1028:            /**
1029:             * Prepares a draft for composing and redirects to the
1030:             * compose view.
1031:             *
1032:             * @param session a <tt>JwmaSession</tt> instance.
1033:             * @param number of the draft message to be continued as
1034:             *       <tt>int</tt>.
1035:             *
1036:             * @throws JwmaException if it fails to execute properly.
1037:             */
1038:            private void doComposeDraft(JwmaSession session, int number)
1039:                    throws JwmaException {
1040:
1041:                JwmaFolderImpl folder = session.getJwmaStore()
1042:                        .getActualFolder();
1043:                //1. check folder existence
1044:                if (!folder.checkMessageExistence(number)) {
1045:                    throw new JwmaException("message.draft.existence");
1046:                }
1047:
1048:                JwmaMessage message = folder.getDraftMessage(number);
1049:
1050:                //store newly created bean
1051:                //store compose message bean
1052:                session.storeBean("jwma.composemessage", message);
1053:
1054:                //redirect to compose view
1055:                session.redirect(JwmaKernel.COMPOSE_VIEW);
1056:            }//doComposeDraft
1057:
1058:            /**
1059:             * Outputs a given message part of the actual message.
1060:             *
1061:             * @param session a <tt>JwmaSession</tt> instance.
1062:             * @param number of the message part to be displayed
1063:             *        as <tt>int</tt>.
1064:             *
1065:             * @throws JwmaException if it fails to execute properly.
1066:             */
1067:            private void doDisplayMessagePart(JwmaSession session, int number)
1068:                    throws JwmaException {
1069:
1070:                try {
1071:
1072:                    //1.get Messagepart
1073:                    JwmaFolderImpl folder = session.getJwmaStore()
1074:                            .getActualFolder();
1075:                    JwmaDisplayMessage actualmsg = (JwmaDisplayMessage) folder
1076:                            .getActualMessage();
1077:
1078:                    JwmaMessagePartImpl part = (JwmaMessagePartImpl) actualmsg
1079:                            .getMessagePart(number);
1080:
1081:                    //2. set content type and file name
1082:                    String type = new ContentType(part.getContentType())
1083:                            .getBaseType();
1084:                    String fname = part.getName();
1085:                    //we do it all for fun or not?
1086:                    if (fname == null) {
1087:                        fname = "easter.egg";
1088:                    }
1089:                    session.getResponse().setContentType(type);
1090:                    session.getResponse().setHeader("Content-Disposition",
1091:                            "filename=" + fname);
1092:
1093:                    //3. stream out part
1094:                    ServletOutputStream out = session.getResponse()
1095:                            .getOutputStream();
1096:                    folder.writeMessagePart(part.getPart(), out);
1097:
1098:                } catch (Exception ex) {
1099:                    throw new JwmaException("message.displaypart.failed")
1100:                            .setException(ex);
1101:                }
1102:            }//doDisplayMessagePart
1103:
1104:            /*** End Message Actions ************************************************/
1105:
1106:            /*** Preferences Actions ************************************************/
1107:
1108:            /**
1109:             * Updates the preferences for system settings.
1110:             * <p>
1111:             * This method handles first-time login too,
1112:             * just allowing to set firstname and lastname.
1113:             *
1114:             * @param session a <tt>JwmaSession</tt> instance.
1115:             *
1116:             * @throws JwmaException if it fails to execute properly.
1117:             */
1118:            private void doUpdatePreferences(JwmaSession session)
1119:                    throws JwmaException {
1120:
1121:                JwmaPreferencesImpl prefs = session.getPreferences();
1122:                JwmaStoreImpl store = session.getJwmaStore();
1123:                //if anything is set then to the default
1124:                JwmaMailIdentityImpl mailid = null;
1125:
1126:                //collect data
1127:                String firstname = session.getRequestParameter("firstname");
1128:                String lastname = session.getRequestParameter("lastname");
1129:                String quotechar = session.getRequestParameter("quotechar");
1130:                String rootfolder = session.getRequestParameter("rootfolder");
1131:                String trashfolder = session.getRequestParameter("trashfolder");
1132:                String draftfolder = session.getRequestParameter("draftfolder");
1133:                String sentmailarchive = session
1134:                        .getRequestParameter("sentmailarchive");
1135:                String readmailarchive = session
1136:                        .getRequestParameter("readmailarchive");
1137:                String language = session.getRequestParameter("language");
1138:                String msgprocessor = session
1139:                        .getRequestParameter("msgprocessor");
1140:                String dfpattern = session.getRequestParameter("dfpattern");
1141:                String style = session.getRequestParameter("style");
1142:
1143:                boolean autoquote = new Boolean(session
1144:                        .getRequestParameter("autoquote")).booleanValue();
1145:                boolean autoempty = new Boolean(session
1146:                        .getRequestParameter("autoempty")).booleanValue();
1147:                boolean automoveread = new Boolean(session
1148:                        .getRequestParameter("automoveread")).booleanValue();
1149:                boolean autoarchivesent = new Boolean(session
1150:                        .getRequestParameter("autoarchivesent")).booleanValue();
1151:                boolean expert = new Boolean(session
1152:                        .getRequestParameter("expert")).booleanValue();
1153:                boolean inlined = new Boolean(session
1154:                        .getRequestParameter("inlined")).booleanValue();
1155:
1156:                prefs.setAutoQuote(autoquote);
1157:                prefs.setAutoEmpty(autoempty);
1158:                prefs.setAutoMoveRead(automoveread);
1159:                prefs.setAutoArchiveSent(autoarchivesent);
1160:                prefs.setDisplayingInlined(inlined);
1161:
1162:                //if not expert, try to update the default mail identity
1163:                if (!expert && !prefs.isExpert()) {
1164:                    //log.debug("updatePreferences():updating mail identity");
1165:                    updateMailIdentity(session, (JwmaMailIdentityImpl) prefs
1166:                            .getMailIdentity());
1167:                }
1168:                prefs.setExpert(expert);
1169:
1170:                if (firstname != null) {
1171:                    prefs.setFirstname(firstname);
1172:                }
1173:                if (lastname != null) {
1174:                    prefs.setLastname(lastname);
1175:                }
1176:                if (quotechar != null) {
1177:                    prefs.setQuoteChar(quotechar);
1178:                }
1179:
1180:                //update send-mail archive
1181:                if (autoarchivesent) {
1182:                    if (!prefs.isExpert()) {
1183:                        //set the new folder, will set the prefs
1184:                        store.setSentMailFolder(prefs.getSentMailArchive());
1185:                    } else {
1186:                        //set the new folder, will set the prefs
1187:                        store.setSentMailFolder(sentmailarchive);
1188:                    }
1189:                }
1190:
1191:                if (automoveread) {
1192:                    if (!prefs.isExpert()) {
1193:                        //set the new folder, will set the prefs
1194:                        store.setReadMailFolder(prefs.getReadMailArchive());
1195:                    } else {
1196:                        //set the new folder, will set the prefs
1197:                        store.setReadMailFolder(readmailarchive);
1198:                    }
1199:                }
1200:
1201:                if (trashfolder != null) {
1202:                    store.setTrashFolder(trashfolder);
1203:                }
1204:                if (draftfolder != null) {
1205:                    store.setDraftFolder(draftfolder);
1206:                }
1207:                if (rootfolder != null) {
1208:                    store.updateRootFolder(rootfolder);
1209:                }
1210:
1211:                if (msgprocessor != null) {
1212:                    prefs.setMessageProcessorName(msgprocessor);
1213:                }
1214:
1215:                if (dfpattern != null) {
1216:                    prefs.setDateFormat(new SimpleDateFormat(dfpattern));
1217:                }
1218:
1219:                if (language != null) {
1220:                    //create locale
1221:                    Locale locale = new Locale(language, "");
1222:                    //set the locale if it is supported and not the one that
1223:                    //is already set
1224:                    if (JwmaKernel.getReference().getConfiguration().getI18N()
1225:                            .isSupportedViewLocale(locale)
1226:                            && !locale.equals(prefs.getLocale())) {
1227:                        prefs.setLocale(locale);
1228:                        session.setLocale();
1229:                    }
1230:                }
1231:                if (style != null) {
1232:                    prefs.setStyle(style);
1233:                }
1234:
1235:                //save
1236:                session.savePreferences();
1237:                session.redirect(JwmaKernel.PREFERENCES_VIEW);
1238:
1239:            }//doUpdatePreferences
1240:
1241:            /**
1242:             * Adds a mail identity.
1243:             *
1244:             * @param session a <tt>JwmaSession</tt> instance.
1245:             *
1246:             * @throws JwmaException if it fails to execute properly.
1247:             */
1248:            private void doAddMailIdentity(JwmaSession session)
1249:                    throws JwmaException {
1250:                JwmaPreferencesImpl prefs = session.getPreferences();
1251:                JwmaMailIdentityImpl mid = prefs.createMailIdentity();
1252:
1253:                RandomAppendPlugin rap = JwmaKernel.getReference()
1254:                        .getRandomAppendPlugin();
1255:
1256:                //retrieve parameters
1257:                String name = session.getRequestParameter("mid.name");
1258:                String from = session.getRequestParameter("mid.from");
1259:                boolean defaultmid = new Boolean(session
1260:                        .getRequestParameter("mid.default")).booleanValue();
1261:
1262:                if (name != null && name.length() > 0) {
1263:                    mid.setName(name);
1264:                } else {
1265:                    throw new JwmaException("preferences.mailid.noname");
1266:                }
1267:                if (from != null) {
1268:                    try {
1269:                        InternetAddress.parse(from);
1270:                    } catch (AddressException adrex) {
1271:                        throw new JwmaException(
1272:                                "preferences.update.addressinvalid", true);
1273:                    }
1274:                    mid.setFrom(from);
1275:                }
1276:                //add the new instance
1277:                prefs.addMailIdentity(mid);
1278:
1279:                //and set it as default if requested
1280:                if (defaultmid) {
1281:                    prefs.setDefaultMailIdentity(mid.getUID());
1282:                }
1283:
1284:                //save
1285:                session.savePreferences();
1286:                //redirect
1287:                session.redirect(JwmaKernel.PREFERENCES_VIEW);
1288:            }//doAddMailIdentity
1289:
1290:            /**
1291:             * Removes a mail identity.
1292:             *
1293:             * @param session a <tt>JwmaSession</tt> instance.
1294:             *
1295:             * @throws JwmaException if it fails to execute properly.
1296:             */
1297:            private void doRemoveMailIdentity(JwmaSession session, String uid)
1298:                    throws JwmaException {
1299:                JwmaPreferencesImpl prefs = session.getPreferences();
1300:
1301:                if (prefs.existsMailIdentity(uid)) {
1302:                    //remove the instance
1303:                    prefs.removeMailIdentity(uid);
1304:                } else {
1305:                    throw new JwmaException("preferences.mailid.invaliduid");
1306:                }
1307:
1308:                //save
1309:                session.savePreferences();
1310:                //redirect
1311:                session.redirect(JwmaKernel.PREFERENCES_VIEW);
1312:            }//doRemoveMailIdentity
1313:
1314:            /**
1315:             * Updates a mail identity.
1316:             *
1317:             * @param session a <tt>JwmaSession</tt> instance.
1318:             *
1319:             * @throws JwmaException if it fails to execute properly.
1320:             */
1321:            private void doUpdateMailIdentity(JwmaSession session, String uid)
1322:                    throws JwmaException {
1323:                JwmaPreferencesImpl prefs = session.getPreferences();
1324:
1325:                if (prefs.existsMailIdentity(uid)) {
1326:                    //make the update
1327:                    updateMailIdentity(session, (JwmaMailIdentityImpl) prefs
1328:                            .getMailIdentity(uid));
1329:                } else {
1330:                    throw new JwmaException("preferences.mailid.invaliduid");
1331:                }
1332:
1333:                //save
1334:                session.savePreferences();
1335:                //redirect
1336:                session.redirect(JwmaKernel.PREFERENCES_VIEW);
1337:            }//doUpdateMailIdentity
1338:
1339:            private void updateMailIdentity(JwmaSession session,
1340:                    JwmaMailIdentityImpl mid) throws JwmaException {
1341:
1342:                RandomAppendPlugin rap = JwmaKernel.getReference()
1343:                        .getRandomAppendPlugin();
1344:                JwmaPreferencesImpl prefs = session.getPreferences();
1345:
1346:                //collect data
1347:                String name = session.getRequestParameter("mid.name");
1348:                String from = session.getRequestParameter("mid.from");
1349:                String replyto = session.getRequestParameter("mid.replyto");
1350:                String signature = session.getRequestParameter("mid.signature");
1351:                String note = session.getRequestParameter("mid.note");
1352:                String rndappendtype = session
1353:                        .getRequestParameter("mid.rndappendtype");
1354:                String relcontact = session
1355:                        .getRequestParameter("mid.relcontact");
1356:                boolean defaultmid = new Boolean(session
1357:                        .getRequestParameter("mid.default")).booleanValue();
1358:
1359:                boolean autosigning = new Boolean(session
1360:                        .getRequestParameter("mid.autosigning")).booleanValue();
1361:
1362:                if (name != null) {
1363:                    mid.setName(name);
1364:                }
1365:                if (from != null) {
1366:                    try {
1367:                        InternetAddress.parse(from);
1368:                    } catch (AddressException adrex) {
1369:                        throw new JwmaException(
1370:                                "preferences.update.addressinvalid", true);
1371:                    }
1372:                    mid.setFrom(from);
1373:                }
1374:                if (replyto != null) {
1375:                    try {
1376:                        InternetAddress.parse(replyto);
1377:                    } catch (AddressException adrex) {
1378:                        throw new JwmaException(
1379:                                "preferences.update.addressinvalid", true);
1380:                    }
1381:                    mid.setReplyTo(replyto);
1382:                }
1383:                if (signature != null) {
1384:                    mid.setSignature(signature);
1385:                } else {
1386:                    mid.setSignature("");
1387:                }
1388:                if (note != null) {
1389:                    mid.setNote(note);
1390:                } else {
1391:                    mid.setNote("");
1392:                }
1393:
1394:                if (rndappendtype != null
1395:                        && rap != null
1396:                        && rap.supportsAppendType(rndappendtype, prefs
1397:                                .getLocale())) {
1398:                    mid.setRandomAppendType(rndappendtype, prefs.getLocale());
1399:                }
1400:
1401:                if (relcontact != null) {
1402:                    mid.setRelatedContact(relcontact);
1403:                } else {
1404:                    mid.setRelatedContact("");
1405:                }
1406:                mid.setAutoSigning(autosigning);
1407:
1408:                if (defaultmid) {
1409:                    prefs.setDefaultMailIdentity(mid.getUID());
1410:                }
1411:            }//updateMailIdenitity
1412:
1413:            /*** End Preferences Actions *******************************************/
1414:
1415:            /*** Helper methods ****************************************************/
1416:
1417:            /**
1418:             * Converts a <tt>String</tt> into an <tt>int</tt>.
1419:             */
1420:            private int toInt(String number) throws JwmaException {
1421:                try {
1422:                    return Integer.parseInt(number);
1423:                } catch (Exception ex) {
1424:                    throw new JwmaException("jwma.number.format");
1425:                }
1426:            }//toInt
1427:
1428:            /**
1429:             * Converts a <tt>String[]</tt> into an <tt>int[]</tt>.
1430:             * Performs a trim on each string.
1431:             */
1432:            private int[] toInts(String[] numbers) throws JwmaException {
1433:                int[] msgnum = new int[numbers.length];
1434:                for (int i = 0; i < numbers.length; i++) {
1435:                    msgnum[i] = toInt(numbers[i].trim());
1436:                }
1437:                return msgnum;
1438:            }//toInts
1439:
1440:            /*** End Helper methods ************************************************/
1441:
1442:            /**
1443:             * Returns servlet info as <tt>String</tt>.
1444:             *
1445:             * @return Info about this servlet as <tt>String</tt>.
1446:             */
1447:            public String getServletInfo() {
1448:                return "jwma (Java WebMail) Controller Servlet";
1449:            }//getServletInfo()
1450:
1451:            private static final int COOKIE_EXPIRES = (60 * 24 * 60 * 60); // d -> h -> min -> s
1452:        }//class JwmaController
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.