View Javadoc

1   /*
2    * $Id: ServletTilesContextFactory.java 531864 2007-04-24 10:24:30Z apetrelli $
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.tiles.servlet.context;
23  
24  import org.apache.tiles.TilesApplicationContext;
25  import org.apache.tiles.context.TilesContextFactory;
26  import org.apache.tiles.context.TilesRequestContext;
27  
28  import javax.servlet.ServletContext;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.util.Map;
32  
33  /***
34   * Creates an instance of the appropriate TilesApplicationContext implementation.
35   *
36   * @version $Rev: 531864 $ $Date: 2007-04-24 12:24:30 +0200 (Tue, 24 Apr 2007) $
37   */
38  public class ServletTilesContextFactory implements TilesContextFactory {
39  
40      /*** {@inheritDoc} */
41      public void init(Map<String, String> configParameters) {
42      }
43  
44      /*** {@inheritDoc} */
45      public TilesApplicationContext createApplicationContext(Object context) {
46          if (context instanceof ServletContext) {
47              ServletContext servletContext = (ServletContext) context;
48              return new ServletTilesApplicationContext(servletContext);
49          }
50          return null;
51      }
52  
53      /*** {@inheritDoc} */
54      public TilesRequestContext createRequestContext(TilesApplicationContext context,
55                                                      Object... requestItems) {
56          if (requestItems.length == 2) {
57              ServletContext servletContext = getServletContext(context);
58              if (servletContext != null) {
59                  return new ServletTilesRequestContext(servletContext,
60                      (HttpServletRequest) requestItems[0],
61                      (HttpServletResponse) requestItems[1]);
62              }
63          }
64  
65          return null;
66      }
67  
68      /***
69       * Returns the original servlet context.
70       *
71       * @param context The application context.
72       * @return The original servlet context, if found.
73       */
74      protected ServletContext getServletContext(TilesApplicationContext context) {
75          if (context instanceof ServletTilesApplicationContext) {
76              ServletTilesApplicationContext app = (ServletTilesApplicationContext) context;
77              return app.getServletContext();
78          }
79          return null;
80  
81      }
82  }