2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.servlet;
34 import java.io.ByteArrayOutputStream;
36 import java.io.FileNotFoundException;
37 import java.io.FileReader;
38 import java.io.IOException;
39 import java.io.PrintWriter;
40 import java.io.StringWriter;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.Enumeration;
44 import java.util.GregorianCalendar;
45 import java.util.HashMap;
46 import java.util.Iterator;
47 import java.util.ListIterator;
48 import java.util.Locale;
50 import java.util.Random;
53 import javax.servlet.http.HttpServletRequest;
54 import javax.servlet.http.HttpServletResponse;
55 import javax.servlet.http.HttpSession;
57 import mir.entity.Entity;
58 import mir.entity.EntityList;
59 import mir.log.LoggerWrapper;
60 import mir.misc.FileHandler;
61 import mir.misc.FileHandlerUserException;
62 import mir.misc.HTMLTemplateProcessor;
63 import mir.misc.StringUtil;
64 import mir.misc.WebdbMultipartRequest;
65 import mir.module.ModuleException;
66 import mir.servlet.ServletModule;
67 import mir.servlet.ServletModuleExc;
68 import mir.servlet.ServletModuleFailure;
69 import mir.servlet.ServletModuleUserExc;
70 import mir.storage.StorageObjectFailure;
71 import mir.util.StringRoutines;
72 import mircoders.entity.EntityComment;
73 import mircoders.entity.EntityContent;
74 import mircoders.global.MirGlobal;
75 import mircoders.media.MediaRequest;
76 import mircoders.module.ModuleComment;
77 import mircoders.module.ModuleContent;
78 import mircoders.module.ModuleImages;
79 import mircoders.module.ModuleTopics;
80 import mircoders.search.AudioSearchTerm;
81 import mircoders.search.ContentSearchTerm;
82 import mircoders.search.ImagesSearchTerm;
83 import mircoders.search.KeywordSearchTerm;
84 import mircoders.search.TextSearchTerm;
85 import mircoders.search.TopicSearchTerm;
86 import mircoders.search.UnIndexedSearchTerm;
87 import mircoders.search.VideoSearchTerm;
88 import mircoders.storage.DatabaseComment;
89 import mircoders.storage.DatabaseContent;
90 import mircoders.storage.DatabaseContentToMedia;
91 import mircoders.storage.DatabaseContentToTopics;
92 import mircoders.storage.DatabaseImages;
93 import mircoders.storage.DatabaseLanguage;
94 import mircoders.storage.DatabaseTopics;
96 import org.apache.commons.net.smtp.SMTPClient;
97 import org.apache.commons.net.smtp.SMTPReply;
98 import org.apache.fop.apps.Driver;
99 import org.apache.fop.apps.XSLTInputHandler;
100 import org.apache.log.Hierarchy;
101 import org.apache.log.Priority;
102 import org.apache.lucene.analysis.standard.StandardAnalyzer;
103 import org.apache.lucene.document.Document;
104 import org.apache.lucene.queryParser.QueryParser;
105 import org.apache.lucene.search.Hits;
106 import org.apache.lucene.search.IndexSearcher;
107 import org.apache.lucene.search.Query;
108 import org.apache.lucene.search.Searcher;
110 import freemarker.template.SimpleHash;
111 import freemarker.template.SimpleList;
112 import freemarker.template.SimpleScalar;
113 import freemarker.template.TemplateModelRoot;
116 * ServletModuleOpenIndy -
117 * is the open-access-servlet, which is responsible for
118 * adding comments to articles &
119 * open-postings to the newswire
121 * @author mir-coders group
122 * @version $Id: ServletModuleOpenIndy.java,v 1.64 2003/03/08 17:18:19 idfx Exp $
126 public class ServletModuleOpenIndy extends ServletModule
129 private String commentFormTemplate, commentFormDoneTemplate,
130 commentFormDupeTemplate;
131 private String postingFormTemplate, postingFormDoneTemplate,
132 postingFormDupeTemplate;
133 private String searchResultsTemplate;
134 private String prepareMailTemplate,sentMailTemplate;
135 private ModuleContent contentModule;
136 private ModuleComment commentModule;
137 private ModuleImages imageModule;
138 private ModuleTopics themenModule;
139 private String directOp ="yes";
140 private String passwdProtection ="yes";
141 // Singelton / Kontruktor
142 private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
143 public static ServletModule getInstance() { return instance; }
145 private ServletModuleOpenIndy() {
148 logger = new LoggerWrapper("ServletModule.OpenIndy");
150 commentFormTemplate = configuration.getString("ServletModule.OpenIndy.CommentTemplate");
151 commentFormDoneTemplate = configuration.getString("ServletModule.OpenIndy.CommentDoneTemplate");
152 commentFormDupeTemplate = configuration.getString("ServletModule.OpenIndy.CommentDupeTemplate");
153 postingFormTemplate = configuration.getString("ServletModule.OpenIndy.PostingTemplate");
154 postingFormDoneTemplate = configuration.getString("ServletModule.OpenIndy.PostingDoneTemplate");
155 postingFormDupeTemplate = configuration.getString("ServletModule.OpenIndy.PostingDupeTemplate");
156 searchResultsTemplate = configuration.getString("ServletModule.OpenIndy.SearchResultsTemplate");
157 prepareMailTemplate = configuration.getString("ServletModule.OpenIndy.PrepareMailTemplate");
158 sentMailTemplate = configuration.getString("ServletModule.OpenIndy.SentMailTemplate");
159 directOp = configuration.getString("DirectOpenposting").toLowerCase();
160 passwdProtection = configuration.getString("PasswdProtection").toLowerCase();
161 mainModule = new ModuleComment(DatabaseComment.getInstance());
162 contentModule = new ModuleContent(DatabaseContent.getInstance());
163 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
164 imageModule = new ModuleImages(DatabaseImages.getInstance());
165 defaultAction="addposting";
168 catch (StorageObjectFailure e) {
169 logger.error("servletmoduleopenindy could not be initialized: " + e.getMessage());
175 * Method for making a comment
178 public void addcomment(HttpServletRequest req, HttpServletResponse res)
179 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure
181 String aid = req.getParameter("aid"); // the article id the comment will belong to
182 String language = req.getParameter("language");
184 if (aid!=null && !aid.equals("")) {
186 SimpleHash mergeData = new SimpleHash();
189 if (passwdProtection.equals("yes")) {
190 String passwd = this.createOneTimePasswd();
191 HttpSession session = req.getSession(false);
192 session.setAttribute("passwd", passwd);
193 mergeData.put("passwd", passwd);
196 if (language != null) {
197 HttpSession session = req.getSession(false);
198 session.setAttribute("Locale", new Locale(language, ""));
199 session.setAttribute("passwd", language);
202 mergeData.put("aid", aid);
204 SimpleHash extraInfo = new SimpleHash();
205 extraInfo.put("languagePopUpData", DatabaseLanguage.getInstance().getPopupData());
207 deliver(req, res, mergeData, extraInfo, commentFormTemplate);
209 catch (Throwable t) {
210 throw new ServletModuleFailure("ServletModuleOpenIndy.addcomment: " + t.getMessage(), t);
213 else throw new ServletModuleExc("aid not set!");
217 * Method for inserting a comment into the Database and delivering
218 * the commentDone Page
221 public void inscomment(HttpServletRequest req, HttpServletResponse res)
222 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure
224 String aid = req.getParameter("to_media"); // the article id the comment will belong to
225 if (aid!=null && !aid.equals(""))
227 // ok, collecting data from form
229 Map withValues = getIntersectingValues(req, DatabaseComment.getInstance());
231 //no html in comments(for now)
232 for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
233 String k=(String)i.next();
234 String v=(String)withValues.get(k);
236 withValues.put(k,StringUtil.removeHTMLTags(v));
238 withValues.put("is_published","1");
239 withValues.put("to_comment_status","1");
241 //checking the onetimepasswd
242 if(passwdProtection.equals("yes")){
243 HttpSession session = req.getSession(false);
244 String sessionPasswd = (String)session.getAttribute("passwd");
245 if ( sessionPasswd == null){
246 throw new ServletModuleUserExc("Lost password");
248 String passwd = req.getParameter("passwd");
249 if ( passwd == null || (!sessionPasswd.equals(passwd))) {
250 throw new ServletModuleUserExc("Missing password");
252 session.invalidate();
255 // inserting into database
256 String id = mainModule.add(withValues);
257 logger.debug("id: "+id);
258 //insert was not successfull
260 deliver(req, res, new SimpleHash(), commentFormDupeTemplate);
262 DatabaseContent.getInstance().setUnproduced("id="+aid);
265 EntityComment comment = (EntityComment) DatabaseComment.getInstance().selectById(id);
266 MirGlobal.localizer().openPostings().afterCommentPosting(comment);
268 catch (Throwable t) {
269 throw new ServletModuleExc(t.getMessage());
273 // redirecting to url
274 // should implement back to article
275 SimpleHash mergeData = new SimpleHash();
276 deliver(req, res, mergeData, commentFormDoneTemplate);
278 catch (Throwable e) {
279 throw new ServletModuleFailure(e);
282 else throw new ServletModuleExc("aid not set!");
287 * Method for delivering the form-Page for open posting
290 public void addposting(HttpServletRequest req, HttpServletResponse res)
291 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure
293 SimpleHash mergeData = new SimpleHash();
296 if(passwdProtection.equals("yes")){
297 String passwd = this.createOneTimePasswd();
298 HttpSession session = req.getSession(false);
299 session.setAttribute("passwd",passwd);
300 mergeData.put("passwd", passwd);
303 String maxMedia = configuration.getString("ServletModule.OpenIndy.MaxMediaUploadItems");
304 String defaultMedia = configuration.getString("ServletModule.OpenIndy.DefaultMediaUploadItems");
305 String numOfMedia = req.getParameter("medianum");
307 if(numOfMedia==null||numOfMedia.equals("")){
308 numOfMedia=defaultMedia;
310 else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
311 numOfMedia = maxMedia;
314 int mediaNum = Integer.parseInt(numOfMedia);
315 SimpleList mediaFields = new SimpleList();
316 for(int i =0; i<mediaNum;i++){
317 Integer mNum = new Integer(i+1);
318 mediaFields.add(mNum.toString());
320 mergeData.put("medianum",numOfMedia);
321 mergeData.put("mediafields",mediaFields);
324 SimpleHash extraInfo = new SimpleHash();
326 extraInfo.put("languagePopUpData", DatabaseLanguage.getInstance().getPopupData() );
327 extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
329 extraInfo.put("topics", themenModule.getTopicsList());
332 catch (Exception e) {
333 logger.error("languagePopUpData or getTopicslist failed "+e.toString());
335 throw new ServletModuleFailure("OpenIndy -- failed getting language or topics: "+e.toString(), e);
340 deliver(req, res, mergeData, extraInfo, postingFormTemplate);
344 * Method for inserting an open posting into the Database and delivering
345 * the postingDone Page
348 public void insposting(HttpServletRequest req, HttpServletResponse res)
349 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure
351 SimpleHash mergeData = new SimpleHash();
352 boolean setMedia=false;
353 boolean setTopic = false;
357 WebdbMultipartRequest mp = null;
358 EntityList mediaList = null;
360 // new MediaRequest, "1" is the id for the openPosting user
361 MediaRequest mediaReq = new MediaRequest("1", true);
362 mp = new WebdbMultipartRequest(req, (FileHandler)mediaReq);
363 mediaList = mediaReq.getEntityList();
365 catch (FileHandlerUserException e) {
366 throw new ServletModuleFailure(e);
369 Map withValues = mp.getParameters();
371 //checking the onetimepasswd
372 if(passwdProtection.equals("yes")){
373 HttpSession session = req.getSession(false);
374 String sessionPasswd = (String)session.getAttribute("passwd");
375 if ( sessionPasswd == null){
376 throw new ServletModuleUserExc("Lost password");
378 String passwd = (String)withValues.get("passwd");
379 if ( passwd == null || (!sessionPasswd.equals(passwd))) {
380 throw new ServletModuleUserExc("Missing password");
382 session.invalidate();
385 if ((((String)withValues.get("title")).length() == 0) ||
386 (((String)withValues.get("description")).length() == 0) ||
387 (((String)withValues.get("content_data")).length() == 0))
388 throw new ServletModuleUserExc("Missing field");
390 // call the routines that escape html
392 for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
393 String k=(String)i.next();
394 String v=(String)withValues.get(k);
396 if (k.equals("content_data")){
397 //this doesn't quite work yet, so for now, all html goes
398 //withValues.put(k,StringUtil.approveHTMLTags(v));
399 withValues.put(k,StringUtil.deleteForbiddenTags(v));
400 } else if (k.equals("description")) {
401 String tmp = StringUtil.deleteForbiddenTags(v);
402 withValues.put(k,StringUtil.deleteHTMLTableTags(tmp));
404 withValues.put(k,StringUtil.removeHTMLTags(v));
409 withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
410 withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
411 withValues.put("is_produced", "0");
412 // by default stuff is published, they can be un-published through the
414 withValues.put("is_published","1");
415 // if op direct article-type == newswire
416 if (directOp.equals("yes")) withValues.put("to_article_type","1");
418 withValues.put("to_publisher","1");
420 // owner is openposting user
421 // ML: this is not multi-language friendly and this can be done in a template
422 // if (withValues.get("creator").toString().equals(""))
423 // withValues.put("creator","Anonym");
425 // inserting content into database
426 String cid = contentModule.add(withValues);
427 logger.debug("id: "+cid);
428 //insert was not successfull
430 //How do we know that it was not succesful cause of a
431 //dupe, what if it failed cause of "No space left on device"?
432 //Or is there something I am missing? Wouldn't it be better
433 //to have an explicit dupe check and then insert? I have no
434 //idea what I am talking about. this comment is in case
435 //I forget to explicitely ask. -mh
436 deliver(req, res, mergeData, postingFormDupeTemplate);
439 String[] to_topicsArr = mp.getParameterValues("to_topic");
441 if (to_topicsArr != null && to_topicsArr.length > 0) {
443 DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
446 catch (Exception e) {
447 logger.error("setting content_x_topic failed");
448 contentModule.deleteById(cid);
449 throw new ServletModuleExc("smod - openindy :: insposting: setting content_x_topic failed: "+e.toString());
453 //if we're here all is ok... associate the media to the article
454 for(int i=0;i<mediaList.size();i++) {
455 Entity mediaEnt = (Entity)mediaList.elementAt(i);
456 DatabaseContentToMedia.getInstance().addMedia(cid,mediaEnt.getId());
460 MirGlobal.localizer().openPostings().afterContentPosting(
461 (EntityContent)contentModule.getById(cid));
463 catch (Throwable t) {
464 throw new ServletModuleFailure(t);
467 catch (Throwable e) {
468 throw new ServletModuleFailure(e);
471 deliver(req, res, mergeData, postingFormDoneTemplate);
475 * Method for preparing and sending a content as an email message
478 public void mail(HttpServletRequest req, HttpServletResponse res)
479 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure
481 String aid = req.getParameter("mail_aid");
483 throw new ServletModuleUserExc("An article id must be specified in requests to email an article. Something therefore went badly wrong....");
486 String to = req.getParameter("mail_to");
487 String from = req.getParameter("mail_from");
488 String from_name = req.getParameter("mail_from_name");
489 String comment = req.getParameter("mail_comment");
490 String mail_language = req.getParameter("mail_language");
492 SimpleHash mergeData = new SimpleHash();
494 if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){
496 for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) {
497 String pName=(String)theParams.nextElement();
498 if (pName.startsWith("mail_")){
499 mergeData.put(pName,new SimpleScalar(req.getParameter(pName)));
502 deliver(req,res,mergeData,prepareMailTemplate);
505 //run checks on to and from and mail_language to make sure no monkey business occurring
506 if (mail_language.indexOf('.') != -1 || mail_language.indexOf('/') != -1 ){
507 throw new ServletModuleUserExc("Sorry, you've entered an illegal character into the language field. Go back and try again, asshole.");
509 if (to.indexOf('\n') != -1
510 || to.indexOf('\r') != -1
511 || to.indexOf(',') != -1
512 || from.indexOf('\n') != -1
513 || from.indexOf('\r') != -1
514 || from.indexOf(',') != -1 ){
515 throw new ServletModuleUserExc("Sorry, you've entered an illegal character into the from or to field. Go back and try again.");
517 EntityContent contentEnt;
519 contentEnt = (EntityContent)contentModule.getById(aid);
522 throw new ServletModuleFailure("Couldn't get content for article "+aid + ": " + e.getMessage(), e);
524 String producerStorageRoot=configuration.getString("Producer.StorageRoot");
525 String producerDocRoot=configuration.getString("Producer.DocRoot");
526 String publishPath = contentEnt.getValue("publish_path");
527 String txtFilePath = producerStorageRoot + producerDocRoot + "/" + mail_language +
528 publishPath + "/" + aid + ".txt";
531 File inputFile = new File(txtFilePath);
535 FileReader in = new FileReader(inputFile);
536 StringWriter out = new StringWriter();
538 while ((c = in.read()) != -1)
541 content= out.toString();
543 catch (FileNotFoundException e){
544 throw new ServletModuleFailure("No text file found in " + txtFilePath, e);
546 catch (IOException e){
547 throw new ServletModuleFailure("Problem reading file in " + txtFilePath, e);
550 content = "To: " + to + "\nReply-To: "+ from + "\n" + content;
551 // put in the comment where it should go
552 if (comment != null) {
553 String commentTextToInsert = "\n\nAttached comment from " + from_name + ":\n" + comment;
555 content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!",commentTextToInsert);
558 throw new ServletModuleFailure("Problem doing regular expression replacement " + e.toString(), e);
563 content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!","");
566 throw new ServletModuleFailure("Problem doing regular expression replacement " + e.toString(), e);
570 SMTPClient client=new SMTPClient();
573 client.connect(configuration.getString("ServletModule.OpenIndy.SMTPServer"));
575 reply = client.getReplyCode();
577 if (!SMTPReply.isPositiveCompletion(reply)) {
579 throw new ServletModuleExc("SMTP server refused connection.");
582 client.sendSimpleMessage(configuration.getString("ServletModule.OpenIndy.EmailIsFrom"), to, content);
585 //mission accomplished
586 deliver(req, res, mergeData, sentMailTemplate);
588 catch(IOException e) {
589 if(client.isConnected()) {
592 } catch(IOException f) {
596 throw new ServletModuleFailure(e);
603 * Method for querying a lucene index
605 public void search(HttpServletRequest req, HttpServletResponse res)
606 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
610 HttpSession session = req.getSession(false);
612 String queryString="";
614 SimpleHash mergeData = new SimpleHash();
616 KeywordSearchTerm dateTerm = new KeywordSearchTerm("date_formatted","search_date","webdb_create_formatted","webdb_create_formatted","webdb_create_formatted");
617 UnIndexedSearchTerm whereTerm = new UnIndexedSearchTerm("","","","where","where");
618 TextSearchTerm creatorTerm = new TextSearchTerm("creator","search_creator","creator","creator","creator");
619 TextSearchTerm titleTerm = new TextSearchTerm("title","search_content","title","title","title");
620 TextSearchTerm descriptionTerm = new TextSearchTerm("description","search_content","description","description","description");
621 ContentSearchTerm contentTerm = new ContentSearchTerm("content_data","search_content","content","","");
622 TopicSearchTerm topicTerm = new TopicSearchTerm();
623 ImagesSearchTerm imagesTerm = new ImagesSearchTerm();
624 AudioSearchTerm audioTerm = new AudioSearchTerm();
625 VideoSearchTerm videoTerm = new VideoSearchTerm();
627 //make the query available to subsequent iterations
629 for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) {
630 String pName=(String)theParams.nextElement();
631 if (pName.startsWith("search_")){
632 mergeData.put(pName,new SimpleScalar(req.getParameter(pName)));
637 mergeData.put("topics", themenModule.getTopicsAsSimpleList());
639 catch(ModuleException e) {
640 logger.debug("Can't get topics: " + e.toString());
643 String searchBackValue = req.getParameter("search_back");
644 String searchForwardValue = req.getParameter("search_forward");
646 if (searchBackValue != null){
647 int totalHits = ((Integer) session.getAttribute("numberOfHits")).intValue();
648 int newPosition=((Integer)session.getAttribute("positionInResults")).intValue()-increment;
651 if (newPosition >= totalHits)
652 newPosition=totalHits-1;
653 session.setAttribute("positionInResults",new Integer(newPosition));
656 if (searchForwardValue != null){
657 int totalHits = ((Integer) session.getAttribute("numberOfHits")).intValue();
658 int newPosition=((Integer)session.getAttribute("positionInResults")).intValue()+increment;
661 if (newPosition >= totalHits)
662 newPosition=totalHits-1;
664 session.setAttribute("positionInResults",new Integer(newPosition));
667 String indexPath=configuration.getString("IndexPath");
670 String creatorFragment = creatorTerm.makeTerm(req);
671 if (creatorFragment != null){
672 queryString = queryString + " +" + creatorFragment;
675 // search title, description, and content for something
676 // the contentTerm uses param "search_boolean" to combine its terms
677 String contentFragment = contentTerm.makeTerm(req);
678 if (contentFragment != null){
679 logger.debug("contentFragment: " + contentFragment);
680 queryString = queryString + " +" + contentFragment;
683 String topicFragment = topicTerm.makeTerm(req);
684 if (topicFragment != null){
685 queryString = queryString + " +" + topicFragment;
688 String imagesFragment = imagesTerm.makeTerm(req);
689 if (imagesFragment != null){
690 queryString = queryString + " +" + imagesFragment;
693 String audioFragment = audioTerm.makeTerm(req);
694 if (audioFragment != null){
695 queryString = queryString + " +" + audioFragment;
698 String videoFragment = videoTerm.makeTerm(req);
699 if (videoFragment != null){
700 queryString = queryString + " +" + videoFragment;
703 if (queryString == null || queryString == ""){
708 Searcher searcher = null;
710 searcher = new IndexSearcher(indexPath);
712 catch(IOException e) {
713 logger.debug("Can't open indexPath: " + indexPath);
714 throw new ServletModuleExc("Problem with Search Index! : "+ e.toString());
719 query = QueryParser.parse(queryString, "content", new StandardAnalyzer());
723 logger.debug("Query don't parse: " + queryString);
724 throw new ServletModuleExc("Problem with Query String! (was '"+queryString+"')");
729 hits = searcher.search(query);
731 catch(IOException e) {
733 logger.debug("Can't get hits: " + e.toString());
734 throw new ServletModuleExc("Problem getting hits!");
738 int end = hits.length();
740 String sortBy=req.getParameter("search_sort");
741 if (sortBy == null || sortBy.equals("")){
742 throw new ServletModuleExc("Please let me sort by something!(missing search_sort)");
745 // here is where the documents will go for storage across sessions
746 ArrayList theDocumentsSorted = new ArrayList();
748 if (sortBy.equals("score")){
749 for(int i = start; i < end; i++) {
750 theDocumentsSorted.add(hits.doc(i));
754 // then we'll sort by date!
755 Map dateToPosition = new HashMap(end,1.0F); //we know how big it will be
756 for(int i = start; i < end; i++) {
757 String creationDate=(hits.doc(i)).get("creationDate");
758 // do a little dance in case two contents created at the same second!
759 if (dateToPosition.containsKey(creationDate)){
760 ((ArrayList) (dateToPosition.get(creationDate))).add(new Integer(i));
763 ArrayList thePositions = new ArrayList();
764 thePositions.add(new Integer(i));
765 dateToPosition.put(creationDate,thePositions);
768 Set keys = dateToPosition.keySet();
769 ArrayList keyList= new ArrayList(keys);
770 Collections.sort(keyList);
771 if (sortBy.equals("date_desc")){
772 Collections.reverse(keyList);
775 if (!sortBy.equals("date_asc")){
776 throw new ServletModuleExc("don't know how to sort by: "+ sortBy);
779 ListIterator keyTraverser = keyList.listIterator();
780 while (keyTraverser.hasNext()){
781 ArrayList positions = (ArrayList)dateToPosition.get((keyTraverser.next()));
782 ListIterator positionsTraverser=positions.listIterator();
783 while (positionsTraverser.hasNext()){
784 theDocumentsSorted.add(hits.doc(((Integer)(positionsTraverser.next())).intValue()));
792 catch (IOException e){
793 logger.debug("Can't close searcher: " + e.toString());
794 throw new ServletModuleFailure("Problem closing searcher(normal):" + e.getMessage(), e);
798 session.removeAttribute("numberOfHits");
799 session.removeAttribute("theDocumentsSorted");
800 session.removeAttribute("positionInResults");
802 session.setAttribute("numberOfHits",new Integer(end));
803 session.setAttribute("theDocumentsSorted",theDocumentsSorted);
804 session.setAttribute("positionInResults",new Integer(0));
807 catch (IOException e){
808 logger.debug("Can't close searcher: " + e.toString());
809 throw new ServletModuleFailure("Problem closing searcher: " + e.getMessage(), e);
816 ArrayList theDocs = (ArrayList)session.getAttribute("theDocumentsSorted");
817 if (theDocs != null){
819 mergeData.put("numberOfHits", ((Integer)session.getAttribute("numberOfHits")).toString());
820 SimpleList theHits = new SimpleList();
821 int pIR=((Integer)session.getAttribute("positionInResults")).intValue();
823 int numHits=((Integer)session.getAttribute("numberOfHits")).intValue();
825 if (!(pIR+increment>=numHits)){
826 mergeData.put("hasNext","y");
829 mergeData.put("hasPrevious","y");
832 if ((pIR+increment)>numHits){
836 terminus=pIR+increment;
838 for(int i = pIR; i < terminus; i++) {
839 SimpleHash h = new SimpleHash();
840 Document theHit = (Document)theDocs.get(i);
841 whereTerm.returnMeta(h,theHit);
842 creatorTerm.returnMeta(h,theHit);
843 titleTerm.returnMeta(h,theHit);
844 descriptionTerm.returnMeta(h,theHit);
845 dateTerm.returnMeta(h,theHit);
846 imagesTerm.returnMeta(h,theHit);
847 audioTerm.returnMeta(h,theHit);
848 videoTerm.returnMeta(h,theHit);
851 mergeData.put("hits",theHits);
854 catch (Throwable e) {
855 logger.error("Can't iterate over hits: " + e.toString());
857 throw new ServletModuleFailure("Problem getting hits: " + e.getMessage(), e);
860 mergeData.put("queryString",queryString);
861 deliver(req,res,mergeData,searchResultsTemplate);
863 catch (NullPointerException n){
864 throw new ServletModuleFailure("Null Pointer: "+n.toString(), n);
869 * Method for dynamically generating a pdf from a fo file
871 public void getpdf(HttpServletRequest req, HttpServletResponse res)
872 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
873 String ID_REQUEST_PARAM = "id";
874 String language = req.getParameter("language");
875 String generateFO=configuration.getString("GenerateFO");
876 String generatePDF=configuration.getString("GeneratePDF");
879 //don't do anything if we are not making FO files, or if we are
880 //pregenerating PDF's
881 if (generateFO.equals("yes") && generatePDF.equals("no")){
882 //fop complains unless you do the logging this way
883 org.apache.log.Logger log = null;
884 Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
885 log = hierarchy.getLoggerFor("fop");
886 log.setPriority(Priority.WARN);
888 String producerStorageRoot=configuration.getString("Producer.StorageRoot");
889 String producerDocRoot=configuration.getString("Producer.DocRoot");
890 // String templateDir=MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
891 String xslSheet=configuration.getString("Producer.HTML2FOStyleSheet");
893 String idParam = req.getParameter(ID_REQUEST_PARAM);
894 if (idParam != null) {
895 EntityContent contentEnt =
896 (EntityContent)contentModule.getById(idParam);
897 String publishPath = StringUtil.webdbDate2path(contentEnt.getValue("date"));
900 if (language == null){
901 foFile = producerStorageRoot + producerDocRoot + "/"
902 + publishPath + idParam + ".fo";
905 foFile = producerStorageRoot + producerDocRoot + "/"
906 + language + publishPath + idParam + ".fo";
908 logger.debug("USING FILES" + foFile + " and " + xslSheet);
909 XSLTInputHandler input = new XSLTInputHandler(new File(foFile),
912 ByteArrayOutputStream out = new ByteArrayOutputStream();
913 res.setContentType("application/pdf");
915 Driver driver = new Driver();
916 driver.setLogger(log);
917 driver.setRenderer(Driver.RENDER_PDF);
918 driver.setOutputStream(out);
919 driver.render(input.getParser(), input.getInputSource());
921 byte[] content = out.toByteArray();
922 res.setContentLength(content.length);
923 res.getOutputStream().write(content);
924 res.getOutputStream().flush();
927 throw new ServletModuleUserExc("Missing id parameter.");
930 catch (Exception ex) {
931 logger.error(ex.toString());
932 throw new ServletModuleFailure(ex);
935 throw new ServletModuleExc("Can't generate a PDF because the config tells me not to.");
939 private void _throwBadContentType (String fileName, String contentType)
940 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
942 logger.error("Wrong file type uploaded!: " + fileName+" "
944 throw new ServletModuleUserExc("The file you uploaded is of the "
945 +"following mime-type: "+contentType
946 +", we do not support this mime-type. "
947 +"Error One or more files of unrecognized type. Sorry");
950 protected String createOneTimePasswd(){
951 Random r = new Random();
952 int random = r.nextInt();
953 long l = System.currentTimeMillis();
954 l = (l*l*l*l)/random;
956 String returnString = ""+l;
957 return returnString.substring(5);
961 /* this is an overwritten method of ServletModule in order
962 to use different bundles for open and admin */
963 public void deliver(HttpServletRequest req, HttpServletResponse res,
964 TemplateModelRoot rtm, TemplateModelRoot popups,
965 String templateFilename) throws ServletModuleFailure
967 if (rtm == null) rtm = new SimpleHash();
969 PrintWriter out = res.getWriter();
970 HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out,
971 getLocale(req), "bundles.open");
974 catch (Throwable e) {
975 throw new ServletModuleFailure(e);