support for the new MediaSearchTerm.
[mir.git] / source / mircoders / servlet / ServletModuleOpenIndy.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mircoders.servlet;\r
33 \r
34 import java.io.ByteArrayOutputStream;\r
35 import java.io.File;\r
36 import java.io.FileNotFoundException;\r
37 import java.io.FileReader;\r
38 import java.io.IOException;\r
39 import java.io.PrintWriter;\r
40 import java.io.StringWriter;\r
41 import java.util.ArrayList;\r
42 import java.util.Arrays;\r
43 import java.util.Collections;\r
44 import java.util.Date;\r
45 import java.util.Enumeration;\r
46 import java.util.GregorianCalendar;\r
47 import java.util.HashMap;\r
48 import java.util.Iterator;\r
49 import java.util.List;\r
50 import java.util.ListIterator;\r
51 import java.util.Map;\r
52 import java.util.Set;\r
53 import java.util.Vector;\r
54 import java.util.*;\r
55 import javax.servlet.http.HttpServletRequest;\r
56 import javax.servlet.http.HttpServletResponse;\r
57 import javax.servlet.http.HttpSession;\r
58 import javax.servlet.http.HttpUtils;\r
59 \r
60 import gnu.regexp.RE;\r
61 import gnu.regexp.REMatch;\r
62 import gnu.regexp.REMatchEnumeration;\r
63 import gnu.regexp.REException;\r
64 \r
65 import org.apache.commons.net.smtp.SMTPClient;\r
66 import org.apache.commons.net.smtp.SMTPReply;\r
67 import org.apache.log.Hierarchy;\r
68 import org.apache.log.Priority;\r
69 import org.apache.lucene.analysis.standard.StandardAnalyzer;\r
70 import org.apache.lucene.document.Document;\r
71 import org.apache.lucene.queryParser.QueryParser;\r
72 import org.apache.lucene.search.Hits;\r
73 import org.apache.lucene.search.IndexSearcher;\r
74 import org.apache.lucene.search.Query;\r
75 import org.apache.lucene.search.Searcher;\r
76 import org.apache.struts.util.MessageResources;\r
77 \r
78 import mir.entity.Entity;\r
79 import mir.entity.EntityList;\r
80 import mir.generator.Generator;\r
81 import mir.log.LoggerWrapper;\r
82 import mir.misc.FileHandler;\r
83 import mir.misc.StringUtil;\r
84 import mir.misc.WebdbMultipartRequest;\r
85 import mir.servlet.ServletModule;\r
86 import mir.servlet.ServletModuleExc;\r
87 import mir.servlet.ServletModuleFailure;\r
88 import mir.servlet.ServletModuleUserExc;\r
89 import mir.storage.StorageObjectFailure;\r
90 import mir.util.*;\r
91 import mircoders.pdf.PDFGenerator;\r
92 import mir.session.*;\r
93 import mir.util.HTTPRequestParser;\r
94 import mir.util.StringRoutines;\r
95 import mircoders.entity.EntityComment;\r
96 import mircoders.entity.EntityContent;\r
97 import mircoders.global.CacheKey;\r
98 import mircoders.global.MirGlobal;\r
99 import mircoders.media.*;\r
100 import mircoders.media.UnsupportedMediaFormatExc;\r
101 import mircoders.module.ModuleComment;\r
102 import mircoders.module.ModuleContent;\r
103 import mircoders.module.ModuleImages;\r
104 import mircoders.module.ModuleTopics;\r
105 import mircoders.search.AudioSearchTerm;\r
106 import mircoders.search.ContentSearchTerm;\r
107 import mircoders.search.ImagesSearchTerm;\r
108 import mircoders.search.KeywordSearchTerm;\r
109 import mircoders.search.MediaSearchTerm;\r
110 import mircoders.search.TextSearchTerm;\r
111 import mircoders.search.TopicSearchTerm;\r
112 import mircoders.search.UnIndexedSearchTerm;\r
113 import mircoders.search.VideoSearchTerm;\r
114 import mircoders.storage.DatabaseComment;\r
115 import mircoders.storage.DatabaseContent;\r
116 import mircoders.storage.DatabaseContentToMedia;\r
117 import mircoders.storage.DatabaseCommentToMedia;\r
118 import mircoders.storage.DatabaseContentToTopics;\r
119 import mircoders.storage.DatabaseImages;\r
120 import mircoders.storage.DatabaseLanguage;\r
121 import mircoders.storage.DatabaseTopics;\r
122 \r
123 /*\r
124  *  ServletModuleOpenIndy -\r
125  *   is the open-access-servlet, which is responsible for\r
126  *    adding comments to articles &\r
127  *    open-postings to the newswire\r
128  *\r
129  * @author mir-coders group\r
130  * @version $Id: ServletModuleOpenIndy.java,v 1.75 2003/04/18 14:01:41 john Exp $\r
131  *\r
132  */\r
133 \r
134 public class ServletModuleOpenIndy extends ServletModule\r
135 {\r
136 \r
137   private String        commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;\r
138   private String        postingFormTemplate, postingFormDoneTemplate, postingFormDupeTemplate;\r
139   private String        searchResultsTemplate;\r
140   private String        prepareMailTemplate,sentMailTemplate;\r
141   private ModuleContent contentModule;\r
142   private ModuleComment commentModule;\r
143   private ModuleImages  imageModule;\r
144   private ModuleTopics  topicsModule;\r
145   private String        directOp ="yes";\r
146   // Singelton / Kontruktor\r
147   private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();\r
148   public static ServletModule getInstance() { return instance; }\r
149 \r
150   private ServletModuleOpenIndy() {\r
151     super();\r
152     try {\r
153       logger = new LoggerWrapper("ServletModule.OpenIndy");\r
154 \r
155       commentFormTemplate = configuration.getString("ServletModule.OpenIndy.CommentTemplate");\r
156       commentFormDoneTemplate = configuration.getString("ServletModule.OpenIndy.CommentDoneTemplate");\r
157       commentFormDupeTemplate = configuration.getString("ServletModule.OpenIndy.CommentDupeTemplate");\r
158 \r
159       postingFormTemplate = configuration.getString("ServletModule.OpenIndy.PostingTemplate");\r
160       postingFormDoneTemplate = configuration.getString("ServletModule.OpenIndy.PostingDoneTemplate");\r
161       postingFormDupeTemplate = configuration.getString("ServletModule.OpenIndy.PostingDupeTemplate");\r
162 \r
163       searchResultsTemplate = configuration.getString("ServletModule.OpenIndy.SearchResultsTemplate");\r
164       prepareMailTemplate = configuration.getString("ServletModule.OpenIndy.PrepareMailTemplate");\r
165       sentMailTemplate = configuration.getString("ServletModule.OpenIndy.SentMailTemplate");\r
166       directOp = configuration.getString("DirectOpenposting").toLowerCase();\r
167       commentModule = new ModuleComment(DatabaseComment.getInstance());\r
168       mainModule = commentModule;\r
169       contentModule = new ModuleContent(DatabaseContent.getInstance());\r
170       topicsModule = new ModuleTopics(DatabaseTopics.getInstance());\r
171       imageModule = new ModuleImages(DatabaseImages.getInstance());\r
172       defaultAction="addposting";\r
173     }\r
174     catch (StorageObjectFailure e) {\r
175       logger.error("servletmoduleopenindy could not be initialized: " + e.getMessage());\r
176     }\r
177   }\r
178 \r
179   /**\r
180    * Method to return an "apology" when open postings are disabled\r
181    *\r
182    * @param aRequest\r
183    * @param aResponse\r
184    * @throws ServletModuleExc\r
185    * @throws ServletModuleUserExc\r
186    * @throws ServletModuleFailure\r
187    */\r
188   public void openPostingDisabled(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
189     deliver(aRequest, aResponse, (Map) null, null,\r
190        configuration.getString("ServletModule.OpenIndy.PostingDisabledTemplate"));\r
191   }\r
192 \r
193   /**\r
194    *  Method for making a comment\r
195    */\r
196 \r
197   public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
198     if (MirGlobal.abuse().getOpenPostingDisabled()) {\r
199       openPostingDisabled(req, res);\r
200 \r
201       return;\r
202     }\r
203 \r
204     String aid = req.getParameter("aid"); // the article id the comment will belong to\r
205 \r
206     if (aid != null && !aid.equals("")) {\r
207       try {\r
208         Map mergeData = new HashMap();\r
209 \r
210         // onetimepasswd\r
211         if (MirGlobal.abuse().getOpenPostingPassword()) {\r
212           String passwd = this.createOneTimePasswd();\r
213           HttpSession session = req.getSession(false);\r
214           session.setAttribute("passwd", passwd);\r
215           mergeData.put("passwd", passwd);\r
216         }\r
217         else {\r
218           mergeData.put("passwd", (String)null);\r
219         }\r
220         mergeData.put("aid", aid);\r
221 \r
222         Map extraInfo = new HashMap();\r
223         extraInfo.put("languagePopUpData", DatabaseLanguage.getInstance().getPopupData());\r
224 \r
225         deliver(req, res, mergeData, extraInfo, commentFormTemplate);\r
226       }\r
227       catch (Throwable t) {\r
228         throw new ServletModuleFailure("ServletModuleOpenIndy.addcomment: " + t.getMessage(), t);\r
229       }\r
230     }\r
231     else\r
232       throw new ServletModuleExc("aid not set!");\r
233   }\r
234 \r
235   /**\r
236    *  Method for inserting a comment into the Database and delivering\r
237    *  the commentDone Page\r
238    */\r
239 \r
240   public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
241     if (MirGlobal.abuse().getOpenPostingDisabled()) {\r
242       openPostingDisabled(req, res);\r
243 \r
244       return;\r
245     }\r
246 \r
247     String aid = req.getParameter("to_media"); // the article id the comment will belong to\r
248     if (aid != null && !aid.equals("")) {\r
249       // ok, collecting data from form\r
250       try {\r
251         Map withValues = getIntersectingValues(req, DatabaseComment.getInstance());\r
252 \r
253         //no html in comments(for now)\r
254         for (Iterator i = withValues.keySet().iterator(); i.hasNext(); ) {\r
255           String k = (String) i.next();\r
256           String v = (String) withValues.get(k);\r
257 \r
258           withValues.put(k, StringUtil.removeHTMLTags(v));\r
259         }\r
260         withValues.put("is_published", "1");\r
261         withValues.put("to_comment_status", "1");\r
262         withValues.put("is_html", "0");\r
263 \r
264         //checking the onetimepasswd\r
265         HttpSession session = req.getSession(false);\r
266         String sessionPasswd = (String) session.getAttribute("passwd");\r
267         if (sessionPasswd != null) {\r
268           String passwd = req.getParameter("passwd");\r
269           if (passwd == null || passwd.length() == 0) {\r
270             throw new ServletModuleUserExc("comment.error.missingpassword", new String[] {});\r
271           }\r
272           if (!sessionPasswd.equals(passwd)) {\r
273             throw new ServletModuleUserExc("comment.error.invalidpassword", new String[] {});\r
274           }\r
275           session.invalidate();\r
276         }\r
277 \r
278         String id = mainModule.add(withValues);\r
279 \r
280         SimpleResponse response = new SimpleResponse();\r
281         response.setResponseGenerator(commentFormDoneTemplate);\r
282 \r
283         if (id == null) {\r
284           deliver(req, res, (Map)null, null, commentFormDupeTemplate);\r
285         }\r
286         else {\r
287           DatabaseContent.getInstance().setUnproduced("id=" + aid);\r
288 \r
289           try {\r
290             EntityComment comment = (EntityComment) DatabaseComment.getInstance().selectById(id);\r
291             MirGlobal.localizer().openPostings().afterCommentPosting(comment);\r
292             MirGlobal.abuse().checkComment(\r
293                 comment, new HTTPAdapters.HTTPRequestAdapter(req), res);\r
294           }\r
295           catch (Throwable t) {\r
296             throw new ServletModuleExc(t.getMessage());\r
297           }\r
298         }\r
299 \r
300         // redirecting to url\r
301         // should implement back to article\r
302         deliver(req, res, response.getResponseValues(), null, response.getResponseGenerator());\r
303       }\r
304       catch (Throwable e) {\r
305         throw new ServletModuleFailure(e);\r
306       }\r
307     }\r
308     else\r
309       throw new ServletModuleExc("aid not set!");\r
310 \r
311   }\r
312 \r
313   /**\r
314    *  Method for delivering the form-Page for open posting\r
315    */\r
316 \r
317   public void addposting(HttpServletRequest req, HttpServletResponse res)\r
318       throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure\r
319   {\r
320     try {\r
321       if (MirGlobal.abuse().getOpenPostingDisabled()) {\r
322         openPostingDisabled(req, res);\r
323 \r
324         return;\r
325       }\r
326 \r
327       Map mergeData = new HashMap();\r
328 \r
329       // onetimepasswd\r
330       if (MirGlobal.abuse().getOpenPostingPassword()) {\r
331         String passwd = generateOnetimePassword();\r
332         HttpSession session = req.getSession(false);\r
333         session.setAttribute("passwd", passwd);\r
334         mergeData.put("passwd", passwd);\r
335       }\r
336       else {\r
337         mergeData.put("passwd", (String)null);\r
338       }\r
339 \r
340       String maxMedia = configuration.getString("ServletModule.OpenIndy.MaxMediaUploadItems");\r
341       String defaultMedia = configuration.getString("ServletModule.OpenIndy.DefaultMediaUploadItems");\r
342       String numOfMedia = req.getParameter("medianum");\r
343 \r
344       if (numOfMedia == null || numOfMedia.equals("")) {\r
345         numOfMedia = defaultMedia;\r
346       }\r
347       else if (Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {\r
348         numOfMedia = maxMedia;\r
349       }\r
350 \r
351       int mediaNum = Integer.parseInt(numOfMedia);\r
352       List mediaFields = new Vector();\r
353       for (int i = 0; i < mediaNum; i++) {\r
354         Integer mNum = new Integer(i + 1);\r
355         mediaFields.add(mNum.toString());\r
356       }\r
357       mergeData.put("medianum", numOfMedia);\r
358       mergeData.put("mediafields", mediaFields);\r
359       mergeData.put("to_topic", null);\r
360 \r
361       Map extraInfo = new HashMap();\r
362       extraInfo.put("languagePopUpData", DatabaseLanguage.getInstance().getPopupData());\r
363       extraInfo.put("themenPopupData", topicsModule.getTopicsAsSimpleList());\r
364 \r
365       extraInfo.put("topics", topicsModule.getTopicsList());\r
366       deliver(req, res, mergeData, extraInfo, postingFormTemplate);\r
367     }\r
368     catch (Throwable t) {\r
369       throw new ServletModuleFailure(t);\r
370     }\r
371   }\r
372 \r
373   /**\r
374    *  Method for inserting an open posting into the Database and delivering\r
375    *  the postingDone Page\r
376    */\r
377 \r
378   public void insposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure\r
379   {\r
380     if (MirGlobal.abuse().getOpenPostingDisabled()) {\r
381       openPostingDisabled(req, res);\r
382 \r
383       return;\r
384     }\r
385 \r
386     Map mergeData = new HashMap();\r
387     boolean setMedia=false;\r
388     boolean setTopic = false;\r
389 \r
390     try {\r
391 \r
392       WebdbMultipartRequest mp = null;\r
393       EntityList mediaList = null;\r
394       try {\r
395         // new MediaRequest, "1" is the id for the openPosting user\r
396         MediaRequest mediaReq = new MediaRequest("1", true);\r
397         mp = new WebdbMultipartRequest(req, (FileHandler)mediaReq);\r
398         mediaList = mediaReq.getEntityList();\r
399       }\r
400       catch (Throwable e) {\r
401         throw new ServletModuleFailure(e);\r
402       }\r
403 \r
404       Map withValues = mp.getParameters();\r
405 \r
406       //checking the onetimepasswd\r
407       HttpSession session = req.getSession(false);\r
408       String sessionPasswd = (String) session.getAttribute("passwd");\r
409       if (sessionPasswd != null){\r
410         String passwd = (String) withValues.get("passwd");\r
411 \r
412         if ( passwd == null || passwd.length()==0) {\r
413           throw new ServletModuleUserExc("posting.error.missingpassword", new String[] {});\r
414         }\r
415         if (!sessionPasswd.equals(passwd)) {\r
416           throw new ServletModuleUserExc("posting.error.invalidpassword", new String[] {});\r
417         }\r
418         session.invalidate();\r
419       }\r
420 \r
421       if ((((String)withValues.get("title")).length() == 0) ||\r
422           (((String)withValues.get("description")).length() == 0) ||\r
423           (((String)withValues.get("content_data")).length() == 0))\r
424         throw new ServletModuleUserExc("posting.error.missingfield", new String[] {});\r
425 \r
426       // call the routines that escape html\r
427 \r
428       for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){\r
429         String k=(String)i.next();\r
430         String v=(String)withValues.get(k);\r
431 \r
432         if (k.equals("content_data")){\r
433           //this doesn't quite work yet, so for now, all html goes\r
434           //withValues.put(k,StringUtil.approveHTMLTags(v));\r
435           withValues.put(k,StringUtil.deleteForbiddenTags(v));\r
436         }\r
437         else if (k.equals("description")) {\r
438           String tmp = StringUtil.deleteForbiddenTags(v);\r
439           withValues.put(k,StringUtil.deleteHTMLTableTags(tmp));\r
440         }\r
441         else {\r
442           withValues.put(k,StringUtil.removeHTMLTags(v));\r
443         }\r
444 \r
445       }\r
446 \r
447       withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));\r
448       withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));\r
449       withValues.put("is_produced", "0");\r
450       withValues.put("is_published","1");\r
451       if (directOp.equals("yes"))\r
452         withValues.put("to_article_type","1");\r
453 \r
454       withValues.put("to_publisher","1");\r
455 \r
456       // inserting  content into database\r
457       String cid = contentModule.add(withValues);\r
458       logger.debug("id: "+cid);\r
459       //insert was not successfull\r
460       if(cid==null){\r
461 \r
462         //How do we know that it was not succesful cause of a\r
463         //dupe, what if it failed cause of "No space left on device"?\r
464         //Or is there something I am missing? Wouldn't it be better\r
465         //to have an explicit dupe check and then insert? I have no\r
466         //idea what I am talking about. this comment is in case\r
467         //I forget to explicitely ask. -mh\r
468         deliver(req, res, mergeData, null, postingFormDupeTemplate);\r
469         return;\r
470       }\r
471 \r
472       String[] to_topicsArr = mp.getParameterValues("to_topic");\r
473 \r
474       if (to_topicsArr != null && to_topicsArr.length > 0) {\r
475         try{\r
476           DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);\r
477           setTopic = true;\r
478         }\r
479         catch (Throwable e) {\r
480           logger.error("setting content_x_topic failed");\r
481           contentModule.deleteById(cid);\r
482           throw new ServletModuleFailure("smod - openindy :: insposting: setting content_x_topic failed: "+e.toString(), e);\r
483         } //end try\r
484       } //end if\r
485 \r
486       //if we're here all is ok... associate the media to the article\r
487       for(int i=0;i<mediaList.size();i++) {\r
488         Entity mediaEnt = (Entity)mediaList.elementAt(i);\r
489         DatabaseContentToMedia.getInstance().addMedia(cid,mediaEnt.getId());\r
490       }\r
491 \r
492       EntityContent article = (EntityContent) contentModule.getById(cid);\r
493 \r
494       try {\r
495         MirGlobal.abuse().checkArticle(article, req, res);\r
496         MirGlobal.localizer().openPostings().afterContentPosting(article);\r
497       }\r
498       catch (Throwable t) {\r
499         logger.error("Error while post-processing article: " + t.getMessage());\r
500       }\r
501     }\r
502     catch (Throwable e) {\r
503       Throwable cause = ExceptionFunctions.traceCauseException(e);\r
504 \r
505       if (cause instanceof UnsupportedMediaFormatExc) {\r
506         throw new ServletModuleUserExc("media.unsupportedformat", new String[] {} );\r
507       }\r
508       throw new ServletModuleFailure(e);\r
509     }\r
510 \r
511     deliver(req, res, mergeData, null, postingFormDoneTemplate);\r
512   }\r
513 \r
514   /**\r
515    * Routine for innitiation tha d\r
516    *\r
517    * @param aRequest\r
518    * @param aResponse\r
519    * @throws ServletModuleExc\r
520    * @throws ServletModuleUserExc\r
521    * @throws ServletModuleFailure\r
522    */\r
523 /*\r
524   public void comment(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
525     try {\r
526       if (MirGlobal.abuse().getOpenPostingDisabled()) {\r
527         openPostingDisabled(aRequest, aResponse);\r
528 \r
529         return;\r
530       }\r
531 \r
532       Request request = new HTTPAdapters.HTTPParsedRequestAdapter(new HTTPParsedRequest(aRequest, 1000000, "/tmp"));\r
533       Session session = new HTTPAdapters.HTTPSessionAdapter(aRequest.getSession());\r
534       SimpleResponse response = new SimpleResponse(\r
535           ServletHelper.makeGenerationData(new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open"));\r
536 \r
537       Iterator i = DatabaseContent.getInstance().getFields().iterator();\r
538       while (i.hasNext()) {\r
539         response.setResponseValue((String) i.next(), null);\r
540       }\r
541 \r
542       MirGlobal.localizer().openPostings().initializeCommentPosting(request, session, response);\r
543 \r
544       ServletHelper.generateOpenPostingResponse(aResponse.getWriter(), response.getResponseValues(), response.getResponseGenerator());\r
545     }\r
546     catch (Throwable t) {\r
547       t.printStackTrace(System.out);\r
548       System.out.println("comment: " + t.toString());\r
549       throw new ServletModuleFailure("ServletModuleOpenIndy.addcomment: " + t.getMessage(), t);\r
550     }\r
551   }\r
552 \r
553   public void processcomment(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
554     try {\r
555       if (MirGlobal.abuse().getOpenPostingDisabled()) {\r
556         openPostingDisabled(aRequest, aResponse);\r
557 \r
558         return;\r
559       }\r
560 \r
561       Request request = new HTTPAdapters.HTTPParsedRequestAdapter(new HTTPParsedRequest(aRequest, 1000000, "/tmp"));\r
562       Session session = new HTTPAdapters.HTTPSessionAdapter(aRequest.getSession());\r
563       SimpleResponse response = new SimpleResponse(\r
564           ServletHelper.makeGenerationData(new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open"));\r
565       Map commentFields = new HashMap();\r
566 \r
567       Iterator i = DatabaseContent.getInstance().getFields().iterator();\r
568       while (i.hasNext()) {\r
569         String field = (String) i.next();\r
570         response.setResponseValue(field, request.getParameter(field));\r
571         if (request.getParameter(field)!=null) {\r
572           commentFields.put(field, request.getParameter(field));\r
573         }\r
574       }\r
575 \r
576       List validationErrors = MirGlobal.localizer().openPostings().validateCommentPosting(request, session);\r
577 \r
578       if (validationErrors != null && validationErrors.size()>0) {\r
579         MirGlobal.localizer().openPostings().processCommentPosting(request, session, response);\r
580 \r
581         ServletHelper.generateOpenPostingResponse(aResponse.getWriter(), response.getResponseValues(), response.getResponseGenerator());\r
582       }\r
583       else {\r
584         EntityComment comment = (EntityComment) commentModule.createNew ();\r
585         comment.setValues(commentFields);\r
586         MirGlobal.abuse().checkComment(comment, aRequest, aResponse);\r
587         MirGlobal.localizer().openPostings().finishCommentPosting(request, session, comment);\r
588 \r
589         String id = comment.insert();\r
590         if(id==null){\r
591           MirGlobal.localizer().openPostings().afterDuplicateCommentPosting(request, session, response, comment);\r
592 \r
593           logger.info("Dupe comment rejected");\r
594 \r
595           ServletHelper.generateOpenPostingResponse(aResponse.getWriter(), response.getResponseValues(), response.getResponseGenerator());\r
596         }\r
597         else {\r
598           // media\r
599           List mediaItems = new Vector();\r
600           i = request.getUploadedFiles().iterator();\r
601           while (i.hasNext()) {\r
602             UploadedFile file = (UploadedFile) i.next();\r
603             Entity mediaItem = MediaUploadProcessor.processMediaUpload(file);\r
604             DatabaseCommentToMedia.getInstance().addMedia(comment.getId(), mediaItem.getId());\r
605           }\r
606 \r
607           MirGlobal.localizer().openPostings().afterCommentPosting(request, session, response, comment);\r
608 \r
609           MirGlobal.abuse().logComment(aRequest.getRemoteAddr(), id, new Date(), (String) aRequest.getHeader("User-Agent"));\r
610           DatabaseContent.getInstance().setUnproduced("id=" + comment.getValue("to_media"));\r
611           logger.info("Comment posted");\r
612           ServletHelper.generateOpenPostingResponse(aResponse.getWriter(), response.getResponseValues(), response.getResponseGenerator());\r
613         }\r
614       }\r
615     }\r
616     catch (Throwable t) {\r
617       ExceptionFunctions.traceCauseException(t).printStackTrace();\r
618 \r
619       throw new ServletModuleFailure("ServletModuleOpenIndy.addcomment: " + t.getMessage(), t);\r
620     }\r
621   }\r
622 */\r
623 \r
624   private static final String SESSION_REQUEST_KEY="sessionid";\r
625 \r
626   public void opensession(HttpServletRequest aRequest, HttpServletResponse aResponse)\r
627       throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
628 \r
629     try {\r
630       Request request = new HTTPAdapters.HTTPParsedRequestAdapter(new HTTPParsedRequest(aRequest, 1000000, "/tmp"));\r
631 \r
632       if (aRequest.isRequestedSessionIdValid() && !aRequest.isRequestedSessionIdFromURL() &&\r
633           !aRequest.getRequestedSessionId().equals(aRequest.getParameter(SESSION_REQUEST_KEY)))\r
634         aRequest.getSession().invalidate();\r
635 \r
636       Session session = new HTTPAdapters.HTTPSessionAdapter(aRequest.getSession());\r
637 \r
638       SimpleResponse response = new SimpleResponse(\r
639           ServletHelper.makeGenerationData(new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)},\r
640              "bundles.open"));\r
641 \r
642       response.setResponseValue("actionURL", aResponse.encodeURL(HttpUtils.getRequestURL(aRequest).toString())+"?"+SESSION_REQUEST_KEY+"="+aRequest.getSession().getId());\r
643 \r
644       SessionHandler handler = MirGlobal.localizer().openPostings().getOpenSessionHandler(request, session);\r
645 \r
646       handler.processRequest(request, session, response);\r
647       ServletHelper.generateOpenPostingResponse(aResponse.getWriter(), response.getResponseValues(), response.getResponseGenerator());\r
648     }\r
649     catch (Throwable t) {\r
650       logger.error(t.toString());\r
651       t.printStackTrace(logger.asPrintWriter(logger.DEBUG_MESSAGE));\r
652 \r
653       throw new ServletModuleFailure(t);\r
654     }\r
655   }\r
656 \r
657   /**\r
658    * Method for preparing and sending a content as an email message\r
659    */\r
660 \r
661   public void mail(HttpServletRequest req, HttpServletResponse res)\r
662       throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure\r
663   {\r
664     String aid = req.getParameter("mail_aid");\r
665     if (aid == null){\r
666       throw new ServletModuleExc("An article id must be specified in requests to email an article.  Something therefore went badly wrong....");\r
667     }\r
668 \r
669     String to = req.getParameter("mail_to");\r
670     String from = req.getParameter("mail_from");\r
671     String from_name = req.getParameter("mail_from_name");\r
672     String comment = req.getParameter("mail_comment");\r
673     String mail_language = req.getParameter("mail_language");\r
674 \r
675     Map mergeData = new HashMap();\r
676 \r
677     if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){\r
678 \r
679       for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) {\r
680         String pName=(String)theParams.nextElement();\r
681         if (pName.startsWith("mail_")){\r
682           mergeData.put( pName,req.getParameter(pName) );\r
683         }\r
684       }\r
685 \r
686       deliver(req, res, mergeData, null, prepareMailTemplate);\r
687     }\r
688     else {\r
689       //run checks on to and from and mail_language to make sure no monkey business occurring\r
690       if (mail_language.indexOf('.') != -1 || mail_language.indexOf('/') != -1 ) {\r
691         throw new ServletModuleExc("Invalid language");\r
692       }\r
693       if (to.indexOf('\n') != -1\r
694           || to.indexOf('\r') != -1\r
695           || to.indexOf(',') != -1) {\r
696         throw new ServletModuleUserExc("email.error.invalidtoaddress", new String[] {to});\r
697       }\r
698       if (from.indexOf('\n') != -1 || from.indexOf('\r') != -1 || from.indexOf(',') != -1 ) {\r
699         throw new ServletModuleUserExc("email.error.invalidfromaddress", new String[] {from});\r
700       }\r
701 \r
702 \r
703       EntityContent contentEnt;\r
704       try{\r
705         contentEnt = (EntityContent)contentModule.getById(aid);\r
706       }\r
707       catch (Throwable e){\r
708         throw new ServletModuleFailure("Couldn't get content for article "+aid + ": " + e.getMessage(), e);\r
709       }\r
710       String producerStorageRoot=configuration.getString("Producer.StorageRoot");\r
711       String producerDocRoot=configuration.getString("Producer.DocRoot");\r
712       String publishPath = contentEnt.getValue("publish_path");\r
713       String txtFilePath = producerStorageRoot + producerDocRoot + "/" + mail_language +\r
714                                                                                                          publishPath + "/" + aid + ".txt";\r
715 \r
716 \r
717       File inputFile = new File(txtFilePath);\r
718       String content;\r
719 \r
720       try{\r
721         FileReader in = new FileReader(inputFile);\r
722         StringWriter out = new StringWriter();\r
723         int c;\r
724         while ((c = in.read()) != -1)\r
725           out.write(c);\r
726         in.close();\r
727         content= out.toString();\r
728       }\r
729       catch (FileNotFoundException e){\r
730         throw new ServletModuleFailure("No text file found in " + txtFilePath, e);\r
731       }\r
732       catch (IOException e){\r
733         throw new ServletModuleFailure("Problem reading file in " + txtFilePath, e);\r
734       }\r
735       // add some headers\r
736       content = "To: " + to + "\nReply-To: "+ from + "\n" + content;\r
737       // put in the comment where it should go\r
738       if (comment != null) {\r
739         String commentTextToInsert = "\n\nAttached comment from " + from_name + ":\n" + comment;\r
740         try {\r
741           content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!",commentTextToInsert);\r
742         }\r
743         catch (Throwable e){\r
744           throw new ServletModuleFailure("Problem doing regular expression replacement " + e.toString(), e);\r
745         }\r
746       }\r
747       else{\r
748         try {\r
749           content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!","");\r
750         }\r
751         catch (Throwable e){\r
752           throw new ServletModuleFailure("Problem doing regular expression replacement " + e.toString(), e);\r
753         }\r
754       }\r
755 \r
756       SMTPClient client=new SMTPClient();\r
757       try {\r
758         int reply;\r
759         client.connect(configuration.getString("ServletModule.OpenIndy.SMTPServer"));\r
760 \r
761         reply = client.getReplyCode();\r
762 \r
763         if (!SMTPReply.isPositiveCompletion(reply)) {\r
764           client.disconnect();\r
765           throw new ServletModuleExc("SMTP server refused connection.");\r
766         }\r
767 \r
768         client.sendSimpleMessage(configuration.getString("ServletModule.OpenIndy.EmailIsFrom"), to, content);\r
769 \r
770         client.disconnect();\r
771         //mission accomplished\r
772         deliver(req, res, mergeData, null, sentMailTemplate);\r
773       }\r
774       catch(IOException e) {\r
775         if(client.isConnected()) {\r
776           try {\r
777             client.disconnect();\r
778           } catch(IOException f) {\r
779             // do nothing\r
780           }\r
781         }\r
782         throw new ServletModuleFailure(e);\r
783       }\r
784     }\r
785   }\r
786 \r
787 \r
788 \r
789   /**\r
790    * Method for querying a lucene index\r
791    *\r
792    * @param req\r
793    * @param res\r
794    * @throws ServletModuleExc\r
795    * @throws ServletModuleUserExc\r
796    * @throws ServletModuleFailure\r
797    */\r
798 \r
799   public void search(HttpServletRequest req, HttpServletResponse res)\r
800       throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
801     try {\r
802       final String[] search_variables = { "search_content", "search_boolean", "search_creator",\r
803           "search_topic", "search_hasImages", "search_hasAudio", "search_hasVideo", "search_sort",\r
804           "search_submit", "search_back", "search_forward" };\r
805       HTTPRequestParser requestParser = new HTTPRequestParser(req);\r
806 \r
807       int increment=10;\r
808 \r
809       HttpSession session = req.getSession(false);\r
810 \r
811       String queryString="";\r
812 \r
813       Map mergeData = new HashMap();\r
814 \r
815       KeywordSearchTerm dateTerm = new KeywordSearchTerm("date_formatted","search_date","webdb_create_formatted","webdb_create_formatted","webdb_create_formatted");\r
816       UnIndexedSearchTerm whereTerm = new UnIndexedSearchTerm("","","","where","where");\r
817       TextSearchTerm creatorTerm = new TextSearchTerm("creator","search_creator","creator","creator","creator");\r
818       TextSearchTerm titleTerm = new TextSearchTerm("title","search_content","title","title","title");\r
819       TextSearchTerm descriptionTerm =  new TextSearchTerm("description","search_content","description","description","description");\r
820       ContentSearchTerm contentTerm = new ContentSearchTerm("content_data","search_content","content","","");\r
821       TopicSearchTerm topicTerm = new TopicSearchTerm();\r
822       ImagesSearchTerm imagesTerm = new ImagesSearchTerm();\r
823       AudioSearchTerm audioTerm = new AudioSearchTerm();\r
824       VideoSearchTerm videoTerm = new VideoSearchTerm();\r
825       MediaSearchTerm mediaTerm = new MediaSearchTerm();\r
826       \r
827       //make the query available to subsequent iterations\r
828 \r
829       Iterator j = Arrays.asList(search_variables).iterator();\r
830       while (j.hasNext()) {\r
831         String variable = (String) j.next();\r
832 \r
833         mergeData.put(variable, requestParser.getParameter(variable));\r
834       }\r
835 \r
836       try{\r
837         mergeData.put("topics", topicsModule.getTopicsAsSimpleList());\r
838       }\r
839       catch(Throwable e) {\r
840         logger.debug("Can't get topics: " + e.toString());\r
841       }\r
842 \r
843       String searchBackValue = req.getParameter("search_back");\r
844       String searchForwardValue = req.getParameter("search_forward");\r
845 \r
846       if (searchBackValue != null){\r
847         int totalHits = ((Integer) session.getAttribute("numberOfHits")).intValue();\r
848         int newPosition=((Integer)session.getAttribute("positionInResults")).intValue()-increment;\r
849         if (newPosition<0)\r
850           newPosition=0;\r
851         if (newPosition >= totalHits)\r
852           newPosition=totalHits-1;\r
853         session.setAttribute("positionInResults",new Integer(newPosition));\r
854       }\r
855       else {\r
856         if (searchForwardValue != null){\r
857           int totalHits = ((Integer) session.getAttribute("numberOfHits")).intValue();\r
858           int newPosition=((Integer)session.getAttribute("positionInResults")).intValue()+increment;\r
859           if (newPosition<0)\r
860             newPosition=0;\r
861           if (newPosition >= totalHits)\r
862             newPosition=totalHits-1;\r
863 \r
864           session.setAttribute("positionInResults",new Integer(newPosition));\r
865         }\r
866         else {\r
867           String indexPath=configuration.getString("IndexPath");\r
868 \r
869 \r
870           String creatorFragment = creatorTerm.makeTerm(req);\r
871           if (creatorFragment != null){\r
872             queryString = queryString + " +" + creatorFragment;\r
873           }\r
874 \r
875           // search title, description, and content for something\r
876           // the contentTerm uses param "search_boolean" to combine its terms\r
877           String contentFragment = contentTerm.makeTerm(req);\r
878           if (contentFragment != null){\r
879             logger.debug("contentFragment: " + contentFragment);\r
880             queryString = queryString + " +" + contentFragment;\r
881           }\r
882 \r
883           String topicFragment = topicTerm.makeTerm(req);\r
884           if (topicFragment != null){\r
885             queryString = queryString + " +" + topicFragment;\r
886           }\r
887 \r
888           String imagesFragment = imagesTerm.makeTerm(req);\r
889           if (imagesFragment != null){\r
890             queryString = queryString + " +" + imagesFragment;\r
891           }\r
892 \r
893           String audioFragment = audioTerm.makeTerm(req);\r
894           if (audioFragment != null){\r
895             queryString = queryString + " +" + audioFragment;\r
896           }\r
897 \r
898           String videoFragment = videoTerm.makeTerm(req);\r
899           if (videoFragment != null){\r
900             queryString = queryString + " +" + videoFragment;\r
901           }\r
902 \r
903           String mediaFragment = mediaTerm.makeTerm(req);\r
904           if (mediaFragment != null){\r
905             queryString = queryString + " +" + mediaFragment;\r
906           }\r
907 \r
908           if (queryString == null || queryString == ""){\r
909             queryString = "";\r
910           }\r
911           else{\r
912             try{\r
913               Searcher searcher = null;\r
914               try {\r
915                 searcher = new IndexSearcher(indexPath);\r
916               }\r
917               catch(IOException e) {\r
918                 logger.debug("Can't open indexPath: " + indexPath);\r
919                 throw new ServletModuleExc("Problem with Search Index! : "+ e.toString());\r
920               }\r
921 \r
922               Query query = null;\r
923               try {\r
924                 query = QueryParser.parse(queryString, "content", new StandardAnalyzer());\r
925               }\r
926               catch(Exception e) {\r
927                 searcher.close();\r
928                 logger.debug("Query don't parse: " + queryString);\r
929                 throw new ServletModuleExc("Problem with Query String! (was '"+queryString+"')");\r
930               }\r
931 \r
932               Hits hits = null;\r
933               try {\r
934                 hits = searcher.search(query);\r
935               }\r
936               catch(IOException e) {\r
937                 searcher.close();\r
938                 logger.debug("Can't get hits: " + e.toString());\r
939                 throw new ServletModuleExc("Problem getting hits!");\r
940               }\r
941 \r
942               int start = 0;\r
943               int end = hits.length();\r
944 \r
945               String sortBy=req.getParameter("search_sort");\r
946               if (sortBy == null || sortBy.equals("")){\r
947                 throw new ServletModuleExc("Please let me sort by something!(missing search_sort)");\r
948               }\r
949 \r
950               // here is where the documents will go for storage across sessions\r
951               ArrayList theDocumentsSorted = new ArrayList();\r
952 \r
953               if (sortBy.equals("score")){\r
954                 for(int i = start; i < end; i++) {\r
955                   theDocumentsSorted.add(hits.doc(i));\r
956                 }\r
957               }\r
958               else{\r
959                 // then we'll sort by date!\r
960                 Map dateToPosition = new HashMap(end,1.0F); //we know how big it will be\r
961                 for(int i = start; i < end; i++) {\r
962                   String creationDate=(hits.doc(i)).get("creationDate");\r
963                   // do a little dance in case two contents created at the same second!\r
964                   if (dateToPosition.containsKey(creationDate)){\r
965                     ((ArrayList) (dateToPosition.get(creationDate))).add(new Integer(i));\r
966                   }\r
967                   else{\r
968                     ArrayList thePositions = new ArrayList();\r
969                     thePositions.add(new Integer(i));\r
970                     dateToPosition.put(creationDate,thePositions);\r
971                   }\r
972                 }\r
973                 Set keys = dateToPosition.keySet();\r
974                 ArrayList keyList= new ArrayList(keys);\r
975                 Collections.sort(keyList);\r
976                 if (sortBy.equals("date_desc")){\r
977                   Collections.reverse(keyList);\r
978                 }\r
979                 else{\r
980                   if (!sortBy.equals("date_asc")){\r
981                     throw new ServletModuleExc("don't know how to sort by: "+ sortBy);\r
982                   }\r
983                 }\r
984                 ListIterator keyTraverser = keyList.listIterator();\r
985                 while (keyTraverser.hasNext()){\r
986                   ArrayList positions = (ArrayList)dateToPosition.get((keyTraverser.next()));\r
987                   ListIterator positionsTraverser=positions.listIterator();\r
988                   while (positionsTraverser.hasNext()){\r
989                     theDocumentsSorted.add(hits.doc(((Integer)(positionsTraverser.next())).intValue()));\r
990                   }\r
991                 }\r
992               }\r
993 \r
994               try{\r
995                 searcher.close();\r
996               }\r
997               catch (IOException e){\r
998                 logger.debug("Can't close searcher: " + e.toString());\r
999                 throw new ServletModuleFailure("Problem closing searcher(normal):" + e.getMessage(), e);\r
1000               }\r
1001 \r
1002 \r
1003               session.removeAttribute("numberOfHits");\r
1004               session.removeAttribute("theDocumentsSorted");\r
1005               session.removeAttribute("positionInResults");\r
1006 \r
1007               session.setAttribute("numberOfHits",new Integer(end));\r
1008               session.setAttribute("theDocumentsSorted",theDocumentsSorted);\r
1009               session.setAttribute("positionInResults",new Integer(0));\r
1010 \r
1011             }\r
1012             catch (IOException e){\r
1013               logger.debug("Can't close searcher: " + e.toString());\r
1014               throw new ServletModuleFailure("Problem closing searcher: " + e.getMessage(), e);\r
1015             }\r
1016           }\r
1017         }\r
1018       }\r
1019 \r
1020       try {\r
1021         ArrayList theDocs = (ArrayList)session.getAttribute("theDocumentsSorted");\r
1022         if (theDocs != null){\r
1023 \r
1024           mergeData.put("numberOfHits", ((Integer)session.getAttribute("numberOfHits")).toString());\r
1025           List theHits = new Vector();\r
1026           int pIR=((Integer)session.getAttribute("positionInResults")).intValue();\r
1027           int terminus;\r
1028           int numHits=((Integer)session.getAttribute("numberOfHits")).intValue();\r
1029 \r
1030           if (!(pIR+increment>=numHits)){\r
1031             mergeData.put("hasNext","y");\r
1032           }\r
1033           else {\r
1034             mergeData.put("hasNext", null);\r
1035           }\r
1036           if (pIR>0){\r
1037             mergeData.put("hasPrevious","y");\r
1038           }\r
1039           else {\r
1040             mergeData.put("hasPrevious", null);\r
1041           }\r
1042 \r
1043           if ((pIR+increment)>numHits){\r
1044             terminus=numHits;\r
1045           }\r
1046           else {\r
1047             terminus=pIR+increment;\r
1048           }\r
1049           for(int i = pIR; i < terminus; i++) {\r
1050             Map h = new HashMap();\r
1051             Document theHit = (Document)theDocs.get(i);\r
1052             whereTerm.returnMeta(h,theHit);\r
1053             creatorTerm.returnMeta(h,theHit);\r
1054             titleTerm.returnMeta(h,theHit);\r
1055             descriptionTerm.returnMeta(h,theHit);\r
1056             dateTerm.returnMeta(h,theHit);\r
1057             imagesTerm.returnMeta(h,theHit);\r
1058             audioTerm.returnMeta(h,theHit);\r
1059             videoTerm.returnMeta(h,theHit);\r
1060             theHits.add(h);\r
1061           }\r
1062           mergeData.put("hits",theHits);\r
1063         }\r
1064       }\r
1065       catch (Throwable e) {\r
1066         logger.error("Can't iterate over hits: " + e.toString());\r
1067 \r
1068         throw new ServletModuleFailure("Problem getting hits: " + e.getMessage(), e);\r
1069       }\r
1070 \r
1071       mergeData.put("queryString",queryString);\r
1072 \r
1073       deliver(req, res, mergeData, null, searchResultsTemplate);\r
1074     }\r
1075     catch (NullPointerException n){\r
1076       throw new ServletModuleFailure("Null Pointer: "+n.toString(), n);\r
1077     }\r
1078   }\r
1079 \r
1080   /*\r
1081    * Method for dynamically generating a pdf using iText\r
1082    */\r
1083 \r
1084 \r
1085   public void getpdf(HttpServletRequest req, HttpServletResponse res)\r
1086       throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {\r
1087     String ID_REQUEST_PARAM = "id";\r
1088     int maxArticlesInNewsleter = 15; // it is nice not to be dos'ed\r
1089     try {\r
1090         String idParam = req.getParameter(ID_REQUEST_PARAM);\r
1091         if (idParam != null) {\r
1092           ByteArrayOutputStream out = new ByteArrayOutputStream();\r
1093           PDFGenerator pdfMaker = new PDFGenerator(out);\r
1094 \r
1095           RE re = new RE("[0-9]+");\r
1096 \r
1097 \r
1098           REMatch[] idMatches=re.getAllMatches(idParam);\r
1099 \r
1100           String cacheSelector="";\r
1101 \r
1102           for (int i = 0; i < idMatches.length; i++){\r
1103             cacheSelector=   cacheSelector + "," + idMatches[i].toString();\r
1104           }\r
1105 \r
1106           \r
1107           if (idMatches.length > 1){\r
1108             pdfMaker.addLine();\r
1109             for (int i = 0; i < idMatches.length  && i < maxArticlesInNewsleter; i++){\r
1110               REMatch aMatch = idMatches[i];\r
1111               String id=aMatch.toString();\r
1112               EntityContent contentEnt = (EntityContent)contentModule.getById(id);\r
1113               pdfMaker.addIndexItem(contentEnt);\r
1114 \r
1115             }\r
1116           }\r
1117 \r
1118           for (int i = 0; i < idMatches.length; i++){\r
1119             REMatch aMatch = idMatches[i];\r
1120 \r
1121             String id=aMatch.toString();\r
1122 \r
1123             EntityContent contentEnt = (EntityContent)contentModule.getById(id);\r
1124             pdfMaker.add(contentEnt);\r
1125 \r
1126           }\r
1127 \r
1128           pdfMaker.stop();\r
1129           res.setContentType("application/pdf");\r
1130           byte[] content = out.toByteArray();\r
1131           res.setContentLength(content.length);\r
1132           res.getOutputStream().write(content);\r
1133           res.getOutputStream().flush();\r
1134 \r
1135         }\r
1136         else {\r
1137           throw new ServletModuleExc("Missing id.");\r
1138         }\r
1139     }\r
1140     catch (Throwable t) {\r
1141       logger.error(t.toString());\r
1142       throw new ServletModuleFailure(t);\r
1143     }\r
1144 \r
1145   }\r
1146 \r
1147 \r
1148   public String generateOnetimePassword() {\r
1149     Random r = new Random();\r
1150     int random = r.nextInt();\r
1151 \r
1152     long l = System.currentTimeMillis();\r
1153 \r
1154     l = (l * l * l * l) / random;\r
1155     if (l < 0)\r
1156       l = l * -1;\r
1157 \r
1158     String returnString = "" + l;\r
1159 \r
1160     return returnString.substring(5);\r
1161   }\r
1162 \r
1163   public void deliver(HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator)\r
1164       throws ServletModuleFailure {\r
1165     try {\r
1166       deliver(aResponse.getWriter(), aRequest, aResponse, aData, anExtra, aGenerator);\r
1167     }\r
1168     catch (Throwable t) {\r
1169       throw new ServletModuleFailure(t);\r
1170     }\r
1171   }\r
1172 \r
1173   public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator)\r
1174       throws ServletModuleFailure {\r
1175     try {\r
1176       Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open");\r
1177       responseData.put("data", aData);\r
1178       responseData.put("extra", anExtra);\r
1179 \r
1180 \r
1181       Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);\r
1182       generator.generate(anOutputWriter, responseData, logger.asPrintWriter(logger.INFO_MESSAGE));\r
1183 \r
1184       anOutputWriter.close();\r
1185     }\r
1186     catch (Throwable e) {\r
1187       logger.error("Error while generating " + aGenerator + ": " + e.getMessage());\r
1188 \r
1189       throw new ServletModuleFailure(e);\r
1190     }\r
1191   }\r
1192 \r
1193   public void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse,PrintWriter out, Throwable anException) {\r
1194     try {\r
1195       logger.error("error: " + anException);\r
1196       Map data = new HashMap();\r
1197 \r
1198       data.put("errorstring", anException.getMessage());\r
1199       data.put("date", StringUtil.date2readableDateTime(new GregorianCalendar()));\r
1200 \r
1201       deliver(out, aRequest, aResponse, data, null, configuration.getString("ServletModule.OpenIndy.ErrorTemplate"));\r
1202     }\r
1203     catch (Throwable e) {\r
1204       throw new ServletModuleFailure(e);\r
1205     }\r
1206   }\r
1207 \r
1208   public void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse,\r
1209                                PrintWriter out, ServletModuleUserExc anException) {\r
1210     try {\r
1211       logger.warn("user error: " + anException.getMessage());\r
1212       Map data = new HashMap();\r
1213 \r
1214       MessageResources messages = MessageResources.getMessageResources("bundles.open");\r
1215       data.put("errorstring",\r
1216               messages.getMessage(getLocale(aRequest), anException.getMessage(), anException.getParameters())\r
1217           );\r
1218       data.put("date", StringUtil.date2readableDateTime(new GregorianCalendar()));\r
1219 \r
1220       deliver(out, aRequest, aResponse, data, null, configuration.getString("ServletModule.OpenIndy.UserErrorTemplate"));\r
1221     }\r
1222     catch (Throwable e) {\r
1223       throw new ServletModuleFailure(e);\r
1224     }\r
1225   }\r
1226 \r
1227   private String createOneTimePasswd() {\r
1228     return "";\r
1229   }\r
1230 }\r