Mercurial Hosting > nabble
comparison src/nabble/view/web/tools/UploadMbox2.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 | |
2 package nabble.view.web.tools; | |
3 | |
4 import fschmidt.util.java.IoUtils; | |
5 import fschmidt.util.mail.Mail; | |
6 import fschmidt.util.mail.MailAddress; | |
7 import fschmidt.util.mail.MailHome; | |
8 import fschmidt.util.mail.PlainTextContent; | |
9 import nabble.model.MailingList; | |
10 import nabble.model.ModelException; | |
11 import nabble.model.ModelHome; | |
12 import nabble.model.Executors; | |
13 import nabble.model.Node; | |
14 import nabble.view.lib.Jtp; | |
15 import nabble.view.lib.Shared; | |
16 import org.apache.commons.fileupload.FileItem; | |
17 import org.apache.commons.fileupload.FileUploadException; | |
18 import org.slf4j.Logger; | |
19 import org.slf4j.LoggerFactory; | |
20 | |
21 import javax.servlet.ServletException; | |
22 import javax.servlet.http.HttpServlet; | |
23 import javax.servlet.http.HttpServletRequest; | |
24 import javax.servlet.http.HttpServletResponse; | |
25 import java.io.File; | |
26 import java.io.FileInputStream; | |
27 import java.io.FileOutputStream; | |
28 import java.io.IOException; | |
29 import java.io.InputStream; | |
30 import java.io.OutputStream; | |
31 import java.io.PrintWriter; | |
32 import java.net.URL; | |
33 import java.util.Date; | |
34 import java.util.Map; | |
35 | |
36 | |
37 public final class UploadMbox2 extends HttpServlet { | |
38 private static final Logger logger = LoggerFactory.getLogger(UploadMbox2.class); | |
39 | |
40 protected void service(HttpServletRequest request,HttpServletResponse response) | |
41 throws ServletException, IOException | |
42 { | |
43 PrintWriter out = response.getWriter(); | |
44 final Map<String,FileItem> map; | |
45 try { | |
46 map = Jtp.getFileItems(request); | |
47 } catch(FileUploadException e) { | |
48 throw new RuntimeException(e); | |
49 } | |
50 final Node forum = Jtp.getSiteNotNull(request).getNode(Long.parseLong(map.get("forum").getString())); | |
51 final String mailErrorsTo = map.get("mailErrorsTo").getString(); | |
52 final int maxErrors = Integer.parseInt(map.get("maxErrors").getString()); | |
53 final boolean runRethread = map.get("runRethread") != null; | |
54 final String exportDir = getInitParameter("exportDir"); | |
55 | |
56 Executors.executeNow(new Runnable(){public void run() { | |
57 try { | |
58 StringBuilder buf = new StringBuilder(); | |
59 InputStream in; | |
60 if (map.get("mbox").getName()!=null && map.get("mbox").getName().length()>0) { | |
61 in = map.get("mbox").getInputStream(); | |
62 } else if (map.get("mboxurl").getString()!=null && map.get("mboxurl").getString().length()>0) { | |
63 in = new URL(map.get("mboxurl").getString()).openStream(); | |
64 } else if (map.get("mboxfile").getString()!=null && map.get("mboxfile").getString().length()>0) { | |
65 in = new FileInputStream(new File(exportDir+map.get("mboxfile").getString())); | |
66 } else throw new RuntimeException("no mbox file or url defined"); | |
67 File file = File.createTempFile("mbox",null); | |
68 try { | |
69 OutputStream out2 = new FileOutputStream(file); | |
70 IoUtils.copyAll(in,out2); | |
71 out2.close(); | |
72 in.close(); | |
73 try { | |
74 MailingList.ImportResult ir = forum.getMailingList().importMbox(file,mailErrorsTo,maxErrors); | |
75 buf.append("Results from mbox import to forum "+forum.getSubject()+":\n"); | |
76 buf.append("imported "+ir.getImported()+" messages\n"); | |
77 buf.append("errors "+ir.getErrors()+"\n"); | |
78 } catch (ModelException e) { | |
79 buf.append("Import error:\n"); | |
80 buf.append(e.getMessage()); | |
81 logger.error("",e); | |
82 } | |
83 Mail mail = MailHome.newMail(); | |
84 MailAddress to = new MailAddress(mailErrorsTo); | |
85 mail.setFrom(to); | |
86 mail.setTo(to); | |
87 mail.setSentDate(new Date()); | |
88 mail.setContent(new PlainTextContent(buf.toString())); | |
89 mail.setSubject("mbox import"); | |
90 ModelHome.send(mail); | |
91 | |
92 } finally { | |
93 file.delete(); | |
94 } | |
95 } catch (IOException e) { | |
96 throw new RuntimeException(e); | |
97 } | |
98 | |
99 if(runRethread) { | |
100 StringBuilder buf = new StringBuilder(); | |
101 buf.append("Rethread procedure scheduled after mbox import for forum '" + forum.getSubject() + "' has finished\n"); | |
102 forum.getMailingList().rethread(); | |
103 Mail mail = MailHome.newMail(); | |
104 MailAddress to = new MailAddress(mailErrorsTo); | |
105 mail.setFrom(to); | |
106 mail.setTo(to); | |
107 mail.setSentDate(new Date()); | |
108 mail.setContent(new PlainTextContent(buf.toString())); | |
109 mail.setSubject("Rethreading finished"); | |
110 ModelHome.send(mail); | |
111 } | |
112 | |
113 }}); | |
114 | |
115 | |
116 out.print( "\r\n<html>\r\n<head>\r\n" ); | |
117 | |
118 Shared.title(request,response,"uploaded mbox file"); | |
119 | |
120 out.print( "\r\n</head>\r\n<body>\r\n<h2>uploading mbox file to " ); | |
121 out.print( (forum.getSubjectHtml()) ); | |
122 out.print( "</h2>\r\n<p>Results will be emailed to " ); | |
123 out.print( (mailErrorsTo) ); | |
124 out.print( " after the import has completed.\r\n<p>return to forum: " ); | |
125 out.print( (Jtp.link(forum)) ); | |
126 out.print( "\r\n</body>\r\n</html>\r\n" ); | |
127 | |
128 } | |
129 } | |
130 |