From 85b219813f982ad28059197c6ebcb0b099f7e4b3 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 25 Nov 2015 20:22:31 +0100 Subject: [PATCH 01/28] Adds required dependencies --- plugins/tiles/pom.xml | 29 +++++++++++++++++++++++++++++ pom.xml | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/plugins/tiles/pom.xml b/plugins/tiles/pom.xml index 3d2a01c5c..57d083f55 100644 --- a/plugins/tiles/pom.xml +++ b/plugins/tiles/pom.xml @@ -38,16 +38,45 @@ org.apache.tiles tiles-core + + org.apache.tiles + tiles-servlet + org.apache.tiles tiles-jsp runtime + + org.apache.tiles + tiles-servlet-wildcard + + + org.apache.tiles + tiles-jsp + + + org.apache.tiles + tiles-freemarker + + + org.apache.tiles + tiles-ognl + + + org.apache.tiles + tiles-el + javax.servlet jsp-api provided + + javax.el + el-api + provided + diff --git a/pom.xml b/pom.xml index 5b33c88ce..098044d63 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ 3.1.1 3.3 5.0.2 - 2.0.6 + 2.2.2 2.3 @@ -576,6 +576,13 @@ provided + + javax.el + el-api + 1.0 + provided + + taglibs standard @@ -602,12 +609,42 @@ tiles-core ${tiles.version} + + org.apache.tiles + tiles-servlet + ${tiles.version} + org.apache.tiles tiles-jsp ${tiles.version} runtime + + org.apache.tiles + tiles-servlet-wildcard + ${tiles.version} + + + org.apache.tiles + tiles-jsp + ${tiles.version} + + + org.apache.tiles + tiles-freemarker + ${tiles.version} + + + org.apache.tiles + tiles-ognl + ${tiles.version} + + + org.apache.tiles + tiles-el + ${tiles.version} + javax.servlet From 70716e945295526d207cab6c66306a3113f8a333 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 25 Nov 2015 20:29:18 +0100 Subject: [PATCH 02/28] Re-implements tiles integration based on new API --- .../StrutsFreeMarkerAttributeRenderer.java | 72 +++++ .../tiles/StrutsTilesContainerFactory.java | 284 ++++++++++++++---- .../struts2/tiles/StrutsTilesInitializer.java | 47 +++ .../struts2/tiles/StrutsTilesListener.java | 56 +--- 4 files changed, 359 insertions(+), 100 deletions(-) create mode 100644 plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java create mode 100644 plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java new file mode 100644 index 000000000..e2669c791 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java @@ -0,0 +1,72 @@ +package org.apache.struts2.tiles; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.config.ConfigurationException; +import com.opensymphony.xwork2.inject.Container; +import freemarker.template.TemplateException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.views.freemarker.FreemarkerResult; +import org.apache.tiles.Attribute; +import org.apache.tiles.context.TilesRequestContext; +import org.apache.tiles.impl.InvalidTemplateException; +import org.apache.tiles.renderer.impl.AbstractTypeDetectingAttributeRenderer; +import org.apache.tiles.servlet.context.ServletTilesRequestContext; +import org.apache.tiles.servlet.context.ServletUtil; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +public class StrutsFreeMarkerAttributeRenderer extends AbstractTypeDetectingAttributeRenderer { + + private static Logger LOG = LogManager.getLogger(StrutsFreeMarkerAttributeRenderer.class); + + @Override + public void write(Object value, Attribute attribute, TilesRequestContext request) throws IOException { + if (value != null) { + if (value instanceof String) { + LOG.trace("Rendering freemarker tile ..."); + + ServletTilesRequestContext servletRequest = ServletUtil.getServletRequest(request); + HttpServletRequest httpRequest = servletRequest.getRequest(); + + ActionContext ctx = ServletActionContext.getActionContext(httpRequest); + if (ctx == null) { + throw new ConfigurationException("There is no ActionContext for current request!"); + } + ActionInvocation invocation = ctx.getActionInvocation(); + + String include = (String) value; + FreemarkerResult result = new FreemarkerResult(include); + result.setWriter(request.getWriter()); + + Container container = ctx.getContainer(); + container.inject(result); + + try { + result.doExecute(include, invocation); + } catch (TemplateException e) { + LOG.error("Exception was thrown during rendering value {}: {}", value, e.getMessage()); + throw new InvalidTemplateException(e); + } + } else { + LOG.error("Value {} is not a String, cannot render template!", value); + throw new InvalidTemplateException("Cannot render a template that is not a string: " + String.valueOf(value)); + } + } else { + LOG.error("Value is null, cannot render template!"); + throw new InvalidTemplateException("Cannot render a null template"); + } + } + + public boolean isRenderable(Object value, Attribute attribute, TilesRequestContext request) { + if (value instanceof String) { + String string = (String) value; + return string.startsWith("/") && string.endsWith(".ftl"); + } + return false; + } + +} diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index 491ee02ab..4c5871db7 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -21,73 +21,249 @@ package org.apache.struts2.tiles; +import ognl.OgnlException; +import ognl.OgnlRuntime; +import ognl.PropertyAccessor; import org.apache.tiles.TilesApplicationContext; -import org.apache.tiles.TilesException; -import org.apache.tiles.context.TilesContextFactory; +import org.apache.tiles.TilesContainer; +import org.apache.tiles.context.ChainedTilesRequestContextFactory; import org.apache.tiles.context.TilesRequestContext; -import org.apache.tiles.definition.DefinitionsFactory; -import org.apache.tiles.factory.TilesContainerFactory; +import org.apache.tiles.context.TilesRequestContextFactory; +import org.apache.tiles.definition.DefinitionsFactoryException; +import org.apache.tiles.definition.pattern.DefinitionPatternMatcherFactory; +import org.apache.tiles.definition.pattern.PatternDefinitionResolver; +import org.apache.tiles.definition.pattern.PrefixedPatternDefinitionResolver; +import org.apache.tiles.definition.pattern.regexp.RegexpDefinitionPatternMatcherFactory; +import org.apache.tiles.definition.pattern.wildcard.WildcardDefinitionPatternMatcherFactory; +import org.apache.tiles.el.ELAttributeEvaluator; +import org.apache.tiles.el.JspExpressionFactoryFactory; +import org.apache.tiles.el.TilesContextBeanELResolver; +import org.apache.tiles.el.TilesContextELResolver; +import org.apache.tiles.evaluator.AttributeEvaluatorFactory; +import org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory; +import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator; +import org.apache.tiles.factory.BasicTilesContainerFactory; +import org.apache.tiles.factory.TilesContainerFactoryException; +import org.apache.tiles.freemarker.context.FreeMarkerTilesRequestContextFactory; import org.apache.tiles.impl.BasicTilesContainer; -import org.apache.tiles.preparer.PreparerFactory; +import org.apache.tiles.impl.mgmt.CachingTilesContainer; +import org.apache.tiles.locale.LocaleResolver; +import org.apache.tiles.ognl.ApplicationScopeNestedObjectExtractor; +import org.apache.tiles.ognl.DelegatePropertyAccessor; +import org.apache.tiles.ognl.NestedObjectDelegatePropertyAccessor; +import org.apache.tiles.ognl.OGNLAttributeEvaluator; +import org.apache.tiles.ognl.PropertyAccessorDelegateFactory; +import org.apache.tiles.ognl.RequestScopeNestedObjectExtractor; +import org.apache.tiles.ognl.SessionScopeNestedObjectExtractor; +import org.apache.tiles.ognl.TilesApplicationContextNestedObjectExtractor; +import org.apache.tiles.ognl.TilesContextPropertyAccessorDelegateFactory; +import org.apache.tiles.renderer.AttributeRenderer; +import org.apache.tiles.renderer.TypeDetectingAttributeRenderer; +import org.apache.tiles.renderer.impl.BasicRendererFactory; +import org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer; +import org.apache.tiles.util.URLUtil; +import javax.el.ArrayELResolver; +import javax.el.BeanELResolver; +import javax.el.CompositeELResolver; +import javax.el.ELResolver; +import javax.el.ListELResolver; +import javax.el.MapELResolver; +import javax.el.ResourceBundleELResolver; +import java.io.IOException; +import java.net.URL; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Set; - -public class StrutsTilesContainerFactory extends TilesContainerFactory { - - - @Override - protected void storeContainerDependencies(Object context, Map initParameters, Map configuration, BasicTilesContainer container) throws TilesException { - TilesContextFactory contextFactory = - (TilesContextFactory) createFactory(configuration, - CONTEXT_FACTORY_INIT_PARAM); - - contextFactory = new StrutsTilesContextFactory(contextFactory); - - DefinitionsFactory defsFactory = - (DefinitionsFactory) createFactory(configuration, - DEFINITIONS_FACTORY_INIT_PARAM); - - PreparerFactory prepFactory = - (PreparerFactory) createFactory(configuration, - PREPARER_FACTORY_INIT_PARAM); - - contextFactory.init(configuration); - TilesApplicationContext tilesContext = - contextFactory.createApplicationContext(context); - - container.setDefinitionsFactory(defsFactory); - container.setContextFactory(contextFactory); - container.setPreparerFactory(prepFactory); - container.setApplicationContext(tilesContext); - } +/** + * Dedicated Struts factory to build Tiles container with support for: + * - Freemarker + * - OGNL (as default) + * - EL + * - Wildcards + * + * If you need additional features create your own listener and factory, + * you can base on code from Tiles' CompleteAutoloadTilesContainerFactory + */ +public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { /** - * Wrapper factory, used to decorate the TilesRequestContext with a - * FreemarkerResult aware version. - * + * The freemarker renderer name. */ - class StrutsTilesContextFactory implements TilesContextFactory { + public static final String FREEMARKER_RENDERER_NAME = "freemarker"; - private TilesContextFactory factory; + /** + * Supported pattern types + */ + public static final String PATTERN_WILDCARD = "WILDCARD"; + public static final String PATTERN_REGEXP = "REGEXP"; - public StrutsTilesContextFactory(TilesContextFactory factory) { - this.factory = factory; - } + @Override + protected BasicTilesContainer instantiateContainer(TilesApplicationContext applicationContext) { + return new CachingTilesContainer(); + } - public void init(Map map) { - factory.init(map); - } + @Override + protected List getTilesRequestContextFactoriesToBeChained(ChainedTilesRequestContextFactory parent) { - public TilesApplicationContext createApplicationContext(Object context) { - return factory.createApplicationContext(context); - } + List factories = super.getTilesRequestContextFactoriesToBeChained(parent); - public TilesRequestContext createRequestContext( - TilesApplicationContext tilesApplicationContext, - Object... requestItems) { - TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, requestItems); - return new StrutsTilesRequestContext(context); + registerRequestContextFactory(FreeMarkerTilesRequestContextFactory.class.getName(), factories, parent); + + return factories; + } + + @Override + protected void registerAttributeRenderers( + BasicRendererFactory rendererFactory, + TilesApplicationContext applicationContext, + TilesRequestContextFactory contextFactory, + TilesContainer container, + AttributeEvaluatorFactory attributeEvaluatorFactory) { + + super.registerAttributeRenderers( + rendererFactory, + applicationContext, + contextFactory, + container, + attributeEvaluatorFactory); + + StrutsFreeMarkerAttributeRenderer freemarkerRenderer = new StrutsFreeMarkerAttributeRenderer(); + freemarkerRenderer.setApplicationContext(applicationContext); + freemarkerRenderer.setAttributeEvaluatorFactory(attributeEvaluatorFactory); + freemarkerRenderer.setRequestContextFactory(contextFactory); + + rendererFactory.registerRenderer(FREEMARKER_RENDERER_NAME, freemarkerRenderer); + } + + @Override + protected AttributeRenderer createDefaultAttributeRenderer( + BasicRendererFactory rendererFactory, + TilesApplicationContext applicationContext, + TilesRequestContextFactory contextFactory, + TilesContainer container, + AttributeEvaluatorFactory attributeEvaluatorFactory) { + + ChainedDelegateAttributeRenderer retValue = new ChainedDelegateAttributeRenderer(); + + retValue.addAttributeRenderer((TypeDetectingAttributeRenderer) rendererFactory + .getRenderer(DEFINITION_RENDERER_NAME)); + retValue.addAttributeRenderer((TypeDetectingAttributeRenderer) rendererFactory + .getRenderer(FREEMARKER_RENDERER_NAME)); + retValue.addAttributeRenderer((TypeDetectingAttributeRenderer) rendererFactory + .getRenderer(TEMPLATE_RENDERER_NAME)); + retValue.addAttributeRenderer((TypeDetectingAttributeRenderer) rendererFactory + .getRenderer(STRING_RENDERER_NAME)); + + retValue.setApplicationContext(applicationContext); + retValue.setRequestContextFactory(contextFactory); + retValue.setAttributeEvaluatorFactory(attributeEvaluatorFactory); + + return retValue; + } + + @Override + protected AttributeEvaluatorFactory createAttributeEvaluatorFactory( + TilesApplicationContext applicationContext, + TilesRequestContextFactory contextFactory, + LocaleResolver resolver) { + + BasicAttributeEvaluatorFactory attributeEvaluatorFactory = new BasicAttributeEvaluatorFactory(new DirectAttributeEvaluator()); + attributeEvaluatorFactory.registerAttributeEvaluator("OGNL", createOGNLEvaluator()); + attributeEvaluatorFactory.registerAttributeEvaluator("EL", createELEvaluator(applicationContext)); + + return attributeEvaluatorFactory; + } + + @Override + protected PatternDefinitionResolver createPatternDefinitionResolver(Class customizationKeyClass) { + + DefinitionPatternMatcherFactory wildcardFactory = new WildcardDefinitionPatternMatcherFactory(); + DefinitionPatternMatcherFactory regexpFactory = new RegexpDefinitionPatternMatcherFactory(); + + PrefixedPatternDefinitionResolver resolver = new PrefixedPatternDefinitionResolver<>(); + resolver.registerDefinitionPatternMatcherFactory(PATTERN_WILDCARD, wildcardFactory); + resolver.registerDefinitionPatternMatcherFactory(PATTERN_REGEXP, regexpFactory); + + return resolver; + } + + @Override + protected List getSourceURLs(TilesApplicationContext applicationContext, + TilesRequestContextFactory contextFactory) { + try { + Set finalSet = new HashSet<>(); + Set webINFSet = applicationContext.getResources("/WEB-INF/**/tiles*.xml"); + Set metaINFSet = applicationContext.getResources("classpath*:META-INF/**/tiles*.xml"); + + if (webINFSet != null) { + finalSet.addAll(webINFSet); + } + if (metaINFSet != null) { + finalSet.addAll(metaINFSet); + } + + return URLUtil.getBaseTilesDefinitionURLs(finalSet); + } catch (IOException e) { + throw new DefinitionsFactoryException("Cannot load definition URLs", e); } } -} + + protected ELAttributeEvaluator createELEvaluator(TilesApplicationContext applicationContext) { + + ELAttributeEvaluator evaluator = new ELAttributeEvaluator(); + evaluator.setApplicationContext(applicationContext); + JspExpressionFactoryFactory efFactory = new JspExpressionFactoryFactory(); + efFactory.setApplicationContext(applicationContext); + evaluator.setExpressionFactory(efFactory.getExpressionFactory()); + + ELResolver elResolver = new CompositeELResolver() { + { + add(new TilesContextELResolver()); + add(new TilesContextBeanELResolver()); + add(new ArrayELResolver(false)); + add(new ListELResolver(false)); + add(new MapELResolver(false)); + add(new ResourceBundleELResolver()); + add(new BeanELResolver(false)); + } + }; + + evaluator.setResolver(elResolver); + + return evaluator; + } + + protected OGNLAttributeEvaluator createOGNLEvaluator() { + try { + PropertyAccessor objectPropertyAccessor = OgnlRuntime.getPropertyAccessor(Object.class); + PropertyAccessor mapPropertyAccessor = OgnlRuntime.getPropertyAccessor(Map.class); + PropertyAccessor applicationContextPropertyAccessor = + new NestedObjectDelegatePropertyAccessor<>( + new TilesApplicationContextNestedObjectExtractor(), + objectPropertyAccessor); + PropertyAccessor requestScopePropertyAccessor = + new NestedObjectDelegatePropertyAccessor<>( + new RequestScopeNestedObjectExtractor(), mapPropertyAccessor); + PropertyAccessor sessionScopePropertyAccessor = + new NestedObjectDelegatePropertyAccessor<>( + new SessionScopeNestedObjectExtractor(), mapPropertyAccessor); + PropertyAccessor applicationScopePropertyAccessor = + new NestedObjectDelegatePropertyAccessor<>( + new ApplicationScopeNestedObjectExtractor(), mapPropertyAccessor); + PropertyAccessorDelegateFactory factory = + new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, applicationContextPropertyAccessor, + requestScopePropertyAccessor, sessionScopePropertyAccessor, + applicationScopePropertyAccessor); + PropertyAccessor tilesRequestAccessor = new DelegatePropertyAccessor<>(factory); + OgnlRuntime.setPropertyAccessor(TilesRequestContext.class, tilesRequestAccessor); + return new OGNLAttributeEvaluator(); + } catch (OgnlException e) { + throw new TilesContainerFactoryException("Cannot initialize OGNL evaluator", e); + } + } + +} \ No newline at end of file diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java new file mode 100644 index 000000000..b2e2700e8 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.tiles; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.tiles.TilesApplicationContext; +import org.apache.tiles.factory.AbstractTilesContainerFactory; +import org.apache.tiles.servlet.wildcard.WildcardServletTilesApplicationContext; +import org.apache.tiles.startup.AbstractTilesInitializer; + +import javax.servlet.ServletContext; + +public class StrutsTilesInitializer extends AbstractTilesInitializer { + + private static final Logger LOG = LogManager.getLogger(StrutsTilesInitializer.class); + + @Override + protected TilesApplicationContext createTilesApplicationContext(TilesApplicationContext preliminaryContext) { + LOG.debug("Initializing Tiles wildcard support ..."); + return new WildcardServletTilesApplicationContext((ServletContext) preliminaryContext.getContext()); + } + + @Override + protected AbstractTilesContainerFactory createContainerFactory(TilesApplicationContext context) { + LOG.trace("Creating dedicated Struts factory to create Tiles container"); + return new StrutsTilesContainerFactory(); + } + +} diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java index b04908af1..cf874b1d0 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesListener.java @@ -1,6 +1,4 @@ /* - * $Id$ - * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -21,57 +19,23 @@ package org.apache.struts2.tiles; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.ServletContext; - -import org.apache.tiles.TilesContainer; -import org.apache.tiles.TilesException; -import org.apache.tiles.factory.TilesContainerFactory; -import org.apache.tiles.web.startup.TilesListener; - -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.tiles.startup.TilesInitializer; +import org.apache.tiles.web.startup.AbstractTilesListener; /** - * Listener used to automatically inject ServletContext - * init parameters so that they don't need to be configured - * explicitly for tiles integration. This is provided - * mainly for backwards compatibility with Struts 2.0.1 - * configuration. + * Listener used to automatically tie Tiles support into Struts * * @since Struts 2.0.2 - * @version $Rev$ - * */ -public class StrutsTilesListener extends TilesListener { +public class StrutsTilesListener extends AbstractTilesListener { private static final Logger LOG = LogManager.getLogger(StrutsTilesListener.class); - private static final Map INIT; - - static { - INIT = new HashMap(); - INIT.put(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM, - StrutsTilesContainerFactory.class.getName()); + @Override + protected TilesInitializer createTilesInitializer() { + LOG.info("Starting Struts Tiles 2 integration ..."); + return new StrutsTilesInitializer(); } - - protected TilesContainer createContainer(ServletContext context) - throws TilesException { - if(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM) == null) { - context = decorate(context); - } - else { - if (LOG.isWarnEnabled()) { - LOG.warn("Tiles container factory is explicitly set. Not injecting struts configuration."); - } - } - return super.createContainer(context); - } - - protected ServletContext decorate(ServletContext context) { - return new ConfiguredServletContext(context, INIT); - } - -} +} \ No newline at end of file From 24a5bce6c436ddaea9f65f792b280fdd6f9c4ae3 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 25 Nov 2015 20:29:24 +0100 Subject: [PATCH 03/28] Drops unused classes --- .../tiles/ConfiguredServletContext.java | 187 ------------------ .../tiles/StrutsTilesRequestContext.java | 125 ------------ 2 files changed, 312 deletions(-) delete mode 100644 plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java delete mode 100644 plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java deleted file mode 100644 index dfc1408de..000000000 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/ConfiguredServletContext.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * $Id$ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.struts2.tiles; - - -import javax.servlet.RequestDispatcher; -import javax.servlet.Servlet; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.*; - -/** - * ServletContext implementation which allows Struts - * to inject initialization parameters into the context - * in order to reduce the amount of configuration required - * within web.xml for using Tiles. - * - * The specified init parameters are only utilized if - * they are not explicitaly defined in the web.xml - * - * @version $Rev$ - * @since Struts 2.0.1 - */ -@SuppressWarnings("deprecation") -public class ConfiguredServletContext implements ServletContext { - - private ServletContext rootContext; - private Map initParameters; - - - public ConfiguredServletContext(ServletContext context, Map initParameters) { - this.rootContext = context; - this.initParameters = initParameters; - } - - public ServletContext getContext(String string) { - return rootContext.getContext(string); - } - - public int getMajorVersion() { - return rootContext.getMajorVersion(); - } - - public int getMinorVersion() { - return rootContext.getMinorVersion(); - } - - public String getMimeType(String string) { - return rootContext.getMimeType(string); - } - - public Set getResourcePaths(String string) { - return rootContext.getResourcePaths(string); - } - - public URL getResource(String string) throws MalformedURLException { - return rootContext.getResource(string); - } - - public InputStream getResourceAsStream(String string) { - return rootContext.getResourceAsStream(string); - } - - public RequestDispatcher getRequestDispatcher(String string) { - return rootContext.getRequestDispatcher(string); - } - - public RequestDispatcher getNamedDispatcher(String string) { - return rootContext.getNamedDispatcher(string); - } - - @SuppressWarnings("deprecation") - public Servlet getServlet(String string) throws ServletException { - return rootContext.getServlet(string); - } - - @SuppressWarnings("deprecation") - public Enumeration getServlets() { - return rootContext.getServlets(); //To change body of implemented methods use File | Settings | File Templates. - } - - @SuppressWarnings("deprecation") - public Enumeration getServletNames() { - return rootContext.getServletNames(); - } - - public void log(String string) { - rootContext.log(string); - } - - @SuppressWarnings("deprecation") - public void log(Exception exception, String string) { - rootContext.log(exception, string); - } - - public void log(String string, Throwable throwable) { - rootContext.log(string, throwable); - } - - public String getRealPath(String string) { - return rootContext.getRealPath(string); - } - - public String getServerInfo() { - return rootContext.getServerInfo(); - } - - public String getInitParameter(String string) { - String parm = rootContext.getInitParameter(string); - if (parm == null) { - return initParameters.get(string); - } - return parm; - } - - public Enumeration getInitParameterNames() { - return new CompositeEnumeration( - rootContext.getInitParameterNames(), - initParameters.keySet().iterator()); - } - - public Object getAttribute(String string) { - return rootContext.getAttribute(string); - } - - public Enumeration getAttributeNames() { - return rootContext.getAttributeNames(); - } - - public void setAttribute(String string, Object object) { - rootContext.setAttribute(string, object); - } - - public void removeAttribute(String string) { - rootContext.removeAttribute(string); - } - - public String getServletContextName() { - return rootContext.getServletContextName(); - } - - class CompositeEnumeration implements Enumeration { - - private Enumeration first; - private Iterator second; - - - public CompositeEnumeration(Enumeration first, Iterator second) { - this.first = first; - this.second = second; - } - - public boolean hasMoreElements() { - return first.hasMoreElements() || second.hasNext(); - } - - public Object nextElement() { - if (first.hasMoreElements()) { - return first.nextElement(); - } - - return second.next(); - } - } -} diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java deleted file mode 100644 index d9319a4f1..000000000 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesRequestContext.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * $Id$ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.struts2.tiles; - -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.ActionInvocation; -import com.opensymphony.xwork2.inject.Container; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.views.freemarker.FreemarkerResult; -import org.apache.tiles.context.TilesRequestContext; -import org.apache.tiles.context.TilesRequestContextWrapper; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -/** - *

- * Default implementation of TilesUtil. - * This class contains default implementation of utilities. This implementation - * is intended to be used without Struts. - *

- * - *

- * TilesUtilImpl implementation used to intercept .ftl requests and - * ensure that they are setup properly to take advantage of the - * {@link FreemarkerResult}. - *

- */ -public class StrutsTilesRequestContext extends TilesRequestContextWrapper { - - private static final Logger LOG = LogManager.getLogger(StrutsTilesRequestContext.class); - - /** - * The mask used to detect requests which should be intercepted. - */ - private String mask; - - /** - * Default constructor. - * Sets the mask to '.ftl' - * - * @param context - */ - public StrutsTilesRequestContext(TilesRequestContext context) { - this(context, ".ftl"); - } - - /** - * Optional constructor used to specify a specific mask. - * - * @param mask - * @param context - */ - public StrutsTilesRequestContext(TilesRequestContext context, String mask) { - super(context); - this.mask = mask; - } - - public void dispatch(String include) throws IOException { - if (include.endsWith(mask)) { - // FIXME This way FreeMarker results still don't have a content-type! - include(include); - } else { - super.dispatch(include); - } - } - - /** - * Enhancement of the default include which allows for freemarker - * templates to be intercepted so that the FreemarkerResult can - * be used in order to setup the appropriate model. - * - * @throws IOException - */ - public void include(String include) throws IOException { - if (include.endsWith(mask)) { - if (LOG.isDebugEnabled()) { - LOG.debug("Intercepting tiles include '" + include + "'. Processing as freemarker result."); - } - HttpServletRequest request = (HttpServletRequest) getRequest(); - HttpServletResponse response = (HttpServletResponse) getResponse(); - - ActionContext ctx = ServletActionContext.getActionContext(request); - ActionInvocation invocation = ctx.getActionInvocation(); - - try { - FreemarkerResult result = new FreemarkerResult(); - result.setWriter(response.getWriter()); - - Container container = ctx.getContainer(); - container.inject(result); - - result.doExecute(include, invocation); - } catch (Exception e) { - LOG.error("Error invoking Freemarker template", e); - throw new IOException("Error invoking Freemarker template." + e.getMessage()); - } - } else { - super.include(include); - } - } - -} From 26f153d262f7a4dfb767c391afece8ac1d9e1c0d Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 25 Nov 2015 20:29:43 +0100 Subject: [PATCH 04/28] Replaces call to deprecated api --- .../java/org/apache/struts2/views/tiles/TilesResult.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/views/tiles/TilesResult.java b/plugins/tiles/src/main/java/org/apache/struts2/views/tiles/TilesResult.java index 490de7966..72d7916fc 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/views/tiles/TilesResult.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/views/tiles/TilesResult.java @@ -28,9 +28,9 @@ import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.result.ServletDispatcherResult; import org.apache.tiles.TilesContainer; -import org.apache.tiles.access.TilesAccess; import com.opensymphony.xwork2.ActionInvocation; +import org.apache.tiles.servlet.context.ServletUtil; /** * @@ -84,6 +84,7 @@ public class TilesResult extends ServletDispatcherResult { public TilesResult(String location) { super(location); } + /** * Dispatches to the given location. Does its forward via a RequestDispatcher. If the * dispatch fails a 404 error will be sent back in the http response. @@ -97,7 +98,8 @@ public class TilesResult extends ServletDispatcherResult { setLocation(location); ServletContext servletContext = ServletActionContext.getServletContext(); - TilesContainer container = TilesAccess.getContainer(servletContext); + + TilesContainer container = ServletUtil.getContainer(servletContext); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); From 6edc0ff5e0410651f7055654116c3346aeaa4ee5 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 25 Nov 2015 20:37:42 +0100 Subject: [PATCH 05/28] Updates Portal Tiles integration --- plugins/portlet-tiles/pom.xml | 4 +++ .../views/tiles/PortletTilesResult.java | 25 ++++++------------- pom.xml | 5 ++++ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/plugins/portlet-tiles/pom.xml b/plugins/portlet-tiles/pom.xml index bb8b6e134..9e9a81faf 100644 --- a/plugins/portlet-tiles/pom.xml +++ b/plugins/portlet-tiles/pom.xml @@ -42,6 +42,10 @@ org.apache.struts struts2-portlet-plugin
+ + org.apache.tiles + tiles-portlet + javax.servlet jsp-api diff --git a/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java b/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java index d10693ceb..736fb8596 100644 --- a/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java +++ b/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java @@ -9,6 +9,7 @@ import org.apache.struts2.portlet.context.PortletActionContext; import org.apache.tiles.TilesContainer; import org.apache.tiles.TilesException; import org.apache.tiles.access.TilesAccess; +import org.apache.tiles.portlet.context.PortletUtil; import javax.portlet.ActionResponse; import javax.portlet.PortletException; @@ -19,11 +20,13 @@ import java.io.IOException; import java.util.Map; /** - * JIRA WW-2749 (STRUTS). + * Dedicated Tile result to be used in Portlet environment + * + * WW-2749 */ public class PortletTilesResult extends ServletDispatcherResult { - private static final long serialVersionUID = -3806939435493086244L; + public static final String TILES_ACTION_NAME = "tilesDirect"; public PortletTilesResult() { super(); @@ -33,10 +36,7 @@ public class PortletTilesResult extends ServletDispatcherResult { super(location); } - // FIXME PATCH du JIRA WW-2749 (STRUTS) - public void doExecute(String location, ActionInvocation invocation) - throws IOException, TemplateException, PortletException, TilesException { - + public void doExecute(String location, ActionInvocation invocation) throws Exception { if (PortletActionContext.getPhase().isAction() || PortletActionContext.getPhase().isEvent()) { executeActionResult(location, invocation); } else { @@ -44,15 +44,10 @@ public class PortletTilesResult extends ServletDispatcherResult { } } - /** - * @param location - * @throws TilesException - */ protected void executeRenderResult(String location) throws TilesException { setLocation(location); - ServletContext servletContext = ServletActionContext.getServletContext(); - TilesContainer container = TilesAccess.getContainer(servletContext); + TilesContainer container = PortletUtil.getContainer(PortletActionContext.getPortletContext()); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); @@ -60,14 +55,10 @@ public class PortletTilesResult extends ServletDispatcherResult { container.render(location, request, response); } - /** - * @param location - * @param invocation - */ protected void executeActionResult(String location, ActionInvocation invocation) { ActionResponse res = PortletActionContext.getActionResponse(); - res.setRenderParameter(PortletConstants.ACTION_PARAM, "tilesDirect"); + res.setRenderParameter(PortletConstants.ACTION_PARAM, TILES_ACTION_NAME); Map sessionMap = invocation.getInvocationContext().getSession(); sessionMap.put(PortletConstants.RENDER_DIRECT_LOCATION, location); diff --git a/pom.xml b/pom.xml index 098044d63..def253947 100644 --- a/pom.xml +++ b/pom.xml @@ -614,6 +614,11 @@ tiles-servlet ${tiles.version} + + org.apache.tiles + tiles-portlet + ${tiles.version} + org.apache.tiles tiles-jsp From 27ad6cb13e9fa75d48f8e5cf26687b49af9ed213 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 25 Nov 2015 20:54:39 +0100 Subject: [PATCH 06/28] Adds missing header with license --- .../views/tiles/PortletTilesResult.java | 21 +++++++++++++++++++ .../StrutsFreeMarkerAttributeRenderer.java | 19 +++++++++++++++++ .../tiles/StrutsTilesContainerFactory.java | 2 -- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java b/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java index 736fb8596..83fcd4e36 100644 --- a/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java +++ b/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java @@ -1,3 +1,24 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.struts2.views.tiles; import com.opensymphony.xwork2.ActionInvocation; diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java index e2669c791..032f7e663 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java @@ -1,3 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.apache.struts2.tiles; import com.opensymphony.xwork2.ActionContext; diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index 4c5871db7..1b01a8d4e 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -1,6 +1,4 @@ /* - * $Id$ - * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information From 388861ff073f54f7959a35cc205b264237d043d0 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Fri, 27 Nov 2015 13:36:52 +0100 Subject: [PATCH 07/28] Registers Tiles model --- .../StrutsFreeMarkerAttributeRenderer.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java index 032f7e663..849053a22 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java @@ -23,18 +23,25 @@ import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.config.ConfigurationException; import com.opensymphony.xwork2.inject.Container; +import freemarker.ext.beans.BeanModel; +import freemarker.template.Configuration; import freemarker.template.TemplateException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.struts2.ServletActionContext; +import org.apache.struts2.views.JspSupportServlet; +import org.apache.struts2.views.freemarker.FreemarkerManager; import org.apache.struts2.views.freemarker.FreemarkerResult; +import org.apache.struts2.views.freemarker.StrutsBeanWrapper; import org.apache.tiles.Attribute; import org.apache.tiles.context.TilesRequestContext; +import org.apache.tiles.freemarker.template.TilesFMModelRepository; import org.apache.tiles.impl.InvalidTemplateException; import org.apache.tiles.renderer.impl.AbstractTypeDetectingAttributeRenderer; import org.apache.tiles.servlet.context.ServletTilesRequestContext; import org.apache.tiles.servlet.context.ServletUtil; +import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.io.IOException; @@ -55,7 +62,8 @@ public class StrutsFreeMarkerAttributeRenderer extends AbstractTypeDetectingAttr if (ctx == null) { throw new ConfigurationException("There is no ActionContext for current request!"); } - ActionInvocation invocation = ctx.getActionInvocation(); + + registerTilesBeanModel(ctx); String include = (String) value; FreemarkerResult result = new FreemarkerResult(include); @@ -65,6 +73,7 @@ public class StrutsFreeMarkerAttributeRenderer extends AbstractTypeDetectingAttr container.inject(result); try { + ActionInvocation invocation = ctx.getActionInvocation(); result.doExecute(include, invocation); } catch (TemplateException e) { LOG.error("Exception was thrown during rendering value {}: {}", value, e.getMessage()); @@ -88,4 +97,20 @@ public class StrutsFreeMarkerAttributeRenderer extends AbstractTypeDetectingAttr return false; } + /** + * This register dedicated BeanModel to support tiles tags. + * It requires {@link org.apache.struts2.views.JspSupportServlet} to be registered in web.xml + */ + protected void registerTilesBeanModel(ActionContext ctx) { + ServletContext servletContext = ServletActionContext.getServletContext(); + Configuration configuration = ctx.getInstance(FreemarkerManager.class).getConfiguration(servletContext); + + StrutsBeanWrapper wrapper = (StrutsBeanWrapper) ctx.getInstance(FreemarkerManager.class).getWrapper(); + + LOG.trace("Adding support for Tiles tags, please remember to register {} in web.xml!", JspSupportServlet.class.getName()); + + BeanModel tilesBeanModel = new BeanModel(new TilesFMModelRepository(), wrapper); + configuration.setSharedVariable("tiles", tilesBeanModel); + } + } From 6622fab25bee1dae9085cca56b87711f3197916d Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Fri, 27 Nov 2015 16:09:37 +0100 Subject: [PATCH 08/28] Fixes dependency clash --- apps/showcase/pom.xml | 5 ----- plugins/bean-validation/pom.xml | 1 - plugins/tiles/pom.xml | 7 +++---- pom.xml | 7 +++---- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/apps/showcase/pom.xml b/apps/showcase/pom.xml index 7173275e0..4e6044beb 100644 --- a/apps/showcase/pom.xml +++ b/apps/showcase/pom.xml @@ -171,11 +171,6 @@ hibernate-validator 5.1.3.Final - - org.glassfish - javax.el - 3.0.0 - diff --git a/plugins/bean-validation/pom.xml b/plugins/bean-validation/pom.xml index b7318efe8..f2500c52b 100644 --- a/plugins/bean-validation/pom.xml +++ b/plugins/bean-validation/pom.xml @@ -58,7 +58,6 @@ org.glassfish javax.el - 3.0.0 test diff --git a/plugins/tiles/pom.xml b/plugins/tiles/pom.xml index 57d083f55..45a9b7e68 100644 --- a/plugins/tiles/pom.xml +++ b/plugins/tiles/pom.xml @@ -73,11 +73,10 @@ provided - javax.el - el-api - provided + org.glassfish + javax.el + true - UTF-8 diff --git a/pom.xml b/pom.xml index def253947..d5489ec6a 100644 --- a/pom.xml +++ b/pom.xml @@ -577,10 +577,9 @@ - javax.el - el-api - 1.0 - provided + org.glassfish + javax.el + 3.0.0 From 2d0fe9f742c9add4a1803d3165a3ed906551dc45 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 08:19:26 +0100 Subject: [PATCH 09/28] Drops Tiles 3 plugin --- plugins/tiles3/pom.xml | 60 ------ .../struts2/views/tiles/TilesResult.java | 81 -------- plugins/tiles3/src/main/resources/LICENSE.txt | 174 ------------------ plugins/tiles3/src/main/resources/NOTICE.txt | 5 - .../src/main/resources/struts-plugin.xml | 34 ---- plugins/tiles3/src/site/site.xml | 57 ------ 6 files changed, 411 deletions(-) delete mode 100644 plugins/tiles3/pom.xml delete mode 100644 plugins/tiles3/src/main/java/org/apache/struts2/views/tiles/TilesResult.java delete mode 100644 plugins/tiles3/src/main/resources/LICENSE.txt delete mode 100644 plugins/tiles3/src/main/resources/NOTICE.txt delete mode 100644 plugins/tiles3/src/main/resources/struts-plugin.xml delete mode 100644 plugins/tiles3/src/site/site.xml diff --git a/plugins/tiles3/pom.xml b/plugins/tiles3/pom.xml deleted file mode 100644 index a029dff8f..000000000 --- a/plugins/tiles3/pom.xml +++ /dev/null @@ -1,60 +0,0 @@ - - - - 4.0.0 - - org.apache.struts - struts2-plugins - 2.5-SNAPSHOT - - - struts2-tiles3-plugin - jar - Struts 2 Tiles 3 Plugin - - - 3.0.1 - UTF-8 - - - - - org.apache.tiles - tiles-core - ${tiles3.version} - - - org.apache.tiles - tiles-extras - ${tiles3.version} - - - org.apache.tiles - tiles-jsp - ${tiles3.version} - runtime - - - - diff --git a/plugins/tiles3/src/main/java/org/apache/struts2/views/tiles/TilesResult.java b/plugins/tiles3/src/main/java/org/apache/struts2/views/tiles/TilesResult.java deleted file mode 100644 index 6bc15bb4e..000000000 --- a/plugins/tiles3/src/main/java/org/apache/struts2/views/tiles/TilesResult.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * $Id$ - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.struts2.views.tiles; - -import com.opensymphony.xwork2.ActionInvocation; -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.result.ServletDispatcherResult; -import org.apache.tiles.TilesContainer; -import org.apache.tiles.access.TilesAccess; -import org.apache.tiles.request.ApplicationContext; -import org.apache.tiles.request.servlet.ServletRequest; -import org.apache.tiles.request.servlet.ServletUtil; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * First implementation of Tiles 3 support - * - * Please follow the link to read more how to configure the result - * http://stackoverflow.com/questions/13337938/how-to-integrate-struts-2-with-tiles-3 - * - * or check the docs - * - * https://cwiki.apache.org/confluence/display/WW/Tiles+3+Plugin - * - * - * @author Ken McWilliams - */ -public class TilesResult extends ServletDispatcherResult { - - public TilesResult() { - super(); - } - - public TilesResult(String location) { - super(location); - } - - @Override - public void doExecute(String location, ActionInvocation invocation) throws Exception { - ServletContext context = ServletActionContext.getServletContext(); - HttpServletRequest request = ServletActionContext.getRequest(); - HttpServletResponse response = ServletActionContext.getResponse(); - - ApplicationContext applicationContext = ServletUtil.getApplicationContext(context); - ServletRequest servletRequest = new ServletRequest(applicationContext, request, response); - - TilesContainer container = initTilesContainer(applicationContext, servletRequest); - - container.startContext(servletRequest); - container.render(location, servletRequest); - } - - protected TilesContainer initTilesContainer(ApplicationContext applicationContext, ServletRequest servletRequest) { - TilesContainer container = TilesAccess.getContainer(applicationContext); - TilesAccess.setCurrentContainer(servletRequest, container); - return container; - } - -} diff --git a/plugins/tiles3/src/main/resources/LICENSE.txt b/plugins/tiles3/src/main/resources/LICENSE.txt deleted file mode 100644 index dd5b3a58a..000000000 --- a/plugins/tiles3/src/main/resources/LICENSE.txt +++ /dev/null @@ -1,174 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. diff --git a/plugins/tiles3/src/main/resources/NOTICE.txt b/plugins/tiles3/src/main/resources/NOTICE.txt deleted file mode 100644 index bfba90c29..000000000 --- a/plugins/tiles3/src/main/resources/NOTICE.txt +++ /dev/null @@ -1,5 +0,0 @@ -Apache Struts -Copyright 2000-2011 The Apache Software Foundation - -This product includes software developed by -The Apache Software Foundation (http://www.apache.org/). \ No newline at end of file diff --git a/plugins/tiles3/src/main/resources/struts-plugin.xml b/plugins/tiles3/src/main/resources/struts-plugin.xml deleted file mode 100644 index 0018a5f37..000000000 --- a/plugins/tiles3/src/main/resources/struts-plugin.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - diff --git a/plugins/tiles3/src/site/site.xml b/plugins/tiles3/src/site/site.xml deleted file mode 100644 index 07a667ec7..000000000 --- a/plugins/tiles3/src/site/site.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - - org.apache.maven.skins - maven-fluido-skin - 1.3.1 - - - Apache Software Foundation - http://www.apache.org/images/asf-logo.gif - http://www.apache.org/ - - - Apache Struts - http://struts.apache.org/img/struts-logo.svg - http://struts.apache.org/ - - - - - - - - - - - - -
-
- Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts - project logos are trademarks of The Apache Software Foundation. -
-
- - - From 5fe760dc67f4b3bd16112ac48494782e570dd242 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 08:19:52 +0100 Subject: [PATCH 10/28] Removes module --- plugins/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/pom.xml b/plugins/pom.xml index 2f1fe76ef..ba4418184 100644 --- a/plugins/pom.xml +++ b/plugins/pom.xml @@ -59,7 +59,6 @@ spring testng tiles - tiles3 From 47ecd001ec8b35e4569d43a481e82166eed6cfb1 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 08:51:03 +0100 Subject: [PATCH 11/28] Ports solution from 2.3.x branch --- .../tiles/StrutsTilesContainerFactory.java | 37 +++--- .../struts2/tiles/StrutsTilesInitializer.java | 3 +- ...ildcardServletTilesApplicationContext.java | 106 ++++++++++++++++++ 3 files changed, 129 insertions(+), 17 deletions(-) create mode 100644 plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index 1b01a8d4e..785082b0c 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -27,6 +27,7 @@ import org.apache.tiles.TilesContainer; import org.apache.tiles.context.ChainedTilesRequestContextFactory; import org.apache.tiles.context.TilesRequestContext; import org.apache.tiles.context.TilesRequestContextFactory; +import org.apache.tiles.definition.DefinitionsFactory; import org.apache.tiles.definition.DefinitionsFactoryException; import org.apache.tiles.definition.pattern.DefinitionPatternMatcherFactory; import org.apache.tiles.definition.pattern.PatternDefinitionResolver; @@ -59,6 +60,7 @@ import org.apache.tiles.renderer.AttributeRenderer; import org.apache.tiles.renderer.TypeDetectingAttributeRenderer; import org.apache.tiles.renderer.impl.BasicRendererFactory; import org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer; +import org.apache.tiles.servlet.context.ServletUtil; import org.apache.tiles.util.URLUtil; import javax.el.ArrayELResolver; @@ -68,9 +70,9 @@ import javax.el.ELResolver; import javax.el.ListELResolver; import javax.el.MapELResolver; import javax.el.ResourceBundleELResolver; +import javax.servlet.ServletContext; import java.io.IOException; import java.net.URL; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -98,9 +100,17 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { public static final String PATTERN_WILDCARD = "WILDCARD"; public static final String PATTERN_REGEXP = "REGEXP"; + /** + * Default pattern to be used to collect Tiles definitions if user didn't configure any + */ + public static final String TILES_DEFAULT_PATTERN = "tiles*.xml"; + @Override protected BasicTilesContainer instantiateContainer(TilesApplicationContext applicationContext) { - return new CachingTilesContainer(); + CachingTilesContainer tilesContainer = new CachingTilesContainer(); + ServletContext servletContext = (ServletContext) applicationContext.getContext(); + ServletUtil.setContainer(servletContext, tilesContainer); + return tilesContainer; } @Override @@ -181,7 +191,7 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { DefinitionPatternMatcherFactory wildcardFactory = new WildcardDefinitionPatternMatcherFactory(); DefinitionPatternMatcherFactory regexpFactory = new RegexpDefinitionPatternMatcherFactory(); - PrefixedPatternDefinitionResolver resolver = new PrefixedPatternDefinitionResolver<>(); + PrefixedPatternDefinitionResolver resolver = new PrefixedPatternDefinitionResolver(); resolver.registerDefinitionPatternMatcherFactory(PATTERN_WILDCARD, wildcardFactory); resolver.registerDefinitionPatternMatcherFactory(PATTERN_REGEXP, regexpFactory); @@ -189,19 +199,9 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { } @Override - protected List getSourceURLs(TilesApplicationContext applicationContext, - TilesRequestContextFactory contextFactory) { + protected List getSourceURLs(TilesApplicationContext applicationContext, TilesRequestContextFactory contextFactory) { try { - Set finalSet = new HashSet<>(); - Set webINFSet = applicationContext.getResources("/WEB-INF/**/tiles*.xml"); - Set metaINFSet = applicationContext.getResources("classpath*:META-INF/**/tiles*.xml"); - - if (webINFSet != null) { - finalSet.addAll(webINFSet); - } - if (metaINFSet != null) { - finalSet.addAll(metaINFSet); - } + Set finalSet = applicationContext.getResources(getTilesDefinitionPattern(applicationContext.getInitParams())); return URLUtil.getBaseTilesDefinitionURLs(finalSet); } catch (IOException e) { @@ -209,6 +209,13 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { } } + protected String getTilesDefinitionPattern(Map params) { + if (params.containsKey(DefinitionsFactory.DEFINITIONS_CONFIG)) { + return params.get(DefinitionsFactory.DEFINITIONS_CONFIG); + } + return TILES_DEFAULT_PATTERN; + } + protected ELAttributeEvaluator createELEvaluator(TilesApplicationContext applicationContext) { ELAttributeEvaluator evaluator = new ELAttributeEvaluator(); diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java index b2e2700e8..0ff33c78c 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java @@ -23,7 +23,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.tiles.TilesApplicationContext; import org.apache.tiles.factory.AbstractTilesContainerFactory; -import org.apache.tiles.servlet.wildcard.WildcardServletTilesApplicationContext; import org.apache.tiles.startup.AbstractTilesInitializer; import javax.servlet.ServletContext; @@ -35,7 +34,7 @@ public class StrutsTilesInitializer extends AbstractTilesInitializer { @Override protected TilesApplicationContext createTilesApplicationContext(TilesApplicationContext preliminaryContext) { LOG.debug("Initializing Tiles wildcard support ..."); - return new WildcardServletTilesApplicationContext((ServletContext) preliminaryContext.getContext()); + return new StrutsWildcardServletTilesApplicationContext((ServletContext) preliminaryContext.getContext()); } @Override diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java new file mode 100644 index 000000000..2328f0602 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.tiles; + +import com.opensymphony.xwork2.config.ConfigurationException; +import com.opensymphony.xwork2.util.WildcardUtil; +import com.opensymphony.xwork2.util.finder.ResourceFinder; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.tiles.servlet.context.ServletTilesApplicationContext; + +import javax.servlet.ServletContext; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +public class StrutsWildcardServletTilesApplicationContext extends ServletTilesApplicationContext { + + private static final Logger LOG = LogManager.getLogger(StrutsWildcardServletTilesApplicationContext.class); + + private ResourceFinder finder; + + public StrutsWildcardServletTilesApplicationContext(ServletContext context) { + super(context); + + Set urls = new HashSet<>(); + + for (Object path : context.getResourcePaths("/")) { + try { + URL url = new File(context.getRealPath(String.valueOf(path))).toURI().toURL(); + urls.add(url); + } catch (MalformedURLException e) { + throw new ConfigurationException(e); + } + } + + try { + Enumeration resources = getClass().getClassLoader().getResources("/"); + while (resources.hasMoreElements()) { + URL resource = resources.nextElement(); + urls.add(resource); + } + } catch (IOException e) { + throw new ConfigurationException(e); + } + + finder = new ResourceFinder(urls.toArray(new URL[urls.size()])); + } + + public Set getResources(String path) throws IOException { + Set resources = new HashSet<>(); + + if (path.startsWith("/")) { + LOG.trace("Using ServletContext to load resource #0", path); + URL resource = getResource(path); + if (resource != null) { + resources.add(resource); + } + } + resources.addAll(findResources(path)); + + return resources; + } + + protected Set findResources(String path) throws IOException { + Set resources = new HashSet<>(); + + LOG.trace("Using ResourceFinder to find matches for #0", path); + + Pattern pattern = WildcardUtil.compileWildcardPattern(path); + Map matches = finder.getResourcesMap(""); + + for (String resource : matches.keySet()) { + if (pattern.matcher(resource).matches()) { + resources.add(matches.get(resource)); + } + } + + LOG.trace("Found resources #0 for path #1", resources, path); + return resources; + } + +} From af82d23c95f592bac84f1cac105bc00163c53b4d Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 08:51:14 +0100 Subject: [PATCH 12/28] Upgrade Tiles dependencies --- plugins/tiles/pom.xml | 13 ------------- pom.xml | 13 +------------ 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/plugins/tiles/pom.xml b/plugins/tiles/pom.xml index 45a9b7e68..be3976217 100644 --- a/plugins/tiles/pom.xml +++ b/plugins/tiles/pom.xml @@ -47,14 +47,6 @@ tiles-jsp runtime - - org.apache.tiles - tiles-servlet-wildcard - - - org.apache.tiles - tiles-jsp - org.apache.tiles tiles-freemarker @@ -67,11 +59,6 @@ org.apache.tiles tiles-el - - javax.servlet - jsp-api - provided - org.glassfish javax.el diff --git a/pom.xml b/pom.xml index d5489ec6a..b468f9226 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ 3.1.1 3.3 5.0.2 - 2.2.2 + 3.0.5 2.3 @@ -618,17 +618,6 @@ tiles-portlet ${tiles.version} - - org.apache.tiles - tiles-jsp - ${tiles.version} - runtime - - - org.apache.tiles - tiles-servlet-wildcard - ${tiles.version} - org.apache.tiles tiles-jsp From b675844a8f0de848d200439ed01ebbb532ca9b53 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 08:51:34 +0100 Subject: [PATCH 13/28] Ports solution to resolve problem with EL functions --- .../org/apache/struts2/views/freemarker/FreemarkerManager.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerManager.java b/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerManager.java index ddc7669eb..3323302ad 100644 --- a/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerManager.java +++ b/core/src/main/java/org/apache/struts2/views/freemarker/FreemarkerManager.java @@ -350,6 +350,7 @@ public class FreemarkerManager { servletContext.setAttribute(ATTR_APPLICATION_MODEL, servletContextModel); } TaglibFactory taglibs = new TaglibFactory(servletContext); + taglibs.setObjectWrapper(wrapper); servletContext.setAttribute(ATTR_JSP_TAGLIBS_MODEL, taglibs); } model.put(KEY_APPLICATION, servletContextModel); From 4638b3345757b900edead4a5e4de4c3d37c0eafe Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 10:13:38 +0100 Subject: [PATCH 14/28] Adds missing Tiles API dependency --- plugins/tiles/pom.xml | 4 ++++ pom.xml | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/plugins/tiles/pom.xml b/plugins/tiles/pom.xml index be3976217..09cd71e0e 100644 --- a/plugins/tiles/pom.xml +++ b/plugins/tiles/pom.xml @@ -34,6 +34,10 @@ Struts 2 Tiles Plugin + + org.apache.tiles + tiles-api + org.apache.tiles tiles-core diff --git a/pom.xml b/pom.xml index b468f9226..0955de923 100644 --- a/pom.xml +++ b/pom.xml @@ -603,6 +603,11 @@ provided + + org.apache.tiles + tiles-api + ${tiles.version} + org.apache.tiles tiles-core From ace6a5d5ee0bdcfc81225832a477dff1bf4effbf Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 10:13:51 +0100 Subject: [PATCH 15/28] Adjusts code to Tiles 3 --- .../StrutsFreeMarkerAttributeRenderer.java | 65 +++---- .../tiles/StrutsTilesContainerFactory.java | 161 ++++++------------ .../struts2/tiles/StrutsTilesInitializer.java | 6 +- ...ildcardServletTilesApplicationContext.java | 34 ++-- 4 files changed, 104 insertions(+), 162 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java index 849053a22..48592e02f 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java @@ -33,68 +33,53 @@ import org.apache.struts2.views.JspSupportServlet; import org.apache.struts2.views.freemarker.FreemarkerManager; import org.apache.struts2.views.freemarker.FreemarkerResult; import org.apache.struts2.views.freemarker.StrutsBeanWrapper; -import org.apache.tiles.Attribute; -import org.apache.tiles.context.TilesRequestContext; import org.apache.tiles.freemarker.template.TilesFMModelRepository; import org.apache.tiles.impl.InvalidTemplateException; -import org.apache.tiles.renderer.impl.AbstractTypeDetectingAttributeRenderer; -import org.apache.tiles.servlet.context.ServletTilesRequestContext; -import org.apache.tiles.servlet.context.ServletUtil; +import org.apache.tiles.request.Request; +import org.apache.tiles.request.render.Renderer; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.io.IOException; -public class StrutsFreeMarkerAttributeRenderer extends AbstractTypeDetectingAttributeRenderer { +public class StrutsFreeMarkerAttributeRenderer implements Renderer { private static Logger LOG = LogManager.getLogger(StrutsFreeMarkerAttributeRenderer.class); @Override - public void write(Object value, Attribute attribute, TilesRequestContext request) throws IOException { - if (value != null) { - if (value instanceof String) { - LOG.trace("Rendering freemarker tile ..."); + public void render(String path, Request request) throws IOException { + if (path != null) { + LOG.trace("Rendering freemarker tile ..."); - ServletTilesRequestContext servletRequest = ServletUtil.getServletRequest(request); - HttpServletRequest httpRequest = servletRequest.getRequest(); + ActionContext ctx = ServletActionContext.getActionContext((HttpServletRequest) request); + if (ctx == null) { + throw new ConfigurationException("There is no ActionContext for current request!"); + } - ActionContext ctx = ServletActionContext.getActionContext(httpRequest); - if (ctx == null) { - throw new ConfigurationException("There is no ActionContext for current request!"); - } + registerTilesBeanModel(ctx); - registerTilesBeanModel(ctx); + FreemarkerResult result = new FreemarkerResult(path); + result.setWriter(request.getWriter()); - String include = (String) value; - FreemarkerResult result = new FreemarkerResult(include); - result.setWriter(request.getWriter()); + Container container = ctx.getContainer(); + container.inject(result); - Container container = ctx.getContainer(); - container.inject(result); - - try { - ActionInvocation invocation = ctx.getActionInvocation(); - result.doExecute(include, invocation); - } catch (TemplateException e) { - LOG.error("Exception was thrown during rendering value {}: {}", value, e.getMessage()); - throw new InvalidTemplateException(e); - } - } else { - LOG.error("Value {} is not a String, cannot render template!", value); - throw new InvalidTemplateException("Cannot render a template that is not a string: " + String.valueOf(value)); + try { + ActionInvocation invocation = ctx.getActionInvocation(); + result.doExecute(path, invocation); + } catch (TemplateException e) { + LOG.error("Exception was thrown during rendering value {}: {}", path, e.getMessage()); + throw new InvalidTemplateException(e); } } else { - LOG.error("Value is null, cannot render template!"); + LOG.error("Path is null, cannot render template!"); throw new InvalidTemplateException("Cannot render a null template"); } } - public boolean isRenderable(Object value, Attribute attribute, TilesRequestContext request) { - if (value instanceof String) { - String string = (String) value; - return string.startsWith("/") && string.endsWith(".ftl"); - } - return false; + @Override + public boolean isRenderable(String path, Request request) { + return path != null && path.startsWith("/") && path.endsWith(".ftl"); } /** diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index 785082b0c..2c9b09444 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -22,13 +22,8 @@ package org.apache.struts2.tiles; import ognl.OgnlException; import ognl.OgnlRuntime; import ognl.PropertyAccessor; -import org.apache.tiles.TilesApplicationContext; import org.apache.tiles.TilesContainer; -import org.apache.tiles.context.ChainedTilesRequestContextFactory; -import org.apache.tiles.context.TilesRequestContext; -import org.apache.tiles.context.TilesRequestContextFactory; import org.apache.tiles.definition.DefinitionsFactory; -import org.apache.tiles.definition.DefinitionsFactoryException; import org.apache.tiles.definition.pattern.DefinitionPatternMatcherFactory; import org.apache.tiles.definition.pattern.PatternDefinitionResolver; import org.apache.tiles.definition.pattern.PrefixedPatternDefinitionResolver; @@ -36,6 +31,7 @@ import org.apache.tiles.definition.pattern.regexp.RegexpDefinitionPatternMatcher import org.apache.tiles.definition.pattern.wildcard.WildcardDefinitionPatternMatcherFactory; import org.apache.tiles.el.ELAttributeEvaluator; import org.apache.tiles.el.JspExpressionFactoryFactory; +import org.apache.tiles.el.ScopeELResolver; import org.apache.tiles.el.TilesContextBeanELResolver; import org.apache.tiles.el.TilesContextELResolver; import org.apache.tiles.evaluator.AttributeEvaluatorFactory; @@ -43,25 +39,22 @@ import org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory; import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator; import org.apache.tiles.factory.BasicTilesContainerFactory; import org.apache.tiles.factory.TilesContainerFactoryException; -import org.apache.tiles.freemarker.context.FreeMarkerTilesRequestContextFactory; -import org.apache.tiles.impl.BasicTilesContainer; import org.apache.tiles.impl.mgmt.CachingTilesContainer; import org.apache.tiles.locale.LocaleResolver; -import org.apache.tiles.ognl.ApplicationScopeNestedObjectExtractor; +import org.apache.tiles.ognl.AnyScopePropertyAccessor; import org.apache.tiles.ognl.DelegatePropertyAccessor; import org.apache.tiles.ognl.NestedObjectDelegatePropertyAccessor; import org.apache.tiles.ognl.OGNLAttributeEvaluator; import org.apache.tiles.ognl.PropertyAccessorDelegateFactory; -import org.apache.tiles.ognl.RequestScopeNestedObjectExtractor; -import org.apache.tiles.ognl.SessionScopeNestedObjectExtractor; +import org.apache.tiles.ognl.ScopePropertyAccessor; import org.apache.tiles.ognl.TilesApplicationContextNestedObjectExtractor; import org.apache.tiles.ognl.TilesContextPropertyAccessorDelegateFactory; -import org.apache.tiles.renderer.AttributeRenderer; -import org.apache.tiles.renderer.TypeDetectingAttributeRenderer; -import org.apache.tiles.renderer.impl.BasicRendererFactory; -import org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer; -import org.apache.tiles.servlet.context.ServletUtil; -import org.apache.tiles.util.URLUtil; +import org.apache.tiles.request.ApplicationContext; +import org.apache.tiles.request.ApplicationResource; +import org.apache.tiles.request.Request; +import org.apache.tiles.request.render.BasicRendererFactory; +import org.apache.tiles.request.render.ChainedDelegateRenderer; +import org.apache.tiles.request.render.Renderer; import javax.el.ArrayELResolver; import javax.el.BeanELResolver; @@ -70,12 +63,11 @@ import javax.el.ELResolver; import javax.el.ListELResolver; import javax.el.MapELResolver; import javax.el.ResourceBundleELResolver; -import javax.servlet.ServletContext; -import java.io.IOException; -import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Locale; import java.util.Map; -import java.util.Set; /** * Dedicated Struts factory to build Tiles container with support for: @@ -106,76 +98,42 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { public static final String TILES_DEFAULT_PATTERN = "tiles*.xml"; @Override - protected BasicTilesContainer instantiateContainer(TilesApplicationContext applicationContext) { - CachingTilesContainer tilesContainer = new CachingTilesContainer(); - ServletContext servletContext = (ServletContext) applicationContext.getContext(); - ServletUtil.setContainer(servletContext, tilesContainer); - return tilesContainer; - } - - @Override - protected List getTilesRequestContextFactoriesToBeChained(ChainedTilesRequestContextFactory parent) { - - List factories = super.getTilesRequestContextFactoriesToBeChained(parent); - - registerRequestContextFactory(FreeMarkerTilesRequestContextFactory.class.getName(), factories, parent); - - return factories; + public TilesContainer createDecoratedContainer(TilesContainer originalContainer, ApplicationContext applicationContext) { + return new CachingTilesContainer(originalContainer); } @Override protected void registerAttributeRenderers( - BasicRendererFactory rendererFactory, - TilesApplicationContext applicationContext, - TilesRequestContextFactory contextFactory, - TilesContainer container, - AttributeEvaluatorFactory attributeEvaluatorFactory) { + final BasicRendererFactory rendererFactory, + final ApplicationContext applicationContext, + final TilesContainer container, + final AttributeEvaluatorFactory attributeEvaluatorFactory) { - super.registerAttributeRenderers( - rendererFactory, - applicationContext, - contextFactory, - container, - attributeEvaluatorFactory); + super.registerAttributeRenderers(rendererFactory, applicationContext, container, attributeEvaluatorFactory); StrutsFreeMarkerAttributeRenderer freemarkerRenderer = new StrutsFreeMarkerAttributeRenderer(); - freemarkerRenderer.setApplicationContext(applicationContext); - freemarkerRenderer.setAttributeEvaluatorFactory(attributeEvaluatorFactory); - freemarkerRenderer.setRequestContextFactory(contextFactory); rendererFactory.registerRenderer(FREEMARKER_RENDERER_NAME, freemarkerRenderer); } @Override - protected AttributeRenderer createDefaultAttributeRenderer( + protected Renderer createDefaultAttributeRenderer( BasicRendererFactory rendererFactory, - TilesApplicationContext applicationContext, - TilesRequestContextFactory contextFactory, + ApplicationContext applicationContext, TilesContainer container, AttributeEvaluatorFactory attributeEvaluatorFactory) { - ChainedDelegateAttributeRenderer retValue = new ChainedDelegateAttributeRenderer(); - - retValue.addAttributeRenderer((TypeDetectingAttributeRenderer) rendererFactory - .getRenderer(DEFINITION_RENDERER_NAME)); - retValue.addAttributeRenderer((TypeDetectingAttributeRenderer) rendererFactory - .getRenderer(FREEMARKER_RENDERER_NAME)); - retValue.addAttributeRenderer((TypeDetectingAttributeRenderer) rendererFactory - .getRenderer(TEMPLATE_RENDERER_NAME)); - retValue.addAttributeRenderer((TypeDetectingAttributeRenderer) rendererFactory - .getRenderer(STRING_RENDERER_NAME)); - - retValue.setApplicationContext(applicationContext); - retValue.setRequestContextFactory(contextFactory); - retValue.setAttributeEvaluatorFactory(attributeEvaluatorFactory); - + ChainedDelegateRenderer retValue = new ChainedDelegateRenderer(); + retValue.addAttributeRenderer(rendererFactory.getRenderer(DEFINITION_RENDERER_NAME)); + retValue.addAttributeRenderer(rendererFactory.getRenderer(FREEMARKER_RENDERER_NAME)); + retValue.addAttributeRenderer(rendererFactory.getRenderer(TEMPLATE_RENDERER_NAME)); + retValue.addAttributeRenderer(rendererFactory.getRenderer(STRING_RENDERER_NAME)); return retValue; } @Override protected AttributeEvaluatorFactory createAttributeEvaluatorFactory( - TilesApplicationContext applicationContext, - TilesRequestContextFactory contextFactory, + ApplicationContext applicationContext, LocaleResolver resolver) { BasicAttributeEvaluatorFactory attributeEvaluatorFactory = new BasicAttributeEvaluatorFactory(new DirectAttributeEvaluator()); @@ -187,26 +145,30 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { @Override protected PatternDefinitionResolver createPatternDefinitionResolver(Class customizationKeyClass) { - DefinitionPatternMatcherFactory wildcardFactory = new WildcardDefinitionPatternMatcherFactory(); DefinitionPatternMatcherFactory regexpFactory = new RegexpDefinitionPatternMatcherFactory(); + PrefixedPatternDefinitionResolver resolver = new PrefixedPatternDefinitionResolver<>(); - PrefixedPatternDefinitionResolver resolver = new PrefixedPatternDefinitionResolver(); - resolver.registerDefinitionPatternMatcherFactory(PATTERN_WILDCARD, wildcardFactory); - resolver.registerDefinitionPatternMatcherFactory(PATTERN_REGEXP, regexpFactory); + resolver.registerDefinitionPatternMatcherFactory("WILDCARD", wildcardFactory); + resolver.registerDefinitionPatternMatcherFactory("REGEXP", regexpFactory); return resolver; } @Override - protected List getSourceURLs(TilesApplicationContext applicationContext, TilesRequestContextFactory contextFactory) { - try { - Set finalSet = applicationContext.getResources(getTilesDefinitionPattern(applicationContext.getInitParams())); + protected List getSources(ApplicationContext applicationContext) { + Collection resources = applicationContext.getResources(getTilesDefinitionPattern(applicationContext.getInitParams())); - return URLUtil.getBaseTilesDefinitionURLs(finalSet); - } catch (IOException e) { - throw new DefinitionsFactoryException("Cannot load definition URLs", e); + List filteredResources = new ArrayList<>(); + if (resources != null) { + for (ApplicationResource resource : resources) { + if (Locale.ROOT.equals(resource.getLocale())) { + filteredResources.add(resource); + } + } } + + return filteredResources; } protected String getTilesDefinitionPattern(Map params) { @@ -216,55 +178,40 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { return TILES_DEFAULT_PATTERN; } - protected ELAttributeEvaluator createELEvaluator(TilesApplicationContext applicationContext) { - + protected ELAttributeEvaluator createELEvaluator(ApplicationContext applicationContext) { ELAttributeEvaluator evaluator = new ELAttributeEvaluator(); - evaluator.setApplicationContext(applicationContext); JspExpressionFactoryFactory efFactory = new JspExpressionFactoryFactory(); efFactory.setApplicationContext(applicationContext); evaluator.setExpressionFactory(efFactory.getExpressionFactory()); - ELResolver elResolver = new CompositeELResolver() { { - add(new TilesContextELResolver()); + BeanELResolver beanElResolver = new BeanELResolver(false); + add(new ScopeELResolver()); + add(new TilesContextELResolver(beanElResolver)); add(new TilesContextBeanELResolver()); add(new ArrayELResolver(false)); add(new ListELResolver(false)); add(new MapELResolver(false)); add(new ResourceBundleELResolver()); - add(new BeanELResolver(false)); + add(beanElResolver); } }; - evaluator.setResolver(elResolver); - return evaluator; } protected OGNLAttributeEvaluator createOGNLEvaluator() { try { PropertyAccessor objectPropertyAccessor = OgnlRuntime.getPropertyAccessor(Object.class); - PropertyAccessor mapPropertyAccessor = OgnlRuntime.getPropertyAccessor(Map.class); - PropertyAccessor applicationContextPropertyAccessor = - new NestedObjectDelegatePropertyAccessor<>( - new TilesApplicationContextNestedObjectExtractor(), - objectPropertyAccessor); - PropertyAccessor requestScopePropertyAccessor = - new NestedObjectDelegatePropertyAccessor<>( - new RequestScopeNestedObjectExtractor(), mapPropertyAccessor); - PropertyAccessor sessionScopePropertyAccessor = - new NestedObjectDelegatePropertyAccessor<>( - new SessionScopeNestedObjectExtractor(), mapPropertyAccessor); - PropertyAccessor applicationScopePropertyAccessor = - new NestedObjectDelegatePropertyAccessor<>( - new ApplicationScopeNestedObjectExtractor(), mapPropertyAccessor); - PropertyAccessorDelegateFactory factory = - new TilesContextPropertyAccessorDelegateFactory( - objectPropertyAccessor, applicationContextPropertyAccessor, - requestScopePropertyAccessor, sessionScopePropertyAccessor, - applicationScopePropertyAccessor); + PropertyAccessor applicationContextPropertyAccessor = new NestedObjectDelegatePropertyAccessor<>( + new TilesApplicationContextNestedObjectExtractor(), objectPropertyAccessor); + PropertyAccessor anyScopePropertyAccessor = new AnyScopePropertyAccessor(); + PropertyAccessor scopePropertyAccessor = new ScopePropertyAccessor(); + PropertyAccessorDelegateFactory factory = new TilesContextPropertyAccessorDelegateFactory( + objectPropertyAccessor, applicationContextPropertyAccessor, anyScopePropertyAccessor, + scopePropertyAccessor); PropertyAccessor tilesRequestAccessor = new DelegatePropertyAccessor<>(factory); - OgnlRuntime.setPropertyAccessor(TilesRequestContext.class, tilesRequestAccessor); + OgnlRuntime.setPropertyAccessor(Request.class, tilesRequestAccessor); return new OGNLAttributeEvaluator(); } catch (OgnlException e) { throw new TilesContainerFactoryException("Cannot initialize OGNL evaluator", e); diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java index 0ff33c78c..37fe1d1a7 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java @@ -21,8 +21,8 @@ package org.apache.struts2.tiles; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.tiles.TilesApplicationContext; import org.apache.tiles.factory.AbstractTilesContainerFactory; +import org.apache.tiles.request.ApplicationContext; import org.apache.tiles.startup.AbstractTilesInitializer; import javax.servlet.ServletContext; @@ -32,13 +32,13 @@ public class StrutsTilesInitializer extends AbstractTilesInitializer { private static final Logger LOG = LogManager.getLogger(StrutsTilesInitializer.class); @Override - protected TilesApplicationContext createTilesApplicationContext(TilesApplicationContext preliminaryContext) { + protected ApplicationContext createTilesApplicationContext(ApplicationContext preliminaryContext) { LOG.debug("Initializing Tiles wildcard support ..."); return new StrutsWildcardServletTilesApplicationContext((ServletContext) preliminaryContext.getContext()); } @Override - protected AbstractTilesContainerFactory createContainerFactory(TilesApplicationContext context) { + protected AbstractTilesContainerFactory createContainerFactory(ApplicationContext context) { LOG.trace("Creating dedicated Struts factory to create Tiles container"); return new StrutsTilesContainerFactory(); } diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java index 2328f0602..d942e9683 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java @@ -24,20 +24,24 @@ import com.opensymphony.xwork2.util.WildcardUtil; import com.opensymphony.xwork2.util.finder.ResourceFinder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.tiles.servlet.context.ServletTilesApplicationContext; +import org.apache.logging.log4j.message.MessageFormatMessage; +import org.apache.tiles.request.ApplicationResource; +import org.apache.tiles.request.locale.URLApplicationResource; +import org.apache.tiles.request.servlet.ServletApplicationContext; import javax.servlet.ServletContext; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.Collection; import java.util.Enumeration; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -public class StrutsWildcardServletTilesApplicationContext extends ServletTilesApplicationContext { +public class StrutsWildcardServletTilesApplicationContext extends ServletApplicationContext { private static final Logger LOG = LogManager.getLogger(StrutsWildcardServletTilesApplicationContext.class); @@ -70,36 +74,42 @@ public class StrutsWildcardServletTilesApplicationContext extends ServletTilesAp finder = new ResourceFinder(urls.toArray(new URL[urls.size()])); } - public Set getResources(String path) throws IOException { - Set resources = new HashSet<>(); + public Collection getResources(String path) { + Set resources = new HashSet<>(); if (path.startsWith("/")) { - LOG.trace("Using ServletContext to load resource #0", path); - URL resource = getResource(path); + LOG.trace("Using ServletContext to load resource {}", path); + ApplicationResource resource = getResource(path); if (resource != null) { resources.add(resource); } } - resources.addAll(findResources(path)); + + try { + resources.addAll(findResources(path)); + } catch (IOException e) { + LOG.error(new MessageFormatMessage("Cannot find resources for [{}]", path), e); + } return resources; } - protected Set findResources(String path) throws IOException { - Set resources = new HashSet<>(); + protected Set findResources(String path) throws IOException { + Set resources = new HashSet<>(); - LOG.trace("Using ResourceFinder to find matches for #0", path); + LOG.trace("Using ResourceFinder to find matches for {}", path); Pattern pattern = WildcardUtil.compileWildcardPattern(path); Map matches = finder.getResourcesMap(""); for (String resource : matches.keySet()) { if (pattern.matcher(resource).matches()) { - resources.add(matches.get(resource)); + URL url = matches.get(resource); + resources.add(new URLApplicationResource("", url)); } } - LOG.trace("Found resources #0 for path #1", resources, path); + LOG.trace("Found resources {} for path {}", resources, path); return resources; } From c07020cb7cc1d1a8eb15cacc7f62be3a864474f8 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 10:14:11 +0100 Subject: [PATCH 16/28] Uses new way to access TileContainer --- .../apache/struts2/views/tiles/TilesResult.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/views/tiles/TilesResult.java b/plugins/tiles/src/main/java/org/apache/struts2/views/tiles/TilesResult.java index 72d7916fc..a4028690a 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/views/tiles/TilesResult.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/views/tiles/TilesResult.java @@ -30,7 +30,11 @@ import org.apache.struts2.result.ServletDispatcherResult; import org.apache.tiles.TilesContainer; import com.opensymphony.xwork2.ActionInvocation; -import org.apache.tiles.servlet.context.ServletUtil; +import org.apache.tiles.access.TilesAccess; +import org.apache.tiles.request.ApplicationContext; +import org.apache.tiles.request.Request; +import org.apache.tiles.request.servlet.ServletRequest; +import org.apache.tiles.request.servlet.ServletUtil; /** * @@ -99,11 +103,14 @@ public class TilesResult extends ServletDispatcherResult { ServletContext servletContext = ServletActionContext.getServletContext(); - TilesContainer container = ServletUtil.getContainer(servletContext); + ApplicationContext applicationContext = ServletUtil.getApplicationContext(servletContext); + TilesContainer container = TilesAccess.getContainer(applicationContext); - HttpServletRequest request = ServletActionContext.getRequest(); - HttpServletResponse response = ServletActionContext.getResponse(); + HttpServletRequest httpRequest = ServletActionContext.getRequest(); + HttpServletResponse httpResponse = ServletActionContext.getResponse(); - container.render(location, request, response); + Request request = new ServletRequest(applicationContext, httpRequest, httpResponse); + + container.render(location, request); } } From 33edfffbe256e071fc33355518f24457da815a8b Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 10:36:46 +0100 Subject: [PATCH 17/28] Migrates tiles-portlet to the same version --- plugins/portlet-tiles/pom.xml | 2 +- .../views/tiles/PortletTilesResult.java | 50 ++++++++++++++----- pom.xml | 5 +- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/plugins/portlet-tiles/pom.xml b/plugins/portlet-tiles/pom.xml index 9e9a81faf..8b710b206 100644 --- a/plugins/portlet-tiles/pom.xml +++ b/plugins/portlet-tiles/pom.xml @@ -44,7 +44,7 @@ org.apache.tiles - tiles-portlet + tiles-request-portlet javax.servlet diff --git a/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java b/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java index 83fcd4e36..f12274b39 100644 --- a/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java +++ b/plugins/portlet-tiles/src/main/java/org/apache/struts2/views/tiles/PortletTilesResult.java @@ -22,22 +22,20 @@ package org.apache.struts2.views.tiles; import com.opensymphony.xwork2.ActionInvocation; -import freemarker.template.TemplateException; -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.result.ServletDispatcherResult; import org.apache.struts2.portlet.PortletConstants; import org.apache.struts2.portlet.context.PortletActionContext; +import org.apache.struts2.result.ServletDispatcherResult; import org.apache.tiles.TilesContainer; import org.apache.tiles.TilesException; import org.apache.tiles.access.TilesAccess; -import org.apache.tiles.portlet.context.PortletUtil; +import org.apache.tiles.request.ApplicationContext; +import org.apache.tiles.request.Request; +import org.apache.tiles.request.portlet.RenderPortletRequest; import javax.portlet.ActionResponse; -import javax.portlet.PortletException; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; +import javax.portlet.PortletContext; +import javax.portlet.RenderRequest; +import javax.portlet.RenderResponse; import java.util.Map; /** @@ -68,12 +66,15 @@ public class PortletTilesResult extends ServletDispatcherResult { protected void executeRenderResult(String location) throws TilesException { setLocation(location); - TilesContainer container = PortletUtil.getContainer(PortletActionContext.getPortletContext()); + PortletContext portletContext = PortletActionContext.getPortletContext(); + RenderRequest request = PortletActionContext.getRenderRequest(); + RenderResponse response = PortletActionContext.getRenderResponse(); - HttpServletRequest request = ServletActionContext.getRequest(); - HttpServletResponse response = ServletActionContext.getResponse(); + TilesContainer container = getCurrentContainer(request, portletContext); + ApplicationContext applicationContext = container.getApplicationContext(); + Request currentRequest = new RenderPortletRequest(applicationContext, portletContext, request, response); - container.render(location, request, response); + container.render(location, currentRequest); } protected void executeActionResult(String location, ActionInvocation invocation) { @@ -87,4 +88,27 @@ public class PortletTilesResult extends ServletDispatcherResult { res.setRenderParameter(PortletConstants.MODE_PARAM, PortletActionContext.getRequest().getPortletMode().toString()); } + protected TilesContainer getCurrentContainer(javax.portlet.PortletRequest request, PortletContext context) { + + TilesContainer container = (TilesContainer) request.getAttribute(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME); + + if (container == null) { + container = getContainer(context); + request.setAttribute(TilesAccess.CURRENT_CONTAINER_ATTRIBUTE_NAME, container); + } + + return container; + } + + protected TilesContainer getContainer(PortletContext context) { + return getContainer(context, TilesAccess.CONTAINER_ATTRIBUTE); + } + + protected TilesContainer getContainer(PortletContext context, String key) { + if (key == null) { + key = TilesAccess.CONTAINER_ATTRIBUTE; + } + return (TilesContainer) context.getAttribute(key); + } + } diff --git a/pom.xml b/pom.xml index 0955de923..14360e48e 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,7 @@ 3.3 5.0.2 3.0.5 + 1.0.6 2.3 @@ -620,8 +621,8 @@ org.apache.tiles - tiles-portlet - ${tiles.version} + tiles-request-portlet + ${tiles-request.version} org.apache.tiles From 6f98d776c398a036c563e863eacd59dedc9cd375 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Tue, 12 Jan 2016 22:16:06 +0100 Subject: [PATCH 18/28] Renames class to match pattern used in Tiles --- .../org/apache/struts2/tiles/StrutsTilesInitializer.java | 2 +- ...java => StrutsWildcardServletApplicationContext.java} | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) rename plugins/tiles/src/main/java/org/apache/struts2/tiles/{StrutsWildcardServletTilesApplicationContext.java => StrutsWildcardServletApplicationContext.java} (92%) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java index 37fe1d1a7..d0f32f0dd 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesInitializer.java @@ -34,7 +34,7 @@ public class StrutsTilesInitializer extends AbstractTilesInitializer { @Override protected ApplicationContext createTilesApplicationContext(ApplicationContext preliminaryContext) { LOG.debug("Initializing Tiles wildcard support ..."); - return new StrutsWildcardServletTilesApplicationContext((ServletContext) preliminaryContext.getContext()); + return new StrutsWildcardServletApplicationContext((ServletContext) preliminaryContext.getContext()); } @Override diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java similarity index 92% rename from plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java rename to plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java index d942e9683..abd78a21a 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletTilesApplicationContext.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java @@ -37,17 +37,18 @@ import java.net.URL; import java.util.Collection; import java.util.Enumeration; import java.util.HashSet; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; -public class StrutsWildcardServletTilesApplicationContext extends ServletApplicationContext { +public class StrutsWildcardServletApplicationContext extends ServletApplicationContext { - private static final Logger LOG = LogManager.getLogger(StrutsWildcardServletTilesApplicationContext.class); + private static final Logger LOG = LogManager.getLogger(StrutsWildcardServletApplicationContext.class); private ResourceFinder finder; - public StrutsWildcardServletTilesApplicationContext(ServletContext context) { + public StrutsWildcardServletApplicationContext(ServletContext context) { super(context); Set urls = new HashSet<>(); @@ -105,7 +106,7 @@ public class StrutsWildcardServletTilesApplicationContext extends ServletApplica for (String resource : matches.keySet()) { if (pattern.matcher(resource).matches()) { URL url = matches.get(resource); - resources.add(new URLApplicationResource("", url)); + resources.add(new URLApplicationResource(url.getPath(), url)); } } From b417894ebc09cdbd2a64d30734ed33cae06e219f Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 13 Jan 2016 12:04:43 +0100 Subject: [PATCH 19/28] Adds missing dependencies to tiles-request --- plugins/tiles/pom.xml | 18 +++++++++++++++++- pom.xml | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/plugins/tiles/pom.xml b/plugins/tiles/pom.xml index 09cd71e0e..0b964af9d 100644 --- a/plugins/tiles/pom.xml +++ b/plugins/tiles/pom.xml @@ -46,10 +46,21 @@ org.apache.tiles tiles-servlet + + org.apache.tiles + tiles-request-api + + + org.apache.tiles + tiles-request-jsp + + + org.apache.tiles + tiles-request-servlet + org.apache.tiles tiles-jsp - runtime org.apache.tiles @@ -68,6 +79,11 @@ javax.el true + + javax.servlet + jsp-api + provided + UTF-8 diff --git a/pom.xml b/pom.xml index 1e0f7083c..5d836d330 100644 --- a/pom.xml +++ b/pom.xml @@ -624,6 +624,21 @@ tiles-request-portlet ${tiles-request.version} + + org.apache.tiles + tiles-request-api + ${tiles-request.version} + + + org.apache.tiles + tiles-request-jsp + ${tiles-request.version} + + + org.apache.tiles + tiles-request-servlet + ${tiles-request.version} + org.apache.tiles tiles-jsp From 334e6ca462ad0cec85de090066ae81e03b199919 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 13 Jan 2016 12:05:03 +0100 Subject: [PATCH 20/28] Defines helper method to fetch ActionContext from request --- .../StrutsFreeMarkerAttributeRenderer.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java index 48592e02f..fbc541650 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java @@ -36,7 +36,9 @@ import org.apache.struts2.views.freemarker.StrutsBeanWrapper; import org.apache.tiles.freemarker.template.TilesFMModelRepository; import org.apache.tiles.impl.InvalidTemplateException; import org.apache.tiles.request.Request; +import org.apache.tiles.request.jsp.JspRequest; import org.apache.tiles.request.render.Renderer; +import org.apache.tiles.request.servlet.ServletRequest; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @@ -51,10 +53,7 @@ public class StrutsFreeMarkerAttributeRenderer implements Renderer { if (path != null) { LOG.trace("Rendering freemarker tile ..."); - ActionContext ctx = ServletActionContext.getActionContext((HttpServletRequest) request); - if (ctx == null) { - throw new ConfigurationException("There is no ActionContext for current request!"); - } + ActionContext ctx = readActionContext(request); registerTilesBeanModel(ctx); @@ -77,6 +76,29 @@ public class StrutsFreeMarkerAttributeRenderer implements Renderer { } } + /** + * Depending how Tiles definition was defined, request can an instance of JspRequest (for JSPs) + * or a ServletRequest (FreeMarker) + */ + protected ActionContext readActionContext(Request request) { + ActionContext ctx = null; + + if (request instanceof ServletRequest) { + HttpServletRequest httpRequest = ((ServletRequest) request).getRequest(); + ctx = ServletActionContext.getActionContext(httpRequest); + } + if (request instanceof JspRequest) { + HttpServletRequest httpRequest = (HttpServletRequest) ((JspRequest) request).getPageContext().getRequest(); + ctx = ServletActionContext.getActionContext(httpRequest); + } + + if (ctx == null) { + throw new ConfigurationException("There is no ActionContext for current request!"); + } + + return ctx; + } + @Override public boolean isRenderable(String path, Request request) { return path != null && path.startsWith("/") && path.endsWith(".ftl"); From 66d29d42c388bc17561d1b3d82f447426dda8db5 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 13 Jan 2016 12:05:58 +0100 Subject: [PATCH 21/28] Overrides method to use defined base instead of fetching it again --- .../tiles/StrutsWildcardServletApplicationContext.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java index abd78a21a..b8f10f9cf 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java @@ -95,6 +95,10 @@ public class StrutsWildcardServletApplicationContext extends ServletApplicationC return resources; } + public ApplicationResource getResource(ApplicationResource base, Locale locale) { + return base; + } + protected Set findResources(String path) throws IOException { Set resources = new HashSet<>(); From 5bd54cf8ac40775e97a8dde9a0cd78ddcd2b8339 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Wed, 13 Jan 2016 17:22:36 +0100 Subject: [PATCH 22/28] Extends logging --- .../struts2/tiles/StrutsFreeMarkerAttributeRenderer.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java index fbc541650..0b19b1df5 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java @@ -51,7 +51,7 @@ public class StrutsFreeMarkerAttributeRenderer implements Renderer { @Override public void render(String path, Request request) throws IOException { if (path != null) { - LOG.trace("Rendering freemarker tile ..."); + LOG.trace("Rendering freemarker tile [{}]", path); ActionContext ctx = readActionContext(request); @@ -83,6 +83,8 @@ public class StrutsFreeMarkerAttributeRenderer implements Renderer { protected ActionContext readActionContext(Request request) { ActionContext ctx = null; + LOG.debug("Obtaining HttpServletRequest based on [{}]", request.getClass().getName()); + if (request instanceof ServletRequest) { HttpServletRequest httpRequest = ((ServletRequest) request).getRequest(); ctx = ServletActionContext.getActionContext(httpRequest); @@ -93,6 +95,7 @@ public class StrutsFreeMarkerAttributeRenderer implements Renderer { } if (ctx == null) { + LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName()); throw new ConfigurationException("There is no ActionContext for current request!"); } @@ -101,6 +104,7 @@ public class StrutsFreeMarkerAttributeRenderer implements Renderer { @Override public boolean isRenderable(String path, Request request) { + LOG.trace("Checking if path [{}] can be rendered", path); return path != null && path.startsWith("/") && path.endsWith(".ftl"); } From b5c96a5888eb3a7b7ca6acbd857b38bda51e2f87 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Thu, 14 Jan 2016 08:17:50 +0100 Subject: [PATCH 23/28] Simplifies logic to fetch HttpServletRequest --- .../tiles/StrutsFreeMarkerAttributeRenderer.java | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java index 0b19b1df5..5102eada4 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsFreeMarkerAttributeRenderer.java @@ -36,9 +36,8 @@ import org.apache.struts2.views.freemarker.StrutsBeanWrapper; import org.apache.tiles.freemarker.template.TilesFMModelRepository; import org.apache.tiles.impl.InvalidTemplateException; import org.apache.tiles.request.Request; -import org.apache.tiles.request.jsp.JspRequest; import org.apache.tiles.request.render.Renderer; -import org.apache.tiles.request.servlet.ServletRequest; +import org.apache.tiles.request.servlet.ServletUtil; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @@ -81,18 +80,10 @@ public class StrutsFreeMarkerAttributeRenderer implements Renderer { * or a ServletRequest (FreeMarker) */ protected ActionContext readActionContext(Request request) { - ActionContext ctx = null; - LOG.debug("Obtaining HttpServletRequest based on [{}]", request.getClass().getName()); - if (request instanceof ServletRequest) { - HttpServletRequest httpRequest = ((ServletRequest) request).getRequest(); - ctx = ServletActionContext.getActionContext(httpRequest); - } - if (request instanceof JspRequest) { - HttpServletRequest httpRequest = (HttpServletRequest) ((JspRequest) request).getPageContext().getRequest(); - ctx = ServletActionContext.getActionContext(httpRequest); - } + HttpServletRequest httpRequest = ServletUtil.getServletRequest(request).getRequest(); + ActionContext ctx = ServletActionContext.getActionContext(httpRequest); if (ctx == null) { LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName()); From 0e85da62282cbf884d058b7850eda38f8017f36e Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Fri, 15 Jan 2016 08:44:25 +0100 Subject: [PATCH 24/28] Uses existing constants instead of literals --- .../org/apache/struts2/tiles/StrutsTilesContainerFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index 2c9b09444..57594afbf 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -149,8 +149,8 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { DefinitionPatternMatcherFactory regexpFactory = new RegexpDefinitionPatternMatcherFactory(); PrefixedPatternDefinitionResolver resolver = new PrefixedPatternDefinitionResolver<>(); - resolver.registerDefinitionPatternMatcherFactory("WILDCARD", wildcardFactory); - resolver.registerDefinitionPatternMatcherFactory("REGEXP", regexpFactory); + resolver.registerDefinitionPatternMatcherFactory(PATTERN_WILDCARD, wildcardFactory); + resolver.registerDefinitionPatternMatcherFactory(PATTERN_REGEXP, regexpFactory); return resolver; } From f645c91ee66416e964b5823f8c9cdc48ba8ef53c Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Fri, 15 Jan 2016 08:45:54 +0100 Subject: [PATCH 25/28] Extracts constants representing expression languages --- .../struts2/tiles/StrutsTilesContainerFactory.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index 57594afbf..840361072 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -97,6 +97,12 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { */ public static final String TILES_DEFAULT_PATTERN = "tiles*.xml"; + /** + * Supported expression languages + */ + public static final String OGNL = "OGNL"; + public static final String EL = "EL"; + @Override public TilesContainer createDecoratedContainer(TilesContainer originalContainer, ApplicationContext applicationContext) { return new CachingTilesContainer(originalContainer); @@ -137,8 +143,8 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { LocaleResolver resolver) { BasicAttributeEvaluatorFactory attributeEvaluatorFactory = new BasicAttributeEvaluatorFactory(new DirectAttributeEvaluator()); - attributeEvaluatorFactory.registerAttributeEvaluator("OGNL", createOGNLEvaluator()); - attributeEvaluatorFactory.registerAttributeEvaluator("EL", createELEvaluator(applicationContext)); + attributeEvaluatorFactory.registerAttributeEvaluator(OGNL, createOGNLEvaluator()); + attributeEvaluatorFactory.registerAttributeEvaluator(EL, createELEvaluator(applicationContext)); return attributeEvaluatorFactory; } From a42a1731f301a712da3d61f43291c5e1126c50ff Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Fri, 15 Jan 2016 10:12:15 +0100 Subject: [PATCH 26/28] Adds support to use Struts' Locale --- .../tiles/StrutsTilesContainerFactory.java | 4 ++ .../tiles/StrutsTilesLocaleResolver.java | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesLocaleResolver.java diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java index 840361072..0cb14f30c 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesContainerFactory.java @@ -161,6 +161,10 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory { return resolver; } + protected LocaleResolver createLocaleResolver(ApplicationContext applicationContext) { + return new StrutsTilesLocaleResolver(); + } + @Override protected List getSources(ApplicationContext applicationContext) { Collection resources = applicationContext.getResources(getTilesDefinitionPattern(applicationContext.getInitParams())); diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesLocaleResolver.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesLocaleResolver.java new file mode 100644 index 000000000..2c0ef9d66 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsTilesLocaleResolver.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.tiles; + +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.LocaleProvider; +import com.opensymphony.xwork2.config.ConfigurationException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.struts2.ServletActionContext; +import org.apache.tiles.locale.LocaleResolver; +import org.apache.tiles.request.Request; +import org.apache.tiles.request.servlet.ServletUtil; + +import javax.servlet.http.HttpServletRequest; +import java.util.Locale; + +public class StrutsTilesLocaleResolver implements LocaleResolver { + + private static Logger LOG = LogManager.getLogger(StrutsTilesLocaleResolver.class); + + @Override + public Locale resolveLocale(Request request) { + HttpServletRequest httpRequest = ServletUtil.getServletRequest(request).getRequest(); + ActionContext ctx = ServletActionContext.getActionContext(httpRequest); + + if (ctx == null) { + LOG.error("Cannot obtain HttpServletRequest from [{}]", request.getClass().getName()); + throw new ConfigurationException("There is no ActionContext for current request!"); + } + + LocaleProvider provider = ctx.getInstance(LocaleProvider.class); + + return provider.getLocale(); + } + +} From 328da4e2258573acd049cdd3fd5bce2f37869adf Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Fri, 15 Jan 2016 10:12:25 +0100 Subject: [PATCH 27/28] Adds support for I18N --- .../tiles/StrutsApplicationResource.java | 55 +++++++++++++++++++ ...rutsWildcardServletApplicationContext.java | 15 ++++- 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java new file mode 100644 index 000000000..6884e9a42 --- /dev/null +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsApplicationResource.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.tiles; + +import org.apache.tiles.request.locale.PostfixedApplicationResource; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +public class StrutsApplicationResource extends PostfixedApplicationResource { + + private final URL url; + + public StrutsApplicationResource(URL url) { + super(url.getPath()); + this.url = url; + } + + @Override + public InputStream getInputStream() throws IOException { + if (new File(url.getPath()).exists()) { + return url.openStream(); + } + return null; + } + + @Override + public long getLastModified() throws IOException { + File file = new File(url.getPath()); + if (file.exists()) { + return file.lastModified(); + } + return 0; + } + +} diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java index b8f10f9cf..2f67f046d 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java @@ -26,13 +26,13 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.MessageFormatMessage; import org.apache.tiles.request.ApplicationResource; -import org.apache.tiles.request.locale.URLApplicationResource; import org.apache.tiles.request.servlet.ServletApplicationContext; import javax.servlet.ServletContext; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.util.Collection; import java.util.Enumeration; @@ -96,7 +96,16 @@ public class StrutsWildcardServletApplicationContext extends ServletApplicationC } public ApplicationResource getResource(ApplicationResource base, Locale locale) { - return base; + String localePath = base.getLocalePath(locale); + if (new File(localePath).exists()) { + try { + return new StrutsApplicationResource(URI.create("file://" + localePath).toURL()); + } catch (MalformedURLException e) { + LOG.warn(new MessageFormatMessage("Cannot access [{}]", localePath), e); + return null; + } + } + return null; } protected Set findResources(String path) throws IOException { @@ -110,7 +119,7 @@ public class StrutsWildcardServletApplicationContext extends ServletApplicationC for (String resource : matches.keySet()) { if (pattern.matcher(resource).matches()) { URL url = matches.get(resource); - resources.add(new URLApplicationResource(url.getPath(), url)); + resources.add(new StrutsApplicationResource(url)); } } From 93375125adb54a9e1e4a000557a202c7ca23fe73 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Sun, 17 Jan 2016 10:25:36 +0100 Subject: [PATCH 28/28] Simplifies logging --- .../tiles/StrutsWildcardServletApplicationContext.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java index 2f67f046d..0fa1bc31e 100644 --- a/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java +++ b/plugins/tiles/src/main/java/org/apache/struts2/tiles/StrutsWildcardServletApplicationContext.java @@ -24,7 +24,6 @@ import com.opensymphony.xwork2.util.WildcardUtil; import com.opensymphony.xwork2.util.finder.ResourceFinder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.message.MessageFormatMessage; import org.apache.tiles.request.ApplicationResource; import org.apache.tiles.request.servlet.ServletApplicationContext; @@ -89,7 +88,7 @@ public class StrutsWildcardServletApplicationContext extends ServletApplicationC try { resources.addAll(findResources(path)); } catch (IOException e) { - LOG.error(new MessageFormatMessage("Cannot find resources for [{}]", path), e); + LOG.error("Cannot find resources for [{}]", path, e); } return resources; @@ -101,7 +100,7 @@ public class StrutsWildcardServletApplicationContext extends ServletApplicationC try { return new StrutsApplicationResource(URI.create("file://" + localePath).toURL()); } catch (MalformedURLException e) { - LOG.warn(new MessageFormatMessage("Cannot access [{}]", localePath), e); + LOG.warn("Cannot access [{}]", localePath, e); return null; } }