1 package info.magnolia.cms.mail.servlets; 2 3 import info.magnolia.cms.beans.runtime.Document; 4 import info.magnolia.cms.beans.runtime.MultipartForm; 5 import info.magnolia.cms.mail.MailConstants; 6 import info.magnolia.cms.mail.MgnlMailFactory; 7 import info.magnolia.cms.mail.commands.MailCommand; 8 import info.magnolia.cms.mail.templates.MailAttachment; 9 import info.magnolia.cms.servlets.ContextSensitiveServlet; 10 import info.magnolia.cms.util.RequestFormUtil; 11 import info.magnolia.context.Context; 12 import info.magnolia.context.MgnlContext; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.IOException ; 16 import java.io.PrintWriter ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Properties ; 21 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.slf4j.Logger; 28 import org.slf4j.LoggerFactory; 29 30 31 35 public class MgnlMailServlet extends ContextSensitiveServlet { 36 37 40 private static final long serialVersionUID = 222L; 41 42 static Logger log = LoggerFactory.getLogger(MgnlMailServlet.class); 43 44 private static final String TYPE = "type"; 45 46 private static final String SUBJECT = "subject"; 47 48 private static final String FROM = "from"; 49 50 private static final String TEXT = "text"; 51 52 private static final String TORECIPIENTS = "torecipients"; 53 54 private static final String CCRECIPIENTS = "ccrecipients"; 55 56 private static final String PARAMETERS = "parameters"; 57 58 private static final String FILE = "file"; 59 60 private static final String TEMPLATE = "template"; 61 62 private static final String ATT_ID = "att"; 63 64 private static final String ACTION = "action"; 65 66 private static final String HTML_TEXT_AREA = "textarea"; 67 68 private static final String HTML_SMALL_TEXT_AREA = "smalltextarea"; 69 70 private static final String HTML_BIG_TEXT_AREA = "bigtextarea"; 71 72 private static final String HTML_TEXT = "text"; 73 74 private static final String HTML_SELECT = "select"; 75 76 private static final String HTML_FILE = "file"; 77 78 private static final String NONE = "<none>"; 79 80 protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 81 throws ServletException , IOException { 82 super.doPost(httpServletRequest, httpServletResponse); 83 84 RequestFormUtil request = new RequestFormUtil(httpServletRequest); 85 86 if (request.getParameter(ACTION) == null) { 87 this.doGet(httpServletRequest, httpServletResponse); 88 return; 89 } 90 91 try { 92 Context ctx = MgnlContext.getInstance(); 94 95 MultipartForm form = (MultipartForm) httpServletRequest.getAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME); 97 Document doc = form.getDocument("file"); 98 99 if (doc != null) { 100 MailAttachment attachment = new MailAttachment(doc.getFile().toURL(), ATT_ID); 101 ctx.put(MailConstants.ATTRIBUTE_ATTACHMENT, attachment); 102 } 103 104 String template = request.getParameter(TEMPLATE); 106 String parameters = request.getParameter(PARAMETERS); 107 String to = request.getParameter(TORECIPIENTS); 108 String cc = request.getParameter(CCRECIPIENTS); 109 110 if (to != null) { 111 ctx.put(MailConstants.ATTRIBUTE_TO, to); 112 } 113 if (cc != null) { 114 ctx.put(MailConstants.ATTRIBUTE_CC, cc); 115 } 116 117 if (log.isDebugEnabled()) { 119 log.info(ctx.getAttributes().toString()); 120 } 121 HashMap map = convertToMap(parameters); 122 ctx.putAll(map); 123 124 if (isTemplate(template)) { 125 ctx.put(MailConstants.ATTRIBUTE_TEMPLATE, template); 127 } 128 else { 129 String type = request.getParameter(TYPE); 131 String subject = request.getParameter(SUBJECT); 132 String from = request.getParameter(FROM); 133 String text = request.getParameter(TEXT); 134 135 if (type != null) { 136 ctx.put(MailConstants.ATTRIBUTE_TYPE, type); 137 } 138 if (subject != null) { 139 ctx.put(MailConstants.ATTRIBUTE_SUBJECT, subject); 140 } 141 if (from != null) { 142 ctx.put(MailConstants.ATTRIBUTE_FROM, from); 143 } 144 if (text != null) { 145 ctx.put(MailConstants.ATTRIBUTE_TEXT, text); 146 } 147 } 148 149 MailCommand command = new MailCommand(); 151 command.execute(ctx); 152 153 setMessage(false, null, "Mail was sent to " + to, httpServletResponse); 154 } 155 catch (Exception e) { 156 setMessage(true, e, "Error while sending email ", httpServletResponse); 157 } 158 finally { 159 doGet(httpServletRequest, httpServletResponse); 160 } 161 } 162 163 private boolean isTemplate(String template) { 164 return StringUtils.isNotEmpty(template) && (!template.trim().equalsIgnoreCase(NONE)); 165 } 166 167 private void setMessage(boolean error, Exception e, String message, HttpServletResponse response) 168 throws IOException { 169 PrintWriter writer = response.getWriter(); 170 writer.write("<h1>Message</h1>"); 171 if (!error) { 172 writer.write("<p class=\"success\">"); 173 } 174 else { 175 writer.write("<p class=\"error\">"); 176 e.printStackTrace(writer); 177 } 178 writer.write(message); 179 writer.write("</p>"); 180 writer.flush(); 181 } 182 183 189 private HashMap convertToMap(String parameters) throws IOException { 190 HashMap map = new HashMap (); 191 ByteArrayInputStream string = new ByteArrayInputStream (parameters.getBytes()); 192 Properties props = new Properties (); 193 props.load(string); 194 195 Iterator iter = props.keySet().iterator(); 196 while (iter.hasNext()) { 197 String key = (String ) iter.next(); 198 map.put(key, props.get(key)); 199 } 200 log.info(map.toString()); 201 return map; 202 } 203 204 208 private String getServletStyle() { 209 StringBuffer buffer = new StringBuffer (); 210 buffer 211 .append("" 212 + "<style type=\"text/css\">\n" 213 + "<!--\n " 214 + "body { background-color: rgb(255,255,255); color: rgb(0,0,0); margin: 0px 0px; font-size: 10pt ; font-family: verdana}" 215 + "p.error { font-size: 8pt; color: red; }" 216 + "p.success { font-size: 8pt; color: green; }" 217 + "p.comments { font-size: 7pt; }" 218 + "textArea {color: blue }" 219 + "input {background-color:white ; font-size: 10pt ; width: 400px}" 220 + "textarea {background-color:white ; font-size: 10pt ; width: 400px}" 221 + "table {font-size: 10pt ; width: 90%}" 222 + "tr {font-size: 10pt}" 223 + "td {font-size: 10pt}" 224 + "h1 {font-family: verdana ; font-size : 12pt}" 225 + "-->\n " 226 + "</style>" 227 + ""); 228 return buffer.toString(); 229 } 230 231 protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 232 throws ServletException , IOException { 233 super.doGet(httpServletRequest, httpServletResponse); 234 StringBuffer sb = displayForm(); 235 236 PrintWriter writer = httpServletResponse.getWriter(); 237 writer.write(sb.toString()); 238 writer.flush(); 239 } 240 241 244 private StringBuffer displayForm() { 245 StringBuffer sb = new StringBuffer (); 246 247 sb.append(getServletStyle()); 248 sb.append("<center><h1>Email Servlet</h1></center>"); 249 250 sb.append("<form method=\"post\" enctype=\"multipart/form-data\">"); 251 sb.append("<input type=\"hidden\" width=\"80%\" name=\"" + ACTION + "\" value=\"action\"/>"); 252 253 sb.append("<h1>Options for all emails</h1>"); 254 255 sb.append("<table>"); 256 addSection("To", TORECIPIENTS, HTML_TEXT, "The main recipients of the email", sb); 257 addSection( 258 "Parameters", 259 PARAMETERS, 260 HTML_TEXT_AREA, 261 "Parameters that can be used when processing the email", 262 sb); 263 addSection( 264 "Template", 265 TEMPLATE, 266 HTML_SELECT, 267 "If a template is set, the type of the mail, the content and subject are retrieved from the repository.<br>" 268 + "The configuration for template us under the following node (/modules/mail/config/templates)", 269 sb); 270 sb.append("</table>"); 271 272 sb.append("<h1>Options for statically defined emails</h1>"); 273 sb.append("<table>"); 274 addSection("From", FROM, HTML_TEXT, "The sender of the email", sb); 275 addSection("Subject", SUBJECT, HTML_TEXT, "The subject of the email", sb); 276 addSection( 277 "Text", 278 TEXT, 279 HTML_BIG_TEXT_AREA, 280 "The content of the email. Note that this is not used if a template is set", 281 sb); 282 addSection("Cc", CCRECIPIENTS, HTML_TEXT, "The cc recipients of the email", sb); 283 addSection("Attach", FILE, HTML_FILE, "The attachment has a default id of '" 284 + ATT_ID 285 + "'. This is only limited in the servlet", sb); 286 addSection( 287 "Email Type", 288 TYPE, 289 HTML_SELECT, 290 "This define the format of the email. In the future more implementation can be added", 291 sb); 292 sb.append("</table>"); 293 sb.append("<center><input type=\"submit\" value=\"send\"/></center>"); 294 sb.append("</form>"); 295 296 return sb; 297 } 298 299 308 private StringBuffer addSection(String label, String name, String htmlInputType, String comments, StringBuffer sb) { 309 sb.append("<tr>"); 310 sb.append("<td width=\"40%\">").append(label).append(" :</td>"); 311 sb.append("<td>"); 312 if (htmlInputType.equals(HTML_SMALL_TEXT_AREA)) { 313 sb.append("<textArea cols=\"80\" width=\"80%\"rows=\"2\" name=\"").append(name).append("\"></textArea>"); 314 } 315 else if (htmlInputType.equals(HTML_SELECT)) { 316 sb.append(getSelectBox(name)); 317 } 318 else if (htmlInputType.equals(HTML_BIG_TEXT_AREA)) { 319 sb.append("<textArea cols=\"80\" width=\"80%\"rows=\"5\" name=\"").append(name).append("\"/></textArea>"); 320 } 321 else if (htmlInputType.equals(HTML_TEXT_AREA)) { 322 sb.append("<textArea cols=\"80\" width=\"80%\"rows=\"4\" name=\"").append(name).append("\"/></textArea>"); 323 } 324 else if (htmlInputType.equals(HTML_TEXT)) { 325 sb.append("<input type=\"text\" width=\"80%\"cols=\"80\" name=\"").append(name).append("\"/>"); 326 } 327 else if (htmlInputType.equals(HTML_FILE)) { 328 sb.append("<input cols=\"80\" width=\"80%\" type=\"file\" name=\"").append(name).append("\"/>"); 329 } 330 sb.append("<p class=\"comments\">").append(comments).append("</p>"); 331 sb.append("</td>"); 332 sb.append("</tr>"); 333 return sb; 334 } 335 336 341 private StringBuffer getSelectBox(String type) { 342 if (type.equalsIgnoreCase(TYPE)) { 343 return getMailTypeSelectBox(); 344 } 345 else if (type.equalsIgnoreCase(TEMPLATE)) { 346 return getTemplateSelectBox(); 347 } 348 else { 349 return new StringBuffer (); 350 } 351 352 } 353 354 private StringBuffer getMailTypeSelectBox() { 355 StringBuffer buffer = new StringBuffer (); 356 buffer.append("<select name=\"" + TYPE + "\">"); 357 addOption(MailConstants.MAIL_TEMPLATE_TEXT, buffer); 358 addOption(MailConstants.MAIL_TEMPLATE_HTML, buffer); 359 addOption(MailConstants.MAIL_TEMPLATE_VELOCITY, buffer); 360 buffer.append("</select>"); 361 return buffer; 362 } 363 364 private void addOption(String option, StringBuffer buffer) { 365 buffer.append("<option value=\"").append(option).append("\">").append(option).append(""); 366 } 367 368 private StringBuffer getTemplateSelectBox() { 369 StringBuffer buffer = new StringBuffer (); 370 buffer.append("<select width=\"80%\""); 371 try { 372 List list = MgnlMailFactory.getInstance().listTemplates(); 373 374 if (list.size() == 0) { 376 buffer.append(" disabled "); 377 } 378 buffer.append(" name=\"" + TEMPLATE + "\">"); 379 380 addOption(NONE, buffer); 381 for (int i = 0; i < list.size(); i++) { 382 String item = (String ) list.get(i); 383 addOption(item, buffer); 384 } 385 } 386 catch (Exception e) { 387 log.error("Error while getting the templates"); 388 } 389 buffer.append("</select>"); 390 return buffer; 391 392 } 393 394 } 395 | Popular Tags |