String headerlist,startmsg,msglimit;
String[] tmpHdr;
long startmillis,endmillis;
int[] msgnumbers;
int i = 0;
BodyPart tmpPart;
String tmpToken;
StringBuffer partcount;
Message[] tmpMsgs = null;
RefileInfo tmpRefile = null;
Properties props = hr.request.props;
String partURL = hr.get("parturl","/getPart");
hr.killToken();
/* Get a copy of the requested server connection */
tmpEmailConn = (EmailConnection) serverConnections.get(connHandle);
if (tmpEmailConn == null) {
handleFatalError(hr,"No server connection");
return;
}
/* Check to see if we are still authenticated */
if (!tmpEmailConn.isAuthenticated) {
handleFatalError(hr,"Not authenticated");
return;
}
/* Get the folder name to operate on */
folderName = hr.get("foldername");
/* Check our special case (INBOX) */
if (!("INBOX".equals(folderName)) && !("inbox".equals(folderName))) {
if ((tmpEmailConn.defaultDir != null) && (folderName != null)) {
folderName = tmpEmailConn.defaultDir + "/" + folderName;
}
}
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"folderName in <message> tag = " +
folderName);
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"defaultDir in <message> tag = " +
tmpEmailConn.defaultDir);
/* Get the selected folder */
try {
selectFolder(hr,tmpEmailConn,folderName);
} catch (FolderNotFoundException e) {
props.put(connHandle + "mailError","Folder: " + folderName + " was not found");
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"Folder: " + folderName + " not found");
return;
} catch (ReadOnlyFolderException e) {
props.put(connHandle + "mailError","Folder: " + folderName +
" is not a valid mailbox");
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"Folder: " + folderName + e.getMessage());
return;
} catch (MessagingException e) {
handleFatalError(hr,"selecting folder " + folderName, e);
return;
}
/* Determine our action */
if ("getheaders".equals(hr.get("action"))) {
/* Get the requested message objects and do a prefetch of our requested headers */
headerlist = hr.get("headerlist");
try {
startmillis = System.currentTimeMillis();
tmpMsgs = tmpEmailConn.currentFolder.getMessageObjects(hr);
endmillis = System.currentTimeMillis();
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,(endmillis-startmillis) +
" milliseconds to retrieve message objects");
fetchHeaderInfo(hr,tmpMsgs,tmpEmailConn,headerlist);
} catch (MessagingException e) {
handleFatalError(hr,"Fetching messages", e);
return;
}
} else if ("getnewheaders".equals(hr.get("action"))) {
/* Get the new message objects and do a prefetch of our requested headers */
headerlist = hr.get("headerlist");
try {
tmpMsgs = tmpEmailConn.currentFolder.getNewMessageObjects(hr);
if (tmpMsgs != null) {
fetchHeaderInfo(hr,tmpMsgs,tmpEmailConn,headerlist);
}
} catch (MessagingException e) {
handleFatalError(hr,"Fetching new messages", e);
return;
}
} else if ("getmsg".equals(hr.get("action"))) {
msgNum = hr.get("msgnum");
headerlist = hr.get("headerlist");
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"MsgNum = " + msgNum);
/* Get the message & headers requested */
try {
tmpMsgs = tmpEmailConn.currentFolder.getMessageObjects(hr,msgNum);
getMsgHeaders(hr,tmpMsgs[0],headerlist);
} catch (MessagingException e) {
handleFatalError(hr,"Fetching message headers", e);
return;
}
/* Put the message server's msg number for this message into a return prop */
props.put(connHandle + "msgnum",Integer.toString(tmpMsgs[0].getMessageNumber()));
try {
/* Get the content type and put it into a property */
props.put(connHandle + msgNum + ".msgmimetype",tmpMsgs[0].getContentType());
/* Get the content object for the request message */
msgContent = tmpMsgs[0].getContent();
/* Determine if it is a multipart message */ /* XXXX */
if (msgContent instanceof String) {
props.put(connHandle + msgNum + ".body",msgContent);
} else if (msgContent instanceof Multipart) {
partcount = new StringBuffer();
/* Take care of the body */
tmpMsgContent = (((Multipart) msgContent).getBodyPart(0)).getContent();
if (tmpMsgContent instanceof String) {
props.put(connHandle + msgNum + ".body",tmpMsgContent);
partIndex = 1;
} else {
partIndex = 0;
}
/* Populate multipart attachment properties - and put our objects into
* the sessionManager for the handler to pickup.
*/
partCount = ((Multipart) msgContent).getCount();
for (;partIndex<partCount;partIndex++) {
tmpPart = ((Multipart) msgContent).getBodyPart(partIndex);
/* put our BodyPart object into the session manager for the
* Handler to find
*/
SessionManager.put(hr.sessionId,"Part" + partIndex,tmpPart);
hr.request.log(Server.LOG_DIAGNOSTIC, hr.prefix,
"creating part: " + hr.sessionId + " " + "Part" + partIndex);
/* Populate the properties for partname & parturl */
if (tmpPart.getFileName() != null) {
props.put(connHandle + msgNum + "." + partIndex +
".partname",tmpPart.getFileName());
props.put(connHandle + msgNum + "." + partIndex + ".parturl",
partURL + "/" + tmpPart.getFileName());
} else if (tmpPart.getDescription() != null) {
props.put(connHandle + msgNum + "." + partIndex +
".partname",tmpPart.getDescription());
props.put(connHandle + msgNum + "." + partIndex + ".parturl",
partURL + "/" + tmpPart.getDescription());
} else {
props.put(connHandle + msgNum + "." + partIndex +
".partname","No Name");
props.put(connHandle + msgNum + "." + partIndex + ".parturl",
partURL + "/NoName");