View Javadoc

1   /*
2    * $Id: TilesDispatchServlet.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  package org.apache.tiles.web.util;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.tiles.TilesContainer;
26  import org.apache.tiles.TilesException;
27  import org.apache.tiles.AttributeContext;
28  import org.apache.tiles.access.TilesAccess;
29  
30  import javax.servlet.ServletException;
31  import javax.servlet.ServletRequest;
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.io.IOException;
36  
37  /***
38   * Tiles dispatching servlet.  Used to invoke
39   * a definition directly.
40   */
41  public class TilesDispatchServlet extends HttpServlet {
42  
43      /***
44       * The logging object.
45       */
46      private static final Log LOG =
47          LogFactory.getLog(TilesDispatchServlet.class);
48  
49      /***
50       * The object that will mutate the attribute context so that it uses
51       * different attributes.
52       */
53      private AttributeContextMutator mutator;
54  
55  
56      /*** {@inheritDoc} */
57      public void init() throws ServletException {
58          super.init();
59          String temp = getInitParameter("mutator");
60          if (temp != null) {
61              try {
62                  mutator = (AttributeContextMutator) Class.forName(temp)
63                          .newInstance();
64              } catch (Exception e) {
65                  throw new ServletException("Unable to instantiate specified context mutator.", e);
66              }
67          } else {
68              mutator = new DefaultMutator();
69          }
70      }
71  
72      /*** {@inheritDoc} */
73      protected void doGet(HttpServletRequest req, HttpServletResponse res)
74          throws ServletException, IOException {
75  
76          TilesContainer container = TilesAccess.getContainer(getServletContext());
77          mutator.mutate(container.getAttributeContext(req, res), req);
78          try {
79              String definition = getDefinitionName(req);
80              if (LOG.isDebugEnabled()) {
81                  LOG.info("Dispatching to tile '" + definition + "'");
82              }
83              container.render(definition, req, res);
84          } catch (TilesException e) {
85              throw new ServletException("Error rendering tile.", e);
86          }
87      }
88  
89      /***
90       * Returns the called definition name for the given request.
91       *
92       * @param request The request to parse.
93       * @return The definition name to render.
94       */
95      protected String getDefinitionName(HttpServletRequest request) {
96          String path = (String) request.getAttribute("javax.servlet.include.servlet_path");
97          if (path == null) {
98              path = request.getServletPath();
99          }
100 
101         int start = path.startsWith("/") ? 1 : 0;
102         int end = path.endsWith(".tiles") ? path.indexOf(".tiles") : path.length();
103 
104         return path.substring(start, end);
105     }
106 
107     /*** {@inheritDoc} */
108     protected void doPost(HttpServletRequest req, HttpServletResponse res)
109         throws ServletException, IOException {
110         LOG.info("Tiles dispatch request received. Redirecting POST to GET.");
111         doGet(req, res);
112     }
113 
114     /***
115      * Default no-op mutator.
116      */
117     class DefaultMutator implements AttributeContextMutator {
118 
119         /*** {@inheritDoc} */
120         public void mutate(AttributeContext context, ServletRequest request) {
121             // noop;
122         }
123     }
124 }