1 13 package info.magnolia.cms.taglibs.util; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.mail.MailConstants; 17 import info.magnolia.cms.mail.MgnlMailFactory; 18 import info.magnolia.cms.mail.templates.MgnlEmail; 19 import info.magnolia.cms.util.ExclusiveWrite; 20 import info.magnolia.cms.util.Resource; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.text.DateFormat ; 26 import java.util.Date ; 27 import java.util.GregorianCalendar ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 import javax.jcr.RepositoryException; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.servlet.jsp.JspException ; 36 import javax.servlet.jsp.tagext.TagSupport ; 37 38 import org.apache.commons.lang.StringUtils; 39 import org.apache.commons.lang.exception.NestableRuntimeException; 40 import org.slf4j.Logger; 41 import org.slf4j.LoggerFactory; 42 43 44 49 public class SimpleMailTag extends TagSupport { 50 51 54 private static final long serialVersionUID = 222L; 55 56 private String nodeCollectionName = "mainColumnParagraphs"; 57 58 private String from; 59 60 private String to; 61 62 private String cc; 63 64 private String bcc; 65 66 private String replyTo; 67 68 private String subject; 69 70 private String redirect; 71 72 private String type; 73 74 77 private String template; 78 79 82 private boolean logging; 83 84 87 private String loggingEncoding = "UTF8"; 88 89 92 private String loggingDirectory = "/mailtracking"; 93 94 97 private String loggingExtension = "log"; 98 99 102 private String loggingFilename; 103 104 107 private static Logger log = LoggerFactory.getLogger(SimpleMailTag.class); 108 109 113 public void setBcc(String bcc) { 114 this.bcc = bcc; 115 } 116 117 121 public void setReplyTo(String replyTo) { 122 this.replyTo = replyTo; 123 } 124 125 129 public void setCc(String cc) { 130 this.cc = cc; 131 } 132 133 137 public void setFrom(String from) { 138 this.from = from; 139 } 140 141 145 public void setNodeCollectionName(String nodeCollectionName) { 146 this.nodeCollectionName = nodeCollectionName; 147 } 148 149 153 public void setTo(String to) { 154 this.to = to; 155 } 156 157 161 public void setSubject(String subject) { 162 this.subject = subject; 163 } 164 165 169 public void setRedirect(String redirect) { 170 this.redirect = redirect; 171 } 172 173 177 public void setType(String type) { 178 this.type = type; 179 } 180 181 public void setLogging(boolean trackMail) { 182 this.logging = trackMail; 183 } 184 185 186 public String getTemplate() { 187 return template; 188 } 189 190 191 public void setTemplate(String template) { 192 this.template = template; 193 } 194 195 196 199 public int doEndTag() throws JspException { 200 201 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 202 203 StringBuffer body = new StringBuffer (); 205 StringBuffer mailTitles = new StringBuffer (); 207 StringBuffer mailValues = new StringBuffer (); 208 mailValues.append(DateFormat.getDateTimeInstance().format(new Date(System.currentTimeMillis()))).append(';'); 210 211 if(nodeCollectionName == null){ 212 this.nodeCollectionName = Resource.getLocalContentNodeCollectionName(request); 213 } 214 215 Content activePage = Resource.getActivePage(request); 216 Content fieldsNode = null; 217 Iterator it; 218 try { 219 Content localContentNode = Resource.getLocalContentNode(request); 220 if(localContentNode != null && localContentNode.hasContent(nodeCollectionName)){ 221 fieldsNode = localContentNode.getContent(nodeCollectionName); 222 } 223 else{ 224 fieldsNode = activePage.getContent(nodeCollectionName); 225 } 226 it = fieldsNode.getChildren().iterator(); 227 } 228 catch (RepositoryException e) { 229 throw new NestableRuntimeException(e); 230 } 231 232 while (it.hasNext()) { 233 Content node = (Content) it.next(); 234 String [] values = request.getParameterValues("field_" + node.getName()); 235 if (values == null) { 236 values = request.getParameterValues(node.getName()); 237 } 238 if (values != null) { 239 body.append(node.getNodeData("title").getString()).append('\n'); 240 mailTitles.append(excelCSVFormat(node.getNodeData("title").getString())).append(';'); 241 StringBuffer csvValue = new StringBuffer (); 242 for (int i = 0; i < values.length; i++) { 243 body.append(values[i]).append('\n'); 244 csvValue.append(values[i]); 245 if(i < values.length-1){ 246 csvValue.append('\n'); 247 } 248 } 249 mailValues.append(excelCSVFormat(csvValue.toString())).append(';'); 250 body.append("\n"); 251 } 252 } 253 254 if(logging){ 255 trackMail(request, activePage.getHandle(), mailTitles, mailValues); 256 } 257 258 String mailType = type; 259 if (StringUtils.isEmpty(mailType)) { 260 mailType = MailConstants.MAIL_TEMPLATE_TEXT; 261 } 262 263 MgnlEmail email; 264 try { 265 if(StringUtils.isEmpty(template)){ 267 email = MgnlMailFactory.getInstance().getEmailFromType(mailType); 268 email.setBody(body.toString(), null); 269 } 270 else{ 271 Map parameters = new HashMap (request.getParameterMap()); 272 parameters.put("all", body.toString()); 273 email = MgnlMailFactory.getInstance().getEmailFromTemplate(template, parameters); 274 } 275 email.setToList(to); 276 email.setCcList(cc); 277 email.setBccList(bcc); 278 email.setReplyToList(replyTo); 279 email.setFrom(from); 280 email.setSubject(subject); 281 MgnlMailFactory.getInstance().getEmailHandler().prepareAndSendMail(email); 282 } 283 catch (Exception e) { 284 log.error(e.getMessage(), e); 286 } 287 288 if (StringUtils.isNotEmpty(redirect)) { 289 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 290 try { 291 response.sendRedirect(request.getContextPath() + redirect); 292 } 293 catch (IOException e) { 294 log.error(e.getMessage(), e); 296 } 297 } 298 release(); 299 return super.doEndTag(); 300 } 301 302 protected String excelCSVFormat(String str) { 303 if(!StringUtils.containsNone(str, "\n;")){ 304 return "\"" + StringUtils.replace(str, "\"","\"\"") + "\""; 305 } 306 return str; 307 } 308 309 protected void trackMail(HttpServletRequest request, String activePagePath, StringBuffer titles, StringBuffer values){ 310 String fileName = this.getLoggingFilename(); 311 if(StringUtils.isEmpty(fileName)){ 312 activePagePath = StringUtils.removeStart(activePagePath, "/"); 313 fileName = StringUtils.replace(activePagePath, "/", "_"); 314 } 315 316 fileName = fileName + "_" + new GregorianCalendar ().get(GregorianCalendar.WEEK_OF_YEAR) + "." + getLoggingExtension(); 317 String folder = pageContext.getServletContext().getRealPath(this.getLoggingDirectory()); 318 319 synchronized (ExclusiveWrite.getInstance()) { 320 new File (folder).mkdirs(); 321 322 File file = new File (folder + File.separator + fileName); 323 boolean exists = file.exists(); 324 325 326 try { 327 FileOutputStream out = new FileOutputStream (file, true); 328 if(!exists){ 329 out.write("Timestamp;".toString().getBytes(this.getLoggingEncoding())); 330 titles.replace(titles.length()-1, titles.length(), "\n"); 331 out.write(titles.toString().getBytes(this.getLoggingEncoding())); 332 } 333 values.replace(values.length()-1, values.length(), "\n"); 334 out.write(values.toString().getBytes(this.getLoggingEncoding())); 335 out.flush(); 336 out.close(); 337 338 } catch (Exception e) { 339 log.error("Exception while tracking mail", e); 340 } 341 } 342 } 343 344 347 public void release() { 348 this.nodeCollectionName = null; 349 this.to = null; 350 this.from = null; 351 this.cc = null; 352 this.bcc = null; 353 this.subject = null; 354 this.type = null; 355 this.template = null; 356 super.release(); 357 } 358 359 360 public String getLoggingDirectory() { 361 return loggingDirectory; 362 } 363 364 365 public void setLoggingDirectory(String loggingDirectory) { 366 this.loggingDirectory = loggingDirectory; 367 } 368 369 370 public String getLoggingEncoding() { 371 return loggingEncoding; 372 } 373 374 375 public void setLoggingEncoding(String loggingEncoding) { 376 this.loggingEncoding = loggingEncoding; 377 } 378 379 380 public boolean isLogging() { 381 return logging; 382 } 383 384 public void setLoggingExtension(String loggingExtension) { 385 this.loggingExtension = loggingExtension; 386 } 387 388 public String getLoggingExtension() { 389 return loggingExtension; 390 } 391 392 public void setLoggingFilename(String loggingFilename) { 393 this.loggingFilename = loggingFilename; 394 } 395 396 public String getLoggingFilename() { 397 return loggingFilename; 398 } 399 400 401 } 402 | Popular Tags |