package example.bulk1;
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;
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";
private static final String SAVE_NOT_STARTED="save_not_started.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;
}
PreparedStatement st=null;
ResultSet set=null;
List<String> list4address=new ArrayList<String>();
List<String> list4name=new ArrayList<String>();
try{
st=ps.getPS(
"SELECT toa,ton FROM bulkemail_ex01 WHERE shouldSend=1");
set=st.executeQuery();
while(set.next()){
list4address.add(set.getString(1));
list4name.add(set.getString(2));
}
set.close();
ps.reusePS(st);
}
catch(Exception ex){
if(st!=null){
ps.disposePS(st);
}
throw new ServletException(ex);
}
// 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.queueByList(template,
"example01",
list4address,
list4name,
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(SaveRcptThreadIsNotRunningException ex){
(req.getRequestDispatcher(SAVE_NOT_STARTED)).
forward(req,res);
return;
}
catch(Exception ex){
throw (new ServletException(ex));
}
}
}