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 any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
31 package mircoders.servlet;
33 import java.io.ByteArrayOutputStream;
34 import java.io.IOException;
35 import java.io.PrintWriter;
36 import java.io.StringWriter;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.GregorianCalendar;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.ListIterator;
45 import java.util.Locale;
47 import java.util.Random;
49 import java.util.Vector;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52 import javax.servlet.http.HttpSession;
53 import javax.servlet.http.HttpUtils;
55 import org.apache.commons.fileupload.FileItem;
56 import org.apache.commons.net.smtp.SMTPClient;
57 import org.apache.commons.net.smtp.SMTPReply;
58 import org.apache.lucene.analysis.standard.StandardAnalyzer;
59 import org.apache.lucene.document.Document;
60 import org.apache.lucene.queryParser.QueryParser;
61 import org.apache.lucene.search.Hits;
62 import org.apache.lucene.search.IndexSearcher;
63 import org.apache.lucene.search.Query;
64 import org.apache.lucene.search.Searcher;
65 import org.apache.struts.util.MessageResources;
67 import gnu.regexp.REMatch;
69 import mir.entity.Entity;
70 import mir.generator.Generator;
71 import mir.log.LoggerWrapper;
72 import mir.misc.StringUtil;
73 import mir.servlet.ServletModule;
74 import mir.servlet.ServletModuleExc;
75 import mir.servlet.ServletModuleFailure;
76 import mir.servlet.ServletModuleUserExc;
77 import mir.session.HTTPAdapters;
78 import mir.session.Request;
79 import mir.session.Session;
80 import mir.session.SessionHandler;
81 import mir.session.SimpleResponse;
82 import mir.session.UploadedFile;
83 import mir.storage.StorageObjectFailure;
84 import mir.util.ExceptionFunctions;
85 import mir.util.HTTPParsedRequest;
86 import mir.util.HTTPRequestParser;
87 import mir.util.StringRoutines;
88 import mircoders.entity.EntityComment;
89 import mircoders.entity.EntityContent;
90 import mircoders.global.CacheKey;
91 import mircoders.global.MirGlobal;
92 import mircoders.media.MediaUploadProcessor;
93 import mircoders.module.ModuleComment;
94 import mircoders.module.ModuleContent;
95 import mircoders.module.ModuleImages;
96 import mircoders.module.ModuleMediaType;
97 import mircoders.module.ModuleTopics;
98 import mircoders.pdf.PDFGenerator;
99 import mircoders.search.AudioSearchTerm;
100 import mircoders.search.ContentSearchTerm;
101 import mircoders.search.ImagesSearchTerm;
102 import mircoders.search.KeywordSearchTerm;
103 import mircoders.search.TextSearchTerm;
104 import mircoders.search.TopicSearchTerm;
105 import mircoders.search.UnIndexedSearchTerm;
106 import mircoders.search.VideoSearchTerm;
107 import mircoders.storage.DatabaseComment;
108 import mircoders.storage.DatabaseContent;
109 import mircoders.storage.DatabaseContentToMedia;
110 import mircoders.storage.DatabaseContentToTopics;
111 import mircoders.storage.DatabaseImages;
112 import mircoders.storage.DatabaseLanguage;
113 import mircoders.storage.DatabaseTopics;
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.89.2.1 2003/05/17 16:21:14 john Exp $
126 public class ServletModuleOpenIndy extends ServletModule
129 private String commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;
130 private String postingFormTemplate, postingFormDoneTemplate, postingFormDupeTemplate;
131 private String searchResultsTemplate;
132 private String prepareMailTemplate,sentMailTemplate,emailAnArticleTemplate;
133 private ModuleContent contentModule;
134 private ModuleComment commentModule;
135 private ModuleImages imageModule;
136 private ModuleTopics topicsModule;
137 private String directOp ="yes";
138 // Singelton / Kontruktor
139 private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
140 public static ServletModule getInstance() { return instance; }
142 private ServletModuleOpenIndy() {
145 logger = new LoggerWrapper("ServletModule.OpenIndy");
147 commentFormTemplate = configuration.getString("ServletModule.OpenIndy.CommentTemplate");
148 commentFormDoneTemplate = configuration.getString("ServletModule.OpenIndy.CommentDoneTemplate");
149 commentFormDupeTemplate = configuration.getString("ServletModule.OpenIndy.CommentDupeTemplate");
151 postingFormTemplate = configuration.getString("ServletModule.OpenIndy.PostingTemplate");
152 postingFormDoneTemplate = configuration.getString("ServletModule.OpenIndy.PostingDoneTemplate");
153 postingFormDupeTemplate = configuration.getString("ServletModule.OpenIndy.PostingDupeTemplate");
155 searchResultsTemplate = configuration.getString("ServletModule.OpenIndy.SearchResultsTemplate");
156 prepareMailTemplate = configuration.getString("ServletModule.OpenIndy.PrepareMailTemplate");
157 emailAnArticleTemplate = configuration.getString("ServletModule.OpenIndy.MailableArticleTemplate");
158 sentMailTemplate = configuration.getString("ServletModule.OpenIndy.SentMailTemplate");
159 directOp = configuration.getString("DirectOpenposting").toLowerCase();
160 commentModule = new ModuleComment(DatabaseComment.getInstance());
161 mainModule = commentModule;
162 contentModule = new ModuleContent(DatabaseContent.getInstance());
163 topicsModule = new ModuleTopics(DatabaseTopics.getInstance());
164 imageModule = new ModuleImages(DatabaseImages.getInstance());
165 defaultAction="addposting";
167 catch (StorageObjectFailure e) {
168 logger.error("servletmoduleopenindy could not be initialized: " + e.getMessage());
173 * Method to return an "apology" when open postings are disabled
177 * @throws ServletModuleExc
178 * @throws ServletModuleUserExc
179 * @throws ServletModuleFailure
181 public void openPostingDisabled(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
182 deliver(aRequest, aResponse, (Map) null, null,
183 configuration.getString("ServletModule.OpenIndy.PostingDisabledTemplate"));
187 * Method for making a comment
190 public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
191 if (MirGlobal.abuse().getOpenPostingDisabled()) {
192 openPostingDisabled(req, res);
197 String aid = req.getParameter("aid"); // the article id the comment will belong to
199 if (aid != null && !aid.equals("")) {
201 Map mergeData = new HashMap();
204 if (MirGlobal.abuse().getOpenPostingPassword()) {
205 String passwd = this.createOneTimePasswd();
206 HttpSession session = req.getSession(false);
207 session.setAttribute("passwd", passwd);
208 mergeData.put("passwd", passwd);
211 mergeData.put("passwd", (String)null);
213 mergeData.put("aid", aid);
215 Map extraInfo = new HashMap();
216 extraInfo.put("languagePopUpData", DatabaseLanguage.getInstance().getPopupData());
218 deliver(req, res, mergeData, extraInfo, commentFormTemplate);
220 catch (Throwable t) {
221 throw new ServletModuleFailure("ServletModuleOpenIndy.addcomment: " + t.getMessage(), t);
225 throw new ServletModuleExc("aid not set!");
229 * Method for inserting a comment into the Database and delivering
230 * the commentDone Page
233 public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
234 if (MirGlobal.abuse().getOpenPostingDisabled()) {
235 openPostingDisabled(req, res);
240 String aid = req.getParameter("to_media"); // the article id the comment will belong to
241 if (aid != null && !aid.equals("")) {
242 // ok, collecting data from form
244 Map withValues = getIntersectingValues(req, DatabaseComment.getInstance());
246 //no html in comments(for now)
247 for (Iterator i = withValues.keySet().iterator(); i.hasNext(); ) {
248 String k = (String) i.next();
249 String v = (String) withValues.get(k);
251 withValues.put(k, StringUtil.removeHTMLTags(v));
253 withValues.put("is_published", "1");
254 withValues.put("to_comment_status", "1");
255 withValues.put("is_html", "0");
257 //checking the onetimepasswd
258 HttpSession session = req.getSession(false);
259 String sessionPasswd = (String) session.getAttribute("passwd");
260 if (sessionPasswd != null) {
261 String passwd = req.getParameter("passwd");
262 if (passwd == null || passwd.length() == 0) {
263 throw new ServletModuleUserExc("comment.error.missingpassword", new String[] {});
265 if (!sessionPasswd.equals(passwd)) {
266 throw new ServletModuleUserExc("comment.error.invalidpassword", new String[] {});
268 session.invalidate();
271 String id = mainModule.add(withValues);
273 SimpleResponse response = new SimpleResponse();
274 response.setResponseGenerator(commentFormDoneTemplate);
277 deliver(req, res, (Map)null, null, commentFormDupeTemplate);
280 DatabaseContent.getInstance().setUnproduced("id=" + aid);
283 EntityComment comment = (EntityComment) DatabaseComment.getInstance().selectById(id);
284 MirGlobal.localizer().openPostings().afterCommentPosting(comment);
285 MirGlobal.abuse().checkComment(
286 comment, new HTTPAdapters.HTTPRequestAdapter(req), res);
288 catch (Throwable t) {
289 throw new ServletModuleExc(t.getMessage());
293 // redirecting to url
294 // should implement back to article
295 deliver(req, res, response.getResponseValues(), null, response.getResponseGenerator());
297 catch (Throwable e) {
298 throw new ServletModuleFailure(e);
302 throw new ServletModuleExc("aid not set!");
307 * Method for delivering the form-Page for open posting
310 public void addposting(HttpServletRequest req, HttpServletResponse res)
311 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure
314 if (MirGlobal.abuse().getOpenPostingDisabled()) {
315 openPostingDisabled(req, res);
320 Map mergeData = new HashMap();
323 if (MirGlobal.abuse().getOpenPostingPassword()) {
324 String passwd = generateOnetimePassword();
325 HttpSession session = req.getSession(false);
326 session.setAttribute("passwd", passwd);
327 mergeData.put("passwd", passwd);
330 mergeData.put("passwd", (String)null);
333 String maxMedia = configuration.getString("ServletModule.OpenIndy.MaxMediaUploadItems");
334 String defaultMedia = configuration.getString("ServletModule.OpenIndy.DefaultMediaUploadItems");
335 String numOfMedia = req.getParameter("medianum");
337 if (numOfMedia == null || numOfMedia.equals("")) {
338 numOfMedia = defaultMedia;
340 else if (Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
341 numOfMedia = maxMedia;
344 int mediaNum = Integer.parseInt(numOfMedia);
345 List mediaFields = new Vector();
346 for (int i = 0; i < mediaNum; i++) {
347 Integer mNum = new Integer(i + 1);
348 mediaFields.add(mNum.toString());
350 mergeData.put("medianum", numOfMedia);
351 mergeData.put("mediafields", mediaFields);
352 mergeData.put("to_topic", null);
354 Map extraInfo = new HashMap();
355 extraInfo.put("languagePopUpData", DatabaseLanguage.getInstance().getPopupData());
356 extraInfo.put("themenPopupData", topicsModule.getTopicsAsSimpleList());
358 extraInfo.put("topics", topicsModule.getTopicsList());
359 deliver(req, res, mergeData, extraInfo, postingFormTemplate);
361 catch (Throwable t) {
362 throw new ServletModuleFailure(t);
367 * Method for inserting an open posting into the Database and delivering
368 * the postingDone Page
371 public void insposting(HttpServletRequest aRequest, HttpServletResponse aResponse) throws
372 ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
373 if (MirGlobal.abuse().getOpenPostingDisabled()) {
374 openPostingDisabled(aRequest, aResponse);
380 HTTPParsedRequest parsedRequest = new HTTPParsedRequest(
382 configuration.getString("Mir.DefaultEncoding"),
383 configuration.getInt("MaxMediaUploadSize")*1024,
384 configuration.getString("TempDir"));
386 Map mergeData = new HashMap();
388 HttpSession session = aRequest.getSession(false);
389 String sessionPasswd = (String) session.getAttribute("passwd");
390 if (sessionPasswd != null) {
391 String passwd = (String) parsedRequest.getParameter("passwd");
393 if (passwd == null || passwd.length() == 0) {
394 throw new ServletModuleUserExc("posting.error.missingpassword", new String[] {});
396 if (!sessionPasswd.equals(passwd)) {
397 throw new ServletModuleUserExc("posting.error.invalidpassword", new String[] {});
399 session.invalidate();
402 if ((((String) parsedRequest.getParameter("title")).length() == 0) ||
403 (((String) parsedRequest.getParameter("description")).length() == 0) ||
404 (((String) parsedRequest.getParameter("content_data")).length() == 0))
405 throw new ServletModuleUserExc("posting.error.missingfield", new String[] {});
407 List mediaList = new Vector();
408 Iterator i = parsedRequest.getFiles().iterator();
410 while (i.hasNext()) {
411 UploadedFile file = new mir.session.CommonsUploadedFileAdapter((FileItem) i.next());
412 Map mediaValues = new HashMap();
414 String suffix = file.getFieldName().substring(5); // media${m}
415 logger.debug("media_title" + suffix);
416 String title = parsedRequest.getParameter("media_title" + suffix);
418 mediaValues.put("title", StringUtil.removeHTMLTags(title));
419 mediaValues.put("creator", StringUtil.removeHTMLTags(parsedRequest.getParameter("creator")));
420 mediaValues.put("to_publisher", "0");
421 mediaValues.put("is_published", "1");
422 mediaValues.put("to_media_folder", "7");
424 mediaList.add(MediaUploadProcessor.processMediaUpload(file, mediaValues));
427 Map withValues = new HashMap();
428 i = DatabaseContent.getInstance().getFields().iterator();
429 while (i.hasNext()) {
430 String field = (String) i.next();
431 String value = parsedRequest.getParameter(field);
433 withValues.put(field, value);
437 for (i = withValues.keySet().iterator(); i.hasNext(); ) {
438 String k = (String) i.next();
439 String v = (String) withValues.get(k);
441 if (k.equals("content_data")) {
442 //this doesn't quite work yet, so for now, all html goes
443 //withValues.put(k,StringUtil.approveHTMLTags(v));
444 withValues.put(k, StringUtil.deleteForbiddenTags(v));
446 else if (k.equals("description")) {
447 String tmp = StringUtil.deleteForbiddenTags(v);
448 withValues.put(k, StringUtil.deleteHTMLTableTags(tmp));
451 withValues.put(k, StringUtil.removeHTMLTags(v));
455 withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
456 withValues.put("publish_path",
457 StringUtil.webdbDate2path( (String) withValues.get("date")));
458 withValues.put("is_produced", "0");
459 withValues.put("is_published", "1");
460 if (directOp.equals("yes"))
461 withValues.put("to_article_type", "1");
463 withValues.put("to_publisher", "1");
465 // inserting content into database
466 String cid = contentModule.add(withValues);
467 logger.debug("id: " + cid);
468 //insert was not successfull
470 deliver(aRequest, aResponse, mergeData, null, postingFormDupeTemplate);
474 List topics = parsedRequest.getParameterList("to_topic");
475 if (topics.size() > 0) {
477 DatabaseContentToTopics.getInstance().setTopics(cid, topics);
479 catch (Throwable e) {
480 logger.error("setting content_x_topic failed");
481 contentModule.deleteById(cid);
482 throw new ServletModuleFailure(
483 "smod - openindy :: insposting: setting content_x_topic failed: " +
488 i = mediaList.iterator();
489 while (i.hasNext()) {
490 Entity mediaEnt = (Entity) i.next();
491 DatabaseContentToMedia.getInstance().addMedia(cid, mediaEnt.getId());
494 EntityContent article = (EntityContent) contentModule.getById(cid);
496 MirGlobal.abuse().checkArticle(
497 article, new HTTPAdapters.HTTPRequestAdapter(aRequest), aResponse);
498 MirGlobal.localizer().openPostings().afterContentPosting(article);
500 catch (Throwable t) {
501 logger.error("Error while post-processing article: " + t.getMessage());
503 deliver(aRequest, aResponse, mergeData, null, postingFormDoneTemplate);
505 catch (Throwable e) {
506 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
507 Throwable cause = ExceptionFunctions.traceCauseException(e);
509 if (cause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {
510 throw new ServletModuleUserExc("media.unsupportedformat", new String[] {});
512 throw new ServletModuleFailure(e);
517 * Due to a serious shortcoming of Tomcat 3.3, an extra sessionid parameter is
518 * generated into open session urls. Tomcat 3.3 makes it impossible to
519 * distinguish between sessions that are identified using a url and those
520 * that are identified using cookies: if both a sessionid cookie and a sessionid
521 * url are available, tomcat 3.3 pretends the url wasn't there...
523 private static final String SESSION_REQUEST_KEY="sessionid";
526 * Dispatch method for open sessions: a flexible extensible and customizable way
527 * for open access. Can be used for postings, but also for lots of other stuff.
531 * @throws ServletModuleExc
532 * @throws ServletModuleUserExc
533 * @throws ServletModuleFailure
536 public void opensession(HttpServletRequest aRequest, HttpServletResponse aResponse)
537 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
541 new HTTPAdapters.HTTPParsedRequestAdapter(new HTTPParsedRequest(aRequest,
542 configuration.getString("Mir.DefaultEncoding"),
543 configuration.getInt("MaxMediaUploadSize")*1024,
544 configuration.getString("TempDir")));
546 if (aRequest.isRequestedSessionIdValid() && !aRequest.isRequestedSessionIdFromURL() &&
547 !aRequest.getRequestedSessionId().equals(aRequest.getParameter(SESSION_REQUEST_KEY)))
548 aRequest.getSession().invalidate();
550 Session session = new HTTPAdapters.HTTPSessionAdapter(aRequest.getSession());
552 SimpleResponse response = new SimpleResponse(
553 ServletHelper.makeGenerationData(aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)},
556 response.setResponseValue("actionURL", aResponse.encodeURL(HttpUtils.getRequestURL(aRequest).toString())+"?"+SESSION_REQUEST_KEY+"="+aRequest.getSession().getId());
558 SessionHandler handler = MirGlobal.localizer().openPostings().getOpenSessionHandler(request, session);
560 handler.processRequest(request, session, response);
561 ServletHelper.generateOpenPostingResponse(aResponse.getWriter(), response.getResponseValues(), response.getResponseGenerator());
563 catch (Throwable t) {
564 logger.error(t.toString());
565 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
567 throw new ServletModuleFailure(t);
572 * Method for preparing and sending a content as an email message
575 public void mail(HttpServletRequest req, HttpServletResponse res)
576 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure
578 String aid = req.getParameter("mail_aid");
580 throw new ServletModuleExc("An article id must be specified in requests to email an article. Something therefore went badly wrong....");
583 String to = req.getParameter("mail_to");
584 String from = req.getParameter("mail_from");
585 String from_name = req.getParameter("mail_from_name");
586 String from_ip = req.getRemoteAddr();
587 String comment = req.getParameter("mail_comment");
588 String mail_language = req.getParameter("mail_language");
590 Map mergeData = new HashMap();
591 mergeData.put("mail_to",to);
592 mergeData.put("mail_from",from);
593 mergeData.put("mail_from_name",from_name);
594 mergeData.put("mail_comment",comment);
595 mergeData.put("mail_aid",aid);
596 mergeData.put("mail_language",mail_language);
599 if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){
600 deliver(req, res, mergeData, null, prepareMailTemplate);
603 //run checks on to and from and mail_language to make sure no monkey business occurring
604 if (mail_language.indexOf('.') != -1 || mail_language.indexOf('/') != -1 ) {
605 throw new ServletModuleExc("Invalid language");
607 if (to.indexOf('\n') != -1
608 || to.indexOf('\r') != -1
609 || to.indexOf(',') != -1) {
610 throw new ServletModuleUserExc("email.error.invalidtoaddress", new String[] {to});
612 if (from.indexOf('\n') != -1 || from.indexOf('\r') != -1 || from.indexOf(',') != -1 ) {
613 throw new ServletModuleUserExc("email.error.invalidfromaddress", new String[] {from});
616 CacheKey theCacheKey=new CacheKey("email",aid+mail_language);
619 if (MirGlobal.mruCache().hasObject(theCacheKey)){
620 logger.info("fetching email text for article "+aid+" from cache");
621 theEmailText = (String) MirGlobal.mruCache().getObject(theCacheKey);
624 EntityContent contentEnt;
626 contentEnt = (EntityContent) contentModule.getById(aid);
627 StringWriter theEMailTextWriter = new StringWriter();
628 PrintWriter dest = new PrintWriter(theEMailTextWriter);
629 Map articleData = new HashMap();
630 articleData.put("article", MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("content", contentEnt));
631 articleData.put("languagecode", mail_language);
632 deliver(dest, req, res, articleData, null, emailAnArticleTemplate, mail_language);
633 theEmailText = theEMailTextWriter.toString();
634 MirGlobal.mruCache().storeObject(theCacheKey, theEmailText);
636 catch (Throwable e) {
637 throw new ServletModuleFailure("Couldn't get content for article " + aid + mail_language + ": " + e.getMessage(), e);
641 String content = theEmailText;
645 content = "To: " + to + "\nReply-To: "+ from + "\nX-Originating-IP: "+ from_ip + "\n" + content;
646 // put in the comment where it should go
647 if (comment != null) {
648 String commentTextToInsert = "\n\nAttached comment from " + from_name + ":\n" + comment;
650 content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!",commentTextToInsert);
653 throw new ServletModuleFailure("Problem doing regular expression replacement " + e.toString(), e);
658 content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!","");
661 throw new ServletModuleFailure("Problem doing regular expression replacement " + e.toString(), e);
665 SMTPClient client=new SMTPClient();
668 client.connect(configuration.getString("ServletModule.OpenIndy.SMTPServer"));
670 reply = client.getReplyCode();
672 if (!SMTPReply.isPositiveCompletion(reply)) {
674 throw new ServletModuleExc("SMTP server refused connection.");
677 client.sendSimpleMessage(configuration.getString("ServletModule.OpenIndy.EmailIsFrom"), to, content);
680 //mission accomplished
681 deliver(req, res, mergeData, null, sentMailTemplate);
683 catch(IOException e) {
684 if(client.isConnected()) {
687 } catch(IOException f) {
691 throw new ServletModuleFailure(e);
699 * Method for querying a lucene index
703 * @throws ServletModuleExc
704 * @throws ServletModuleUserExc
705 * @throws ServletModuleFailure
708 public void search(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
710 final String[] search_variables = {
711 "search_content", "search_boolean", "search_creator",
712 "search_topic", "search_hasImages", "search_hasAudio", "search_hasVideo", "search_sort",
713 "search_submit", "search_back", "search_forward"};
714 HTTPRequestParser requestParser = new HTTPRequestParser(req);
718 HttpSession session = req.getSession(false);
720 String queryString = "";
722 Map mergeData = new HashMap();
724 KeywordSearchTerm dateTerm = new KeywordSearchTerm("date_formatted", "search_date", "webdb_create_formatted", "webdb_create_formatted", "webdb_create_formatted");
725 UnIndexedSearchTerm whereTerm = new UnIndexedSearchTerm("", "", "", "where", "where");
726 TextSearchTerm creatorTerm = new TextSearchTerm("creator", "search_creator", "creator", "creator", "creator");
727 TextSearchTerm titleTerm = new TextSearchTerm("title", "search_content", "title", "title", "title");
728 TextSearchTerm descriptionTerm = new TextSearchTerm("description", "search_content", "description", "description", "description");
729 ContentSearchTerm contentTerm = new ContentSearchTerm("content_data", "search_content", "content", "", "");
730 TopicSearchTerm topicTerm = new TopicSearchTerm();
731 ImagesSearchTerm imagesTerm = new ImagesSearchTerm();
732 AudioSearchTerm audioTerm = new AudioSearchTerm();
733 VideoSearchTerm videoTerm = new VideoSearchTerm();
735 //make the query available to subsequent iterations
737 Iterator j = Arrays.asList(search_variables).iterator();
738 while (j.hasNext()) {
739 String variable = (String) j.next();
741 mergeData.put(variable, requestParser.getParameter(variable));
745 mergeData.put("topics", topicsModule.getTopicsAsSimpleList());
747 catch (Throwable e) {
748 logger.debug("Can't get topics: " + e.toString());
751 String searchBackValue = req.getParameter("search_back");
752 String searchForwardValue = req.getParameter("search_forward");
754 if (searchBackValue != null) {
755 int totalHits = ( (Integer) session.getAttribute("numberOfHits")).intValue();
756 int newPosition = ( (Integer) session.getAttribute("positionInResults")).intValue() - increment;
759 if (newPosition >= totalHits)
760 newPosition = totalHits - 1;
761 session.setAttribute("positionInResults", new Integer(newPosition));
764 if (searchForwardValue != null) {
765 int totalHits = ( (Integer) session.getAttribute("numberOfHits")).intValue();
766 int newPosition = ( (Integer) session.getAttribute("positionInResults")).intValue() + increment;
769 if (newPosition >= totalHits)
770 newPosition = totalHits - 1;
772 session.setAttribute("positionInResults", new Integer(newPosition));
775 String indexPath = configuration.getString("IndexPath");
777 String creatorFragment = creatorTerm.makeTerm(req);
778 if (creatorFragment != null) {
779 queryString = queryString + " +" + creatorFragment;
782 // search title, description, and content for something
783 // the contentTerm uses param "search_boolean" to combine its terms
784 String contentFragment = contentTerm.makeTerm(req);
785 if (contentFragment != null) {
786 logger.debug("contentFragment: " + contentFragment);
787 queryString = queryString + " +" + contentFragment;
790 String topicFragment = topicTerm.makeTerm(req);
791 if (topicFragment != null) {
792 queryString = queryString + " +" + topicFragment;
795 String imagesFragment = imagesTerm.makeTerm(req);
796 if (imagesFragment != null) {
797 queryString = queryString + " +" + imagesFragment;
800 String audioFragment = audioTerm.makeTerm(req);
801 if (audioFragment != null) {
802 queryString = queryString + " +" + audioFragment;
805 String videoFragment = videoTerm.makeTerm(req);
806 if (videoFragment != null) {
807 queryString = queryString + " +" + videoFragment;
810 if (queryString == null || queryString == "") {
815 Searcher searcher = null;
817 searcher = new IndexSearcher(indexPath);
819 catch (IOException e) {
820 logger.debug("Can't open indexPath: " + indexPath);
821 throw new ServletModuleExc("Problem with Search Index! : " + e.toString());
826 query = QueryParser.parse(queryString, "content", new StandardAnalyzer());
828 catch (Exception e) {
830 logger.debug("Query don't parse: " + queryString);
831 throw new ServletModuleExc("Problem with Query String! (was '" + queryString + "')");
836 hits = searcher.search(query);
838 catch (IOException e) {
840 logger.debug("Can't get hits: " + e.toString());
841 throw new ServletModuleExc("Problem getting hits!");
845 int end = hits.length();
847 String sortBy = req.getParameter("search_sort");
848 if (sortBy == null || sortBy.equals("")) {
849 throw new ServletModuleExc("Please let me sort by something!(missing search_sort)");
852 // here is where the documents will go for storage across sessions
853 ArrayList theDocumentsSorted = new ArrayList();
855 if (sortBy.equals("score")) {
856 for (int i = start; i < end; i++) {
857 theDocumentsSorted.add(hits.doc(i));
861 // then we'll sort by date!
862 Map dateToPosition = new HashMap(end, 1.0F); //we know how big it will be
863 for (int i = start; i < end; i++) {
864 String creationDate = (hits.doc(i)).get("creationDate");
865 // do a little dance in case two contents created at the same second!
866 if (dateToPosition.containsKey(creationDate)) {
867 ( (ArrayList) (dateToPosition.get(creationDate))).add(new Integer(i));
870 ArrayList thePositions = new ArrayList();
871 thePositions.add(new Integer(i));
872 dateToPosition.put(creationDate, thePositions);
875 Set keys = dateToPosition.keySet();
876 ArrayList keyList = new ArrayList(keys);
877 Collections.sort(keyList);
878 if (sortBy.equals("date_desc")) {
879 Collections.reverse(keyList);
882 if (!sortBy.equals("date_asc")) {
883 throw new ServletModuleExc("don't know how to sort by: " + sortBy);
886 ListIterator keyTraverser = keyList.listIterator();
887 while (keyTraverser.hasNext()) {
888 ArrayList positions = (ArrayList) dateToPosition.get( (keyTraverser.next()));
889 ListIterator positionsTraverser = positions.listIterator();
890 while (positionsTraverser.hasNext()) {
891 theDocumentsSorted.add(hits.doc( ( (Integer) (positionsTraverser.next())).intValue()));
899 catch (IOException e) {
900 logger.debug("Can't close searcher: " + e.toString());
901 throw new ServletModuleFailure("Problem closing searcher(normal):" + e.getMessage(), e);
904 session.removeAttribute("numberOfHits");
905 session.removeAttribute("theDocumentsSorted");
906 session.removeAttribute("positionInResults");
908 session.setAttribute("numberOfHits", new Integer(end));
909 session.setAttribute("theDocumentsSorted", theDocumentsSorted);
910 session.setAttribute("positionInResults", new Integer(0));
913 catch (IOException e) {
914 logger.debug("Can't close searcher: " + e.toString());
915 throw new ServletModuleFailure("Problem closing searcher: " + e.getMessage(), e);
922 ArrayList theDocs = (ArrayList) session.getAttribute("theDocumentsSorted");
923 if (theDocs != null) {
925 mergeData.put("numberOfHits", ( (Integer) session.getAttribute("numberOfHits")).toString());
926 List theHits = new Vector();
927 int pIR = ( (Integer) session.getAttribute("positionInResults")).intValue();
929 int numHits = ( (Integer) session.getAttribute("numberOfHits")).intValue();
931 if (! (pIR + increment >= numHits)) {
932 mergeData.put("hasNext", "y");
935 mergeData.put("hasNext", null);
938 mergeData.put("hasPrevious", "y");
941 mergeData.put("hasPrevious", null);
944 if ( (pIR + increment) > numHits) {
948 terminus = pIR + increment;
950 for (int i = pIR; i < terminus; i++) {
951 Map h = new HashMap();
952 Document theHit = (Document) theDocs.get(i);
953 whereTerm.returnMeta(h, theHit);
954 creatorTerm.returnMeta(h, theHit);
955 titleTerm.returnMeta(h, theHit);
956 descriptionTerm.returnMeta(h, theHit);
957 dateTerm.returnMeta(h, theHit);
958 imagesTerm.returnMeta(h, theHit);
959 audioTerm.returnMeta(h, theHit);
960 videoTerm.returnMeta(h, theHit);
963 mergeData.put("hits", theHits);
966 catch (Throwable e) {
967 logger.error("Can't iterate over hits: " + e.toString());
969 throw new ServletModuleFailure("Problem getting hits: " + e.getMessage(), e);
972 mergeData.put("queryString", queryString);
974 deliver(req, res, mergeData, null, searchResultsTemplate);
976 catch (NullPointerException n) {
977 throw new ServletModuleFailure("Null Pointer: " + n.toString(), n);
982 * Method for dynamically generating a pdf using iText
985 public void getpdf(HttpServletRequest req, HttpServletResponse res)
986 throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
987 long starttime=System.currentTimeMillis();
988 String ID_REQUEST_PARAM = "id";
989 int maxArticlesInNewsleter = 15; // it is nice not to be dos'ed
991 String idParam = req.getParameter(ID_REQUEST_PARAM);
992 if (idParam != null) {
995 RE re = new RE("[0-9]+");
998 REMatch[] idMatches=re.getAllMatches(idParam);
1000 String cacheSelector="";
1002 for (int i = 0; i < idMatches.length; i++){
1003 cacheSelector= cacheSelector + "," + idMatches[i].toString();
1006 String cacheType="pdf";
1008 CacheKey theCacheKey = new CacheKey(cacheType,cacheSelector);
1012 if (MirGlobal.mruCache().hasObject(theCacheKey)){
1013 logger.info("fetching pdf from cache");
1014 thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey);
1017 logger.info("generating pdf and caching it");
1018 ByteArrayOutputStream out = new ByteArrayOutputStream();
1019 PDFGenerator pdfMaker = new PDFGenerator(out);
1021 if (idMatches.length > 1){
1023 for (int i = 0; i < idMatches.length && i < maxArticlesInNewsleter; i++){
1024 REMatch aMatch = idMatches[i];
1025 String id=aMatch.toString();
1026 EntityContent contentEnt = (EntityContent)contentModule.getById(id);
1027 pdfMaker.addIndexItem(contentEnt);
1031 for (int i = 0; i < idMatches.length; i++){
1032 REMatch aMatch = idMatches[i];
1033 String id=aMatch.toString();
1034 EntityContent contentEnt = (EntityContent)contentModule.getById(id);
1036 pdfMaker.add(contentEnt);
1040 thePDF = out.toByteArray();
1042 //and save all our hard work!
1043 MirGlobal.mruCache().storeObject(theCacheKey,thePDF);
1046 res.setContentType("application/pdf");
1047 res.setContentLength(thePDF.length);
1048 res.getOutputStream().write(thePDF);
1049 res.getOutputStream().flush();
1050 String elapsedtime=(new Long(System.currentTimeMillis()-starttime)).toString();
1051 logger.info("pdf retireval took "+elapsedtime + " milliseconds" );
1055 throw new ServletModuleExc("Missing id.");
1058 catch (Throwable t) {
1059 logger.error(t.toString());
1060 throw new ServletModuleFailure(t);
1066 public String generateOnetimePassword() {
1067 Random r = new Random();
1068 int random = r.nextInt();
1070 long l = System.currentTimeMillis();
1072 l = (l * l * l * l) / random;
1076 String returnString = "" + l;
1078 return returnString.substring(5);
1081 public void deliver(HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator) throws ServletModuleFailure {
1083 deliver(aResponse.getWriter(), aRequest, aResponse, aData, anExtra, aGenerator);
1085 catch (Throwable t) {
1086 throw new ServletModuleFailure(t);
1090 public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator)
1091 throws ServletModuleFailure {
1093 Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open");
1094 responseData.put("data", aData);
1095 responseData.put("extra", anExtra);
1098 Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);
1099 generator.generate(anOutputWriter, responseData, logger);
1101 anOutputWriter.close();
1103 catch (Throwable e) {
1104 logger.error("Error while generating " + aGenerator + ": " + e.getMessage());
1106 throw new ServletModuleFailure(e);
1110 public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator,String aLocaleString)
1111 throws ServletModuleFailure {
1113 Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { new Locale(aLocaleString,""), getFallbackLocale(aRequest)}, "bundles.open");
1114 responseData.put("data", aData);
1115 responseData.put("extra", anExtra);
1118 Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator);
1119 generator.generate(anOutputWriter, responseData, logger);
1121 anOutputWriter.close();
1123 catch (Throwable e) {
1124 logger.error("Error while generating " + aGenerator + ": " + e.getMessage());
1126 throw new ServletModuleFailure(e);
1131 public void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse,PrintWriter out, Throwable anException) {
1133 logger.error("error: " + anException);
1134 Map data = new HashMap();
1136 data.put("errorstring", anException.getMessage());
1137 data.put("date", StringUtil.date2readableDateTime(new GregorianCalendar()));
1139 deliver(out, aRequest, aResponse, data, null, configuration.getString("ServletModule.OpenIndy.ErrorTemplate"));
1141 catch (Throwable e) {
1142 throw new ServletModuleFailure(e);
1146 public void handleUserError(HttpServletRequest aRequest, HttpServletResponse aResponse,
1147 PrintWriter out, ServletModuleUserExc anException) {
1149 logger.warn("user error: " + anException.getMessage());
1150 Map data = new HashMap();
1152 MessageResources messages = MessageResources.getMessageResources("bundles.open");
1153 data.put("errorstring", messages.getMessage(getLocale(aRequest), anException.getMessage(), anException.getParameters()));
1154 data.put("date", StringUtil.date2readableDateTime(new GregorianCalendar()));
1156 deliver(out, aRequest, aResponse, data, null, configuration.getString("ServletModule.OpenIndy.UserErrorTemplate"));
1158 catch (Throwable e) {
1159 throw new ServletModuleFailure(e);
1163 private String createOneTimePasswd() {