view 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
line wrap: on
line source


package nabble.view.web.tools;

import fschmidt.util.java.IoUtils;
import fschmidt.util.mail.Mail;
import fschmidt.util.mail.MailAddress;
import fschmidt.util.mail.MailHome;
import fschmidt.util.mail.PlainTextContent;
import nabble.model.MailingList;
import nabble.model.ModelException;
import nabble.model.ModelHome;
import nabble.model.Executors;
import nabble.model.Node;
import nabble.view.lib.Jtp;
import nabble.view.lib.Shared;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Date;
import java.util.Map;


public final class UploadMbox2 extends HttpServlet {
	private static final Logger logger = LoggerFactory.getLogger(UploadMbox2.class);

	protected void service(HttpServletRequest request,HttpServletResponse response)
		throws ServletException, IOException
	{
		PrintWriter out = response.getWriter();
		final Map<String,FileItem> map;
		try {
			map = Jtp.getFileItems(request);
		} catch(FileUploadException e) {
			throw new RuntimeException(e);
		}
		final Node forum = Jtp.getSiteNotNull(request).getNode(Long.parseLong(map.get("forum").getString()));
		final String mailErrorsTo = map.get("mailErrorsTo").getString();
		final int maxErrors = Integer.parseInt(map.get("maxErrors").getString());
		final boolean runRethread = map.get("runRethread") != null;
		final String exportDir = getInitParameter("exportDir");

		Executors.executeNow(new Runnable(){public void run() {
			try {
				StringBuilder buf = new StringBuilder();
				InputStream in;
				if (map.get("mbox").getName()!=null && map.get("mbox").getName().length()>0) {
					in = map.get("mbox").getInputStream();
				} else if (map.get("mboxurl").getString()!=null && map.get("mboxurl").getString().length()>0) {
					in = new URL(map.get("mboxurl").getString()).openStream();
				} else if (map.get("mboxfile").getString()!=null && map.get("mboxfile").getString().length()>0) {
					in = new FileInputStream(new File(exportDir+map.get("mboxfile").getString()));
				} else throw new RuntimeException("no mbox file or url defined");
				File file = File.createTempFile("mbox",null);
				try {
					OutputStream out2 = new FileOutputStream(file);
					IoUtils.copyAll(in,out2);
					out2.close();
					in.close();
					try {
						MailingList.ImportResult ir = forum.getMailingList().importMbox(file,mailErrorsTo,maxErrors);
						buf.append("Results from mbox import to forum "+forum.getSubject()+":\n");
						buf.append("imported "+ir.getImported()+" messages\n");
						buf.append("errors "+ir.getErrors()+"\n");
					} catch (ModelException e) {
						buf.append("Import error:\n");
						buf.append(e.getMessage());
						logger.error("",e);
					}
					Mail mail = MailHome.newMail();
					MailAddress to = new MailAddress(mailErrorsTo);
					mail.setFrom(to);
					mail.setTo(to);
					mail.setSentDate(new Date());
					mail.setContent(new PlainTextContent(buf.toString()));
					mail.setSubject("mbox import");
					ModelHome.send(mail);

				} finally {
					file.delete();
				}
			} catch (IOException e) {
				throw new RuntimeException(e);
			}

			if(runRethread) {
				StringBuilder buf = new StringBuilder();
				buf.append("Rethread procedure scheduled after mbox import for forum '" + forum.getSubject() + "' has finished\n");
				forum.getMailingList().rethread();
				Mail mail = MailHome.newMail();
				MailAddress to = new MailAddress(mailErrorsTo);
				mail.setFrom(to);
				mail.setTo(to);
				mail.setSentDate(new Date());
				mail.setContent(new PlainTextContent(buf.toString()));
				mail.setSubject("Rethreading finished");
				ModelHome.send(mail);
			}

		}});

		
		out.print( "\r\n<html>\r\n<head>\r\n" );

		Shared.title(request,response,"uploaded mbox file");
		
		out.print( "\r\n</head>\r\n<body>\r\n<h2>uploading mbox file to " );
		out.print( (forum.getSubjectHtml()) );
		out.print( "</h2>\r\n<p>Results will be emailed to " );
		out.print( (mailErrorsTo) );
		out.print( " after the import has completed.\r\n<p>return to forum: " );
		out.print( (Jtp.link(forum)) );
		out.print( "\r\n</body>\r\n</html>\r\n" );

	}
}