From: <fg...@us...> - 2013-10-19 20:58:39
|
Revision: 4390 http://openutils.svn.sourceforge.net/openutils/?rev=4390&view=rev Author: fgiust Date: 2013-10-19 20:58:36 +0000 (Sat, 19 Oct 2013) Log Message: ----------- new renderExceptionHandler Modified Paths: -------------- magnoliamodules/trunk/openutils-mgnlutils/src/main/resources/META-INF/magnolia/mgnlutils.xml Added Paths: ----------- magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/ClassicRenderExceptionHandler.java magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/RuntimeRenderException.java Added: magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/ClassicRenderExceptionHandler.java =================================================================== --- magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/ClassicRenderExceptionHandler.java (rev 0) +++ magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/ClassicRenderExceptionHandler.java 2013-10-19 20:58:36 UTC (rev 4390) @@ -0,0 +1,125 @@ +/** + * + * Generic utilities for Magnolia CMS (http://www.openmindlab.com/lab/products/mgnlutils.html) + * Copyright(C) 2009-2012, Openmind S.r.l. http://www.openmindonline.it + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ +package it.openutils.mgnlutils.util; + +import info.magnolia.cms.beans.config.ServerConfiguration; +import info.magnolia.context.MgnlContext; +import info.magnolia.init.MagnoliaConfigurationProperties; +import info.magnolia.rendering.context.RenderingContext; +import info.magnolia.rendering.engine.RenderException; +import info.magnolia.rendering.engine.RenderExceptionHandler; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; + +import javax.inject.Inject; +import javax.jcr.RepositoryException; + +import org.apache.commons.lang.exception.ExceptionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Simple exception renderer that avoid the ugly yellow box that popped up in magnolia 4.5. Just let the exception flow + * up to tomcat, which usually shows a more meaningful error page. + * @author fgiust + * @version $Id$ + */ +public class ClassicRenderExceptionHandler implements RenderExceptionHandler +{ + + private static Logger log = LoggerFactory.getLogger(ClassicRenderExceptionHandler.class); + + private ServerConfiguration serverConfiguration; + + private MagnoliaConfigurationProperties configurationProperties; + + @Inject + public ClassicRenderExceptionHandler(ServerConfiguration config) + { + this.serverConfiguration = config; + } + + @Inject + public void setConfigurationProperties(MagnoliaConfigurationProperties configurationProperties) + { + this.configurationProperties = configurationProperties; + } + + @Override + public void handleException(RenderException renderException, RenderingContext renderingContext) + { + String path; + try + { + path = renderingContext.getCurrentContent().getPath(); + } + catch (RepositoryException e) + { + path = "Can't read content"; + } + String id = renderingContext.getRenderableDefinition().getId(); + + String msg = "Error while rendering [" + + path + + "] with template [" + + id + + "]: " + + ExceptionUtils.getMessage(renderException); + if (serverConfiguration.isAdmin() || configurationProperties.getBooleanProperty("magnolia.develop")) + { + log.error(msg, renderException); + + throw new RuntimeRenderException(renderException); + } + else + { + log.error(msg, renderException); + + try + { + PrintWriter out = getPrintWriterFor(renderingContext.getAppendable()); + out.write("\n <!-- "); + out.write(msg); + out.write("\n-->\n"); + + out.flush(); + } + + catch (IOException e) + { + throw new RuntimeException("Can't log template exception.", e); + } + } + + } + + private PrintWriter getPrintWriterFor(Writer out) + { + return (out instanceof PrintWriter) ? (PrintWriter) out : new PrintWriter(out); + } + + protected void inPublicMode(String msg, RenderException renderException, PrintWriter out) + { + log.error(msg, renderException); + } + +} Property changes on: magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/ClassicRenderExceptionHandler.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +Author Date Id Revision \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/RuntimeRenderException.java =================================================================== --- magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/RuntimeRenderException.java (rev 0) +++ magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/RuntimeRenderException.java 2013-10-19 20:58:36 UTC (rev 4390) @@ -0,0 +1,37 @@ +/** + * + * Generic utilities for Magnolia CMS (http://www.openmindlab.com/lab/products/mgnlutils.html) + * Copyright(C) 2009-2012, Openmind S.r.l. http://www.openmindonline.it + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ +package it.openutils.mgnlutils.util; + +import info.magnolia.rendering.engine.RenderException; + + +/** + * Runtime version of RenderException, used to let exceptions flow up to tomcat. + * @author fgiust + * @version $Id$ + */ +public class RuntimeRenderException extends RuntimeException +{ + + public RuntimeRenderException(RenderException cause) + { + super(cause.getMessage(), cause.getCause()); + } + +} Property changes on: magnoliamodules/trunk/openutils-mgnlutils/src/main/java/it/openutils/mgnlutils/util/RuntimeRenderException.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +Author Date Id Revision \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: magnoliamodules/trunk/openutils-mgnlutils/src/main/resources/META-INF/magnolia/mgnlutils.xml =================================================================== --- magnoliamodules/trunk/openutils-mgnlutils/src/main/resources/META-INF/magnolia/mgnlutils.xml 2013-10-19 20:58:16 UTC (rev 4389) +++ magnoliamodules/trunk/openutils-mgnlutils/src/main/resources/META-INF/magnolia/mgnlutils.xml 2013-10-19 20:58:36 UTC (rev 4390) @@ -7,14 +7,25 @@ <class>it.openutils.mgnlutils.setup.MgnlUtilsModule</class> <versionHandler>it.openutils.mgnlutils.setup.MgnlUtilsModuleVersionHandler</versionHandler> <version>${project.version}</version> + <components> + <id>main</id> + <component> + <type>info.magnolia.rendering.engine.RenderExceptionHandler</type> + <implementation>it.openutils.mgnlutils.util.ClassicRenderExceptionHandler</implementation> + </component> + </components> <dependencies> <dependency> <name>core</name> - <version>4.5/*</version> + <version>4.5.11/*</version> </dependency> <dependency> <name>criteria</name> <version>5.0.1/*</version> </dependency> + <dependency> + <name>rendering</name> + <version>4.5.11/*</version> + </dependency> </dependencies> </module> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |