EmailConnection tmpEmailConn;
String folderName;
String tmpDefDir;
Folder[] mailFolders;
Message[] tmpMsgs;
Folder tmpFldr;
hr.killToken();
/* Get a copy of the requested server connection */
tmpEmailConn = (EmailConnection) serverConnections.get(connHandle);
if (tmpEmailConn == null) {
handleFatalError(hr,"Couldn't find a server connection to retrieve!");
return;
}
/* Check to see if we are still authenticated */
if (!tmpEmailConn.isAuthenticated) {
handleFatalError(hr,"Not Authenticated");
return;
}
/* Check to see if we have a mailbox dir specified */
tmpDefDir = hr.get("dir");
if (tmpDefDir != null) {
tmpEmailConn.defaultDir = tmpDefDir;
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"Setting defaultDir in <folder> tag = " + tmpEmailConn.defaultDir);
try {
tmpEmailConn.folderRoot = tmpEmailConn.userMsgStore.getFolder(
tmpEmailConn.defaultDir);
} catch (MessagingException e) {
handleFatalError(hr,"Folder directory: " + tmpDefDir, e);
return;
}
}
// XXX if we never defined a folder directory We're dead here!
/* Check to see if we just want to list folders */
if ("list".equals(hr.get("action"))) {
String delim = hr.get("delim", "/");
String glob = hr.get("glob");
StringBuffer sb = new StringBuffer("INBOX");
try {
/* Get an array of the Folders under folderRoot */
mailFolders = tmpEmailConn.folderRoot.list();
/* Parse through that list, getting the names of the folders */
for (int i=0;i<mailFolders.length;i++) {
String name = mailFolders[i].getName();
if (glob!= null && !Glob.match(glob, name)) {
continue;
}
try {
mailFolders[i].getMessageCount();
sb.append(delim).append(name);
} catch (Exception ex) {
// System.out.println(" BAD: " + name);
hr.request.log(Server.LOG_ERROR,connHandle,
ex.getMessage());
}
}
props.put(connHandle + "folders",sb.toString());
// System.out.println("Got: " + connHandle + ".folders" + " => " + sb.toString());
} catch (FolderNotFoundException e) {
props.put(connHandle + "mailError","Couldn't find default mail folder dir for: " +
tmpEmailConn.user);
hr.request.log(Server.LOG_ERROR,connHandle,
"Couldn't find default mail folder dir for: " + tmpEmailConn.user);
} catch (MessagingException e) {
handleFatalError(hr,"Looking for folders", e);
}
return;
}
/* Get the folder name to operate on */
folderName = hr.get("foldername");
if (folderName == null) {
debug(hr,"NULL foldername passed to the folder tag!");
return;
}
/* Check our special case (INBOX) */
if (!("INBOX".equalsIgnoreCase(folderName)) && (tmpEmailConn.defaultDir != null)) {
folderName = tmpEmailConn.defaultDir + "/" + folderName;
}
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"folderName in <folder> tag = " +
folderName);
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,"defaultDir in <folder> tag = " +
tmpEmailConn.defaultDir);
/* Check to see if we are creating a new folder. */
/* */
/* Note: the JavaMail API is a little weird here with regard */
/* to how folders are created. The getFolder() method */
/* of the Store object does not create a folder on the */
/* server - it only retrieves a 'handle' to a folder. */
/* To create a folder, you must first check if it exists */
/* already, then call the create() method on the object */
/* returned from the getFolder() call. */
if ("create".equals(hr.get("action"))) {
try {
tmpFldr = tmpEmailConn.userMsgStore.getFolder(folderName);
if (!tmpFldr.exists()) {
tmpFldr.create(Folder.HOLDS_MESSAGES);
} else {
props.put(connHandle + "mailError","Folder: " + folderName + " already exists");
hr.request.log(Server.LOG_DIAGNOSTIC,connHandle,
"Folder: " + folderName + " already exists!");