View Javadoc

1   package org.apache.velocity.tools.view;
2   
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  import java.util.Enumeration;
23  import java.util.Set;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpSession;
26  import javax.servlet.ServletContext;
27  import org.apache.velocity.tools.generic.ContextTool;
28  import org.apache.velocity.tools.generic.ValueParser;
29  import org.apache.velocity.tools.view.ViewContext;
30  
31  /**
32   * <p>Extension of {@link ContextTool} that includes keys and values
33   * from the {@link HttpServletRequest}, {@link HttpSession} and
34   * {@link ServletContext}.</p>
35   * <p><pre>
36   * Template example(s):
37   *  #foreach( $key in $context.keys )
38   *    $key = $context.get($key)
39   *  #end
40   *
41   * Toolbox configuration:
42   * &lt;tools&gt;
43   *   &lt;toolbox scope="request"&gt;
44   *     &lt;tool class="org.apache.velocity.tools.view.ViewContextTool"/&gt;
45   *   &lt;/toolbox&gt;
46   * &lt;/tools&gt;
47   * </pre></p>
48   *
49   * <p>This class is only designed for use as a request-scope VelocityView tool.</p>
50   *
51   * @author Nathan Bubna
52   * @since VelocityTools 2.0
53   * @version $Id: ViewContextTool.java 385122 2006-03-11 18:37:42Z nbubna $
54   */
55  public class ViewContextTool extends ContextTool
56  {
57      protected HttpServletRequest request;
58      protected HttpSession session;
59      protected ServletContext application;
60  
61  
62      @Override
63      protected void configure(ValueParser parser)
64      {
65          // do ContextTool config first
66          super.configure(parser);
67  
68          this.request = (HttpServletRequest)parser.getValue(ViewContext.REQUEST);
69          this.session = request.getSession(false);
70          this.application = (ServletContext)parser.getValue(ViewContext.SERVLET_CONTEXT_KEY);
71      }
72  
73      @Override
74      protected void fillKeyset(Set keys)
75      {
76          // start with the standard ContextTool's keys
77          super.fillKeyset(keys);
78  
79          // get request attribute keys
80          Enumeration e = request.getAttributeNames();
81          while (e.hasMoreElements())
82          {
83              keys.add(e.nextElement());
84          }
85  
86          // get session attribute keys if we have a session
87          if (session != null)
88          {
89              e = session.getAttributeNames();
90              while (e.hasMoreElements())
91              {
92                  keys.add(e.nextElement());
93              }
94          }
95  
96          // get request attribute keys
97          e = application.getAttributeNames();
98          while (e.hasMoreElements())
99          {
100             keys.add(e.nextElement());
101         }
102     }
103 
104 }