X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fservlet%2FServletModuleOpenIndy.java;h=6896876a9e6e5850eb42dbea4e3e46eaca1d6293;hb=a0e0629d9b38c2eb4eba6d9420ccbc6d1b44ab00;hp=0f0f47aaa0f32b8c0d880fb300cce9bbc1dba40c;hpb=a459f111d85598df56c6ab711cec11632676b39c;p=mir.git diff --git a/source/mircoders/servlet/ServletModuleOpenIndy.java b/source/mircoders/servlet/ServletModuleOpenIndy.java index 0f0f47aa..6896876a 100755 --- a/source/mircoders/servlet/ServletModuleOpenIndy.java +++ b/source/mircoders/servlet/ServletModuleOpenIndy.java @@ -18,29 +18,25 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with any library licensed under the Apache Software License, - * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library - * (or with modified versions of the above that use the same license as the above), - * and distribute linked combinations including the two. You must obey the - * GNU General Public License in all respects for all of the code used other than - * the above mentioned libraries. If you modify this file, you may extend this - * exception to your version of the file, but you are not obligated to do so. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. * If you do not wish to do so, delete this exception statement from your version. */ package mircoders.servlet; import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Enumeration; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Iterator; @@ -91,12 +87,13 @@ import mir.util.HTTPRequestParser; import mir.util.StringRoutines; import mircoders.entity.EntityComment; import mircoders.entity.EntityContent; +import mircoders.global.CacheKey; import mircoders.global.MirGlobal; import mircoders.media.MediaUploadProcessor; -import mircoders.media.UnsupportedMediaFormatExc; import mircoders.module.ModuleComment; import mircoders.module.ModuleContent; import mircoders.module.ModuleImages; +import mircoders.module.ModuleMediaType; import mircoders.module.ModuleTopics; import mircoders.pdf.PDFGenerator; import mircoders.search.AudioSearchTerm; @@ -122,7 +119,7 @@ import mircoders.storage.DatabaseTopics; * open-postings to the newswire * * @author mir-coders group - * @version $Id: ServletModuleOpenIndy.java,v 1.80 2003/04/21 12:42:51 idfx Exp $ + * @version $Id: ServletModuleOpenIndy.java,v 1.89.2.1 2003/05/17 16:21:14 john Exp $ * */ @@ -132,7 +129,7 @@ public class ServletModuleOpenIndy extends ServletModule private String commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate; private String postingFormTemplate, postingFormDoneTemplate, postingFormDupeTemplate; private String searchResultsTemplate; - private String prepareMailTemplate,sentMailTemplate; + private String prepareMailTemplate,sentMailTemplate,emailAnArticleTemplate; private ModuleContent contentModule; private ModuleComment commentModule; private ModuleImages imageModule; @@ -157,6 +154,7 @@ public class ServletModuleOpenIndy extends ServletModule searchResultsTemplate = configuration.getString("ServletModule.OpenIndy.SearchResultsTemplate"); prepareMailTemplate = configuration.getString("ServletModule.OpenIndy.PrepareMailTemplate"); + emailAnArticleTemplate = configuration.getString("ServletModule.OpenIndy.MailableArticleTemplate"); sentMailTemplate = configuration.getString("ServletModule.OpenIndy.SentMailTemplate"); directOp = configuration.getString("DirectOpenposting").toLowerCase(); commentModule = new ModuleComment(DatabaseComment.getInstance()); @@ -495,7 +493,8 @@ public class ServletModuleOpenIndy extends ServletModule EntityContent article = (EntityContent) contentModule.getById(cid); try { - MirGlobal.abuse().checkArticle(article, aRequest, aResponse); + MirGlobal.abuse().checkArticle( + article, new HTTPAdapters.HTTPRequestAdapter(aRequest), aResponse); MirGlobal.localizer().openPostings().afterContentPosting(article); } catch (Throwable t) { @@ -507,7 +506,7 @@ public class ServletModuleOpenIndy extends ServletModule e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE)); Throwable cause = ExceptionFunctions.traceCauseException(e); - if (cause instanceof UnsupportedMediaFormatExc) { + if (cause instanceof ModuleMediaType.UnsupportedMimeTypeExc) { throw new ServletModuleUserExc("media.unsupportedformat", new String[] {}); } throw new ServletModuleFailure(e); @@ -538,7 +537,11 @@ public class ServletModuleOpenIndy extends ServletModule throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { try { - Request request = new HTTPAdapters.HTTPParsedRequestAdapter(new HTTPParsedRequest(aRequest, 1000000, "/tmp")); + Request request = + new HTTPAdapters.HTTPParsedRequestAdapter(new HTTPParsedRequest(aRequest, + configuration.getString("Mir.DefaultEncoding"), + configuration.getInt("MaxMediaUploadSize")*1024, + configuration.getString("TempDir"))); if (aRequest.isRequestedSessionIdValid() && !aRequest.isRequestedSessionIdFromURL() && !aRequest.getRequestedSessionId().equals(aRequest.getParameter(SESSION_REQUEST_KEY))) @@ -547,7 +550,7 @@ public class ServletModuleOpenIndy extends ServletModule Session session = new HTTPAdapters.HTTPSessionAdapter(aRequest.getSession()); SimpleResponse response = new SimpleResponse( - ServletHelper.makeGenerationData(new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)}, + ServletHelper.makeGenerationData(aResponse, new Locale[] {getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open")); response.setResponseValue("actionURL", aResponse.encodeURL(HttpUtils.getRequestURL(aRequest).toString())+"?"+SESSION_REQUEST_KEY+"="+aRequest.getSession().getId()); @@ -580,20 +583,20 @@ public class ServletModuleOpenIndy extends ServletModule String to = req.getParameter("mail_to"); String from = req.getParameter("mail_from"); String from_name = req.getParameter("mail_from_name"); + String from_ip = req.getRemoteAddr(); String comment = req.getParameter("mail_comment"); String mail_language = req.getParameter("mail_language"); Map mergeData = new HashMap(); + mergeData.put("mail_to",to); + mergeData.put("mail_from",from); + mergeData.put("mail_from_name",from_name); + mergeData.put("mail_comment",comment); + mergeData.put("mail_aid",aid); + mergeData.put("mail_language",mail_language); - if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){ - - for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) { - String pName=(String)theParams.nextElement(); - if (pName.startsWith("mail_")){ - mergeData.put( pName,req.getParameter(pName) ); - } - } + if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){ deliver(req, res, mergeData, null, prepareMailTemplate); } else { @@ -610,41 +613,36 @@ public class ServletModuleOpenIndy extends ServletModule throw new ServletModuleUserExc("email.error.invalidfromaddress", new String[] {from}); } + CacheKey theCacheKey=new CacheKey("email",aid+mail_language); + String theEmailText; - EntityContent contentEnt; - try{ - contentEnt = (EntityContent)contentModule.getById(aid); - } - catch (Throwable e){ - throw new ServletModuleFailure("Couldn't get content for article "+aid + ": " + e.getMessage(), e); + if (MirGlobal.mruCache().hasObject(theCacheKey)){ + logger.info("fetching email text for article "+aid+" from cache"); + theEmailText = (String) MirGlobal.mruCache().getObject(theCacheKey); } - String producerStorageRoot=configuration.getString("Producer.StorageRoot"); - String producerDocRoot=configuration.getString("Producer.DocRoot"); - String publishPath = contentEnt.getValue("publish_path"); - String txtFilePath = producerStorageRoot + producerDocRoot + "/" + mail_language + - publishPath + "/" + aid + ".txt"; - - - File inputFile = new File(txtFilePath); - String content; - - try{ - FileReader in = new FileReader(inputFile); - StringWriter out = new StringWriter(); - int c; - while ((c = in.read()) != -1) - out.write(c); - in.close(); - content= out.toString(); - } - catch (FileNotFoundException e){ - throw new ServletModuleFailure("No text file found in " + txtFilePath, e); - } - catch (IOException e){ - throw new ServletModuleFailure("Problem reading file in " + txtFilePath, e); + else { + EntityContent contentEnt; + try { + contentEnt = (EntityContent) contentModule.getById(aid); + StringWriter theEMailTextWriter = new StringWriter(); + PrintWriter dest = new PrintWriter(theEMailTextWriter); + Map articleData = new HashMap(); + articleData.put("article", MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("content", contentEnt)); + articleData.put("languagecode", mail_language); + deliver(dest, req, res, articleData, null, emailAnArticleTemplate, mail_language); + theEmailText = theEMailTextWriter.toString(); + MirGlobal.mruCache().storeObject(theCacheKey, theEmailText); + } + catch (Throwable e) { + throw new ServletModuleFailure("Couldn't get content for article " + aid + mail_language + ": " + e.getMessage(), e); + } } + + String content = theEmailText; + + // add some headers - content = "To: " + to + "\nReply-To: "+ from + "\n" + content; + content = "To: " + to + "\nReply-To: "+ from + "\nX-Originating-IP: "+ from_ip + "\n" + content; // put in the comment where it should go if (comment != null) { String commentTextToInsert = "\n\nAttached comment from " + from_name + ":\n" + comment; @@ -697,350 +695,374 @@ public class ServletModuleOpenIndy extends ServletModule + /** + * Method for querying a lucene index + * + * @param req + * @param res + * @throws ServletModuleExc + * @throws ServletModuleUserExc + * @throws ServletModuleFailure + */ - /** - * Method for querying a lucene index - * - * @param req - * @param res - * @throws ServletModuleExc - * @throws ServletModuleUserExc - * @throws ServletModuleFailure - */ - - public void search(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { - try { - final String[] search_variables = { "search_content", "search_boolean", "search_creator", - "search_topic", "search_hasImages", "search_hasAudio", "search_hasVideo", "search_sort", - "search_submit", "search_back", "search_forward" }; - HTTPRequestParser requestParser = new HTTPRequestParser(req); - - int increment=10; + public void search(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + try { + final String[] search_variables = { + "search_content", "search_boolean", "search_creator", + "search_topic", "search_hasImages", "search_hasAudio", "search_hasVideo", "search_sort", + "search_submit", "search_back", "search_forward"}; + HTTPRequestParser requestParser = new HTTPRequestParser(req); - HttpSession session = req.getSession(false); + int increment = 10; - String queryString=""; + HttpSession session = req.getSession(false); - Map mergeData = new HashMap(); + String queryString = ""; - KeywordSearchTerm dateTerm = new KeywordSearchTerm("date_formatted","search_date","webdb_create_formatted","webdb_create_formatted","webdb_create_formatted"); - UnIndexedSearchTerm whereTerm = new UnIndexedSearchTerm("","","","where","where"); - TextSearchTerm creatorTerm = new TextSearchTerm("creator","search_creator","creator","creator","creator"); - TextSearchTerm titleTerm = new TextSearchTerm("title","search_content","title","title","title"); - TextSearchTerm descriptionTerm = new TextSearchTerm("description","search_content","description","description","description"); - ContentSearchTerm contentTerm = new ContentSearchTerm("content_data","search_content","content","",""); - TopicSearchTerm topicTerm = new TopicSearchTerm(); - ImagesSearchTerm imagesTerm = new ImagesSearchTerm(); - AudioSearchTerm audioTerm = new AudioSearchTerm(); - VideoSearchTerm videoTerm = new VideoSearchTerm(); + Map mergeData = new HashMap(); - //make the query available to subsequent iterations + KeywordSearchTerm dateTerm = new KeywordSearchTerm("date_formatted", "search_date", "webdb_create_formatted", "webdb_create_formatted", "webdb_create_formatted"); + UnIndexedSearchTerm whereTerm = new UnIndexedSearchTerm("", "", "", "where", "where"); + TextSearchTerm creatorTerm = new TextSearchTerm("creator", "search_creator", "creator", "creator", "creator"); + TextSearchTerm titleTerm = new TextSearchTerm("title", "search_content", "title", "title", "title"); + TextSearchTerm descriptionTerm = new TextSearchTerm("description", "search_content", "description", "description", "description"); + ContentSearchTerm contentTerm = new ContentSearchTerm("content_data", "search_content", "content", "", ""); + TopicSearchTerm topicTerm = new TopicSearchTerm(); + ImagesSearchTerm imagesTerm = new ImagesSearchTerm(); + AudioSearchTerm audioTerm = new AudioSearchTerm(); + VideoSearchTerm videoTerm = new VideoSearchTerm(); - Iterator j = Arrays.asList(search_variables).iterator(); - while (j.hasNext()) { - String variable = (String) j.next(); + //make the query available to subsequent iterations - mergeData.put(variable, requestParser.getParameter(variable)); - } + Iterator j = Arrays.asList(search_variables).iterator(); + while (j.hasNext()) { + String variable = (String) j.next(); - try{ - mergeData.put("topics", topicsModule.getTopicsAsSimpleList()); - } - catch(Throwable e) { - logger.debug("Can't get topics: " + e.toString()); - } + mergeData.put(variable, requestParser.getParameter(variable)); + } - String searchBackValue = req.getParameter("search_back"); - String searchForwardValue = req.getParameter("search_forward"); + try { + mergeData.put("topics", topicsModule.getTopicsAsSimpleList()); + } + catch (Throwable e) { + logger.debug("Can't get topics: " + e.toString()); + } - if (searchBackValue != null){ - int totalHits = ((Integer) session.getAttribute("numberOfHits")).intValue(); - int newPosition=((Integer)session.getAttribute("positionInResults")).intValue()-increment; - if (newPosition<0) - newPosition=0; + String searchBackValue = req.getParameter("search_back"); + String searchForwardValue = req.getParameter("search_forward"); + + if (searchBackValue != null) { + int totalHits = ( (Integer) session.getAttribute("numberOfHits")).intValue(); + int newPosition = ( (Integer) session.getAttribute("positionInResults")).intValue() - increment; + if (newPosition < 0) + newPosition = 0; + if (newPosition >= totalHits) + newPosition = totalHits - 1; + session.setAttribute("positionInResults", new Integer(newPosition)); + } + else { + if (searchForwardValue != null) { + int totalHits = ( (Integer) session.getAttribute("numberOfHits")).intValue(); + int newPosition = ( (Integer) session.getAttribute("positionInResults")).intValue() + increment; + if (newPosition < 0) + newPosition = 0; if (newPosition >= totalHits) - newPosition=totalHits-1; - session.setAttribute("positionInResults",new Integer(newPosition)); + newPosition = totalHits - 1; + + session.setAttribute("positionInResults", new Integer(newPosition)); } else { - if (searchForwardValue != null){ - int totalHits = ((Integer) session.getAttribute("numberOfHits")).intValue(); - int newPosition=((Integer)session.getAttribute("positionInResults")).intValue()+increment; - if (newPosition<0) - newPosition=0; - if (newPosition >= totalHits) - newPosition=totalHits-1; - - session.setAttribute("positionInResults",new Integer(newPosition)); - } - else { - String indexPath=configuration.getString("IndexPath"); + String indexPath = configuration.getString("IndexPath"); + String creatorFragment = creatorTerm.makeTerm(req); + if (creatorFragment != null) { + queryString = queryString + " +" + creatorFragment; + } - String creatorFragment = creatorTerm.makeTerm(req); - if (creatorFragment != null){ - queryString = queryString + " +" + creatorFragment; - } - - // search title, description, and content for something - // the contentTerm uses param "search_boolean" to combine its terms - String contentFragment = contentTerm.makeTerm(req); - if (contentFragment != null){ - logger.debug("contentFragment: " + contentFragment); - queryString = queryString + " +" + contentFragment; - } + // search title, description, and content for something + // the contentTerm uses param "search_boolean" to combine its terms + String contentFragment = contentTerm.makeTerm(req); + if (contentFragment != null) { + logger.debug("contentFragment: " + contentFragment); + queryString = queryString + " +" + contentFragment; + } - String topicFragment = topicTerm.makeTerm(req); - if (topicFragment != null){ - queryString = queryString + " +" + topicFragment; - } + String topicFragment = topicTerm.makeTerm(req); + if (topicFragment != null) { + queryString = queryString + " +" + topicFragment; + } - String imagesFragment = imagesTerm.makeTerm(req); - if (imagesFragment != null){ - queryString = queryString + " +" + imagesFragment; - } + String imagesFragment = imagesTerm.makeTerm(req); + if (imagesFragment != null) { + queryString = queryString + " +" + imagesFragment; + } - String audioFragment = audioTerm.makeTerm(req); - if (audioFragment != null){ - queryString = queryString + " +" + audioFragment; - } + String audioFragment = audioTerm.makeTerm(req); + if (audioFragment != null) { + queryString = queryString + " +" + audioFragment; + } - String videoFragment = videoTerm.makeTerm(req); - if (videoFragment != null){ - queryString = queryString + " +" + videoFragment; - } + String videoFragment = videoTerm.makeTerm(req); + if (videoFragment != null) { + queryString = queryString + " +" + videoFragment; + } - if (queryString == null || queryString == ""){ - queryString = ""; - } - else{ - try{ - Searcher searcher = null; - try { - searcher = new IndexSearcher(indexPath); - } - catch(IOException e) { - logger.debug("Can't open indexPath: " + indexPath); - throw new ServletModuleExc("Problem with Search Index! : "+ e.toString()); - } + if (queryString == null || queryString == "") { + queryString = ""; + } + else { + try { + Searcher searcher = null; + try { + searcher = new IndexSearcher(indexPath); + } + catch (IOException e) { + logger.debug("Can't open indexPath: " + indexPath); + throw new ServletModuleExc("Problem with Search Index! : " + e.toString()); + } - Query query = null; - try { - query = QueryParser.parse(queryString, "content", new StandardAnalyzer()); - } - catch(Exception e) { - searcher.close(); - logger.debug("Query don't parse: " + queryString); - throw new ServletModuleExc("Problem with Query String! (was '"+queryString+"')"); - } + Query query = null; + try { + query = QueryParser.parse(queryString, "content", new StandardAnalyzer()); + } + catch (Exception e) { + searcher.close(); + logger.debug("Query don't parse: " + queryString); + throw new ServletModuleExc("Problem with Query String! (was '" + queryString + "')"); + } - Hits hits = null; - try { - hits = searcher.search(query); - } - catch(IOException e) { - searcher.close(); - logger.debug("Can't get hits: " + e.toString()); - throw new ServletModuleExc("Problem getting hits!"); - } + Hits hits = null; + try { + hits = searcher.search(query); + } + catch (IOException e) { + searcher.close(); + logger.debug("Can't get hits: " + e.toString()); + throw new ServletModuleExc("Problem getting hits!"); + } - int start = 0; - int end = hits.length(); + int start = 0; + int end = hits.length(); - String sortBy=req.getParameter("search_sort"); - if (sortBy == null || sortBy.equals("")){ - throw new ServletModuleExc("Please let me sort by something!(missing search_sort)"); - } + String sortBy = req.getParameter("search_sort"); + if (sortBy == null || sortBy.equals("")) { + throw new ServletModuleExc("Please let me sort by something!(missing search_sort)"); + } - // here is where the documents will go for storage across sessions - ArrayList theDocumentsSorted = new ArrayList(); + // here is where the documents will go for storage across sessions + ArrayList theDocumentsSorted = new ArrayList(); - if (sortBy.equals("score")){ - for(int i = start; i < end; i++) { - theDocumentsSorted.add(hits.doc(i)); - } + if (sortBy.equals("score")) { + for (int i = start; i < end; i++) { + theDocumentsSorted.add(hits.doc(i)); } - else{ - // then we'll sort by date! - Map dateToPosition = new HashMap(end,1.0F); //we know how big it will be - for(int i = start; i < end; i++) { - String creationDate=(hits.doc(i)).get("creationDate"); - // do a little dance in case two contents created at the same second! - if (dateToPosition.containsKey(creationDate)){ - ((ArrayList) (dateToPosition.get(creationDate))).add(new Integer(i)); - } - else{ - ArrayList thePositions = new ArrayList(); - thePositions.add(new Integer(i)); - dateToPosition.put(creationDate,thePositions); - } - } - Set keys = dateToPosition.keySet(); - ArrayList keyList= new ArrayList(keys); - Collections.sort(keyList); - if (sortBy.equals("date_desc")){ - Collections.reverse(keyList); - } - else{ - if (!sortBy.equals("date_asc")){ - throw new ServletModuleExc("don't know how to sort by: "+ sortBy); - } + } + else { + // then we'll sort by date! + Map dateToPosition = new HashMap(end, 1.0F); //we know how big it will be + for (int i = start; i < end; i++) { + String creationDate = (hits.doc(i)).get("creationDate"); + // do a little dance in case two contents created at the same second! + if (dateToPosition.containsKey(creationDate)) { + ( (ArrayList) (dateToPosition.get(creationDate))).add(new Integer(i)); } - ListIterator keyTraverser = keyList.listIterator(); - while (keyTraverser.hasNext()){ - ArrayList positions = (ArrayList)dateToPosition.get((keyTraverser.next())); - ListIterator positionsTraverser=positions.listIterator(); - while (positionsTraverser.hasNext()){ - theDocumentsSorted.add(hits.doc(((Integer)(positionsTraverser.next())).intValue())); - } + else { + ArrayList thePositions = new ArrayList(); + thePositions.add(new Integer(i)); + dateToPosition.put(creationDate, thePositions); } } - - try{ - searcher.close(); + Set keys = dateToPosition.keySet(); + ArrayList keyList = new ArrayList(keys); + Collections.sort(keyList); + if (sortBy.equals("date_desc")) { + Collections.reverse(keyList); + } + else { + if (!sortBy.equals("date_asc")) { + throw new ServletModuleExc("don't know how to sort by: " + sortBy); + } } - catch (IOException e){ - logger.debug("Can't close searcher: " + e.toString()); - throw new ServletModuleFailure("Problem closing searcher(normal):" + e.getMessage(), e); + ListIterator keyTraverser = keyList.listIterator(); + while (keyTraverser.hasNext()) { + ArrayList positions = (ArrayList) dateToPosition.get( (keyTraverser.next())); + ListIterator positionsTraverser = positions.listIterator(); + while (positionsTraverser.hasNext()) { + theDocumentsSorted.add(hits.doc( ( (Integer) (positionsTraverser.next())).intValue())); + } } + } - - session.removeAttribute("numberOfHits"); - session.removeAttribute("theDocumentsSorted"); - session.removeAttribute("positionInResults"); - - session.setAttribute("numberOfHits",new Integer(end)); - session.setAttribute("theDocumentsSorted",theDocumentsSorted); - session.setAttribute("positionInResults",new Integer(0)); - + try { + searcher.close(); } - catch (IOException e){ + catch (IOException e) { logger.debug("Can't close searcher: " + e.toString()); - throw new ServletModuleFailure("Problem closing searcher: " + e.getMessage(), e); + throw new ServletModuleFailure("Problem closing searcher(normal):" + e.getMessage(), e); } - } - } - } - try { - ArrayList theDocs = (ArrayList)session.getAttribute("theDocumentsSorted"); - if (theDocs != null){ + session.removeAttribute("numberOfHits"); + session.removeAttribute("theDocumentsSorted"); + session.removeAttribute("positionInResults"); - mergeData.put("numberOfHits", ((Integer)session.getAttribute("numberOfHits")).toString()); - List theHits = new Vector(); - int pIR=((Integer)session.getAttribute("positionInResults")).intValue(); - int terminus; - int numHits=((Integer)session.getAttribute("numberOfHits")).intValue(); + session.setAttribute("numberOfHits", new Integer(end)); + session.setAttribute("theDocumentsSorted", theDocumentsSorted); + session.setAttribute("positionInResults", new Integer(0)); - if (!(pIR+increment>=numHits)){ - mergeData.put("hasNext","y"); - } - else { - mergeData.put("hasNext", null); - } - if (pIR>0){ - mergeData.put("hasPrevious","y"); - } - else { - mergeData.put("hasPrevious", null); - } - - if ((pIR+increment)>numHits){ - terminus=numHits; - } - else { - terminus=pIR+increment; } - for(int i = pIR; i < terminus; i++) { - Map h = new HashMap(); - Document theHit = (Document)theDocs.get(i); - whereTerm.returnMeta(h,theHit); - creatorTerm.returnMeta(h,theHit); - titleTerm.returnMeta(h,theHit); - descriptionTerm.returnMeta(h,theHit); - dateTerm.returnMeta(h,theHit); - imagesTerm.returnMeta(h,theHit); - audioTerm.returnMeta(h,theHit); - videoTerm.returnMeta(h,theHit); - theHits.add(h); + catch (IOException e) { + logger.debug("Can't close searcher: " + e.toString()); + throw new ServletModuleFailure("Problem closing searcher: " + e.getMessage(), e); } - mergeData.put("hits",theHits); } } - catch (Throwable e) { - logger.error("Can't iterate over hits: " + e.toString()); + } - throw new ServletModuleFailure("Problem getting hits: " + e.getMessage(), e); - } + try { + ArrayList theDocs = (ArrayList) session.getAttribute("theDocumentsSorted"); + if (theDocs != null) { + + mergeData.put("numberOfHits", ( (Integer) session.getAttribute("numberOfHits")).toString()); + List theHits = new Vector(); + int pIR = ( (Integer) session.getAttribute("positionInResults")).intValue(); + int terminus; + int numHits = ( (Integer) session.getAttribute("numberOfHits")).intValue(); - mergeData.put("queryString",queryString); + if (! (pIR + increment >= numHits)) { + mergeData.put("hasNext", "y"); + } + else { + mergeData.put("hasNext", null); + } + if (pIR > 0) { + mergeData.put("hasPrevious", "y"); + } + else { + mergeData.put("hasPrevious", null); + } - deliver(req, res, mergeData, null, searchResultsTemplate); + if ( (pIR + increment) > numHits) { + terminus = numHits; + } + else { + terminus = pIR + increment; + } + for (int i = pIR; i < terminus; i++) { + Map h = new HashMap(); + Document theHit = (Document) theDocs.get(i); + whereTerm.returnMeta(h, theHit); + creatorTerm.returnMeta(h, theHit); + titleTerm.returnMeta(h, theHit); + descriptionTerm.returnMeta(h, theHit); + dateTerm.returnMeta(h, theHit); + imagesTerm.returnMeta(h, theHit); + audioTerm.returnMeta(h, theHit); + videoTerm.returnMeta(h, theHit); + theHits.add(h); + } + mergeData.put("hits", theHits); + } } - catch (NullPointerException n){ - throw new ServletModuleFailure("Null Pointer: "+n.toString(), n); + catch (Throwable e) { + logger.error("Can't iterate over hits: " + e.toString()); + + throw new ServletModuleFailure("Problem getting hits: " + e.getMessage(), e); } + + mergeData.put("queryString", queryString); + + deliver(req, res, mergeData, null, searchResultsTemplate); + } + catch (NullPointerException n) { + throw new ServletModuleFailure("Null Pointer: " + n.toString(), n); } + } - /* - * Method for dynamically generating a pdf using iText - */ + /* + * Method for dynamically generating a pdf using iText + */ + public void getpdf(HttpServletRequest req, HttpServletResponse res) + throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { + long starttime=System.currentTimeMillis(); + String ID_REQUEST_PARAM = "id"; + int maxArticlesInNewsleter = 15; // it is nice not to be dos'ed + try { + String idParam = req.getParameter(ID_REQUEST_PARAM); + if (idParam != null) { - public void getpdf(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure { - String ID_REQUEST_PARAM = "id"; - try { - String idParam = req.getParameter(ID_REQUEST_PARAM); - if (idParam != null) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - PDFGenerator pdfMaker = new PDFGenerator(out); - RE re = new RE("[0-9]+"); + RE re = new RE("[0-9]+"); - REMatch[] idMatches=re.getAllMatches(idParam); + REMatch[] idMatches=re.getAllMatches(idParam); - if (idMatches.length > 1){ - pdfMaker.addLine(); - for (int i = 0; i < idMatches.length; i++){ - REMatch aMatch = idMatches[i]; - String id=aMatch.toString(); - EntityContent contentEnt = (EntityContent)contentModule.getById(id); - pdfMaker.addIndexItem(contentEnt); + String cacheSelector=""; - } - } + for (int i = 0; i < idMatches.length; i++){ + cacheSelector= cacheSelector + "," + idMatches[i].toString(); + } - for (int i = 0; i < idMatches.length; i++){ - REMatch aMatch = idMatches[i]; + String cacheType="pdf"; - String id=aMatch.toString(); + CacheKey theCacheKey = new CacheKey(cacheType,cacheSelector); - EntityContent contentEnt = (EntityContent)contentModule.getById(id); - pdfMaker.add(contentEnt); + byte[] thePDF; - } + if (MirGlobal.mruCache().hasObject(theCacheKey)){ + logger.info("fetching pdf from cache"); + thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey); + } + else { + logger.info("generating pdf and caching it"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + PDFGenerator pdfMaker = new PDFGenerator(out); + + if (idMatches.length > 1){ + pdfMaker.addLine(); + for (int i = 0; i < idMatches.length && i < maxArticlesInNewsleter; i++){ + REMatch aMatch = idMatches[i]; + String id=aMatch.toString(); + EntityContent contentEnt = (EntityContent)contentModule.getById(id); + pdfMaker.addIndexItem(contentEnt); + } + } - pdfMaker.stop(); - res.setContentType("application/pdf"); - byte[] content = out.toByteArray(); - res.setContentLength(content.length); - res.getOutputStream().write(content); - res.getOutputStream().flush(); + for (int i = 0; i < idMatches.length; i++){ + REMatch aMatch = idMatches[i]; + String id=aMatch.toString(); + EntityContent contentEnt = (EntityContent)contentModule.getById(id); + + pdfMaker.add(contentEnt); + } + + pdfMaker.stop(); + thePDF = out.toByteArray(); + + //and save all our hard work! + MirGlobal.mruCache().storeObject(theCacheKey,thePDF); + } + + res.setContentType("application/pdf"); + res.setContentLength(thePDF.length); + res.getOutputStream().write(thePDF); + res.getOutputStream().flush(); + String elapsedtime=(new Long(System.currentTimeMillis()-starttime)).toString(); + logger.info("pdf retireval took "+elapsedtime + " milliseconds" ); - } - else { - throw new ServletModuleExc("Missing id."); - } } - catch (Throwable t) { - logger.error(t.toString()); - throw new ServletModuleFailure(t); + else { + throw new ServletModuleExc("Missing id."); } - } + catch (Throwable t) { + logger.error(t.toString()); + throw new ServletModuleFailure(t); + } + + } + + public String generateOnetimePassword() { Random r = new Random(); int random = r.nextInt(); @@ -1056,8 +1078,7 @@ public class ServletModuleOpenIndy extends ServletModule return returnString.substring(5); } - public void deliver(HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator) - throws ServletModuleFailure { + public void deliver(HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator) throws ServletModuleFailure { try { deliver(aResponse.getWriter(), aRequest, aResponse, aData, anExtra, aGenerator); } @@ -1069,7 +1090,27 @@ public class ServletModuleOpenIndy extends ServletModule public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator) throws ServletModuleFailure { try { - Map responseData = ServletHelper.makeGenerationData(new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open"); + Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open"); + responseData.put("data", aData); + responseData.put("extra", anExtra); + + + Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator); + generator.generate(anOutputWriter, responseData, logger); + + anOutputWriter.close(); + } + catch (Throwable e) { + logger.error("Error while generating " + aGenerator + ": " + e.getMessage()); + + throw new ServletModuleFailure(e); + } + } + + public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator,String aLocaleString) + throws ServletModuleFailure { + try { + Map responseData = ServletHelper.makeGenerationData(aResponse, new Locale[] { new Locale(aLocaleString,""), getFallbackLocale(aRequest)}, "bundles.open"); responseData.put("data", aData); responseData.put("extra", anExtra); @@ -1086,6 +1127,7 @@ public class ServletModuleOpenIndy extends ServletModule } } + public void handleError(HttpServletRequest aRequest, HttpServletResponse aResponse,PrintWriter out, Throwable anException) { try { logger.error("error: " + anException); @@ -1108,9 +1150,7 @@ public class ServletModuleOpenIndy extends ServletModule Map data = new HashMap(); MessageResources messages = MessageResources.getMessageResources("bundles.open"); - data.put("errorstring", - messages.getMessage(getLocale(aRequest), anException.getMessage(), anException.getParameters()) - ); + data.put("errorstring", messages.getMessage(getLocale(aRequest), anException.getMessage(), anException.getParameters())); data.put("date", StringUtil.date2readableDateTime(new GregorianCalendar())); deliver(out, aRequest, aResponse, data, null, configuration.getString("ServletModule.OpenIndy.UserErrorTemplate"));