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.85 2003/05/04 11:42:07 john Exp $
*
*/
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");
* 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);
-
- 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);
-
- }
- }
-
- 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();
- res.setContentType("application/pdf");
- byte[] content = out.toByteArray();
- res.setContentLength(content.length);
- res.getOutputStream().write(content);
- res.getOutputStream().flush();
+
+
+ RE re = new RE("[0-9]+");
+
+
+ REMatch[] idMatches=re.getAllMatches(idParam);
+
+ String cacheSelector="";
+
+ for (int i = 0; i < idMatches.length; i++){
+ cacheSelector= cacheSelector + "," + idMatches[i].toString();
+ }
+
+ String cacheType="pdf";
+
+ CacheKey theCacheKey = new CacheKey(cacheType,cacheSelector);
+
+ 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();