1 16 package org.apache.cocoon.mail; 17 18 import java.text.SimpleDateFormat ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 25 import javax.mail.Folder ; 26 import javax.mail.Message ; 27 import javax.mail.Provider ; 28 import javax.mail.Store ; 29 import javax.mail.URLName ; 30 31 import org.apache.avalon.framework.context.ContextException; 32 import org.apache.avalon.framework.logger.Logger; 33 import org.apache.avalon.framework.parameters.Parameters; 34 import org.apache.avalon.framework.thread.ThreadSafe; 35 import org.apache.cocoon.ProcessingException; 36 import org.apache.cocoon.acting.ServiceableAction; 37 import org.apache.cocoon.environment.ObjectModelHelper; 38 import org.apache.cocoon.environment.Redirector; 39 import org.apache.cocoon.environment.Request; 40 import org.apache.cocoon.environment.Session; 41 import org.apache.cocoon.environment.SourceResolver; 42 import org.apache.cocoon.mail.command.AbstractMailCommand; 43 44 58 public class MailAction extends ServiceableAction implements ThreadSafe { 59 60 63 public final static String REQUEST_ATTRIBUTE_FOLDER = "folder"; 64 67 public final static String REQUEST_ATTRIBUTE_FOLDERS = "folders"; 68 71 public final static String REQUEST_ATTRIBUTE_MESSAGE = "message"; 72 75 public final static String REQUEST_ATTRIBUTE_MESSAGES = "messages"; 76 77 78 88 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters par) throws Exception { 89 Map actionMap = new HashMap (); 90 91 Request request = ObjectModelHelper.getRequest(objectModel); 92 93 String command = request.getParameter("cmd"); 94 String folderName = request.getParameter("folder"); 95 String userid = request.getParameter("mail-userid"); 96 String password = request.getParameter("mail-password"); 97 98 Session session = request.getSession(true); 100 MailContext mailContext = (MailContext) session.getAttribute(MailContext.SESSION_MAIL_CONTEXT); 101 if (mailContext == null) { 102 mailContext = new MailContextHttpSession(null); 105 mailContext.enableLogging(getLogger()); 106 session.setAttribute(MailContext.SESSION_MAIL_CONTEXT, mailContext); 107 } 108 109 javax.mail.Session mailSession = null; 111 Store mailStore = null; 112 try { 113 try { 114 mailSession = (javax.mail.Session ) mailContext.get(MailContext.MAIL_SESSION_ENTRY); 115 } catch (ContextException ce) { 116 Properties sessionProperties = new Properties (); 118 String [] allParameterNames = par.getNames(); 119 for (int i = 0; i < allParameterNames.length; i++) { 120 String parameterName = allParameterNames[i]; 121 final String PARAMETER_NAME_PREFIX = "javax.mail.Session.props:"; 122 if (parameterName.startsWith(PARAMETER_NAME_PREFIX)) { 123 String sessionPropName = parameterName.substring(PARAMETER_NAME_PREFIX.length()); 124 String sessionPropValue = par.getParameter(parameterName, null); 125 if (sessionPropValue != null) { 126 getLogger().debug("Add session property " + 127 String.valueOf(sessionPropName) + ": " + 128 String.valueOf(sessionPropValue)); 129 sessionProperties.put(sessionPropName, sessionPropValue); 130 } 131 } 132 } 133 mailSession = javax.mail.Session.getDefaultInstance(sessionProperties, null); 134 checkProviders(mailSession); 135 mailContext.put(MailContext.MAIL_SESSION_ENTRY, mailSession); 136 } 137 } catch (Exception e) { 138 String message = "Cannot create mail session"; 139 getLogger().error(message, e); 140 throw new ProcessingException(message, e); 141 } 142 143 String storeURLNameExpanded = null; 145 String storeURLNameTemplate = par.getParameter("store-urlname", null); 146 try { 147 try { 148 mailStore = (Store ) mailContext.get(MailContext.MAIL_STORE_ENTRY); 149 } catch (ContextException ce) { 150 151 storeURLNameExpanded = getURLNameExpanded(storeURLNameTemplate, userid, password); 153 154 URLName urlNameExpanded = new URLName (storeURLNameExpanded); 155 getLogger().info("get store using URLName " + String.valueOf(urlNameExpanded)); 156 mailStore = mailSession.getStore(urlNameExpanded); 157 mailStore.connect(); 158 mailContext.put(MailContext.MAIL_STORE_ENTRY, mailStore); 159 } 160 } catch (Exception e) { 161 String message = "Cannot get store, and connect " + String.valueOf(storeURLNameExpanded); 162 getLogger().error(message, e); 163 throw new ProcessingException(message, e); 164 } 165 166 if (folderName != null) { 167 mailContext.put(MailContext.MAIL_CURRENT_WORKING_FOLDER_ENTRY, folderName); 170 } else { 171 folderName = (String ) mailContext.get(MailContext.MAIL_CURRENT_WORKING_FOLDER_ENTRY); 173 } 174 actionMap.put(MailContext.MAIL_CURRENT_WORKING_FOLDER_ENTRY, folderName); 175 176 if (command != null) { 177 mailContext.put(MailContext.MAIL_CURRENT_WORKING_COMMAND_ENTRY, command); 178 } else { 179 command = (String ) mailContext.get(MailContext.MAIL_CURRENT_WORKING_COMMAND_ENTRY); 180 } 181 actionMap.put(MailContext.MAIL_CURRENT_WORKING_COMMAND_ENTRY, command); 182 183 185 mailContext.setRequest(request); 187 populateRequestAttribute(request, mailContext); 188 mailContext.setRequest(null); 191 192 return actionMap; 193 } 194 195 196 204 protected String getURLNameExpanded(String storeURLNameTemplate, String userid, String password) { 205 String tokenStart = "''"; 206 String tokenEnd = "''"; 207 Properties filters = new Properties (); 208 filters.put("mail-userid", userid); 209 filters.put("mail-passwd", password); 210 211 String filteredURLName = filter(tokenStart, tokenEnd, storeURLNameTemplate, filters); 212 return filteredURLName; 213 } 214 215 216 225 protected String filter(final String tokenStart, final String tokenEnd, String s, Properties tokens) { 226 int index = s.indexOf(tokenStart); 227 228 if (index > -1) { 229 try { 230 StringBuffer b = new StringBuffer (); 231 int i = 0; 232 String token = null; 233 String value = null; 234 235 do { 236 int endIndex = s.indexOf(tokenEnd, index + tokenStart.length() + 1); 237 if (endIndex == -1) { 238 break; 239 } 240 token = s.substring(index + tokenStart.length(), endIndex); 241 b.append(s.substring(i, index)); 242 if (tokens.containsKey(token)) { 243 value = (String ) tokens.get(token); 244 b.append(value); 245 i = index + tokenStart.length() + token.length() + tokenEnd.length(); 246 } else { 247 b.append(tokenStart); 249 i = index + tokenStart.length(); 250 } 251 } while ((index = s.indexOf(tokenStart, i)) > -1); 252 253 b.append(s.substring(i)); 254 return b.toString(); 255 } catch (StringIndexOutOfBoundsException e) { 256 return s; 257 } 258 } else { 259 return s; 260 } 261 } 262 263 264 269 protected void checkProviders(javax.mail.Session session) { 270 Provider [] providers = session.getProviders(); 271 for (int i = 0; i < providers.length; i++) { 273 getLogger().info("mail provider " + providers[i]); 274 } 275 } 276 277 278 289 protected void populateRequestAttribute(Request request, MailContext mailContext) throws Exception { 290 String folderName = (String ) mailContext.get(MailContext.MAIL_CURRENT_WORKING_FOLDER_ENTRY); 291 request.setAttribute(MailContext.MAIL_CURRENT_WORKING_FOLDER_ENTRY, folderName); 292 String command = (String ) mailContext.get(MailContext.MAIL_CURRENT_WORKING_COMMAND_ENTRY); 293 request.setAttribute(MailContext.MAIL_CURRENT_WORKING_COMMAND_ENTRY, command); 294 295 List javaMailResult = retrieveJavaMailObjects(mailContext); 297 Iterator javaMailResultIterator; 298 299 javaMailResultIterator = javaMailResult.iterator(); 301 putXMLizerToRequestAttribute(request, javaMailResultIterator); 303 } 304 305 306 312 protected void putXMLizerToRequestAttribute(Request request, Iterator resultIterator) { 313 if (resultIterator != null) { 314 Logger logger = getLogger(); 316 317 String datePattern = "dd.MM.yyyy HH:mm"; 319 SimpleDateFormat sdf = new SimpleDateFormat (datePattern); 320 321 while (resultIterator.hasNext()) { 322 Object objRef = resultIterator.next(); 323 324 getLogger().debug("Creating XMLizer for " + String.valueOf(objRef)); 325 326 if (objRef instanceof Folder ) { 327 MailContentHandlerDelegate.FolderXMLizer fx = new MailContentHandlerDelegate.FolderXMLizer((Folder ) objRef); 328 fx.enableLogging(logger); 329 request.setAttribute(REQUEST_ATTRIBUTE_FOLDER, fx); 330 } else if (objRef instanceof Folder []) { 331 Folder [] folders = (Folder []) objRef; 332 MailContentHandlerDelegate.FolderXMLizer[] fxs = new MailContentHandlerDelegate.FolderXMLizer[folders.length]; 333 for (int i = 0; i < folders.length; i++) { 334 fxs[i] = new MailContentHandlerDelegate.FolderXMLizer(folders[i]); 335 fxs[i].enableLogging(logger); 336 } 337 request.setAttribute(REQUEST_ATTRIBUTE_FOLDERS, fxs); 339 } else if (objRef instanceof Message ) { 340 MailContentHandlerDelegate.MessageXMLizer mx = new MailContentHandlerDelegate.MessageXMLizer((Message ) objRef); 341 mx.enableLogging(logger); 342 mx.setSimpleDateFormat(sdf); 343 request.setAttribute(REQUEST_ATTRIBUTE_MESSAGE, mx); 344 } else if (objRef instanceof Message []) { 345 MailContentHandlerDelegate.MessageEnvelopeXMLizer mex = new MailContentHandlerDelegate.MessageEnvelopeXMLizer((Message []) objRef); 346 mex.enableLogging(logger); 347 mex.setSimpleDateFormat(sdf); 348 request.setAttribute(REQUEST_ATTRIBUTE_MESSAGES, mex); 349 } 350 } 351 } 352 } 353 354 355 362 protected List retrieveJavaMailObjects(MailContext mailContext) throws ProcessingException { 363 364 List result = null; 365 try { 366 MailCommandManager mam = new MailCommandManager(); 368 mam.enableLogging(getLogger()); 369 370 MailCommandBuilder mab = new MailCommandBuilder(); 372 mab.enableLogging(getLogger()); 373 AbstractMailCommand ama = mab.buildAbstractMailCommand(mailContext); 374 375 getLogger().debug("Executing " + String.valueOf(ama)); 376 377 result = mam.execute(ama); 379 380 return result; 382 } catch (Exception e) { 383 String message = "Cannot retrieve javamail objects"; 384 getLogger().error(message, e); 385 throw new ProcessingException(message, e); 386 } 387 } 388 } 389 390 | Popular Tags |