- if(cid==null){
- //How do we know that it was not succesful cause of a
- //dupe, what if it failed cause of "No space left on device"?
- //Or is there something I am missing? Wouldn't it be better
- //to have an explicit dupe check and then insert? I have no
- //idea what I am talking about. this comment is in case
- //I forget to explicitely ask. -mh
- deliver(req, res, mergeData, postingFormDupeTemplate);
- }
-
- String[] to_topicsArr = mp.getParameterValues("to_topic");
- if (to_topicsArr != null && to_topicsArr.length > 0) {
- try{
- DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
- theLog.printError("setting content_x_topic success");
- } catch (Exception e) {
- theLog.printError("setting content_x_topic failed");
- } //end try
- } //end if
-
- // if op contains uploaddata
- String mediaId=null;
- int i=1;
- for(Iterator it = mp.requestList.iterator(); it.hasNext();){
- MpRequest mpReq = (MpRequest)it.next();
- String fileName = mpReq.getFilename();
-
- //This is just a temporary way to get the content-type via
- //the .extension , we need to use a magic method, by looking
- //at the header (first few bytes) of the file.
- //the Oreilly method sucks cause it relies on the
- //content-type the client browser sends and that's
- //too often application-octet stream. -mh
- String contentType = FileUtil.guessContentTypeFromName(fileName);
- HashMap mediaValues = new HashMap();
-
- theLog.printError("CONTENT TYPE IS: "+contentType);
-
- //The map file should be Mir/content-types.properties, it's the
- //default Sun Java file+ some entries that it did not have.
- //so if you support a new media type you have to make sure that
- //it is in this file -mh
- if ((contentType==null) || (contentType=="application/octet-stream")) {
- throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
+ if (cid == null) {
+ deliver(aRequest, aResponse, mergeData, null, postingFormDupeTemplate);
+ return;
+ }
+
+ List topics = parsedRequest.getParameterList("to_topic");
+ if (topics.size() > 0) {
+ try {
+ DatabaseContentToTopics.getInstance().setTopics(cid, topics);
+ }
+ catch (Throwable e) {
+ logger.error("setting content_x_topic failed");
+ contentModule.deleteById(cid);
+ throw new ServletModuleFailure(
+ "smod - openindy :: insposting: setting content_x_topic failed: " +
+ e.toString(), e);
+ }
+ }
+
+ i = mediaList.iterator();
+ while (i.hasNext()) {
+ Entity mediaEnt = (Entity) i.next();
+ DatabaseContentToMedia.getInstance().addMedia(cid, mediaEnt.getId());
+ }
+
+ EntityContent article = (EntityContent) contentModule.getById(cid);
+ try {
+ MirGlobal.abuse().checkArticle(
+ article, new HTTPAdapters.HTTPRequestAdapter(aRequest), aResponse);
+ MirGlobal.localizer().openPostings().afterContentPosting(article);
+ }
+ catch (Throwable t) {
+ logger.error("Error while post-processing article: " + t.getMessage());
+ }
+ deliver(aRequest, aResponse, mergeData, null, postingFormDoneTemplate);
+ }
+ catch (Throwable e) {
+ e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+ Throwable cause = ExceptionFunctions.traceCauseException(e);
+
+ if (cause instanceof ModuleMediaType.UnsupportedMimeTypeExc) {
+ throw new ServletModuleUserExc("media.unsupportedformat", new String[] {});
+ }
+ throw new ServletModuleFailure(e);
+ }
+ }
+
+ /**
+ * Due to a serious shortcoming of Tomcat 3.3, an extra sessionid parameter is
+ * generated into open session urls. Tomcat 3.3 makes it impossible to
+ * distinguish between sessions that are identified using a url and those
+ * that are identified using cookies: if both a sessionid cookie and a sessionid
+ * url are available, tomcat 3.3 pretends the url wasn't there...
+ */
+ 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.
+ *
+ * @param aRequest
+ * @param aResponse
+ * @throws ServletModuleExc
+ * @throws ServletModuleUserExc
+ * @throws ServletModuleFailure
+ */
+
+ public void opensession(HttpServletRequest aRequest, HttpServletResponse aResponse)
+ throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
+
+ try {
+ 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)))
+ aRequest.getSession().invalidate();
+
+ Session session = new HTTPAdapters.HTTPSessionAdapter(aRequest.getSession());
+
+ SimpleResponse response = new SimpleResponse(
+ ServletHelper.makeGenerationData(aRequest, aResponse, new Locale[] { getResponseLocale(aRequest.getSession(), aRequest), getFallbackLocale(aRequest)},
+ "bundles.open"));
+
+ 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);
+
+ handler.processRequest(request, session, response);
+ ServletHelper.generateOpenPostingResponse(aResponse.getWriter(), response.getResponseValues(), response.getResponseGenerator());
+ }
+ catch (Throwable t) {
+ logger.error(t.toString());
+ t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+
+ throw new ServletModuleFailure(t);
+ }
+ }
+
+ /**
+ * Method for preparing and sending a content as an email message
+ */
+
+ public void mail(HttpServletRequest req, HttpServletResponse res)
+ throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure
+ {
+ String aid = req.getParameter("mail_aid");
+ if (aid == null){
+ throw new ServletModuleExc("An article id must be specified in requests to email an article. Something therefore went badly wrong....");
+ }
+
+ 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("")){
+ deliver(req, res, mergeData, null, prepareMailTemplate);
+ }
+ else {
+ //run checks on to and from and mail_language to make sure no monkey business occurring
+ if (mail_language.indexOf('.') != -1 || mail_language.indexOf('/') != -1 ) {
+ throw new ServletModuleExc("Invalid language");
+ }
+ if (to.indexOf('\n') != -1
+ || to.indexOf('\r') != -1
+ || to.indexOf(',') != -1) {
+ throw new ServletModuleUserExc("email.error.invalidtoaddress", new String[] {to});
+ }
+ 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);
+ }
+ 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 + "\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;
+ try {
+ content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!",commentTextToInsert);
+ }
+ catch (Throwable e){
+ throw new ServletModuleFailure("Problem doing regular expression replacement " + e.toString(), e);
+ }
+ }
+ else{
+ try {
+ content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!","");
+ }
+ catch (Throwable e){
+ throw new ServletModuleFailure("Problem doing regular expression replacement " + e.toString(), e);
+ }
+ }
+
+ SMTPClient client=new SMTPClient();
+ try {
+ int reply;
+ client.connect(configuration.getString("ServletModule.OpenIndy.SMTPServer"));
+
+ reply = client.getReplyCode();
+
+ if (!SMTPReply.isPositiveCompletion(reply)) {
+ client.disconnect();
+ throw new ServletModuleExc("SMTP server refused connection.");