1 16 package org.apache.cocoon.mail; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 import javax.mail.Folder ; 22 import javax.mail.MessagingException ; 23 import javax.mail.Store ; 24 import org.apache.avalon.framework.context.Context; 26 import org.apache.avalon.framework.context.ContextException; 27 import org.apache.avalon.framework.context.DefaultContext; 28 import org.apache.avalon.framework.logger.Logger; 29 import org.apache.avalon.framework.logger.LogEnabled; 30 import org.apache.cocoon.environment.Request; 31 32 39 public class MailContext extends DefaultContext implements LogEnabled { 40 43 public final static String SESSION_MAIL_CONTEXT = "mail-context"; 44 45 48 public final static String MAIL_SESSION_ENTRY = "mail-session"; 49 52 public final static String MAIL_STORE_ENTRY = "mail-store"; 53 54 57 public final static String MAIL_CURRENT_WORKING_FOLDER_ENTRY = "mail-current-working-folder"; 58 61 public final static String MAIL_CURRENT_WORKING_COMMAND_ENTRY = "mail-current-working-command"; 62 63 private Request request; 64 private Logger logger; 65 66 67 72 MailContext(Context parent) { 73 super(parent); 74 } 75 76 77 82 public void setRequest(Request request) { 83 this.request = request; 84 } 85 86 87 108 public Object get(Object key) throws ContextException { 109 String keyString = (String ) key; 110 111 final String PARAM_PREFIX_ENTRY = "param:"; 112 final String PARAM_INTEGER_PREFIX_ENTRY = "param-integer:"; 113 final String PARAM_FOLDER_PREFIX_ENTRY = "param-folder:"; 114 115 if (keyString.startsWith(PARAM_PREFIX_ENTRY)) { 116 String paramName = keyString.substring(PARAM_PREFIX_ENTRY.length()); 117 String paramValue = getParameter(paramName); 118 if (paramValue == null) { 119 String message = "No parameter " + String.valueOf(keyString) + " available."; 120 throw new ContextException(message); 121 } 122 return paramValue; 123 } else if (keyString.startsWith(PARAM_INTEGER_PREFIX_ENTRY)) { 124 String paramName = keyString.substring(PARAM_INTEGER_PREFIX_ENTRY.length()); 125 try { 126 Integer paramValue = getParameterAsInteger(paramName); 127 return paramValue; 128 } catch (NumberFormatException nfe) { 129 String message = "Cannot create Integer for parameter " + String.valueOf(keyString); 130 throw new ContextException(message, nfe); 131 } 132 } else if (keyString.startsWith(PARAM_FOLDER_PREFIX_ENTRY)) { 133 String paramName = keyString.substring(PARAM_FOLDER_PREFIX_ENTRY.length()); 134 String folderName = getParameter(paramName); 135 if (folderName == null) { 136 try { 139 folderName = (String ) super.get(MAIL_CURRENT_WORKING_FOLDER_ENTRY); 140 } catch (ContextException ce) { 141 String message = "No " + MAIL_CURRENT_WORKING_FOLDER_ENTRY + " entry available "; 143 getLogger().error(message); 144 throw new ContextException(message, ce); 145 } 146 } 147 148 Folder folder = null; 150 try { 151 folder = (Folder ) getFolder(folderName); 152 } catch (ContextException ce) { 153 155 Store store = (Store ) get(MAIL_STORE_ENTRY); 156 try { 158 if (!store.isConnected()) { 159 store.connect(); 160 } 161 final String DEFAULT_FOLDER_NAME = "~"; 162 163 if (folderName.equals(DEFAULT_FOLDER_NAME) || folderName.length() == 0) { 165 folder = store.getDefaultFolder(); 166 } else { 167 folder = store.getFolder(folderName); 168 } 169 170 putFolder(folder); 172 } catch (MessagingException me) { 173 String message = "Cannot get folder " + String.valueOf(folderName); 174 throw new ContextException(message, ce); 175 } 176 } 177 return folder; 178 } else { 179 return super.get(key); 180 } 181 } 182 183 184 191 public Folder getTheFolder(String entry) throws ContextException { 192 Folder f; 193 try { 194 f = (Folder ) get("param-folder:" + entry); 195 } catch (Exception e) { 196 String message = "Cannot get Folder object for " + String.valueOf(entry); 197 throw new ContextException(message, e); 198 } 199 return f; 200 } 201 202 203 210 public Object getFolder(String folderName) throws ContextException { 211 folderName = canoncializeFoldername(folderName); 213 214 final String key = "folder:" + folderName; 215 getLogger().debug("Getting folder " + String.valueOf(key)); 216 217 Object o = super.get(key); 218 getLogger().debug("Successfully getting folder " + String.valueOf(key) + ": " + String.valueOf(o)); 219 return o; 220 } 221 222 223 226 public void removeStore() { 227 try { 228 getLogger().info("Remove store " + String.valueOf(this)); 229 removeAllFolders(); 230 231 Map map = getContextData(); 232 Store store = (Store ) map.remove(MAIL_STORE_ENTRY); 233 if (store != null) { 234 MailCommandManager.closeStore(store); 235 } 236 } catch (Exception e) { 237 String message = "Cannot remove store"; 238 getLogger().error(message, e); 239 } 240 } 241 242 243 246 public void removeAllFolders() { 247 try { 248 getLogger().info("Remove folders " + String.valueOf(this)); 249 250 Map map = getContextData(); 251 Set entrySet = map.entrySet(); 252 Iterator i = entrySet.iterator(); 253 while (i.hasNext()) { 254 Map.Entry me = (Map.Entry ) i.next(); 255 String key = (String ) me.getKey(); 256 if (key.startsWith("folder:")) { 257 Folder f = (Folder ) me.getValue(); 258 MailCommandManager.closeFolder(f); 259 i.remove(); 260 } 261 } 262 } catch (Exception e) { 263 String message = "Cannot remove all folders"; 264 getLogger().error(message, e); 265 } 266 } 267 268 269 275 public void putFolder(Folder folder) throws ContextException { 276 String folderName = folder.getFullName(); 277 folderName = canoncializeFoldername(folderName); 279 280 final String key = "folder:" + folderName; 281 282 getLogger().debug("Putting folder key: " + String.valueOf(key) + 283 " folder " + String.valueOf(folder)); 284 285 try { 287 Object objRef = super.get(key); 288 if (objRef != null) { 289 try { 291 Folder f = (Folder ) objRef; 292 MailCommandManager.closeFolder(f); 293 } catch (MessagingException me) { 294 String message = "Cannot close folder"; 295 getLogger().warn(message, me); 296 } 297 } 298 } catch (ContextException e) { 299 } 301 302 304 super.put(key, folder); 305 } 306 307 308 314 public void putFolder(Folder [] folders) throws ContextException { 315 for (int i = 0; i < folders.length; i++) { 316 putFolder(folders[i]); 317 } 318 } 319 320 321 326 public void enableLogging(Logger logger) { 327 this.logger = logger; 328 } 329 330 331 332 338 protected String getParameter(String key) { 339 String value = request.getParameter(key); 340 return value; 341 } 342 343 344 350 protected Integer getParameterAsInteger(String key) { 351 String value = request.getParameter(key); 352 Integer i = new Integer (value); 353 return i; 354 } 355 356 357 362 protected Logger getLogger() { 363 return this.logger; 364 } 365 366 367 373 protected String canoncializeFoldername(String fn) { 374 return fn; 376 } 377 378 } 379 380 | Popular Tags |