package example.bulk2;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.bentofw.bulkemail.*;
import com.bentofw.sql.*;
import com.bentofw.util.*;
/**
* @author Jun Inamori
*/
public class Queue extends HttpServlet{
private static PooledPS ps;
private static BulkEmailSender sender;
// SQL statment that will be called by the background Thread to
// select the list of recipients and insert them into:
// bulkemail_rcpt
// To set the message ID, the SQL statement should include one
// bind variable.
private static final String SQL
="INSERT INTO bulkemail_rcpt (mid,toa,ton)
SELECT ?,toa,ton FROM bulkemail_ex02 WHERE shouldSend=1";
public void init()
throws ServletException{
try{
ps=PooledPS.getInstance();
sender=BulkEmailSender.getInstance();
sender.prepare();
}
catch(Exception ex){
ex.printStackTrace();
throw new ServletException(ex);
}
}
public void destroy(){
try{
sender.stopBgThread();
}
catch(Exception ex){
ex.printStackTrace();
}
// Do this at the very last!
// Required only if you use PooledStatement
PooledPS.closeIfOpen();
}
private static final String DONE="done.html";
private static final String MISSING="missing.html";
private static final String LONG="long.html";
private static final String NOT_STARTED="not_started.html";
private static final String GOING_TO_STOP="going_to_stop.html";
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
String subject=req.getParameter("subject").trim();
String from_ad=req.getParameter("from_ad").trim();
String from_na=req.getParameter("from_na").trim();
String cset4email=req.getParameter("cset4email").trim();
String message=req.getParameter("message").trim();
// If any of the parameters is empty ...
if((subject.length() * from_ad.length() *
from_na.length() * cset4email.length() *
message.length())==0){
(req.getRequestDispatcher(MISSING)).forward(req,res);
return;
}
if(message.length()>1000){
(req.getRequestDispatcher(LONG)).forward(req,res);
return;
}
// 7bit will be enough for Content-Transfer-Encoding
// in most cases.
BulkEmailTemplate template
=new BulkEmailTemplate(subject,
from_ad,
from_na,
message,
cset4email,
"7bit");
try{
sender.queueBySql(template,
"example02",
SQL,
System.currentTimeMillis());
(req.getRequestDispatcher(DONE)).forward(req,res);
return;
}
catch(BgThreadIsNotStartedException ex){
(req.getRequestDispatcher(NOT_STARTED)).
forward(req,res);
return;
}
catch(BgThreadIsGoingToStopException ex){
(req.getRequestDispatcher(GOING_TO_STOP)).
forward(req,res);
return;
}
catch(Exception ex){
throw (new ServletException(ex));
}
}
}