KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > mailTemplate > PreviewMessageAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.HashMap JavaDoc;
39
40 /**
41  * <p>Prepares message to preview on JSP page
42  * </p>
43  * <p><a HREF="PreviewMessageAction.java.htm"><i>View Source</i></a></p>
44  * <p/>
45  *
46  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
47  * @version $Revision: 1.11 $ $Date: 2006/03/10 17:10:27 $
48  * @struts.action path="/core/mailTemplate/previewMessage"
49  * scope="request"
50  * validate="false"
51  * roles="core-mailTemplate-previewMessage"
52  * @struts.action-forward name="previewMessage"
53  * path=".core.mailTemplate.previewMessage"
54  */

55 public final class PreviewMessageAction extends BaseAction {
56     /**
57      * @param mapping The ActionMapping used to select this instance
58      * @param form The optional ActionForm bean for this request (if any)
59      * @param request The HTTP request we are proceeding
60      * @param response The HTTP response we are creating
61      * @return an ActionForward instance describing where and how
62      * control should be forwarded, or null if response
63      * has already been completed
64      */

65     public ActionForward execute(ActionMapping mapping, ActionForm form,
66                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
67
68         Map JavaDoc model = (Map JavaDoc) request.getAttribute("model");
69         if ( model == null ) {
70             model = new HashMap JavaDoc();
71         }
72         MailEngine mailEngine = (MailEngine) getBean(Constants.MAIL_ENGINE_BEAN);
73
74         String JavaDoc templateIdentifier = (String JavaDoc) 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             // mail template not found. it might be deleted by someone else
81
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 JavaDoc locale = (String JavaDoc) request.getAttribute("locale");
88         if ( locale == null ) {
89             locale = (RequestUtils.getUserLocale(request, null)).getLanguage();
90         }
91
92         String JavaDoc from = null;
93         try {
94             from = mailEngine.mergeTemplate(templateIdentifier, "from", locale, model, false);
95             // validate address
96
mailEngine.prepareAddress(from, (String JavaDoc) 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 JavaDoc 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 JavaDoc 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             // replace encoded line breaks (&lt;br /&gt;) with normal tags (<br />);
120

121             try {
122                 body = RegExUtil.replaceAll(body, "&lt;br\040*/?&gt;", "<br />");
123             } catch ( MalformedPatternException e ) {
124                 // ignore
125
}
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