package mircoders.servlet;
import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
-import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import mir.util.StringRoutines;
import mircoders.entity.EntityComment;
import mircoders.entity.EntityContent;
+import mircoders.global.CacheKey;
import mircoders.global.MirGlobal;
import mircoders.media.MediaUploadProcessor;
import mircoders.module.ModuleComment;
* open-postings to the newswire
*
* @author mir-coders group
- * @version $Id: ServletModuleOpenIndy.java,v 1.84 2003/04/29 02:36:50 zapata Exp $
+ * @version $Id: ServletModuleOpenIndy.java,v 1.89.2.1 2003/05/17 16:21:14 john Exp $
*
*/
private String commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;
private String postingFormTemplate, postingFormDoneTemplate, postingFormDupeTemplate;
private String searchResultsTemplate;
- private String prepareMailTemplate,sentMailTemplate;
+ private String prepareMailTemplate,sentMailTemplate,emailAnArticleTemplate;
private ModuleContent contentModule;
private ModuleComment commentModule;
private ModuleImages imageModule;
searchResultsTemplate = configuration.getString("ServletModule.OpenIndy.SearchResultsTemplate");
prepareMailTemplate = configuration.getString("ServletModule.OpenIndy.PrepareMailTemplate");
+ emailAnArticleTemplate = configuration.getString("ServletModule.OpenIndy.MailableArticleTemplate");
sentMailTemplate = configuration.getString("ServletModule.OpenIndy.SentMailTemplate");
directOp = configuration.getString("DirectOpenposting").toLowerCase();
commentModule = new ModuleComment(DatabaseComment.getInstance());
Session session = new HTTPAdapters.HTTPSessionAdapter(aRequest.getSession());
SimpleResponse response = new SimpleResponse(
- ServletHelper.makeGenerationData(new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)},
+ ServletHelper.makeGenerationData(aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)},
"bundles.open"));
response.setResponseValue("actionURL", aResponse.encodeURL(HttpUtils.getRequestURL(aRequest).toString())+"?"+SESSION_REQUEST_KEY+"="+aRequest.getSession().getId());
String to = req.getParameter("mail_to");
String from = req.getParameter("mail_from");
String from_name = req.getParameter("mail_from_name");
+ String from_ip = req.getRemoteAddr();
String comment = req.getParameter("mail_comment");
String mail_language = req.getParameter("mail_language");
Map mergeData = new HashMap();
+ mergeData.put("mail_to",to);
+ mergeData.put("mail_from",from);
+ mergeData.put("mail_from_name",from_name);
+ mergeData.put("mail_comment",comment);
+ mergeData.put("mail_aid",aid);
+ mergeData.put("mail_language",mail_language);
- if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){
-
- for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) {
- String pName=(String)theParams.nextElement();
- if (pName.startsWith("mail_")){
- mergeData.put( pName,req.getParameter(pName) );
- }
- }
+ if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){
deliver(req, res, mergeData, null, prepareMailTemplate);
}
else {
throw new ServletModuleUserExc("email.error.invalidfromaddress", new String[] {from});
}
+ CacheKey theCacheKey=new CacheKey("email",aid+mail_language);
+ String theEmailText;
- EntityContent contentEnt;
- try{
- contentEnt = (EntityContent)contentModule.getById(aid);
- }
- catch (Throwable e){
- throw new ServletModuleFailure("Couldn't get content for article "+aid + ": " + e.getMessage(), e);
- }
- String producerStorageRoot=configuration.getString("Producer.StorageRoot");
- String producerDocRoot=configuration.getString("Producer.DocRoot");
- String publishPath = contentEnt.getValue("publish_path");
- String txtFilePath = producerStorageRoot + producerDocRoot + "/" + mail_language +
- publishPath + "/" + aid + ".txt";
-
-
- File inputFile = new File(txtFilePath);
- String content;
-
- try{
- FileReader in = new FileReader(inputFile);
- StringWriter out = new StringWriter();
- int c;
- while ((c = in.read()) != -1)
- out.write(c);
- in.close();
- content= out.toString();
+ if (MirGlobal.mruCache().hasObject(theCacheKey)){
+ logger.info("fetching email text for article "+aid+" from cache");
+ theEmailText = (String) MirGlobal.mruCache().getObject(theCacheKey);
}
- catch (FileNotFoundException e){
- throw new ServletModuleFailure("No text file found in " + txtFilePath, e);
- }
- catch (IOException e){
- throw new ServletModuleFailure("Problem reading file in " + txtFilePath, e);
+ else {
+ EntityContent contentEnt;
+ try {
+ contentEnt = (EntityContent) contentModule.getById(aid);
+ StringWriter theEMailTextWriter = new StringWriter();
+ PrintWriter dest = new PrintWriter(theEMailTextWriter);
+ Map articleData = new HashMap();
+ articleData.put("article", MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("content", contentEnt));
+ articleData.put("languagecode", mail_language);
+ deliver(dest, req, res, articleData, null, emailAnArticleTemplate, mail_language);
+ theEmailText = theEMailTextWriter.toString();
+ MirGlobal.mruCache().storeObject(theCacheKey, theEmailText);
+ }
+ catch (Throwable e) {
+ throw new ServletModuleFailure("Couldn't get content for article " + aid + mail_language + ": " + e.getMessage(), e);
+ }
}
+
+ String content = theEmailText;
+
+
// add some headers
- content = "To: " + to + "\nReply-To: "+ from + "\n" + content;
+ content = "To: " + to + "\nReply-To: "+ from + "\nX-Originating-IP: "+ from_ip + "\n" + content;
// put in the comment where it should go
if (comment != null) {
String commentTextToInsert = "\n\nAttached comment from " + from_name + ":\n" + comment;
* Method for dynamically generating a pdf using iText
*/
- public void getpdf(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
+ public void getpdf(HttpServletRequest req, HttpServletResponse res)
+ throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
+ long starttime=System.currentTimeMillis();
String ID_REQUEST_PARAM = "id";
+ int maxArticlesInNewsleter = 15; // it is nice not to be dos'ed
try {
String idParam = req.getParameter(ID_REQUEST_PARAM);
if (idParam != null) {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- PDFGenerator pdfMaker = new PDFGenerator(out);
- RE re = new RE("[0-9]+");
- REMatch[] idMatches = re.getAllMatches(idParam);
+ RE re = new RE("[0-9]+");
- if (idMatches.length > 1) {
- pdfMaker.addLine();
- for (int i = 0; i < idMatches.length; i++) {
- REMatch aMatch = idMatches[i];
- String id = aMatch.toString();
- EntityContent contentEnt = (EntityContent) contentModule.getById(id);
- pdfMaker.addIndexItem(contentEnt);
- }
- }
+ REMatch[] idMatches=re.getAllMatches(idParam);
- for (int i = 0; i < idMatches.length; i++) {
- REMatch aMatch = idMatches[i];
+ String cacheSelector="";
- String id = aMatch.toString();
+ for (int i = 0; i < idMatches.length; i++){
+ cacheSelector= cacheSelector + "," + idMatches[i].toString();
+ }
- EntityContent contentEnt = (EntityContent) contentModule.getById(id);
- pdfMaker.add(contentEnt);
+ String cacheType="pdf";
- }
+ CacheKey theCacheKey = new CacheKey(cacheType,cacheSelector);
- pdfMaker.stop();
- res.setContentType("application/pdf");
- byte[] content = out.toByteArray();
- res.setContentLength(content.length);
- res.getOutputStream().write(content);
- res.getOutputStream().flush();
+ byte[] thePDF;
+
+ if (MirGlobal.mruCache().hasObject(theCacheKey)){
+ logger.info("fetching pdf from cache");
+ thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey);
+ }
+ else {
+ logger.info("generating pdf and caching it");
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ PDFGenerator pdfMaker = new PDFGenerator(out);
+
+ if (idMatches.length > 1){
+ pdfMaker.addLine();
+ for (int i = 0; i < idMatches.length && i < maxArticlesInNewsleter; i++){
+ REMatch aMatch = idMatches[i];
+ String id=aMatch.toString();
+ EntityContent contentEnt = (EntityContent)contentModule.getById(id);
+ pdfMaker.addIndexItem(contentEnt);
+ }
+ }
+
+ for (int i = 0; i < idMatches.length; i++){
+ REMatch aMatch = idMatches[i];
+ String id=aMatch.toString();
+ EntityContent contentEnt = (EntityContent)contentModule.getById(id);
+
+ pdfMaker.add(contentEnt);
+ }
+
+ pdfMaker.stop();
+ thePDF = out.toByteArray();
+
+ //and save all our hard work!
+ MirGlobal.mruCache().storeObject(theCacheKey,thePDF);
+ }
+
+ res.setContentType("application/pdf");
+ res.setContentLength(thePDF.length);
+ res.getOutputStream().write(thePDF);
+ res.getOutputStream().flush();
+ String elapsedtime=(new Long(System.currentTimeMillis()-starttime)).toString();
+ logger.info("pdf retireval took "+elapsedtime + " milliseconds" );
}
else {
- throw new ServletModuleExc("Missing id.");
+ throw new ServletModuleExc("Missing id.");
}
}
catch (Throwable t) {
}
+
public String generateOnetimePassword() {
Random r = new Random();
int random = r.nextInt();
public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator)
throws ServletModuleFailure {
try {
- Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open");
+ Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open");
responseData.put("data", aData);
responseData.put("extra", anExtra);
}
}
+ public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator,String aLocaleString)
+ throws ServletModuleFailure {
+ try {
+ Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { new Locale(aLocaleString,""), getFallbackLocale(aRequest)}, "bundles.open");
+ responseData.put("data", aData);
+ responseData.put("extra", anExtra);
+
+
+ Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);
+ generator.generate(anOutputWriter, responseData, logger);
+
+ anOutputWriter.close();
+ }
+ catch (Throwable e) {
+ logger.error("Error while generating " + aGenerator + ": " + e.getMessage());
+
+ throw new ServletModuleFailure(e);
+ }
+ }
+
+
public void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse,PrintWriter out, Throwable anException) {
try {
logger.error("error: " + anException);