1 16 package com.blandware.atleap.webapp.action.core.mailTemplate; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.util.StringUtil; 20 import com.blandware.atleap.common.util.RegExUtil; 21 import com.blandware.atleap.model.core.MailTemplate; 22 import com.blandware.atleap.service.core.MailTemplateManager; 23 import com.blandware.atleap.service.exception.MailAddressException; 24 import com.blandware.atleap.service.util.MailEngine; 25 import com.blandware.atleap.webapp.action.core.BaseAction; 26 import org.apache.struts.action.ActionForm; 27 import org.apache.struts.action.ActionForward; 28 import org.apache.struts.action.ActionMapping; 29 import org.apache.struts.action.ActionMessage; 30 import org.apache.struts.action.ActionMessages; 31 import org.apache.struts.util.RequestUtils; 32 import org.apache.velocity.exception.VelocityException; 33 import org.apache.oro.text.regex.MalformedPatternException; 34 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import java.util.Map ; 38 import java.util.HashMap ; 39 40 55 public final class PreviewMessageAction extends BaseAction { 56 65 public ActionForward execute(ActionMapping mapping, ActionForm form, 66 HttpServletRequest request, HttpServletResponse response) throws Exception { 67 68 Map model = (Map ) request.getAttribute("model"); 69 if ( model == null ) { 70 model = new HashMap (); 71 } 72 MailEngine mailEngine = (MailEngine) getBean(Constants.MAIL_ENGINE_BEAN); 73 74 String templateIdentifier = (String ) request.getAttribute("identifier"); 75 76 MailTemplateManager mailTemplateManager = (MailTemplateManager) getBean(Constants.MAIL_TEMPLATE_MANAGER_BEAN); 77 MailTemplate template = mailTemplateManager.findMailTemplateByIdentifier(templateIdentifier); 78 79 if ( template == null ) { 80 ActionMessages errors = new ActionMessages(); 82 errors.add("mailTemplateNotFound", new ActionMessage("core.mailTemplate.errors.notFound")); 83 saveErrors(request, errors); 84 return mapping.findForward("listMailTemplates"); 85 } 86 87 String locale = (String ) request.getAttribute("locale"); 88 if ( locale == null ) { 89 locale = (RequestUtils.getUserLocale(request, null)).getLanguage(); 90 } 91 92 String from = null; 93 try { 94 from = mailEngine.mergeTemplate(templateIdentifier, "from", locale, model, false); 95 mailEngine.prepareAddress(from, (String ) template.getCharset().get(locale)); 97 from = StringUtil.htmlEncode(from); 98 } catch ( VelocityException e ) { 99 from = e.getLocalizedMessage(); 100 } catch ( MailAddressException e ) { 101 from = e.getLocalizedMessage(); 102 } 103 104 String subject = null; 105 try { 106 subject = mailEngine.mergeTemplate(templateIdentifier, "subject", locale, model, false); 107 subject = StringUtil.htmlEncode(subject); 108 } catch ( VelocityException e ) { 109 subject = e.getLocalizedMessage(); 110 } 111 112 String body = null; 113 try { 114 body = mailEngine.mergeTemplate(templateIdentifier, "body", locale, model, !template.isPlain()); 115 if ( template.isPlain() ) { 116 body = StringUtil.htmlEncode(body); 117 } 118 119 121 try { 122 body = RegExUtil.replaceAll(body, "<br\040*/?>", "<br />"); 123 } catch ( MalformedPatternException e ) { 124 } 126 127 } catch ( VelocityException e ) { 128 body = e.getLocalizedMessage(); 129 } 130 131 request.setAttribute("from", from); 132 request.setAttribute("subject", subject); 133 request.setAttribute("body", body); 134 if ( template.isPlain() ) { 135 request.setAttribute("templateIsPlain", "true"); 136 } 137 138 return mapping.findForward("previewMessage"); 139 } 140 } | Popular Tags |