1 16 package org.apache.cocoon.acting; 17 18 import org.apache.avalon.framework.configuration.Configurable; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.thread.ThreadSafe; 24 25 import org.apache.cocoon.environment.ObjectModelHelper; 26 import org.apache.cocoon.environment.Redirector; 27 import org.apache.cocoon.environment.Request; 28 import org.apache.cocoon.environment.SourceResolver; 29 import org.apache.cocoon.mail.MailSender; 30 31 import org.apache.commons.lang.StringUtils; 32 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import javax.mail.MessagingException ; 37 import javax.mail.internet.AddressException ; 38 39 126 public class Sendmail extends ServiceableAction 127 implements ThreadSafe, Configurable { 128 129 private static final String STATUS = "status"; 130 private static final String MESSAGE = "message"; 131 132 133 public static final String REQUEST_ATTRIBUTE = "org.apache.cocoon.acting.Sendmail"; 134 135 private String smtpHost; 136 private String smtpUser; 137 private String smtpPassword; 138 139 public void configure(Configuration conf) throws ConfigurationException { 140 if (getLogger().isDebugEnabled()) { 141 getLogger().debug("configure"); 142 } 143 144 smtpHost = conf.getChild("smtp-host").getValue(conf.getAttribute("smtphost", null)); 146 smtpUser = conf.getChild("smtp-user").getValue(null); 147 smtpPassword = conf.getChild("smtp-password").getValue(null); 148 149 if (getLogger().isDebugEnabled()) { 150 if (smtpHost != null) 151 getLogger().debug("Using " + smtpHost + " as the smtp server"); 152 if (smtpUser != null) 153 getLogger().debug("Using " + smtpUser + " as the smtp user"); 154 } 155 } 156 157 public Map act(Redirector redirector, 158 SourceResolver resolver, 159 Map objectModel, 160 String source, 161 Parameters parameters) 162 throws Exception { 163 boolean success = false; 164 Map status = null; 165 166 MailSender mms = null; 167 try { 168 Request request = ObjectModelHelper.getRequest(objectModel); 169 170 String smtpHost = parameters.getParameter("smtp-host", parameters.getParameter("smtphost", this.smtpHost)); 172 String smtpUser = parameters.getParameter("smtp-user", this.smtpUser); 173 String smtpPassword = parameters.getParameter("smtp-password", this.smtpPassword); 174 175 if ("".equals(smtpHost)) { 177 smtpHost = this.smtpHost; 178 } 179 if ("".equals(smtpUser)) { 180 smtpUser = this.smtpUser; 181 } 182 if ("".equals(smtpPassword)) { 183 smtpPassword = this.smtpPassword; 184 } 185 186 mms = (MailSender) this.manager.lookup(MailSender.ROLE); 187 188 if (smtpHost != null || smtpUser != null) { 190 mms.setSmtpHost(smtpHost, smtpUser, smtpPassword); 191 } 192 193 if (parameters.isParameter("from")) { 194 mms.setFrom(parameters.getParameter("from", null)); 195 } 196 if (parameters.isParameter("to")) { 197 mms.setTo(parameters.getParameter("to", null)); 198 } 199 if (parameters.isParameter("replyTo")) { 200 mms.setReplyTo(parameters.getParameter("replyTo", null)); 201 } 202 if (parameters.isParameter("cc")) { 203 mms.setCc(parameters.getParameter("cc", null)); 204 } 205 if (parameters.isParameter("bcc")) { 206 mms.setBcc(parameters.getParameter("bcc", null)); 207 } 208 if (parameters.isParameter("subject")) { 209 mms.setSubject(parameters.getParameter("subject", null)); 210 } 211 if (parameters.isParameter("charset")) { 212 mms.setCharset(parameters.getParameter("charset", null)); 213 } 214 215 if (parameters.isParameter("src")) { 216 mms.setBodyFromSrc(parameters.getParameter("src", null)); 217 if (parameters.isParameter("srcMimeType")) { 218 mms.setBodyFromSrcMimeType( 219 parameters.getParameter("srcMimeType", null)); 220 } 221 } else if (parameters.isParameter("body")) { 222 mms.setBody(parameters.getParameter("body", null)); 223 } 224 225 if (parameters.isParameter("attachments")) { 226 String fileName[] = StringUtils.split(parameters.getParameter("attachments")); 227 for (int i = 0; i < fileName.length; i++) { 228 String srcName = fileName[i]; 229 230 if (srcName.indexOf(":") == -1) { 231 Object obj = request.get(srcName); 232 mms.addAttachment(obj); 233 if (getLogger().isDebugEnabled()) { 234 getLogger().debug("request-attachment: " + obj); 235 } 236 } else { 237 mms.addAttachmentURL(srcName, 238 null, 239 srcName.substring(srcName.lastIndexOf('/') + 1)); 240 if (getLogger().isDebugEnabled()) { 241 getLogger().debug("sitemap-attachment: " + srcName); 242 } 243 } 244 } 245 } 246 247 mms.send(); 248 249 success = true; 250 status = new HashMap (3); 251 status.put(Sendmail.STATUS, "success"); 252 253 } catch (AddressException e) { 254 getLogger().warn("AddressException: ", e); 255 256 status = new HashMap (3); 257 status.put(Sendmail.STATUS, "user-error"); 258 status.put(Sendmail.MESSAGE, e.getMessage()); 259 260 } catch (MessagingException e) { 261 getLogger().warn("MessagingException: " + 262 "An error occured while sending email.", e); 263 264 status = new HashMap (3); 265 status.put(Sendmail.STATUS, "server-error"); 266 status.put(Sendmail.MESSAGE, 267 "An error occured while sending email: " + e.getMessage()); 268 269 } catch (ServiceException e) { 270 getLogger().error("ServiceException: " + 271 "An error occured while initializing.", e); 272 273 status = new HashMap (3); 274 status.put(Sendmail.STATUS, "server-error"); 275 status.put(Sendmail.MESSAGE, 276 "An exception was thrown while sending email: " + e.getMessage()); 277 278 } catch (Exception e) { 279 getLogger().error("An exception was thrown while sending email.", e); 280 281 status = new HashMap (3); 282 status.put(Sendmail.STATUS, "server-error"); 283 status.put(Sendmail.MESSAGE, "An exception was thrown while sending email."); 284 285 } finally { 286 ObjectModelHelper.getRequest(objectModel).setAttribute(Sendmail.REQUEST_ATTRIBUTE, 287 status); 288 this.manager.release(mms); 289 } 290 291 return success ? status : null; 292 } 293 } 294 | Popular Tags |