001: package org.claros.intouch.webmail.services;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.util.StringTokenizer;
006:
007: import javax.servlet.ServletException;
008: import javax.servlet.http.HttpServletRequest;
009: import javax.servlet.http.HttpServletResponse;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.claros.commons.auth.models.AuthProfile;
014: import org.claros.commons.mail.models.ConnectionMetaHandler;
015: import org.claros.commons.mail.models.ConnectionProfile;
016: import org.claros.commons.mail.utility.Constants;
017: import org.claros.intouch.common.services.BaseService;
018: import org.claros.intouch.webmail.controllers.FolderController;
019: import org.claros.intouch.webmail.factory.FolderControllerFactory;
020: import org.claros.intouch.webmail.models.FolderDbObject;
021: import org.claros.intouch.webmail.controllers.MailController;
022: import org.claros.intouch.webmail.factory.MailControllerFactory;
023:
024: public class DeleteMailService extends BaseService {
025:
026: /**
027: *
028: */
029: private static final long serialVersionUID = -475691838460818926L;
030: private static Log log = LogFactory.getLog(DeleteMailService.class);
031:
032: /**
033: * The doGet method of the servlet. <br>
034: *
035: * This method is called when a form has its tag value method equals to get.
036: *
037: * @param request the request send by the client to the server
038: * @param response the response send by the server to the client
039: * @throws ServletException if an error occurred
040: * @throws IOException if an error occurred
041: */
042: public void doGet(HttpServletRequest request,
043: HttpServletResponse response) throws ServletException,
044: IOException {
045:
046: response.setHeader("Expires", "-1");
047: response.setHeader("Pragma", "no-cache");
048: response.setHeader("Cache-control", "no-cache");
049: response.setHeader("Content-Type", "text/html; charset=utf-8");
050:
051: PrintWriter out = response.getWriter();
052:
053: String ids = request.getParameter("ids");
054: String folder = request.getParameter("folder");
055: if (ids != null && folder != null) {
056: try {
057: AuthProfile auth = getAuthProfile(request);
058: ConnectionMetaHandler handler = (ConnectionMetaHandler) request
059: .getSession().getAttribute("handler");
060: ConnectionProfile profile = (ConnectionProfile) request
061: .getSession().getAttribute("profile");
062:
063: MailControllerFactory factory = new MailControllerFactory(
064: auth, profile, handler, folder);
065: MailController mailCont = factory.getMailController();
066:
067: // determine the ids of the messages to be deleted or moved to the trash folder
068: if (ids != null && ids.length() > 1) {
069: StringTokenizer token = new StringTokenizer(ids
070: .substring(1), "_");
071: int size = token.countTokens();
072:
073: int msgs[] = new int[size];
074: int counter = 0;
075: while (token.hasMoreTokens()) {
076: msgs[counter] = Integer.parseInt(token
077: .nextToken());
078: counter++;
079: }
080:
081: // get the trash folder object
082: FolderControllerFactory fFactory = new FolderControllerFactory(
083: auth, profile, handler);
084: FolderController foldCont = fFactory
085: .getFolderController();
086: FolderDbObject fItem = foldCont.getTrashFolder();
087:
088: // action time
089: if (profile.getProtocol().equals(Constants.POP3)) {
090: if (fItem.getId().equals(folder)) {
091: // mailCont.deleteEmails(msgs);
092: mailCont.markAsDeleted(msgs);
093: } else {
094: mailCont.moveEmails(msgs, ""
095: + fItem.getId());
096: }
097: } else {
098: if (fItem.getFolderName().equals(folder)) {
099: mailCont.markAsDeleted(msgs);
100: } else {
101: mailCont.moveEmails(msgs, fItem
102: .getFolderName());
103: }
104: }
105: }
106: out.print("ok");
107: } catch (Exception e) {
108: log.info("error deleting mail", e);
109: out.print("fail");
110: }
111: }
112: }
113:
114: /**
115: * The doPost method of the servlet. <br>
116: *
117: * This method is called when a form has its tag value method equals to post.
118: *
119: * @param request the request send by the client to the server
120: * @param response the response send by the server to the client
121: * @throws ServletException if an error occurred
122: * @throws IOException if an error occurred
123: */
124: public void doPost(HttpServletRequest request,
125: HttpServletResponse response) throws ServletException,
126: IOException {
127: doGet(request, response);
128: }
129:
130: }
|