1 |
|
package com.ozacc.mail.impl; |
2 |
|
|
3 |
|
import java.io.File; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.InputStream; |
6 |
|
import java.io.StringReader; |
7 |
|
import java.io.StringWriter; |
8 |
|
import java.util.HashMap; |
9 |
|
import java.util.Iterator; |
10 |
|
import java.util.List; |
11 |
|
import java.util.Map; |
12 |
|
|
13 |
|
import org.apache.commons.logging.Log; |
14 |
|
import org.apache.commons.logging.LogFactory; |
15 |
|
import org.apache.velocity.VelocityContext; |
16 |
|
import org.apache.velocity.app.Velocity; |
17 |
|
import org.apache.velocity.exception.MethodInvocationException; |
18 |
|
import org.apache.velocity.exception.ParseErrorException; |
19 |
|
import org.apache.velocity.exception.ResourceNotFoundException; |
20 |
|
import org.apache.velocity.runtime.log.LogSystem; |
21 |
|
import org.jdom.Document; |
22 |
|
import org.jdom.Element; |
23 |
|
import org.jdom.JDOMException; |
24 |
|
import org.jdom.input.SAXBuilder; |
25 |
|
import org.jdom.output.XMLOutputter; |
26 |
|
|
27 |
|
import com.ozacc.mail.Mail; |
28 |
|
import com.ozacc.mail.MailBuildException; |
29 |
|
import com.ozacc.mail.MultipleMailBuilder; |
30 |
|
import com.ozacc.mail.VelocityMultipleMailBuilder; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
1 |
public class JDomXMLMailBuilder implements MultipleMailBuilder, VelocityMultipleMailBuilder { |
43 |
|
|
44 |
1 |
private static Log log = LogFactory.getLog(JDomXMLMailBuilder.class); |
45 |
|
|
46 |
1 |
private static String CACHE_KEY_SEPARATOR = "#"; |
47 |
|
|
48 |
1 |
private static String DEFAULT_MAIL_ID = "DEFAULT"; |
49 |
|
|
50 |
9 |
protected LogSystem velocityLogSystem = new VelocityLogSystem(); |
51 |
|
|
52 |
9 |
private boolean cacheEnabled = false; |
53 |
|
|
54 |
9 |
protected Map templateCache = new HashMap(); |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
18 |
public JDomXMLMailBuilder() {} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
public Mail buildMail(String classPath) throws MailBuildException { |
69 |
4 |
Document doc = getDocumentFromClassPath(classPath); |
70 |
2 |
return build(doc.getRootElement()); |
71 |
|
} |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
public Mail buildMail(String classPath, VelocityContext context) throws MailBuildException { |
83 |
1 |
String cacheKey = classPath + CACHE_KEY_SEPARATOR + DEFAULT_MAIL_ID; |
84 |
|
String templateXmlText; |
85 |
1 |
if (!hasTemplateCache(cacheKey)) { |
86 |
1 |
Document doc = getDocumentFromClassPath(classPath); |
87 |
1 |
templateXmlText = cacheTemplateText(doc, cacheKey); |
88 |
|
} else { |
89 |
0 |
templateXmlText = getTemplateCache(cacheKey); |
90 |
|
} |
91 |
|
try { |
92 |
1 |
return build(templateXmlText, context); |
93 |
0 |
} catch (Exception e) { |
94 |
0 |
throw new MailBuildException("¥á¡¼¥?¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
public Mail buildMail(File file) throws MailBuildException { |
106 |
2 |
Document doc = getDocumentFromFile(file); |
107 |
1 |
return build(doc.getRootElement()); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
public Mail buildMail(File file, VelocityContext context) throws MailBuildException { |
120 |
1 |
String cacheKey = file.getAbsolutePath() + CACHE_KEY_SEPARATOR + DEFAULT_MAIL_ID; |
121 |
|
String templateXmlText; |
122 |
1 |
if (!hasTemplateCache(cacheKey)) { |
123 |
1 |
Document doc = getDocumentFromFile(file); |
124 |
1 |
templateXmlText = cacheTemplateText(doc, cacheKey); |
125 |
|
} else { |
126 |
0 |
templateXmlText = getTemplateCache(cacheKey); |
127 |
|
} |
128 |
|
try { |
129 |
1 |
return build(templateXmlText, context); |
130 |
0 |
} catch (Exception e) { |
131 |
0 |
throw new MailBuildException("¥á¡¼¥?¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
private String cacheTemplateText(Document doc, String cacheKey) { |
136 |
2 |
XMLOutputter output = new XMLOutputter(); |
137 |
4 |
String templateXmlText = "<!DOCTYPE mail PUBLIC \"" + Mail.DOCTYPE_PUBLIC + "\" \"" |
138 |
2 |
+ Mail.DOCTYPE_SYSTEM + "\">\n" + output.outputString(doc.getRootElement()); |
139 |
2 |
log.debug("°Ê²¼¤ÎXML¥Ç¡¼¥¿¤ò¥¥ã¥Ã¥·¥å¤·¤Þ¤¹¡£\n" + templateXmlText); |
140 |
2 |
putTemplateCache(cacheKey, templateXmlText); |
141 |
2 |
return templateXmlText; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
protected Document getDocumentFromClassPath(String classPath) throws MailBuildException { |
151 |
5 |
InputStream is = getClass().getResourceAsStream(classPath); |
152 |
5 |
SAXBuilder builder = new SAXBuilder(true); |
153 |
5 |
builder.setEntityResolver(new DTDEntityResolver()); |
154 |
|
Document doc; |
155 |
|
try { |
156 |
5 |
doc = builder.build(is); |
157 |
1 |
} catch (JDOMException e) { |
158 |
1 |
throw new MailBuildException("XML¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£" + e.getMessage(), e); |
159 |
1 |
} catch (IOException e) { |
160 |
1 |
throw new MailBuildException("XML¥Õ¥¡¥¤¥?¤ÎÆÉ¤ß¹?¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
161 |
5 |
} finally { |
162 |
5 |
if (is != null) { |
163 |
|
try { |
164 |
4 |
is.close(); |
165 |
0 |
} catch (IOException e) { |
166 |
|
|
167 |
|
} |
168 |
|
} |
169 |
5 |
} |
170 |
3 |
return doc; |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
protected Document getDocumentFromFile(File file) { |
180 |
6 |
SAXBuilder builder = new SAXBuilder(true); |
181 |
6 |
builder.setEntityResolver(new DTDEntityResolver()); |
182 |
|
Document doc; |
183 |
|
try { |
184 |
6 |
doc = builder.build(file); |
185 |
0 |
} catch (JDOMException e) { |
186 |
0 |
throw new MailBuildException("XML¤Î¥Ñ¡¼¥¹¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£" + e.getMessage(), e); |
187 |
1 |
} catch (IOException e) { |
188 |
1 |
throw new MailBuildException("XML¥Õ¥¡¥¤¥?¤ÎÆÉ¤ß¹?¤ß¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
189 |
|
} |
190 |
5 |
return doc; |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
protected Mail build(Element mailElement) { |
200 |
7 |
Mail mail = new Mail(); |
201 |
7 |
setFrom(mailElement, mail); |
202 |
7 |
setRecipients(mailElement, mail); |
203 |
7 |
setSubject(mailElement, mail); |
204 |
7 |
setBody(mailElement, mail); |
205 |
7 |
setReplyTo(mailElement, mail); |
206 |
7 |
setReturnPath(mailElement, mail); |
207 |
|
|
208 |
7 |
setHtml(mailElement, mail); |
209 |
|
|
210 |
7 |
return mail; |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
protected Mail build(String templateText, VelocityContext context) throws Exception, |
228 |
|
ParseErrorException, |
229 |
|
MethodInvocationException, |
230 |
|
ResourceNotFoundException, |
231 |
|
IOException, JDOMException { |
232 |
2 |
if (log.isDebugEnabled()) { |
233 |
2 |
log.debug("¥½¡¼¥¹XML¥Ç¡¼¥¿\n" + templateText); |
234 |
|
} |
235 |
|
|
236 |
2 |
Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, velocityLogSystem); |
237 |
2 |
Velocity.init(); |
238 |
2 |
StringWriter w = new StringWriter(); |
239 |
2 |
Velocity.evaluate(context, w, "XML Mail Data", templateText); |
240 |
|
|
241 |
2 |
if (log.isDebugEnabled()) { |
242 |
2 |
log.debug("VelocityContext¤È¥Þ¡¼¥¸¸å¤ÎXML¥Ç¡¼¥¿\n" + w.toString()); |
243 |
|
} |
244 |
|
|
245 |
2 |
StringReader reader = new StringReader(w.toString()); |
246 |
2 |
SAXBuilder builder = new SAXBuilder(true); |
247 |
2 |
builder.setEntityResolver(new DTDEntityResolver()); |
248 |
2 |
Document mergedDoc = builder.build(reader); |
249 |
|
|
250 |
2 |
return build(mergedDoc.getRootElement()); |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
protected void setReturnPath(Element root, Mail mail) { |
258 |
7 |
Element returnPathElem = root.getChild("returnPath"); |
259 |
7 |
if (returnPathElem != null && returnPathElem.getAttributeValue("email") != null) { |
260 |
2 |
mail.setReturnPath(returnPathElem.getAttributeValue("email")); |
261 |
|
} |
262 |
7 |
} |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
protected void setReplyTo(Element root, Mail mail) { |
269 |
7 |
Element replyToElem = root.getChild("replyTo"); |
270 |
7 |
if (replyToElem != null && replyToElem.getAttributeValue("email") != null) { |
271 |
2 |
mail.setReplyTo(replyToElem.getAttributeValue("email")); |
272 |
|
} |
273 |
7 |
} |
274 |
|
|
275 |
|
|
276 |
|
|
277 |
|
|
278 |
|
|
279 |
|
protected void setBody(Element root, Mail mail) { |
280 |
7 |
Element bodyElem = root.getChild("body"); |
281 |
7 |
if (bodyElem != null) { |
282 |
7 |
mail.setText(bodyElem.getTextTrim()); |
283 |
|
} |
284 |
7 |
} |
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
protected void setHtml(Element root, Mail mail) { |
291 |
7 |
Element htmlElem = root.getChild("html"); |
292 |
7 |
if (htmlElem != null) { |
293 |
2 |
mail.setHtmlText(htmlElem.getTextTrim()); |
294 |
|
} |
295 |
7 |
} |
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
protected void setSubject(Element root, Mail mail) { |
302 |
7 |
Element subjectElem = root.getChild("subject"); |
303 |
7 |
if (subjectElem != null) { |
304 |
7 |
mail.setSubject(subjectElem.getTextTrim()); |
305 |
|
} |
306 |
7 |
} |
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
protected void setRecipients(Element root, Mail mail) { |
313 |
7 |
Element recipientsElem = root.getChild("recipients"); |
314 |
7 |
if (recipientsElem == null) { |
315 |
1 |
return; |
316 |
|
} |
317 |
|
|
318 |
6 |
List recipientElemList = recipientsElem.getChildren(); |
319 |
20 |
for (int i = 0, max = recipientElemList.size(); i < max; i++) { |
320 |
14 |
Element e = (Element)recipientElemList.get(i); |
321 |
14 |
if ("to".equals(e.getName())) { |
322 |
8 |
if (e.getAttributeValue("email") != null) { |
323 |
8 |
if (e.getAttributeValue("name") != null) { |
324 |
6 |
mail.addTo(e.getAttributeValue("email"), e.getAttributeValue("name")); |
325 |
|
} else { |
326 |
2 |
mail.addTo(e.getAttributeValue("email")); |
327 |
|
} |
328 |
|
} |
329 |
6 |
} else if ("cc".equals(e.getName())) { |
330 |
4 |
if (e.getAttributeValue("email") != null) { |
331 |
4 |
if (e.getAttributeValue("name") != null) { |
332 |
2 |
mail.addCc(e.getAttributeValue("email"), e.getAttributeValue("name")); |
333 |
|
} else { |
334 |
2 |
mail.addCc(e.getAttributeValue("email")); |
335 |
|
} |
336 |
|
} |
337 |
|
} else { |
338 |
2 |
if (e.getAttributeValue("email") != null) { |
339 |
2 |
mail.addBcc(e.getAttributeValue("email")); |
340 |
|
} |
341 |
|
} |
342 |
|
} |
343 |
6 |
} |
344 |
|
|
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
protected void setFrom(Element root, Mail mail) { |
350 |
7 |
Element fromElem = root.getChild("from"); |
351 |
7 |
if (fromElem != null && fromElem.getAttributeValue("email") != null) { |
352 |
6 |
if (fromElem.getAttributeValue("name") != null) { |
353 |
12 |
mail.setFrom(fromElem.getAttributeValue("email"), fromElem |
354 |
6 |
.getAttributeValue("name")); |
355 |
|
} else { |
356 |
0 |
mail.setFrom(fromElem.getAttributeValue("email")); |
357 |
|
} |
358 |
|
} |
359 |
7 |
} |
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
public synchronized void clearCache() { |
365 |
0 |
log.debug("¥Æ¥ó¥×¥?¡¼¥È¥¥ã¥Ã¥·¥å¤ò¥¯¥?¥¢¤·¤Þ¤¹¡£"); |
366 |
0 |
templateCache.clear(); |
367 |
0 |
} |
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
public boolean isCacheEnabled() { |
373 |
0 |
return cacheEnabled; |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
public void setCacheEnabled(boolean cacheEnabled) { |
380 |
0 |
if (!cacheEnabled) { |
381 |
0 |
clearCache(); |
382 |
|
} |
383 |
0 |
this.cacheEnabled = cacheEnabled; |
384 |
0 |
} |
385 |
|
|
386 |
|
protected boolean hasTemplateCache(String key) { |
387 |
2 |
if (cacheEnabled) { |
388 |
0 |
return templateCache.containsKey(key); |
389 |
|
} |
390 |
2 |
return false; |
391 |
|
} |
392 |
|
|
393 |
|
protected void putTemplateCache(String key, String templateXmlText) { |
394 |
2 |
if (cacheEnabled) { |
395 |
0 |
log.debug("¥Æ¥ó¥×¥?¡¼¥È¤ò¥¥ã¥Ã¥·¥å¤·¤Þ¤¹¡£[key='" + key + "']"); |
396 |
0 |
templateCache.put(key, templateXmlText); |
397 |
|
} |
398 |
2 |
} |
399 |
|
|
400 |
|
protected String getTemplateCache(String key) { |
401 |
0 |
if (hasTemplateCache(key)) { |
402 |
0 |
log.debug("¥Æ¥ó¥×¥?¡¼¥È¥¥ã¥Ã¥·¥å¤òÊÖ¤·¤Þ¤¹¡£[key='" + key + "']"); |
403 |
0 |
return (String)templateCache.get(key); |
404 |
|
} |
405 |
0 |
return null; |
406 |
|
} |
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
|
411 |
|
public Mail buildMail(String classPath, VelocityContext context, String mailId) |
412 |
|
throws MailBuildException { |
413 |
0 |
if (mailId == null || "".equals(mailId)) { |
414 |
0 |
throw new IllegalArgumentException("¥á¡¼¥?ID¤¬»ØÄꤵ¤?¤Æ¤¤¤Þ¤»¤ó¡£"); |
415 |
|
} |
416 |
|
|
417 |
0 |
String cacheKey = classPath + CACHE_KEY_SEPARATOR + mailId; |
418 |
|
String templateXmlText; |
419 |
0 |
if (!hasTemplateCache(cacheKey)) { |
420 |
0 |
Document doc = getDocumentFromClassPath(classPath); |
421 |
0 |
templateXmlText = getAndCacheTemplateText(doc, mailId, cacheKey); |
422 |
|
} else { |
423 |
0 |
templateXmlText = getTemplateCache(cacheKey); |
424 |
|
} |
425 |
|
try { |
426 |
0 |
return build(templateXmlText, context); |
427 |
0 |
} catch (Exception e) { |
428 |
0 |
throw new MailBuildException("¥á¡¼¥?¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
429 |
|
} |
430 |
|
} |
431 |
|
|
432 |
|
private String getAndCacheTemplateText(Document doc, String mailId, String cacheKey) |
433 |
|
throws MailBuildException { |
434 |
0 |
Element mailElem = getElementById(doc, mailId); |
435 |
0 |
XMLOutputter output = new XMLOutputter(); |
436 |
0 |
String templateXmlText = output.outputString(mailElem); |
437 |
|
|
438 |
0 |
putTemplateCache(cacheKey, templateXmlText); |
439 |
0 |
return templateXmlText; |
440 |
|
} |
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
public Mail buildMail(File file, VelocityContext context, String mailId) |
446 |
|
throws MailBuildException { |
447 |
0 |
if (mailId == null || "".equals(mailId)) { |
448 |
0 |
throw new IllegalArgumentException("¥á¡¼¥?ID¤¬»ØÄꤵ¤?¤Æ¤¤¤Þ¤»¤ó¡£"); |
449 |
|
} |
450 |
|
|
451 |
0 |
String cacheKey = file.getAbsolutePath() + CACHE_KEY_SEPARATOR + mailId; |
452 |
|
String templateXmlText; |
453 |
0 |
if (!hasTemplateCache(cacheKey)) { |
454 |
0 |
Document doc = getDocumentFromFile(file); |
455 |
0 |
templateXmlText = getAndCacheTemplateText(doc, mailId, cacheKey); |
456 |
|
} else { |
457 |
0 |
templateXmlText = getTemplateCache(cacheKey); |
458 |
|
} |
459 |
|
try { |
460 |
0 |
return build(templateXmlText, context); |
461 |
0 |
} catch (Exception e) { |
462 |
0 |
throw new MailBuildException("¥á¡¼¥?¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
463 |
|
} |
464 |
|
} |
465 |
|
|
466 |
|
|
467 |
|
|
468 |
|
|
469 |
|
public Mail buildMail(String classPath, String mailId) throws MailBuildException { |
470 |
0 |
Document doc = getDocumentFromClassPath(classPath); |
471 |
0 |
Element mailElem = getElementById(doc, mailId); |
472 |
0 |
return build(mailElem); |
473 |
|
} |
474 |
|
|
475 |
|
|
476 |
|
|
477 |
|
|
478 |
|
public Mail buildMail(File file, String mailId) throws MailBuildException { |
479 |
3 |
Document doc = getDocumentFromFile(file); |
480 |
3 |
Element mailElem = getElementById(doc, mailId); |
481 |
2 |
return build(mailElem); |
482 |
|
} |
483 |
|
|
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
|
489 |
|
|
490 |
|
|
491 |
|
private Element getElementById(Document doc, String id) { |
492 |
3 |
Element mailsElem = doc.getRootElement(); |
493 |
3 |
List mailElemList = mailsElem.getChildren("mail"); |
494 |
10 |
for (Iterator itr = mailElemList.iterator(); itr.hasNext();) { |
495 |
6 |
Element mailElem = (Element)itr.next(); |
496 |
6 |
String mailId = mailElem.getAttributeValue("id"); |
497 |
6 |
if (mailId.equals(id)) { |
498 |
2 |
return mailElem; |
499 |
|
} |
500 |
|
} |
501 |
1 |
throw new MailBuildException("»ØÄꤵ¤?¤¿ID[" + id + "]¤Î¥á¡¼¥?¥Ç¡¼¥¿¤Ï¸«¤Ä¤«¤ê¤Þ¤»¤ó¤Ç¤·¤¿¡£"); |
502 |
|
} |
503 |
|
|
504 |
|
} |