001: package org.claros.mini.actions;
002:
003: import java.util.ArrayList;
004: import java.util.Locale;
005:
006: import javax.servlet.http.HttpServletRequest;
007: import javax.servlet.http.HttpServletResponse;
008: import org.apache.struts.action.ActionForm;
009: import org.apache.struts.action.ActionForward;
010: import org.apache.struts.action.ActionMapping;
011:
012: import org.claros.commons.mail.models.ConnectionMetaHandler;
013: import org.claros.commons.mail.models.ConnectionProfile;
014: import org.claros.commons.mail.models.Email;
015: import org.claros.commons.mail.models.EmailPart;
016: import org.claros.commons.mail.parser.HTMLMessageParser;
017: import org.claros.commons.mail.utility.Constants;
018: import org.claros.commons.models.AuthProfile;
019: import org.claros.commons.utility.Utility;
020: import org.claros.mini.common.BaseLoggedAction;
021: import org.claros.mini.controllers.FolderController;
022: import org.claros.mini.controllers.MailController;
023: import org.claros.mini.factory.FolderControllerFactory;
024: import org.claros.mini.factory.MailControllerFactory;
025: import org.claros.mini.models.FolderDbItem;
026:
027: /**
028: * @version 1.0
029: * @author Umut Gökbayrak
030: */
031: public class ReadMailAction extends BaseLoggedAction {
032:
033: public ActionForward myExecute(ActionMapping mapping,
034: ActionForm form, HttpServletRequest request,
035: HttpServletResponse response) throws Exception {
036: String sMsg = (String) getVariable(request, "msg");
037: String folder = (String) getVariable(request, "fid");
038: Long iMsg = null;
039: try {
040: iMsg = new Long(sMsg);
041: } catch (NumberFormatException ne) {
042: // do nothing sier
043: }
044:
045: if (iMsg != null) {
046: request.setAttribute("msg", sMsg);
047:
048: AuthProfile auth = getAuthProfile(request);
049: ConnectionMetaHandler handler = getConnectionHandler(request);
050: ConnectionProfile profile = getConnectionProfile(request);
051:
052: MailControllerFactory factory = new MailControllerFactory(
053: auth, profile, handler, folder);
054: MailController mailCont = factory.getMailController();
055:
056: Email email = mailCont.getEmailById(iMsg);
057:
058: // this is mini claros. text part is preferred
059:
060: String part = (String) getVariable(request, "part");
061: int i = -1;
062: if (part != null) {
063: try {
064: i = Integer.parseInt(part);
065: } catch (NumberFormatException e) {
066: }
067: }
068: if (i == -1) {
069: i = findTextBody(email.getParts());
070: if (i == -1) {
071: i = findHtmlBody(email.getParts());
072: }
073: }
074:
075: String bodyText = new String();
076: if (i != -1) {
077: EmailPart body = (EmailPart) email.getParts().get(i);
078:
079: if (body.getContent() == null
080: || ((String) body.getContent()).trim().length() == 0) {
081: int j = findHtmlBody(email.getParts());
082: if (j != -1) {
083: body = (EmailPart) email.getParts().get(j);
084: }
085: }
086:
087: String cType = body.getContentType();
088: if (cType.toLowerCase().startsWith("text/html")) {
089: String bodyContent = (String) body.getContent();
090: if (bodyContent.indexOf("¬") >= 0) {
091: bodyContent = bodyContent.substring(0,
092: bodyContent.indexOf("¬"));
093: }
094:
095: String tmp = HTMLMessageParser
096: .prepareInlineHTMLContent(email,
097: bodyContent);
098: bodyText += tmp;
099: } else if (cType.toLowerCase().startsWith("text/plain")) {
100: request.getSession().setAttribute("showDefaultCss",
101: new Boolean(true));
102:
103: String bodyContent = "No body content to display.";
104: if (body.getContent() instanceof String) {
105: bodyContent = (String) body.getContent();
106: if (bodyContent.indexOf("¬") >= 0) {
107: bodyContent = bodyContent.substring(0,
108: bodyContent.indexOf("¬"));
109: }
110: }
111: String tmp = Utility.replaceAllOccurances(
112: bodyContent, "\n", "<br>");
113: bodyText += tmp;
114: } else {
115: bodyText = "Only plain text and HTML parts are supported.";
116: }
117: } else {
118: bodyText += "No body content to display";
119: }
120:
121: if (bodyText.length() > 0) {
122: email.setBodyText(bodyText);
123: }
124: if (profile.getProtocol().equals(Constants.POP3)) {
125: mailCont.markAsRead(iMsg);
126: }
127: request.getSession().setAttribute("email", email);
128:
129: String showAll = (String) getVariable(request, "show");
130: if (showAll != null && showAll.equals("true")) {
131: request.setAttribute("showAll", new Boolean(true));
132: }
133:
134: String fid = (String) getVariable(request, "fid");
135:
136: FolderControllerFactory foldFact = new FolderControllerFactory(
137: auth, profile, handler);
138: FolderController folderCont = foldFact
139: .getFolderController();
140: FolderDbItem fItem = folderCont.getFolder(fid);
141: request.setAttribute("folderName", fItem.getFolderName()
142: .toUpperCase(new Locale("en", "US")));
143: }
144:
145: return mapping.findForward("success");
146: }
147:
148: /**
149: * @param list
150: * @return
151: */
152: private int findHtmlBody(ArrayList parts) {
153: for (int i = 0; i < parts.size(); i++) {
154: EmailPart body = (EmailPart) parts.get(i);
155: String cType = body.getContentType();
156: if (cType.toLowerCase().startsWith("text/html")) {
157: return i;
158: }
159: }
160: return -1;
161: }
162:
163: private int findTextBody(ArrayList parts) {
164: for (int i = 0; i < parts.size(); i++) {
165: EmailPart body = (EmailPart) parts.get(i);
166: String cType = body.getContentType();
167: if (cType.toLowerCase().startsWith("text/plain")) {
168: return i;
169: }
170: }
171: return -1;
172: }
173: }
|