From b865b9333cb77f34a540a5ec102687af24f5cf24 Mon Sep 17 00:00:00 2001 From: zapata Date: Thu, 1 May 2003 01:42:11 +0000 Subject: [PATCH] - major bugfix: many many threads were being created until a "can't create native thread" exception --- source/Mir.java | 41 +++++--------- source/OpenMir.java | 18 +++---- source/default.properties | 2 +- .../localizer/MirCachingLocalizerDecorator.java | 63 +++++++++++++++++++--- .../basic/MirBasicAdminInterfaceLocalizer.java | 15 +++--- .../basic/MirBasicArticlePostingHandler.java | 63 ++++++++-------------- .../basic/MirBasicChildArticlePostingHandler.java | 62 +++++++++++++++++++++ .../basic/MirBasicCommentPostingHandler.java | 42 +++++---------- .../basic/MirBasicGeneratorLocalizer.java | 1 - .../basic/MirBasicPostingSessionHandler.java | 61 ++++++++++++++++++--- 10 files changed, 235 insertions(+), 133 deletions(-) create mode 100755 source/mircoders/localizer/basic/MirBasicChildArticlePostingHandler.java diff --git a/source/Mir.java b/source/Mir.java index 820c41b6..c67ecfcb 100755 --- a/source/Mir.java +++ b/source/Mir.java @@ -80,7 +80,7 @@ import mircoders.storage.DatabaseUsers; * Mir.java - main servlet, that dispatches to servletmodules * * @author $Author: zapata $ - * @version $Id: Mir.java,v 1.47 2003/04/28 01:57:14 zapata Exp $ + * @version $Id: Mir.java,v 1.48 2003/05/01 01:42:11 zapata Exp $ * */ public class Mir extends AbstractServlet { @@ -179,33 +179,23 @@ public class Mir extends AbstractServlet { if (aRequest.getServerPort() == 443) { http = "https"; - } else { + } + else { http = "http"; } - //make sure client browsers don't cache anything setNoCaching(aResponse); - //FIXME: this seems kind of hackish and only here because we can have - // default other than the one that the browser is set to. Locale locale = new Locale(getDefaultLanguage(aRequest), ""); - String htmlcharset = "UTF-8"; - try { - htmlcharset = MirPropertiesConfiguration.instance().getString("Mir.DefaultHTMLCharset"); - } - catch (Throwable t) { - } - aResponse.setContentType("text/html; charset=" + htmlcharset); + aResponse.setContentType("text/html; charset=" + + configuration.getString("Mir.DefaultHTMLCharset", "UTF-8")); String moduleName = aRequest.getParameter("module"); checkLanguage(session, aRequest); - - // Authentication - if (((moduleName != null) && moduleName.equals("login")) || - (userEntity == null)) { + if (((moduleName != null) && moduleName.equals("login")) || (userEntity == null)) { String user = aRequest.getParameter("login"); String passwd = aRequest.getParameter("password"); logger.debug("--login: evaluating for user: " + user); @@ -269,8 +259,6 @@ public class Mir extends AbstractServlet { logger.info("--logout"); session.invalidate(); - //session = aRequest.getSession(true); - //checkLanguage(session, aRequest); _sendLoginPage(aResponse, aRequest, aResponse.getWriter()); return; @@ -282,9 +270,8 @@ public class Mir extends AbstractServlet { String redirectString = aRequest.getRequestURI(); String queryString = aRequest.getQueryString(); - if ((queryString != null) && !queryString.equals("")) { + if ((queryString != null) && queryString.length()!=0) { redirectString += ("?" + aRequest.getQueryString()); - logger.debug("STORING: " + redirectString); session.setAttribute("login.target", redirectString); } @@ -295,14 +282,12 @@ public class Mir extends AbstractServlet { // If no module is specified goto standard startpage if ((moduleName == null) || moduleName.equals("")) { - logger.debug("no module: redirect to standardpage"); +// logger.debug("no module: redirect to standardpage"); _sendStartPage(aResponse, aRequest, aResponse.getWriter(), userEntity); return; } - // end of auth - // From now on regular dispatching... try { // get servletmodule by parameter and continue with dispacher ServletModule smod = getServletModuleForName(moduleName); @@ -386,8 +371,8 @@ public class Mir extends AbstractServlet { fallbackLocale); out.close(); } - catch (Exception e) { - logger.error("Error in UserErrorTemplate"); + catch (Throwable e) { + logger.error("Error handling user error" + e.toString()); } } @@ -405,8 +390,8 @@ public class Mir extends AbstractServlet { modelRoot,null,out, getLocale(aRequest), getFallbackLocale()); out.close(); } - catch (Exception e) { - logger.error("Error in ErrorTemplate"); + catch (Throwable e) { + logger.error("Error handling error: " + e.toString()); } } @@ -473,7 +458,7 @@ public class Mir extends AbstractServlet { mergeData.put("searchorder", null); mergeData.put("selectarticleurl", null); - ServletHelper.generateResponse(aResponse.getWriter(), mergeData, startTemplate); + ServletHelper.generateResponse(out, mergeData, startTemplate); } catch (Exception e) { e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE)); diff --git a/source/OpenMir.java b/source/OpenMir.java index 8c57e54c..2a5b7ccc 100755 --- a/source/OpenMir.java +++ b/source/OpenMir.java @@ -18,13 +18,13 @@ * 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. */ @@ -47,7 +47,7 @@ import mircoders.servlet.ServletModuleOpenIndy; * OpenMir.java - main servlet for open posting and comment feature to articles * * @author RK 1999-2001, the mir-coders group - * @version $Id: OpenMir.java,v 1.34 2003/04/21 12:42:46 idfx Exp $ + * @version $Id: OpenMir.java,v 1.35 2003/05/01 01:42:11 zapata Exp $ * */ @@ -113,7 +113,7 @@ public class OpenMir extends AbstractServlet { String requestLanguage = aRequest.getParameter("language"); String sessionLanguage = (String) aSession.getAttribute("language"); String acceptLanguage = aRequest.getLocale().getLanguage(); - String defaultLanguage = MirGlobal.config().getString("Mir.Login.DefaultLanguage", "en"); + String defaultLanguage = configuration.getString("Mir.Login.DefaultLanguage", "en"); String language = requestLanguage; diff --git a/source/default.properties b/source/default.properties index 913e3486..6d40facc 100755 --- a/source/default.properties +++ b/source/default.properties @@ -162,7 +162,7 @@ Producer.StorageRoot=/pub/Dokumente/Indymedia/de-tech/Mir/produced # :::<0|1 (recursion off or on)> [, ....] ServletModule.FileEdit.Configuration= \ - includes:/pub/Dokumente/Indymedia/de-tech/Mir/produced:.*\\.inc:1 + includes:/pub/Dokumente/Indymedia/de-tech/Mir/produced/inc:.*\\.inc:1 # # diff --git a/source/mircoders/localizer/MirCachingLocalizerDecorator.java b/source/mircoders/localizer/MirCachingLocalizerDecorator.java index e6d55617..340d2bd5 100755 --- a/source/mircoders/localizer/MirCachingLocalizerDecorator.java +++ b/source/mircoders/localizer/MirCachingLocalizerDecorator.java @@ -18,18 +18,22 @@ * 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.localizer; +import mir.generator.Generator; +import mir.generator.WriterEngine; + + public class MirCachingLocalizerDecorator implements MirLocalizer { private MirLocalizer localizer; private MirProducerLocalizer producerLocalizer; @@ -53,7 +57,7 @@ public class MirCachingLocalizerDecorator implements MirLocalizer { public MirGeneratorLocalizer generators() throws MirLocalizerFailure, MirLocalizerExc { if (generatorLocalizer==null) { - generatorLocalizer = localizer.generators(); + generatorLocalizer = new MirCachingGeneratorLocalizer(localizer.generators()); } return generatorLocalizer; @@ -91,5 +95,48 @@ public class MirCachingLocalizerDecorator implements MirLocalizer { return adminInterfaceLocalizer; }; + private static class MirCachingGeneratorLocalizer implements MirGeneratorLocalizer { + private MirGeneratorLocalizer master; + private WriterEngine writerEngine; + private Generator.GeneratorLibrary producerGeneratorLibrary; + private Generator.GeneratorLibrary adminGeneratorLibrary; + private Generator.GeneratorLibrary openPostingGeneratorLibrary; + + public MirCachingGeneratorLocalizer(MirGeneratorLocalizer aMaster) { + master = aMaster; + } + + public WriterEngine makeWriterEngine() throws MirLocalizerExc, MirLocalizerFailure { + if (writerEngine==null) { + writerEngine = master.makeWriterEngine(); + } + + return writerEngine; + }; + + public Generator.GeneratorLibrary makeProducerGeneratorLibrary() throws MirLocalizerExc, MirLocalizerFailure { + if (producerGeneratorLibrary==null) { + producerGeneratorLibrary = master.makeProducerGeneratorLibrary(); + } + + return producerGeneratorLibrary; + }; + + public Generator.GeneratorLibrary makeAdminGeneratorLibrary() throws MirLocalizerExc, MirLocalizerFailure { + if (adminGeneratorLibrary==null) { + adminGeneratorLibrary = master.makeAdminGeneratorLibrary(); + } + + return adminGeneratorLibrary; + }; + + public Generator.GeneratorLibrary makeOpenPostingGeneratorLibrary() throws MirLocalizerExc, MirLocalizerFailure { + if (openPostingGeneratorLibrary==null) { + openPostingGeneratorLibrary = master.makeOpenPostingGeneratorLibrary(); + } + + return openPostingGeneratorLibrary; + }; + } } \ No newline at end of file diff --git a/source/mircoders/localizer/basic/MirBasicAdminInterfaceLocalizer.java b/source/mircoders/localizer/basic/MirBasicAdminInterfaceLocalizer.java index 313b9c75..97a3f285 100755 --- a/source/mircoders/localizer/basic/MirBasicAdminInterfaceLocalizer.java +++ b/source/mircoders/localizer/basic/MirBasicAdminInterfaceLocalizer.java @@ -18,15 +18,16 @@ * 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.localizer.basic; import java.text.SimpleDateFormat; diff --git a/source/mircoders/localizer/basic/MirBasicArticlePostingHandler.java b/source/mircoders/localizer/basic/MirBasicArticlePostingHandler.java index fe50c134..3923bcb7 100755 --- a/source/mircoders/localizer/basic/MirBasicArticlePostingHandler.java +++ b/source/mircoders/localizer/basic/MirBasicArticlePostingHandler.java @@ -27,6 +27,7 @@ * 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.localizer.basic; import java.util.GregorianCalendar; @@ -68,6 +69,16 @@ public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler protected DatabaseContentToMedia contentToMedia = DatabaseContentToMedia.getInstance(); protected DatabaseContent contentDatabase = DatabaseContent.getInstance(); + public MirBasicArticlePostingHandler() { + super(); + + setResponseGenerators( + configuration.getString("Localizer.OpenSession.article.EditTemplate"), + configuration.getString("Localizer.OpenSession.article.DupeTemplate"), + configuration.getString("Localizer.OpenSession.article.UnsupportedMediaTemplate"), + configuration.getString("Localizer.OpenSession.article.DoneTemplate")); + } + protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { super.initializeResponseData(aRequest, aSession, aResponse); @@ -86,14 +97,14 @@ public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler testFieldEntered(aRequest, "content_data", "validationerror.missing", aResults); } - public void finalizeArticle(Request aRequest, Session aSession, EntityContent aContent) throws SessionExc, SessionFailure { - aContent.setValueForProperty("is_published", "1"); - aContent.setValueForProperty("is_produced", "0"); - aContent.setValueForProperty("date", StringUtil.date2webdbDate(new GregorianCalendar())); - aContent.setValueForProperty("is_html","0"); - aContent.setValueForProperty("publish_path", StringUtil.webdbDate2path(aContent.getValue("date"))); - aContent.setValueForProperty("to_article_type", "1"); - aContent.setValueForProperty("to_publisher", "1"); + public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure { + anArticle.setValueForProperty("is_published", "1"); + anArticle.setValueForProperty("is_produced", "0"); + anArticle.setValueForProperty("date", StringUtil.date2webdbDate(new GregorianCalendar())); + anArticle.setValueForProperty("is_html","0"); + anArticle.setValueForProperty("publish_path", StringUtil.webdbDate2path(anArticle.getValue("date"))); + anArticle.setValueForProperty("to_article_type", "1"); + anArticle.setValueForProperty("to_publisher", "1"); } public void setArticleTopics(Request aRequest, Session aSession, EntityContent aContent) throws SessionExc, SessionFailure { @@ -122,7 +133,7 @@ public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler id = article.insert(); if (id == null) { logger.info("Duplicate article rejected"); - throw new DuplicateArticleExc("Duplicate article rejected"); + throw new DuplicatePostingExc("Duplicate article rejected"); } aSession.setAttribute("content", article); @@ -165,36 +176,4 @@ public class MirBasicArticlePostingHandler extends MirBasicPostingSessionHandler logger.info("article posted"); }; - protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.article.EditTemplate")); - }; - - protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure { - aResponse.setResponseValue("errors", anErrors); - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.article.EditTemplate")); - }; - - protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.article.DoneTemplate")); - }; - - protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure { - anError.printStackTrace(); - Throwable rootCause = ExceptionFunctions.traceCauseException(anError); - - if (rootCause instanceof DuplicateArticleExc) - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.article.DupeTemplate")); - if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) { - aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType()); - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.article.UnsupportedMediaTemplate")); - } - else - super.makeErrorResponse(aRequest, aSession, aResponse, anError); - }; - - protected static class DuplicateArticleExc extends SessionExc { - public DuplicateArticleExc(String aMessage) { - super(aMessage); - } - } -} +} \ No newline at end of file diff --git a/source/mircoders/localizer/basic/MirBasicChildArticlePostingHandler.java b/source/mircoders/localizer/basic/MirBasicChildArticlePostingHandler.java new file mode 100755 index 00000000..492d47c7 --- /dev/null +++ b/source/mircoders/localizer/basic/MirBasicChildArticlePostingHandler.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * 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. + * If you do not wish to do so, delete this exception statement from your version. + */ + +package mircoders.localizer.basic; + +import mir.session.*; +import mircoders.entity.*; + + +public class MirBasicChildArticlePostingHandler extends MirBasicArticlePostingHandler { + public MirBasicChildArticlePostingHandler() { + super(); + + setNormalResponseGenerator(configuration.getString("Localizer.OpenSession.article.EditTemplate")); + } + + public void finalizeArticle(Request aRequest, Session aSession, EntityContent anArticle) throws SessionExc, SessionFailure { + super.finalizeArticle(aRequest, aSession, anArticle); + + anArticle.setValueForProperty("to_media", (String) aSession.getAttribute("to_media")); + } + + protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure { + super.initializeSession(aRequest, aSession); + + String parentId = aRequest.getParameter("to_media"); + if (parentId==null) + throw new SessionExc("initializeSession: parent id not set!"); + + aSession.setAttribute("to_media", parentId); + }; + + + +} \ No newline at end of file diff --git a/source/mircoders/localizer/basic/MirBasicCommentPostingHandler.java b/source/mircoders/localizer/basic/MirBasicCommentPostingHandler.java index 6571dd3d..61edd556 100755 --- a/source/mircoders/localizer/basic/MirBasicCommentPostingHandler.java +++ b/source/mircoders/localizer/basic/MirBasicCommentPostingHandler.java @@ -65,6 +65,17 @@ public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler protected ModuleComment commentModule = new ModuleComment(DatabaseComment.getInstance()); protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance(); + + public MirBasicCommentPostingHandler() { + super(); + + setResponseGenerators( + configuration.getString("Localizer.OpenSession.comment.EditTemplate"), + configuration.getString("Localizer.OpenSession.comment.DupeTemplate"), + configuration.getString("Localizer.OpenSession.comment.DoneTemplate"), + configuration.getString("Localizer.OpenSession.comment.UnsupportedMediaTemplate")); + } + protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { super.initializeResponseData(aRequest, aSession, aResponse); @@ -86,7 +97,7 @@ public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler String articleId = aRequest.getParameter("to_media"); if (articleId==null) - throw new SessionExc("initializeCommentPosting: article id not set!"); + throw new SessionExc("initializeSession: article id not set!"); aSession.setAttribute("to_media", articleId); }; @@ -95,6 +106,7 @@ public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler aComment.setValueForProperty("is_published", "1"); aComment.setValueForProperty("to_comment_status", "1"); aComment.setValueForProperty("is_html","0"); + aComment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media")); } public void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure { @@ -104,7 +116,6 @@ public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler EntityComment comment = (EntityComment) commentModule.createNew(); comment.setValues(values); - comment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media")); finalizeComment(aRequest, aSession, comment); id = comment.insert(); if (id == null) { @@ -148,33 +159,6 @@ public class MirBasicCommentPostingHandler extends MirBasicPostingSessionHandler logger.info("Comment posted"); }; - protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate")); - }; - - protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure { - aResponse.setResponseValue("errors", anErrors); - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate")); - }; - - protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DoneTemplate")); - }; - - protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure { - anError.printStackTrace(); - Throwable rootCause = ExceptionFunctions.traceCauseException(anError); - - if (rootCause instanceof DuplicateCommentExc) - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate")); - if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) { - aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType()); - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.UnsupportedMediaTemplate")); - } - else - super.makeErrorResponse(aRequest, aSession, aResponse, anError); - }; - protected static class DuplicateCommentExc extends SessionExc { public DuplicateCommentExc(String aMessage) { super(aMessage); diff --git a/source/mircoders/localizer/basic/MirBasicGeneratorLocalizer.java b/source/mircoders/localizer/basic/MirBasicGeneratorLocalizer.java index c6e4cbb7..f3b205dc 100755 --- a/source/mircoders/localizer/basic/MirBasicGeneratorLocalizer.java +++ b/source/mircoders/localizer/basic/MirBasicGeneratorLocalizer.java @@ -47,7 +47,6 @@ public class MirBasicGeneratorLocalizer implements MirGeneratorLocalizer { repository = new GeneratorLibraryRepository(); logger = new LoggerWrapper("Localizer.Basic.Generator"); - buildRepository(repository); } diff --git a/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java b/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java index 0152cab1..e89d1192 100755 --- a/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java +++ b/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java @@ -56,6 +56,7 @@ import mircoders.module.ModuleComment; import mircoders.storage.DatabaseComment; import mircoders.storage.DatabaseCommentToMedia; import mircoders.storage.DatabaseContent; +import mircoders.module.*; /** * @@ -71,6 +72,12 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler { protected LoggerWrapper logger; protected MirPropertiesConfiguration configuration; + private String normalResponseGenerator; + private String dupeResponseGenerator; + private String unsupportedMediaTypeResponseGenerator; + private String finalResponseGenerator; + + public MirBasicPostingSessionHandler() { logger = new LoggerWrapper("Localizer.OpenPosting"); try { @@ -83,6 +90,18 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler { } } + protected void setNormalResponseGenerator(String aGenerator) { + normalResponseGenerator = aGenerator; + } + + protected void setResponseGenerators(String aNormalResponseGenerator, String aDupeResponseGenerator, + String anUnsupportedMediaTypeResponseGenerator, String aFinalResponseGenerator) { + setNormalResponseGenerator(aNormalResponseGenerator); + dupeResponseGenerator = aDupeResponseGenerator; + unsupportedMediaTypeResponseGenerator = anUnsupportedMediaTypeResponseGenerator; + finalResponseGenerator = aFinalResponseGenerator; + } + public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { if (aSession.getAttribute("initialRequest")==null) { initialRequest(aRequest, aSession, aResponse); @@ -170,9 +189,34 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler { aResponse.setResponseValue("errors", null); } - protected abstract void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure; - protected abstract void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure; - protected abstract void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure; + protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + aResponse.setResponseGenerator(normalResponseGenerator); + }; + + protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure { + aResponse.setResponseValue("errors", anErrors); + aResponse.setResponseGenerator(normalResponseGenerator); + }; + + protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + aResponse.setResponseGenerator(finalResponseGenerator); + }; + + protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure { + anError.printStackTrace(); + Throwable rootCause = ExceptionFunctions.traceCauseException(anError); + + if (rootCause instanceof DuplicatePostingExc) + aResponse.setResponseGenerator(dupeResponseGenerator); + if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) { + aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType()); + aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator); + } + else { + aResponse.setResponseValue("errorstring", anError.getMessage()); + aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate")); + } + }; protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure { }; @@ -202,11 +246,6 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler { protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure { } - protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure { - aResponse.setResponseValue("errorstring", anError.getMessage()); - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate")); - }; - /** * Class that represents a validation error * @@ -364,4 +403,10 @@ public abstract class MirBasicPostingSessionHandler implements SessionHandler { return result; } + protected static class DuplicatePostingExc extends SessionExc { + public DuplicatePostingExc(String aMessage) { + super(aMessage); + } + } + } -- 2.11.0