1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.tiles2;
23
24 import java.nio.file.InvalidPathException;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.Collections;
28 import java.util.List;
29
30 import org.apache.tiles.TilesContainer;
31 import org.apache.tiles.definition.DefinitionsFactory;
32 import org.apache.tiles.definition.UnresolvingLocaleDefinitionsFactory;
33 import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
34 import org.apache.tiles.factory.BasicTilesContainerFactory;
35 import org.apache.tiles.locale.LocaleResolver;
36 import org.apache.tiles.request.ApplicationContext;
37 import org.apache.tiles.request.ApplicationResource;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class TilesPluginContainerFactory extends BasicTilesContainerFactory {
42
43
44
45
46 private transient final Logger log =
47 LoggerFactory.getLogger(TilesPluginContainerFactory.class);
48
49 @Override
50 public TilesContainer createContainer(ApplicationContext applicationContext) {
51 TilesPluginContainer container = instantiateContainer(applicationContext);
52 container.setApplicationContext(applicationContext);
53 LocaleResolver resolver = createLocaleResolver(applicationContext);
54 container.setLocaleResolver(resolver);
55 container.setDefinitionsFactory(createDefinitionsFactory(applicationContext,
56 resolver));
57 AttributeEvaluatorFactory attributeEvaluatorFactory = createAttributeEvaluatorFactory(
58 applicationContext, resolver);
59 container.setAttributeEvaluatorFactory(attributeEvaluatorFactory);
60 container.setPreparerFactory(createPreparerFactory(applicationContext));
61 TilesContainer injectedContainer = createDecoratedContainer(container, applicationContext);
62 container.setRendererFactory(createRendererFactory(applicationContext,
63 injectedContainer, attributeEvaluatorFactory));
64 return injectedContainer;
65 }
66
67 @Override
68 protected TilesPluginContainer instantiateContainer(
69 ApplicationContext context) {
70 return new TilesPluginContainer();
71 }
72
73
74
75
76
77
78
79
80
81
82 protected List<ApplicationResource> getSources(ApplicationContext applicationContext) {
83 String param = applicationContext
84 .getInitParams().get(DefinitionsFactory.DEFINITIONS_CONFIG);
85 if (param == null) {
86 param = applicationContext
87 .getInitParams().get("definitions-config");
88 }
89
90 boolean error = true;
91 if (param != null) {
92 if (param.isEmpty()) {
93 log.warn("getSources - empty parameters");
94 } else if (param.contains("\u0000")) {
95 log.warn("getSources - ignore file with nul-characters '{}'", param.replace('\u0000', '_') );
96 } else {
97 try {
98 final Path p = Paths.get(param).normalize();
99 if (p.toString().charAt(0) != '.') {
100 error = false;
101 } else {
102 log.warn("getSources - path not normalized '{}'", p.toString());
103 }
104 } catch (InvalidPathException e) {
105 log.warn("getSources - illegal path '{}'", e.getInput(), e);
106 param = null;
107 }
108 }
109 }
110
111 if (error) {
112 param = "/WEB-INF/tiles.xml";
113 log.debug("getSources - use default '{}'", param);
114 } else {
115 log.debug("getSources - use '{}'", param);
116 }
117
118 return Collections
119 .singletonList(applicationContext.getResource(param));
120 }
121
122
123
124
125
126
127
128
129
130
131
132 public DefinitionsFactory createDefinitionsFactory(TilesPluginContainer container,
133 ApplicationContext applicationContext) {
134 LocaleResolver resolver = container.getLocaleResolverIntern();
135 return createDefinitionsFactory(applicationContext, resolver);
136 }
137 }