View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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       * The {@code Log} instance for this class.
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       * Returns a list containing the URLs to be parsed. By default, it returns a
75       * list containing the URL point to "definitions-config".
76       *
77       * @param applicationContext The Tiles application context.
78       *
79       * @return The source URLs.
80       * @since Struts 1.4.1
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      * Creates the definitions factory. By default it creates a
124      * {@link UnresolvingLocaleDefinitionsFactory} with default dependencies.
125      *
126      * @param container The Tiles-Plugin-Container.
127      * @param applicationContext The Tiles application context.
128      *
129      * @return The definitions factory.
130      * @since Struts 1.4.1
131      */
132     public DefinitionsFactory createDefinitionsFactory(TilesPluginContainer container,
133             ApplicationContext applicationContext) {
134         LocaleResolver resolver = container.getLocaleResolverIntern();
135         return createDefinitionsFactory(applicationContext, resolver);
136     }
137 }