X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fservlet%2FServletModuleOpenIndy.java;h=6172d1c05dfb55a1c3179499f4579f0d2f56fceb;hb=aa507bfd18e723d21e63454a26af3320bb8c27f2;hp=f7d0bf9fc0cd6c7cdeb5fdf3f3933d8fb3842015;hpb=b721133f7b4c615c4af6230102238b9b13882eec;p=mir.git diff --git a/source/mircoders/servlet/ServletModuleOpenIndy.java b/source/mircoders/servlet/ServletModuleOpenIndy.java index f7d0bf9f..6172d1c0 100755 --- a/source/mircoders/servlet/ServletModuleOpenIndy.java +++ b/source/mircoders/servlet/ServletModuleOpenIndy.java @@ -30,17 +30,16 @@ package mircoders.servlet; +import gnu.regexp.RE; +import gnu.regexp.REMatch; + 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; @@ -51,24 +50,10 @@ import java.util.Map; import java.util.Random; import java.util.Set; import java.util.Vector; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpUtils; - -import org.apache.commons.fileupload.FileItem; -import org.apache.commons.net.smtp.SMTPClient; -import org.apache.commons.net.smtp.SMTPReply; -import org.apache.lucene.analysis.standard.StandardAnalyzer; -import org.apache.lucene.document.Document; -import org.apache.lucene.queryParser.QueryParser; -import org.apache.lucene.search.Hits; -import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.Searcher; -import org.apache.struts.util.MessageResources; -import gnu.regexp.RE; -import gnu.regexp.REMatch; import mir.entity.Entity; import mir.generator.Generator; @@ -105,6 +90,7 @@ import mircoders.search.ContentSearchTerm; import mircoders.search.ImagesSearchTerm; import mircoders.search.KeywordSearchTerm; import mircoders.search.TextSearchTerm; +import mircoders.search.TopicMatrixSearchTerm; import mircoders.search.TopicSearchTerm; import mircoders.search.UnIndexedSearchTerm; import mircoders.search.VideoSearchTerm; @@ -116,6 +102,18 @@ import mircoders.storage.DatabaseImages; import mircoders.storage.DatabaseLanguage; import mircoders.storage.DatabaseTopics; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.net.smtp.SMTPClient; +import org.apache.commons.net.smtp.SMTPReply; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.queryParser.QueryParser; +import org.apache.lucene.search.Hits; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.Searcher; +import org.apache.struts.util.MessageResources; + /* * ServletModuleOpenIndy - * is the open-access-servlet, which is responsible for @@ -123,7 +121,7 @@ import mircoders.storage.DatabaseTopics; * open-postings to the newswire * * @author mir-coders group - * @version $Id: ServletModuleOpenIndy.java,v 1.88 2003/05/06 12:22:07 john Exp $ + * @version $Id: ServletModuleOpenIndy.java,v 1.89.2.8 2003/10/23 14:55:25 rk Exp $ * */ @@ -357,7 +355,7 @@ public class ServletModuleOpenIndy extends ServletModule Map extraInfo = new HashMap(); extraInfo.put("languagePopUpData", DatabaseLanguage.getInstance().getPopupData()); - extraInfo.put("themenPopupData", topicsModule.getTopicsAsSimpleList()); + extraInfo.put("themenPopupData", DatabaseTopics.getInstance().getPopupData()); extraInfo.put("topics", topicsModule.getTopicsList()); deliver(req, res, mergeData, extraInfo, postingFormTemplate); @@ -527,6 +525,35 @@ public class ServletModuleOpenIndy extends ServletModule private static final String SESSION_REQUEST_KEY="sessionid"; /** + * Selects the language for the response. + * + * @param session + * @param aRequest + */ + + protected Locale getResponseLocale(HttpSession aSession, HttpServletRequest aRequest) { + String requestLanguage = aRequest.getParameter("language"); + String sessionLanguage = (String) aSession.getAttribute("language"); + String acceptLanguage = aRequest.getLocale().getLanguage(); + String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en"); + + String language = requestLanguage; + + if (language==null) + language = sessionLanguage; + + if (language==null) + language = acceptLanguage; + + if (language==null) + language = defaultLanguage; + + aSession.setAttribute("language", language); + + return new Locale(language, ""); + } + + /** * Dispatch method for open sessions: a flexible extensible and customizable way * for open access. Can be used for postings, but also for lots of other stuff. * @@ -554,10 +581,10 @@ 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(aRequest, aResponse, new Locale[] { getResponseLocale(aRequest.getSession(), aRequest), getFallbackLocale(aRequest)}, "bundles.open")); - response.setResponseValue("actionURL", aResponse.encodeURL(HttpUtils.getRequestURL(aRequest).toString())+"?"+SESSION_REQUEST_KEY+"="+aRequest.getSession().getId()); + response.setResponseValue("actionURL", aResponse.encodeURL(MirGlobal.config().getString("RootUri") + "/servlet/OpenMir")+"?"+SESSION_REQUEST_KEY+"="+aRequest.getSession().getId()); SessionHandler handler = MirGlobal.localizer().openPostings().getOpenSessionHandler(request, session); @@ -587,6 +614,7 @@ 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"); @@ -597,8 +625,8 @@ public class ServletModuleOpenIndy extends ServletModule 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("")){ deliver(req, res, mergeData, null, prepareMailTemplate); } @@ -615,37 +643,37 @@ public class ServletModuleOpenIndy extends ServletModule if (from.indexOf('\n') != -1 || from.indexOf('\r') != -1 || from.indexOf(',') != -1 ) { throw new ServletModuleUserExc("email.error.invalidfromaddress", new String[] {from}); } - + CacheKey theCacheKey=new CacheKey("email",aid+mail_language); String theEmailText; - + if (MirGlobal.mruCache().hasObject(theCacheKey)){ - logger.info("fetching email text for article "+aid+" from cache"); - theEmailText = (String) MirGlobal.mruCache().getObject(theCacheKey); + logger.info("fetching email text for article "+aid+" from cache"); + theEmailText = (String) MirGlobal.mruCache().getObject(theCacheKey); } 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); - } + 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; @@ -731,6 +759,7 @@ public class ServletModuleOpenIndy extends ServletModule TextSearchTerm descriptionTerm = new TextSearchTerm("description", "search_content", "description", "description", "description"); ContentSearchTerm contentTerm = new ContentSearchTerm("content_data", "search_content", "content", "", ""); TopicSearchTerm topicTerm = new TopicSearchTerm(); + TopicMatrixSearchTerm topicMatrixTerm = new TopicMatrixSearchTerm(); ImagesSearchTerm imagesTerm = new ImagesSearchTerm(); AudioSearchTerm audioTerm = new AudioSearchTerm(); VideoSearchTerm videoTerm = new VideoSearchTerm(); @@ -745,7 +774,7 @@ public class ServletModuleOpenIndy extends ServletModule } try { - mergeData.put("topics", topicsModule.getTopicsAsSimpleList()); + mergeData.put("topics", DatabaseTopics.getInstance().getPopupData()); } catch (Throwable e) { logger.debug("Can't get topics: " + e.toString()); @@ -795,6 +824,11 @@ public class ServletModuleOpenIndy extends ServletModule queryString = queryString + " +" + topicFragment; } + String topicMatrixFragment = topicMatrixTerm.makeTerm(req); + if (topicMatrixFragment != null) { + queryString = queryString + " +" + topicMatrixFragment; + } + String imagesFragment = imagesTerm.makeTerm(req); if (imagesFragment != null) { queryString = queryString + " +" + imagesFragment; @@ -993,69 +1027,69 @@ public class ServletModuleOpenIndy extends ServletModule try { String idParam = req.getParameter(ID_REQUEST_PARAM); if (idParam != null) { - - - RE re = new RE("[0-9]+"); - - - REMatch[] idMatches=re.getAllMatches(idParam); - - String cacheSelector=""; - - for (int i = 0; i < idMatches.length; i++){ - cacheSelector= cacheSelector + "," + idMatches[i].toString(); - } - - String cacheType="pdf"; - - CacheKey theCacheKey = new CacheKey(cacheType,cacheSelector); - - byte[] thePDF; - - if (MirGlobal.mruCache().hasObject(theCacheKey)){ - logger.info("fetching pdf from cache"); - thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey); - } - else { - logger.info("generating pdf and caching it"); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - PDFGenerator pdfMaker = new PDFGenerator(out); - - if (idMatches.length > 1){ - pdfMaker.addLine(); - for (int i = 0; i < idMatches.length && i < maxArticlesInNewsleter; i++){ - REMatch aMatch = idMatches[i]; - String id=aMatch.toString(); - EntityContent contentEnt = (EntityContent)contentModule.getById(id); - pdfMaker.addIndexItem(contentEnt); - } - } - - for (int i = 0; i < idMatches.length; i++){ - REMatch aMatch = idMatches[i]; - String id=aMatch.toString(); - EntityContent contentEnt = (EntityContent)contentModule.getById(id); - - pdfMaker.add(contentEnt); - } - - pdfMaker.stop(); - thePDF = out.toByteArray(); - - //and save all our hard work! - MirGlobal.mruCache().storeObject(theCacheKey,thePDF); - } - - res.setContentType("application/pdf"); - res.setContentLength(thePDF.length); - res.getOutputStream().write(thePDF); - res.getOutputStream().flush(); - String elapsedtime=(new Long(System.currentTimeMillis()-starttime)).toString(); - logger.info("pdf retireval took "+elapsedtime + " milliseconds" ); + + + RE re = new RE("[0-9]+"); + + + REMatch[] idMatches=re.getAllMatches(idParam); + + String cacheSelector=""; + + for (int i = 0; i < idMatches.length; i++){ + cacheSelector= cacheSelector + "," + idMatches[i].toString(); + } + + String cacheType="pdf"; + + CacheKey theCacheKey = new CacheKey(cacheType,cacheSelector); + + byte[] thePDF; + + if (MirGlobal.mruCache().hasObject(theCacheKey)){ + logger.info("fetching pdf from cache"); + thePDF = (byte[]) MirGlobal.mruCache().getObject(theCacheKey); + } + else { + logger.info("generating pdf and caching it"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + PDFGenerator pdfMaker = new PDFGenerator(out); + + if (idMatches.length > 1){ + pdfMaker.addLine(); + for (int i = 0; i < idMatches.length && i < maxArticlesInNewsleter; i++){ + REMatch aMatch = idMatches[i]; + String id=aMatch.toString(); + EntityContent contentEnt = (EntityContent)contentModule.getById(id); + pdfMaker.addIndexItem(contentEnt); + } + } + + for (int i = 0; i < idMatches.length; i++){ + REMatch aMatch = idMatches[i]; + String id=aMatch.toString(); + EntityContent contentEnt = (EntityContent)contentModule.getById(id); + + pdfMaker.add(contentEnt); + } + + pdfMaker.stop(); + thePDF = out.toByteArray(); + + //and save all our hard work! + MirGlobal.mruCache().storeObject(theCacheKey,thePDF); + } + + res.setContentType("application/pdf"); + res.setContentLength(thePDF.length); + res.getOutputStream().write(thePDF); + res.getOutputStream().flush(); + String elapsedtime=(new Long(System.currentTimeMillis()-starttime)).toString(); + logger.info("pdf retireval took "+elapsedtime + " milliseconds" ); } else { - throw new ServletModuleExc("Missing id."); + throw new ServletModuleExc("Missing id."); } } catch (Throwable t) { @@ -1064,7 +1098,7 @@ public class ServletModuleOpenIndy extends ServletModule } } - + public String generateOnetimePassword() { Random r = new Random(); @@ -1093,7 +1127,7 @@ 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(aRequest, aResponse, new Locale[] { getLocale(aRequest), getFallbackLocale(aRequest)}, "bundles.open"); responseData.put("data", aData); responseData.put("extra", anExtra); @@ -1113,7 +1147,7 @@ public class ServletModuleOpenIndy extends ServletModule public void deliver(PrintWriter anOutputWriter, HttpServletRequest aRequest, HttpServletResponse aResponse, Map aData, Map anExtra, String aGenerator,String aLocaleString) throws ServletModuleFailure { try { - Map responseData = ServletHelper.makeGenerationData(new Locale[] { new Locale(aLocaleString,""), getFallbackLocale(aRequest)}, "bundles.open"); + Map responseData = ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { new Locale(aLocaleString,""), getFallbackLocale(aRequest)}, "bundles.open"); responseData.put("data", aData); responseData.put("extra", anExtra);