From: <fg...@us...> - 2010-12-08 20:43:38
|
Revision: 3171 http://openutils.svn.sourceforge.net/openutils/?rev=3171&view=rev Author: fgiust Date: 2010-12-08 20:43:32 +0000 (Wed, 08 Dec 2010) Log Message: ----------- MGNLSTRIPES-7 update to magnolia 4.4 Modified Paths: -------------- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlActionResolver.java trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesMagnoliaFilter.java trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java Modified: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlActionResolver.java =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlActionResolver.java 2010-12-08 20:35:51 UTC (rev 3170) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/MgnlActionResolver.java 2010-12-08 20:43:32 UTC (rev 3171) @@ -35,8 +35,8 @@ /** - * ActionResolver that extends <code>NameBasedActionResolver</code>, registering any found Stripe action as a - * Magnolia paragraph. + * ActionResolver that extends <code>NameBasedActionResolver</code>, registering any found Stripe action as a Magnolia + * paragraph. * @author fgiust * @version $Id: $ */ @@ -77,7 +77,6 @@ /** * Generate a paragraph name from "Dialog" Annotation. - * @param binding Stripe action binding * @return paragraph name */ protected String annotationDialogToParagraphName(Class< ? extends ActionBean> clazz) @@ -110,9 +109,9 @@ */ protected String actionNameToParagraphName(String binding) { - String dialogName = StringUtils.lowerCase(StringUtils.substringBeforeLast(StringUtils.substringAfterLast( - binding, - "/"), ".")); + String dialogName = StringUtils.lowerCase(StringUtils.substringBeforeLast( + StringUtils.substringAfterLast(binding, "/"), + ".")); return dialogName; } Modified: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesMagnoliaFilter.java =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesMagnoliaFilter.java 2010-12-08 20:35:51 UTC (rev 3170) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesMagnoliaFilter.java 2010-12-08 20:43:32 UTC (rev 3171) @@ -19,17 +19,29 @@ package it.openutils.magnoliastripes; +import info.magnolia.cms.filters.DispatchRule; +import info.magnolia.cms.filters.DispatchRules; +import info.magnolia.cms.filters.Mapping; import info.magnolia.cms.filters.MgnlFilter; +import info.magnolia.cms.filters.WebContainerResources; +import info.magnolia.cms.util.RequestHeaderUtil; +import info.magnolia.cms.util.ServletUtils; import info.magnolia.context.MgnlContext; import info.magnolia.context.RepositoryAcquiringStrategy; import info.magnolia.context.WebContext; import info.magnolia.context.WebContextImpl; +import info.magnolia.objectfactory.Components; import info.magnolia.voting.Voter; import info.magnolia.voting.Voting; +import java.util.ArrayList; +import java.util.Collection; +import java.util.regex.Pattern; + import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import net.sourceforge.stripes.config.Configuration; import net.sourceforge.stripes.controller.StripesFilter; @@ -52,19 +64,97 @@ private boolean enabled = true; - public boolean bypasses(HttpServletRequest request) + private DispatchRules dispatchRules = new DispatchRules(); + + private Mapping mapping = new Mapping(); + + private WebContainerResources webContainerResources = Components.getSingleton(WebContainerResources.class); + + /** + * {@inheritDoc} + */ + @Override + public void init(FilterConfig filterConfig) throws ServletException { - if (!isEnabled()) + super.init(filterConfig); + filterConfig.getServletContext().setAttribute(Configuration.class.getName(), getInstanceConfiguration()); + } + + /** + * Wraps the HttpServletRequest with a StripesServletRequest. This is done to ensure that any form posts that + * contain file uploads get handled appropriately. + * @param servletRequest the HttpServletRequest handed to the dispatcher by the container + * @return an instance of StripesRequestWrapper, which is an HttpServletRequestWrapper + * @throws StripesServletException if the wrapper cannot be constructed + */ + @Override + protected StripesRequestWrapper wrapRequest(HttpServletRequest servletRequest) throws StripesServletException + { + try { + return StripesRequestWrapper.findStripesWrapper(servletRequest); + } + catch (IllegalStateException e) + { + StripesRequestWrapper srw = new StripesRequestWrapper(servletRequest); + + if (MgnlContext.hasInstance()) + { + // be sure that the request wrapper gets setted in mgnlcontext too + WebContext webContext = (WebContext) MgnlContext.getInstance(); + RepositoryAcquiringStrategy strategy = ((WebContextImpl) webContext).getRepositoryStrategy(); + webContext.init(servletRequest, webContext.getResponse(), webContext.getServletContext()); + ((WebContextImpl) webContext).setRepositoryStrategy(strategy); + } + return srw; + } + } + + public boolean matches(HttpServletRequest request) + { + return isEnabled() && matchesDispatching(request) && mapsTo(request) && !bypasses(request); + } + + protected boolean mapsTo(HttpServletRequest request) + { + if (getMapping().getMappings().isEmpty()) + { return true; } - if (MgnlContext.hasInstance()) + return getMapping().match(request).isMatching(); + } + + protected boolean matchesDispatching(HttpServletRequest request) + { + if (webContainerResources == null) { - return Voting.Factory.getDefaultVoting().vote(bypasses, request) > 0; + return true; } + boolean toWebContainerResource = webContainerResources.isWebContainerResource(request); + boolean toMagnoliaResource = !toWebContainerResource; + + DispatchRule dispatchRule = getDispatchRules().getDispatchRule(ServletUtils.getDispatcherType(request)); + if (toMagnoliaResource && dispatchRule.isToMagnoliaResources()) + return true; + if (toWebContainerResource && dispatchRule.isToWebContainerResources()) + return true; return false; } + protected boolean bypasses(HttpServletRequest request) + { + Voting voting = Voting.HIGHEST_LEVEL; + if (voting.vote(bypasses, request) > 0) + return true; + + return false; + } + + public void destroy() + { + // nothing to do here + } + public Voter[] getBypasses() { return this.bypasses; @@ -95,44 +185,54 @@ this.enabled = enabled; } - /** - * {@inheritDoc} - */ - @Override - public void init(FilterConfig filterConfig) throws ServletException + public DispatchRules getDispatchRules() { - super.init(filterConfig); - filterConfig.getServletContext().setAttribute(Configuration.class.getName(), getInstanceConfiguration()); + return dispatchRules; } - /** - * Wraps the HttpServletRequest with a StripesServletRequest. This is done to ensure that any form posts that - * contain file uploads get handled appropriately. - * @param servletRequest the HttpServletRequest handed to the dispatcher by the container - * @return an instance of StripesRequestWrapper, which is an HttpServletRequestWrapper - * @throws StripesServletException if the wrapper cannot be constructed - */ - @Override - protected StripesRequestWrapper wrapRequest(HttpServletRequest servletRequest) throws StripesServletException + public void setDispatchRules(DispatchRules dispatching) { - try + this.dispatchRules = dispatching; + } + + public Collection<String> getMappings() + { + ArrayList<String> result = new ArrayList<String>(); + for (Pattern pattern : getMapping().getMappings()) { - return StripesRequestWrapper.findStripesWrapper(servletRequest); + result.add(pattern.pattern()); } - catch (IllegalStateException e) - { - StripesRequestWrapper srw = new StripesRequestWrapper(servletRequest); + return result; + } - if (MgnlContext.hasInstance()) - { - // be sure that the request wrapper gets setted in mgnlcontext too - WebContext webContext = (WebContext) MgnlContext.getInstance(); - RepositoryAcquiringStrategy strategy = ((WebContextImpl) webContext).getRepositoryStrategy(); - webContext.init(servletRequest, webContext.getResponse(), webContext.getServletContext()); - ((WebContextImpl) webContext).setRepositoryStrategy(strategy); - } - return srw; - } + protected Mapping getMapping() + { + return mapping; } + public void addMapping(String mapping) + { + this.getMapping().addMapping(mapping); + } + + // ---- utility methods ----- + protected boolean acceptsGzipEncoding(HttpServletRequest request) + { + return RequestHeaderUtil.acceptsGzipEncoding(request); + } + + protected boolean acceptsEncoding(final HttpServletRequest request, final String name) + { + return RequestHeaderUtil.acceptsEncoding(request, name); + } + + protected boolean headerContains(final HttpServletRequest request, final String header, final String value) + { + return RequestHeaderUtil.headerContains(request, header, value); + } + + protected void addAndVerifyHeader(HttpServletResponse response, String name, String value) + { + RequestHeaderUtil.addAndVerifyHeader(response, name, value); + } } Modified: trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java =================================================================== --- trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java 2010-12-08 20:35:51 UTC (rev 3170) +++ trunk/openutils-mgnlstripes/src/main/java/it/openutils/magnoliastripes/StripesRenderer.java 2010-12-08 20:43:32 UTC (rev 3171) @@ -165,9 +165,6 @@ return MgnlContext.getWebContext("StripesRenderer can only be used with a WebContext"); } - /** - * {@inheritDoc} - */ protected void renderCommon(String templatePath, Map<String, String[]> nodeDataMap, Writer out) throws IOException { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |