mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Ports solution from 2.3.x branch
This commit is contained in:
+22
-15
@@ -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<T> resolver = new PrefixedPatternDefinitionResolver<>();
|
||||
PrefixedPatternDefinitionResolver<T> resolver = new PrefixedPatternDefinitionResolver<T>();
|
||||
resolver.registerDefinitionPatternMatcherFactory(PATTERN_WILDCARD, wildcardFactory);
|
||||
resolver.registerDefinitionPatternMatcherFactory(PATTERN_REGEXP, regexpFactory);
|
||||
|
||||
@@ -189,19 +199,9 @@ public class StrutsTilesContainerFactory extends BasicTilesContainerFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<URL> getSourceURLs(TilesApplicationContext applicationContext,
|
||||
TilesRequestContextFactory contextFactory) {
|
||||
protected List<URL> getSourceURLs(TilesApplicationContext applicationContext, TilesRequestContextFactory contextFactory) {
|
||||
try {
|
||||
Set<URL> finalSet = new HashSet<>();
|
||||
Set<URL> webINFSet = applicationContext.getResources("/WEB-INF/**/tiles*.xml");
|
||||
Set<URL> metaINFSet = applicationContext.getResources("classpath*:META-INF/**/tiles*.xml");
|
||||
|
||||
if (webINFSet != null) {
|
||||
finalSet.addAll(webINFSet);
|
||||
}
|
||||
if (metaINFSet != null) {
|
||||
finalSet.addAll(metaINFSet);
|
||||
}
|
||||
Set<URL> 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<String, String> 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();
|
||||
|
||||
@@ -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
|
||||
|
||||
+106
@@ -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<URL> 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<URL> 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<URL> getResources(String path) throws IOException {
|
||||
Set<URL> 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<URL> findResources(String path) throws IOException {
|
||||
Set<URL> resources = new HashSet<>();
|
||||
|
||||
LOG.trace("Using ResourceFinder to find matches for #0", path);
|
||||
|
||||
Pattern pattern = WildcardUtil.compileWildcardPattern(path);
|
||||
Map<String, URL> 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user