From: <fg...@us...> - 2008-09-15 22:06:42
|
Revision: 910 http://openutils.svn.sourceforge.net/openutils/?rev=910&view=rev Author: fgiust Date: 2008-09-15 22:06:51 +0000 (Mon, 15 Sep 2008) Log Message: ----------- stripes 1.5 support Modified Paths: -------------- trunk/openutils-mgnlstripes/pom.xml trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRenderer.java trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.paragraph-renderers.stripes.xml Added Paths: ----------- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesRequestWrapper.java trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesResponseWrapper.java trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.template-renderers.stripes.xml Removed Paths: ------------- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRequestWrapper.java trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphResponseWrapper.java trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/tag/ trunk/openutils-mgnlstripes/src/main/resources/META-INF/mgnlstripes.tld Modified: trunk/openutils-mgnlstripes/pom.xml =================================================================== --- trunk/openutils-mgnlstripes/pom.xml 2008-08-18 19:37:24 UTC (rev 909) +++ trunk/openutils-mgnlstripes/pom.xml 2008-09-15 22:06:51 UTC (rev 910) @@ -39,7 +39,7 @@ <dependency> <groupId>net.sourceforge.stripes</groupId> <artifactId>stripes</artifactId> - <version>1.4.3</version> + <version>1.5</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> @@ -50,12 +50,12 @@ <dependency> <groupId>info.magnolia</groupId> <artifactId>magnolia-core</artifactId> - <version>3.5.4</version> + <version>3.6.1</version> </dependency> <dependency> <groupId>info.magnolia</groupId> <artifactId>magnolia-module-admininterface</artifactId> - <version>3.5.4</version> + <version>3.6.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> Copied: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesRequestWrapper.java (from rev 909, trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRequestWrapper.java) =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesRequestWrapper.java (rev 0) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesRequestWrapper.java 2008-09-15 22:06:51 UTC (rev 910) @@ -0,0 +1,187 @@ +/** + * Copyright Openmind 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.magnoliastripes; + +import info.magnolia.context.MgnlContext; +import info.magnolia.context.WebContext; + +import java.io.IOException; +import java.io.Writer; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; + +import net.sourceforge.stripes.exception.StripesServletException; + + +/** + * Magnolia request wrapper for Stripes actions. + * @author fgiust + * @version $Id: StripesParagraphRequestWrapper.java 10833 2008-09-15 15:39:08Z fgiust $ + */ +class MgnlStripesRequestWrapper extends HttpServletRequestWrapper +{ + + /** + * Hacked servlet path. + */ + private String servletPath; + + /** + * Parameter map. + */ + private Map<String, String[]> parameterMap; + + private Writer out; + + /** + * Instantiate a new request wrapper. + * @param request original HttpServletRequest + * @param servletPath modified servlet path (matches Stripes binding) + * @param paragraphsData map containing paragraph attributes + * @throws StripesServletException if any other error occurs constructing the wrapper + */ + public MgnlStripesRequestWrapper( + HttpServletRequest request, + String servletPath, + Map<String, String[]> paragraphsData, + Writer out) throws StripesServletException + { + super(request); + this.servletPath = servletPath; + this.out = out; + + parameterMap = new HashMap<String, String[]>(); + parameterMap.putAll(request.getParameterMap()); + parameterMap.putAll(paragraphsData); + } + + /** + * {@inheritDoc} + */ + @Override + public String getServletPath() + { + return servletPath; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings("unchecked") + @Override + public Enumeration<String> getParameterNames() + { + return ((Hashtable) parameterMap).keys(); + } + + /** + * {@inheritDoc} + */ + @Override + public String[] getParameterValues(String name) + { + return parameterMap.get(name); + } + + /** + * {@inheritDoc} + */ + @Override + public Map<String, String[]> getParameterMap() + { + return parameterMap; + } + + /** + * {@inheritDoc} + */ + @Override + public String getParameter(String name) + { + String[] values = getParameterValues(name); + if (values != null && values.length > 0) + { + return values[0]; + } + else + { + return null; + } + } + + /** + * {@inheritDoc} + */ + @Override + public RequestDispatcher getRequestDispatcher(String path) + { + return new MagnoliaRequestDispatcher(path, out); + } + + /** + * A {@link RequestDispatcher} that uses {@link WebContext} for including a resource. + * @author fgiust + * @version $Id: StripesParagraphRequestWrapper.java 10833 2008-09-15 15:39:08Z fgiust $ + */ + private static class MagnoliaRequestDispatcher implements RequestDispatcher + { + + /** + * The url this RequestDispatcher is bound to. + */ + private String url; + + private Writer out; + + /** + * Created a new MagnoliaRequestDispatcher for the given url. + * @param url url passed to {@link HttpServletRequest#getRequestDispatcher()} + */ + public MagnoliaRequestDispatcher(String url, Writer out) + { + this.url = url; + this.out = out; + } + + /** + * {@inheritDoc} + */ + public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException + { + ((WebContext) MgnlContext.getInstance()).include(url, out); + } + + /** + * {@inheritDoc} + */ + public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException + { + ((WebContext) MgnlContext.getInstance()).include(url, out); + } + + } +} \ No newline at end of file Property changes on: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesRequestWrapper.java ___________________________________________________________________ Added: svn:mergeinfo + Copied: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesResponseWrapper.java (from rev 909, trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphResponseWrapper.java) =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesResponseWrapper.java (rev 0) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesResponseWrapper.java 2008-09-15 22:06:51 UTC (rev 910) @@ -0,0 +1,60 @@ +/** + * Copyright Openmind 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.magnoliastripes; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; + +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpServletResponseWrapper; + + +/** + * A response wrapper for the Stripes paragraph (replace the standard writer with the Magnolia one). + * @author fgiust + * @version $Id: StripesParagraphResponseWrapper.java 10833 2008-09-15 15:39:08Z fgiust $ + */ +class MgnlStripesResponseWrapper extends HttpServletResponseWrapper +{ + + /** + * Writer that should be used for output. + */ + private PrintWriter out; + + /** + * @param response HttpServletResponse + * @param out writer guven by Magnolia. + */ + public MgnlStripesResponseWrapper(HttpServletResponse response, Writer out) + { + super(response); + this.out = new PrintWriter(out); + } + + /** + * {@inheritDoc} + */ + @Override + public PrintWriter getWriter() throws IOException + { + return out; + } + +} \ No newline at end of file Property changes on: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlStripesResponseWrapper.java ___________________________________________________________________ Added: svn:mergeinfo + Modified: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRenderer.java =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRenderer.java 2008-08-18 19:37:24 UTC (rev 909) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRenderer.java 2008-09-15 22:06:51 UTC (rev 910) @@ -17,345 +17,13 @@ */ package it.openutils.magnoliastripes; -import info.magnolia.cms.beans.config.Paragraph; -import info.magnolia.cms.beans.runtime.ParagraphRenderer; -import info.magnolia.cms.core.Content; -import info.magnolia.cms.core.NodeData; -import info.magnolia.context.MgnlContext; -import info.magnolia.context.WebContext; - -import java.io.IOException; -import java.io.Writer; -import java.lang.reflect.InvocationTargetException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Stack; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.jsp.PageContext; - -import net.sourceforge.stripes.action.ActionBeanContext; -import net.sourceforge.stripes.action.Resolution; -import net.sourceforge.stripes.config.Configuration; -import net.sourceforge.stripes.controller.DispatcherHelper; -import net.sourceforge.stripes.controller.DispatcherServlet; -import net.sourceforge.stripes.controller.ExecutionContext; -import net.sourceforge.stripes.controller.LifecycleStage; -import net.sourceforge.stripes.controller.StripesConstants; -import net.sourceforge.stripes.controller.StripesFilter; -import net.sourceforge.stripes.exception.StripesServletException; -import net.sourceforge.stripes.validation.BooleanTypeConverter; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - /** - * <p> - * A Magnolia paragraph renderer that delegates to Stripes actions. Most of the code is just a cut and paste from - * Stripes' {@link DispatcherServlet}, adapted to work withing magnolia by: - * </p> - * <ul> - * <li>wrap the request in order to modify the request path and provide a custom requestDispatcher</li> - * <li>wrap the response in order to provide a custom Writer</li> - * <li>use a fake servlet instance/context to setup a Stripe context (there is no servlet here)</li> - * <li>injiect any paragraph property as a parameter</li> - * </ul> - * <p> - * <strong>Todo:</strong> - * </p> - * <ul> - * <li>Handle multipart forms in request wrapper (should be easy to do)</li> - * <li>A better way of handling multivalued properties in paragraph</li> - * <li>Handle binary properties in paragraph</li> - * </ul> + * @deprecated use {@link StripesRenderer} which acts both as a ParagraphRenderer and as a TemplateRenderer * @author fgiust - * @version $Id: $ + * @version $Id: StripesParagraphRenderer.java 10833 2008-09-15 15:39:08Z fgiust $ */ -public class StripesParagraphRenderer implements ParagraphRenderer +@Deprecated +public class StripesParagraphRenderer extends StripesRenderer { - private Boolean alwaysInvokeValidate; - - /** - * Logger. - */ - private Logger log = LoggerFactory.getLogger(StripesParagraphRenderer.class); - - /** - * <p> - * Invokes the following instance level methods in order to coordinate the processing of requests: - * </p> - * <ul> - * <li>{@link #resolveActionBean(ExecutionContext)}</li> - * <li>{@link #resolveHandler(ExecutionContext)}</li> - * <li>{@link #doBindingAndValidation(ExecutionContext)}</li> - * <li>{@link #doCustomValidation(ExecutionContext)}</li> - * <li>{@link #handleValidationErrors(ExecutionContext)}</li> - * <li>{@link #invokeEventHandler(ExecutionContext)}</li> - * </ul> - * <p> - * If any of the above methods return a {@link Resolution} the rest of the request processing is aborted and the - * resolution is executed. - * </p> - * {@inheritDoc} - */ - public void render(Content content, Paragraph paragraph, Writer out) throws IOException - { - // @fgiust start custom magnolia setup - - WebContext webContext = (WebContext) MgnlContext.getInstance(); - HttpServletResponse response = new StripesParagraphResponseWrapper(webContext.getResponse(), out); - - final String templatePath = paragraph.getTemplatePath(); - Map<String, String[]> nodeDataMap = contentToMap(content); - - HttpServletRequest request; - try - { - request = new StripesParagraphRequestWrapper( - ((WebContext) MgnlContext.getInstance()).getRequest(), - templatePath, - nodeDataMap, - out); - - ((StripesParagraphRequestWrapper) request).setLocale(MgnlContext.getLocale()); - } - catch (StripesServletException e) - { - throw new RuntimeException(e); - } - - // @todo fgiust force include instead of forwarding. Not needed anymore since we are wrapping requestDispatcher? - request.setAttribute(StripesConstants.REQ_ATTR_INCLUDE_PATH, templatePath); - - PageContext pageContext = webContext.getPageContext(); - ServletContext servletContext = webContext.getServletContext(); - - if (pageContext != null) - { - pageContext.getServletContext(); - } - // @fgiust end custom magnolia setup - - // It sucks that we have to do this here (in the request cycle), but there doesn't - // seem to be a good way to get at the Configuration from the Filter in init() - doOneTimeConfiguration(); - - log.debug("Dispatching request to URL: ", request.getRequestURI()); - - try - { - Configuration config = (Configuration) servletContext.getAttribute(Configuration.class.getName()); - - if (config == null) - { - // avoid NPES, an error has already been logged. - log.warn("Stripes configuration not found for request " - + request.getAttribute("javax.servlet.forward.request_uri") - + " while including " - + request.getRequestURI()); - - return; - } - - // First manufacture an ActionBeanContext - final ActionBeanContext context = config - .getActionBeanContextFactory() - .getContextInstance(request, response); - context.setServletContext(servletContext); - - // Then setup the ExecutionContext that we'll use to process this request - final ExecutionContext ctx = new ExecutionContext(); - ctx.setInterceptors(config.getInterceptors(LifecycleStage.ActionBeanResolution)); - ctx.setLifecycleStage(LifecycleStage.ActionBeanResolution); - ctx.setActionBeanContext(context); - - if (pageContext != null) - { - DispatcherHelper.setPageContext(pageContext); - } - - // Resolve the ActionBean, and if an interceptor returns a resolution, bail now - saveActionBean(request); - Resolution resolution = DispatcherHelper.resolveActionBean(ctx); - - if (resolution == null) - { - resolution = DispatcherHelper.resolveHandler(ctx); - - if (resolution == null) - { - // Then run binding and validation - resolution = DispatcherHelper.doBindingAndValidation(ctx, true); - - if (resolution == null) - { - // Then continue on to custom validation - resolution = DispatcherHelper.doCustomValidation(ctx, alwaysInvokeValidate); - - if (resolution == null) - { - // And then validation error handling - resolution = DispatcherHelper.handleValidationErrors(ctx); - - if (resolution == null) - { - // And finally(ish) invoking of the event handler - resolution = DispatcherHelper.invokeEventHandler(ctx); - - // If the event produced errors, fill them in - DispatcherHelper.fillInValidationErrors(ctx); - } - } - } - } - } - - // Whatever stage it came from, execute the resolution - if (resolution != null) - { - DispatcherHelper.executeResolution(ctx, resolution); - } - } - catch (RuntimeException re) - { - throw re; - } - catch (InvocationTargetException ite) - { - if (ite.getTargetException() instanceof RuntimeException) - { - throw (RuntimeException) ite.getTargetException(); - } - else - { - Throwable targetEx = ite.getTargetException(); - throw new RuntimeException(targetEx.getClass().getName() + ": " + targetEx.getMessage(), targetEx); - } - } - catch (Exception e) - { - throw new RuntimeException("Exception encountered processing request: " + e.getMessage(), e); - } - finally - { - - if (pageContext != null) - { - // *Don't* release the page context, if set it will be closed by the main magnolia template - // JspFactory.getDefaultFactory().releasePageContext(pageContext); - DispatcherHelper.setPageContext(null); - } - restoreActionBean(request); - } - } - - /** - * @param content paragraph node - * @return a map of Strings (converted nodedata) - */ - @SuppressWarnings("unchecked") - protected Map<String, String[]> contentToMap(Content content) - { - Collection<NodeData> paragraphsData = content.getNodeDataCollection(); - Map<String, String[]> nodeDataMap = new HashMap<String, String[]>(); - for (NodeData nodeData : paragraphsData) - { - String name = StringUtils.replaceChars(nodeData.getName(), "{}", "[]"); - String value = nodeData.getString(); - if (StringUtils.contains(name, "multiple")) - { - nodeDataMap.put(name, StringUtils.split(value, "\n")); - } - else - { - nodeDataMap.put(name, new String[]{value }); - } - } - return nodeDataMap; - } - - /** - * Performs a simple piece of one time configuration that requires access to the Configuration object delivered - * through the Stripes Filter. - */ - private void doOneTimeConfiguration() - { - if (alwaysInvokeValidate == null) - { - // Check to see if, in this application, validate() methods should always be run - // even when validation errors already exist - String callValidateWhenErrorsExist = StripesFilter - .getConfiguration() - .getBootstrapPropertyResolver() - .getProperty(DispatcherServlet.RUN_CUSTOM_VALIDATION_WHEN_ERRORS); - - if (callValidateWhenErrorsExist != null) - { - BooleanTypeConverter c = new BooleanTypeConverter(); - this.alwaysInvokeValidate = c.convert(callValidateWhenErrorsExist, Boolean.class, null); - } - else - { - this.alwaysInvokeValidate = false; // Default behaviour - } - } - } - - /** - * Fetches, and lazily creates if required, a Stack in the request to store ActionBeans should the current request - * involve forwards or includes to other ActionBeans. - * @param request the current HttpServletRequest - * @param create create a Stack if not already set - * @return the Stack if present, or if creation is requested - */ - @SuppressWarnings("unchecked") - protected Stack getActionBeanStack(HttpServletRequest request, boolean create) - { - Stack stack = (Stack) request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN_STACK); - if (stack == null && create) - { - stack = new Stack(); - request.setAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN_STACK, stack); - } - - return stack; - } - - /** - * Saves the current value of the 'actionBean' attribute in the request so that it can be restored at a later date - * by calling {@link #restoreActionBean(HttpServletRequest)}. If no ActionBean is currently stored in the request, - * nothing is changed. - * @param request the current HttpServletRequest - */ - @SuppressWarnings("unchecked") - protected void saveActionBean(HttpServletRequest request) - { - if (request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN) != null) - { - Stack stack = getActionBeanStack(request, true); - stack.push(request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN)); - } - } - - /** - * Restores the previous value of the 'actionBean' attribute in the request. If no ActionBeans have been saved using - * {@link #saveActionBean(HttpServletRequest)} then this method has no effect. - * @param request the current HttpServletRequest - */ - @SuppressWarnings("unchecked") - protected void restoreActionBean(HttpServletRequest request) - { - Stack stack = getActionBeanStack(request, false); - if (stack != null && !stack.empty()) - { - request.setAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN, stack.pop()); - } - } - -} +} \ No newline at end of file Deleted: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRequestWrapper.java =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRequestWrapper.java 2008-08-18 19:37:24 UTC (rev 909) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphRequestWrapper.java 2008-09-15 22:06:51 UTC (rev 910) @@ -1,198 +0,0 @@ -/** - * Copyright Openmind 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.magnoliastripes; - -import info.magnolia.context.MgnlContext; -import info.magnolia.context.WebContext; - -import java.io.IOException; -import java.io.Writer; -import java.util.Enumeration; -import java.util.Hashtable; -import java.util.Locale; -import java.util.Map; - -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; - -import net.sourceforge.stripes.controller.StripesRequestWrapper; -import net.sourceforge.stripes.exception.StripesServletException; - - -/** - * Magnolia request wrapper for Stripes actions. - * @author fgiust - * @version $Id: $ - */ -class StripesParagraphRequestWrapper extends StripesRequestWrapper -{ - - /** - * Hacked servlet path. - */ - private String servletPath; - - /** - * Parameter map. - */ - private Map<String, String[]> parameterMap; - - private Writer out; - - /** - * Instantiate a new request wrapper. - * @param request original HttpServletRequest - * @param servletPath modified servlet path (matches Stripes binding) - * @param paragraphsData map containing paragraph attributes - * @throws StripesServletException if any other error occurs constructing the wrapper - */ - @SuppressWarnings("unchecked") - public StripesParagraphRequestWrapper( - HttpServletRequest request, - String servletPath, - Map<String, String[]> paragraphsData, - Writer out) throws StripesServletException - { - super(request); - this.servletPath = servletPath; - this.out = out; - - parameterMap = new Hashtable<String, String[]>(); - parameterMap.putAll(request.getParameterMap()); - parameterMap.putAll(paragraphsData); - } - - /** - * {@inheritDoc} - */ - @Override - public String getServletPath() - { - return servletPath; - } - - /** - * {@inheritDoc} - */ - @Override - protected void setLocale(Locale locale) - { - super.setLocale(locale); - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings("unchecked") - @Override - public Enumeration<String> getParameterNames() - { - return ((Hashtable) parameterMap).keys(); - } - - /** - * {@inheritDoc} - */ - @Override - public String[] getParameterValues(String name) - { - return parameterMap.get(name); - } - - /** - * {@inheritDoc} - */ - @Override - public Map<String, String[]> getParameterMap() - { - - return parameterMap; - } - - /** - * {@inheritDoc} - */ - @Override - public String getParameter(String name) - { - String[] values = getParameterValues(name); - if (values != null && values.length > 0) - { - return values[0]; - } - else - { - return null; - } - } - - /** - * {@inheritDoc} - */ - @Override - public RequestDispatcher getRequestDispatcher(String path) - { - return new MagnoliaRequestDispatcher(path, out); - } - - /** - * A {@link RequestDispatcher} that uses {@link WebContext} for including a resource. - * @author fgiust - * @version $Id: $ - */ - private static class MagnoliaRequestDispatcher implements RequestDispatcher - { - - /** - * The url this RequestDispatcher is bound to. - */ - private String url; - - private Writer out; - - /** - * Created a new MagnoliaRequestDispatcher for the given url. - * @param url url passed to {@link HttpServletRequest#getRequestDispatcher()} - */ - public MagnoliaRequestDispatcher(String url, Writer out) - { - this.url = url; - this.out = out; - } - - /** - * {@inheritDoc} - */ - public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException - { - ((WebContext) MgnlContext.getInstance()).include(url, out); - } - - /** - * {@inheritDoc} - */ - public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException - { - ((WebContext) MgnlContext.getInstance()).include(url, out); - } - - } -} \ No newline at end of file Deleted: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphResponseWrapper.java =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphResponseWrapper.java 2008-08-18 19:37:24 UTC (rev 909) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesParagraphResponseWrapper.java 2008-09-15 22:06:51 UTC (rev 910) @@ -1,60 +0,0 @@ -/** - * Copyright Openmind 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.magnoliastripes; - -import java.io.IOException; -import java.io.PrintWriter; -import java.io.Writer; - -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpServletResponseWrapper; - - -/** - * A response wrapper for the Stripes paragraph (replace the standard writer with the Magnolia one). - * @author fgiust - * @version $Id: $ - */ -class StripesParagraphResponseWrapper extends HttpServletResponseWrapper -{ - - /** - * Writer that should be used for output. - */ - private PrintWriter out; - - /** - * @param response HttpServletResponse - * @param out writer guven by Magnolia. - */ - public StripesParagraphResponseWrapper(HttpServletResponse response, Writer out) - { - super(response); - this.out = new PrintWriter(out); - } - - /** - * {@inheritDoc} - */ - @Override - public PrintWriter getWriter() throws IOException - { - return out; - } - -} \ No newline at end of file Added: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java (rev 0) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java 2008-09-15 22:06:51 UTC (rev 910) @@ -0,0 +1,193 @@ +/** + * Copyright Openmind 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.magnoliastripes; + +import info.magnolia.cms.beans.config.Paragraph; +import info.magnolia.cms.beans.config.Template; +import info.magnolia.cms.beans.runtime.ParagraphRenderer; +import info.magnolia.cms.beans.runtime.TemplateRenderer; +import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.NodeData; +import info.magnolia.context.MgnlContext; +import info.magnolia.context.WebContext; + +import java.io.IOException; +import java.io.Writer; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.sourceforge.stripes.controller.DispatcherServlet; +import net.sourceforge.stripes.controller.StripesConstants; +import net.sourceforge.stripes.controller.StripesRequestWrapper; +import net.sourceforge.stripes.exception.StripesServletException; + +import org.apache.commons.lang.StringUtils; + + +/** + * <p> + * A Magnolia paragraph renderer that delegates to Stripes actions. Most of the code is just a cut and paste from + * Stripes' {@link DispatcherServlet}, adapted to work withing magnolia by: + * </p> + * <ul> + * <li>wrap the request in order to modify the request path and provide a custom requestDispatcher</li> + * <li>wrap the response in order to provide a custom Writer</li> + * <li>use a fake servlet instance/context to setup a Stripe context (there is no servlet here)</li> + * <li>injiect any paragraph property as a parameter</li> + * </ul> + * <p> + * <strong>Todo:</strong> + * </p> + * <ul> + * <li>Handle multipart forms in request wrapper (should be easy to do)</li> + * <li>A better way of handling multivalued properties in paragraph</li> + * <li>Handle binary properties in paragraph</li> + * </ul> + * @author fgiust + * @version $Id$ + */ +public class StripesRenderer implements ParagraphRenderer, TemplateRenderer +{ + + private StripesDispatcherServlet stripesDispatcherServlet = new StripesDispatcherServlet(); + + /** + * {@inheritDoc} + */ + public void renderTemplate(Template template, HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException + { + final String templatePath = template.getPath(); + Map<String, String[]> nodeDataMap = contentToMap(MgnlContext.getAggregationState().getCurrentContent()); + + renderCommon(templatePath, nodeDataMap, response.getWriter()); + } + + /** + * {@inheritDoc} + */ + public void render(Content content, Paragraph paragraph, Writer out) throws IOException + { + // @fgiust start custom magnolia setup + + final String templatePath = paragraph.getTemplatePath(); + Map<String, String[]> nodeDataMap = contentToMap(content); + + renderCommon(templatePath, nodeDataMap, out); + } + + /** + * {@inheritDoc} + */ + protected void renderCommon(String templatePath, Map<String, String[]> nodeDataMap, Writer out) throws IOException + { + + WebContext webContext = (WebContext) MgnlContext.getInstance(); + HttpServletResponse response = new MgnlStripesResponseWrapper(webContext.getResponse(), out); + + HttpServletRequest request; + try + { + + HttpServletRequest originalreq = new MgnlStripesRequestWrapper(((WebContext) MgnlContext.getInstance()) + .getRequest(), templatePath, nodeDataMap, out); + + request = new StripesRequestWrapper(originalreq); + } + catch (StripesServletException e) + { + throw new RuntimeException(e); + } + + request.setAttribute(StripesConstants.REQ_ATTR_INCLUDE_PATH, templatePath); + + try + { + stripesDispatcherServlet.doPost(request, response); + } + catch (ServletException e) + { + throw new RuntimeException(e); + } + } + + /** + * @param content paragraph node + * @return a map of Strings (converted nodedata) + */ + @SuppressWarnings("unchecked") + protected Map<String, String[]> contentToMap(Content content) + { + Collection<NodeData> paragraphsData = content.getNodeDataCollection(); + Map<String, String[]> nodeDataMap = new HashMap<String, String[]>(); + for (NodeData nodeData : paragraphsData) + { + String name = StringUtils.replaceChars(nodeData.getName(), "{}", "[]"); + String value = nodeData.getString(); + if (StringUtils.contains(name, "multiple")) + { + nodeDataMap.put(name, StringUtils.split(value, "\n")); + } + else + { + nodeDataMap.put(name, new String[]{value }); + } + } + return nodeDataMap; + } + + /** + * just needed to make the protected doPost() accessible + * @author fgiust + * @version $Id$ + */ + protected class StripesDispatcherServlet extends DispatcherServlet + { + + /** + * Stable serialVersionUID. + */ + private static final long serialVersionUID = 42L; + + /** + * {@inheritDoc} + */ + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException + { + super.doPost(request, response); + } + + /** + * {@inheritDoc} + */ + @Override + public ServletContext getServletContext() + { + return ((WebContext) MgnlContext.getInstance()).getServletContext(); + } + + } + +} Property changes on: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Deleted: trunk/openutils-mgnlstripes/src/main/resources/META-INF/mgnlstripes.tld =================================================================== --- trunk/openutils-mgnlstripes/src/main/resources/META-INF/mgnlstripes.tld 2008-08-18 19:37:24 UTC (rev 909) +++ trunk/openutils-mgnlstripes/src/main/resources/META-INF/mgnlstripes.tld 2008-09-15 22:06:51 UTC (rev 910) @@ -1,198 +0,0 @@ -<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" - version="2.0"> - <description>mgnlstripes</description> - <display-name>mgnlstripes tags</display-name> - <tlib-version>1.0</tlib-version> - <short-name>mgnlstripes</short-name> - <uri>mgnlstripes</uri> - <tag> - <description> Stripes' HTML form tag. Provides a standard HTML form tag interface, but allows other stripes input - tags to re-populate their values. Also includes a hidden field in the form for the page name that the form is - being rendered on (to allow forwarding to the same page in the case of a validation error. </description> - <display-name>form</display-name> - <name>form</name> - <tag-class>it.openutils.magnoliastripes.tag.ExtendedFormTag</tag-class> - <body-content>JSP</body-content> - <attribute> - <description> The URL to which this form will post. Expected to be a web-app relative path - the tag will prepend - the context path to any action that begins with a slash and does not already contain the context path. Should - match a URL to which an ActionBean has been bound. Required unless 'beanclass' is provided. </description> - <name>action</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description> The fully qualified name of an ActionBean class, or alternatively a Class instance for an ActionBean - class. An alternative to the 'action' attribute, the 'beanclass' attribute will generate an action appropriate - for the ActionBean identified. Note that if an "ActionBean" that does not yet exist is identified an exception - will be thrown! </description> - <name>beanclass</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - <type>java.lang.Object</type> - </attribute> - <attribute> - <description> The name of the form field that should receive focus when the page is loaded. Two special values are - recognized, 'first' and the empty string; these values cause the form to set focus on the first element in the - form. If any value is set, and the form has validation errors, the behaviour is altered and the first field with - validation errors is focused instead. </description> - <name>focus</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - <type>java.lang.String</type> - </attribute> - <attribute> - <description> A comma separated list of content types that it is acceptable to submit through this form. (HTML - Pass-through) </description> - <name>accept</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description> A comma separated list of possible character sets for form data. Will be written to the page as - accept-charset. (HTML Pass-through) </description> - <name>acceptcharset</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description> The mime type used to encode the content of this form. This value will be overridden if one or more - Stripes file input tags is used within the body of the form. </description> - <name>enctype</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description> The HTTP method used for sending data to the server. Options are GET and POST. Default is GET. (HTML - Pass-through) </description> - <name>method</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>The unique name of the form. (HTML Pass-through)</description> - <name>name</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description> Where the target URL is to be opened. One of _blank, _self, _parent and _top. (HTML Pass-through) - </description> - <name>target</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when the form is reset. (HTML Pass-through)</description> - <name>onreset</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run prior to the form being submitted to the server, (HTML Pass-through)</description> - <name>onsubmit</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <!-- Start: Standard HTML attributes --> - <attribute> - <description>The CSS class to be applied to the element. (HTML Pass-through)</description> - <name>class</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Text direction. (HTML Pass-through)</description> - <name>dir</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>A unique identifier for the HTML tag on the pgae. (HTML Pass-through)</description> - <name>id</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>The language code of the element. (HTML Pass-through)</description> - <name>lang</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run on each mouse click. (HTML Pass-through)</description> - <name>onclick</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run on a double-click of the mouse. (HTML Pass-through)</description> - <name>ondblclick</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when a key is depressed. (HTML Pass-through)</description> - <name>onkeydown</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when a key is pressed and released. (HTML Pass-through)</description> - <name>onkeypress</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when a key is released. (HTML Pass-through)</description> - <name>onkeyup</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when a mouse button is depressed. (HTML Pass-through)</description> - <name>onmousedown</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when the mouse pointer is moved. (HTML Pass-through)</description> - <name>onmousemove</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when the mouse pointer moves out of the element. (HTML Pass-through)</description> - <name>onmouseout</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when the mouse pointer moves over the element. (HTML Pass-through)</description> - <name>onmouseover</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Scripting code run when a mouse button is released. (HTML Pass-through)</description> - <name>onmouseup</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Inline CSS style fragment that applies to the element (HTML Pass-through)</description> - <name>style</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <attribute> - <description>Tool-tip text for the element. (HTML Pass-through)</description> - <name>title</name> - <required>false</required> - <rtexprvalue>true</rtexprvalue> - </attribute> - <!-- End: Standard HTML attributes --> - <dynamic-attributes>false</dynamic-attributes> - </tag> -</taglib> \ No newline at end of file Modified: trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.paragraph-renderers.stripes.xml =================================================================== --- trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.paragraph-renderers.stripes.xml 2008-08-18 19:37:24 UTC (rev 909) +++ trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.paragraph-renderers.stripes.xml 2008-09-15 22:06:51 UTC (rev 910) @@ -16,7 +16,7 @@ <sv:value>stripes</sv:value> </sv:property> <sv:property sv:name="class" sv:type="String"> - <sv:value>it.openutils.magnoliastripes.StripesParagraphRenderer</sv:value> + <sv:value>it.openutils.magnoliastripes.StripesRenderer</sv:value> </sv:property> <sv:node sv:name="MetaData"> <sv:property sv:name="jcr:primaryType" sv:type="Name"> Added: trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.template-renderers.stripes.xml =================================================================== --- trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.template-renderers.stripes.xml (rev 0) +++ trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.template-renderers.stripes.xml 2008-09-15 22:06:51 UTC (rev 910) @@ -0,0 +1,47 @@ +<?xml version="1.0" encoding="UTF-8"?> +<sv:node sv:name="stripes" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:mgnl="http://www.magnolia.info/jcr/mgnl" + xmlns:rep="internal" xmlns:mix="http://www.jcp.org/jcr/mix/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" + xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema" + xmlns:fn_old="http://www.w3.org/2004/10/xpath-functions" xmlns:sv="http://www.jcp.org/jcr/sv/1.0" xmlns:jcrfn="http://www.jcp.org/jcr/xpath-functions/1.0"> + <sv:property sv:name="jcr:primaryType" sv:type="Name"> + <sv:value>mgnl:contentNode</sv:value> + </sv:property> + <sv:property sv:name="jcr:mixinTypes" sv:type="Name"> + <sv:value>mix:versionable</sv:value> + </sv:property> + <sv:property sv:name="jcr:uuid" sv:type="String"> + <sv:value>42369a0d-1d6c-469b-b17d-dc5171435db0</sv:value> + </sv:property> + <sv:property sv:name="name" sv:type="String"> + <sv:value>stripes</sv:value> + </sv:property> + <sv:property sv:name="class" sv:type="String"> + <sv:value>it.openutils.magnoliastripes.StripesRenderer</sv:value> + </sv:property> + <sv:node sv:name="MetaData"> + <sv:property sv:name="jcr:primaryType" sv:type="Name"> + <sv:value>mgnl:metaData</sv:value> + </sv:property> + <sv:property sv:name="jcr:uuid" sv:type="String"> + <sv:value>fd0eff1b-ba74-4c6e-ab93-b8a344175496</sv:value> + </sv:property> + <sv:property sv:name="mgnl:authorid" sv:type="String"> + <sv:value>superuser</sv:value> + </sv:property> + <sv:property sv:name="mgnl:creationdate" sv:type="Date"> + <sv:value>2007-04-12T03:58:33.504+02:00</sv:value> + </sv:property> + <sv:property sv:name="mgnl:lastaction" sv:type="Date"> + <sv:value>2007-07-23T22:39:14.178+01:00</sv:value> + </sv:property> + <sv:property sv:name="mgnl:lastmodified" sv:type="Date"> + <sv:value>2007-07-23T22:39:40.986+01:00</sv:value> + </sv:property> + <sv:property sv:name="mgnl:activatorid" sv:type="String"> + <sv:value>superuser</sv:value> + </sv:property> + <sv:property sv:name="mgnl:activated" sv:type="Boolean"> + <sv:value>false</sv:value> + </sv:property> + </sv:node> +</sv:node> Property changes on: trunk/openutils-mgnlstripes/src/main/resources/mgnl-bootstrap/stripes/config.modules.stripes.template-renderers.stripes.xml ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |