package org.claros.mini.actions;
import java.util.ArrayList;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.claros.commons.mail.models.ConnectionMetaHandler;
import org.claros.commons.mail.models.ConnectionProfile;
import org.claros.commons.mail.models.Email;
import org.claros.commons.mail.models.EmailPart;
import org.claros.commons.mail.parser.HTMLMessageParser;
import org.claros.commons.mail.utility.Constants;
import org.claros.commons.models.AuthProfile;
import org.claros.commons.utility.Utility;
import org.claros.mini.common.BaseLoggedAction;
import org.claros.mini.controllers.FolderController;
import org.claros.mini.controllers.MailController;
import org.claros.mini.factory.FolderControllerFactory;
import org.claros.mini.factory.MailControllerFactory;
import org.claros.mini.models.FolderDbItem;
/**
* @version 1.0
* @author Umut Gkbayrak
*/
public class ReadMailAction extends BaseLoggedAction {
public ActionForward myExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
String sMsg = (String)getVariable(request, "msg");
String folder = (String)getVariable(request, "fid");
Long iMsg = null;
try {
iMsg = new Long(sMsg);
} catch (NumberFormatException ne) {
// do nothing sier
}
if (iMsg != null) {
request.setAttribute("msg", sMsg);
AuthProfile auth = getAuthProfile(request);
ConnectionMetaHandler handler = getConnectionHandler(request);
ConnectionProfile profile = getConnectionProfile(request);
MailControllerFactory factory = new MailControllerFactory(auth, profile, handler, folder);
MailController mailCont = factory.getMailController();
Email email = mailCont.getEmailById(iMsg);
// this is mini claros. text part is preferred
String part = (String)getVariable(request, "part");
int i = -1;
if (part != null) {
try {
i = Integer.parseInt(part);
} catch (NumberFormatException e) {}
}
if (i == -1) {
i = findTextBody(email.getParts());
if (i == -1) {
i = findHtmlBody(email.getParts());
}
}
String bodyText = new String();
if (i != -1) {
EmailPart body = (EmailPart)email.getParts().get(i);
if (body.getContent() == null || ((String)body.getContent()).trim().length() == 0) {
int j = findHtmlBody(email.getParts());
if (j != -1) {
body = (EmailPart)email.getParts().get(j);
}
}
String cType = body.getContentType();
if (cType.toLowerCase().startsWith("text/html")) {
String bodyContent = (String)body.getContent();
if (bodyContent.indexOf("") >= 0) {
bodyContent = bodyContent.substring(0, bodyContent.indexOf(""));
}
String tmp = HTMLMessageParser.prepareInlineHTMLContent(email, bodyContent);
bodyText += tmp;
} else if (cType.toLowerCase().startsWith("text/plain")) {
request.getSession().setAttribute("showDefaultCss", new Boolean(true));
String bodyContent = "No body content to display.";
if (body.getContent() instanceof String) {
bodyContent = (String)body.getContent();
if (bodyContent.indexOf("") >= 0) {
bodyContent = bodyContent.substring(0, bodyContent.indexOf(""));
}
}
String tmp = Utility.replaceAllOccurances(bodyContent, "\n", "<br>");
bodyText += tmp;
} else {
bodyText = "Only plain text and HTML parts are supported.";
}
} else {
bodyText += "No body content to display";
}
if (bodyText.length() > 0) {
email.setBodyText(bodyText);
}
if (profile.getProtocol().equals(Constants.POP3)) {
mailCont.markAsRead(iMsg);
}
request.getSession().setAttribute("email", email);
String showAll = (String)getVariable(request, "show");
if (showAll != null && showAll.equals("true")) {
request.setAttribute("showAll", new Boolean(true));
}
String fid = (String)getVariable(request, "fid");
FolderControllerFactory foldFact = new FolderControllerFactory(auth, profile, handler);
FolderController folderCont = foldFact.getFolderController();
FolderDbItem fItem = folderCont.getFolder(fid);
request.setAttribute("folderName", fItem.getFolderName().toUpperCase(new Locale("en", "US")));
}
return mapping.findForward("success");
}
/**
* @param list
* @return
*/
private int findHtmlBody(ArrayList parts) {
for (int i=0;i<parts.size();i++) {
EmailPart body = (EmailPart)parts.get(i);
String cType = body.getContentType();
if (cType.toLowerCase().startsWith("text/html")) {
return i;
}
}
return -1;
}
private int findTextBody(ArrayList parts) {
for (int i=0;i<parts.size();i++) {
EmailPart body = (EmailPart)parts.get(i);
String cType = body.getContentType();
if (cType.toLowerCase().startsWith("text/plain")) {
return i;
}
}
return -1;
}
}
|