View Javadoc

1   /*
2    * $Id: ContextHolder.java 765774 2009-04-16 21:43:00Z 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.velocity.template;
23  
24  import javax.servlet.ServletContext;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.apache.velocity.context.Context;
29  
30  /**
31   * An object that holds the current state of Velocity in a Servlet environment.
32   *
33   * @version $Rev: 765774 $ $Date: 2009-04-17 07:43:00 +1000 (Fri, 17 Apr 2009) $
34   * @since 2.2.0
35   */
36  public class ContextHolder {
37  
38      /**
39       * The Velocity context.
40       */
41      private Context velocityContext;
42  
43      /**
44       * The HTTP request.
45       */
46      private HttpServletRequest request;
47  
48      /**
49       * The HTTP response.
50       */
51      private HttpServletResponse response;
52  
53      /**
54       * The servlet context.
55       */
56      private ServletContext application;
57  
58      /**
59       * Sets the current {@link HttpServletRequest}. This is required for this
60       * tool to operate and will throw a NullPointerException if this is not set
61       * or is set to {@code null}.
62       *
63       * @param request The HTTP request.
64       * @since 2.2.0
65       */
66      public void setRequest(HttpServletRequest request) {
67          if (request == null) {
68              throw new NullPointerException("request should not be null");
69          }
70          this.request = request;
71      }
72  
73      /**
74       * Sets the current {@link HttpServletResponse}. This is required for this
75       * tool to operate and will throw a NullPointerException if this is not set
76       * or is set to {@code null}.
77       *
78       * @param response The HTTP response.
79       * @since 2.2.0
80       */
81      public void setResponse(HttpServletResponse response) {
82          if (response == null) {
83              throw new NullPointerException("response should not be null");
84          }
85          this.response = response;
86      }
87  
88      /**
89       * Sets the {@link ServletContext}. This is required for this tool to
90       * operate and will throw a NullPointerException if this is not set or is
91       * set to {@code null}.
92       *
93       * @param application The Servlet context.
94       * @since 2.2.0
95       */
96      public void setServletContext(ServletContext application) {
97          if (application == null) {
98              throw new NullPointerException("servlet context should not be null");
99          }
100         this.application = application;
101     }
102 
103     /**
104      * Sets the Velocity {@link Context}. This is required for this tool to
105      * operate and will throw a NullPointerException if this is not set or is
106      * set to {@code null}.
107      *
108      * @param context The Velocity context.
109      * @since 2.2.0
110      */
111     public void setVelocityContext(Context context) {
112         if (context == null) {
113             throw new NullPointerException(
114                     "velocity context should not be null");
115         }
116         this.velocityContext = context;
117     }
118 
119     /**
120      * Returns the HTTP request.
121      *
122      * @return The HTTP request.
123      * @since 2.2.0
124      */
125     protected HttpServletRequest getRequest() {
126         return request;
127     }
128 
129     /**
130      * Returns the HTTP response.
131      *
132      * @return The HTTP response.
133      * @since 2.2.0
134      */
135     protected HttpServletResponse getResponse() {
136         return response;
137     }
138 
139     /**
140      * Returns the Servlet context.
141      *
142      * @return The Servlet context..
143      * @since 2.2.0
144      */
145     protected ServletContext getServletContext() {
146         return application;
147     }
148 
149     /**
150      * Returns the Velocity context..
151      *
152      * @return The Velocity context.
153      * @since 2.2.0
154      */
155     protected Context getVelocityContext() {
156         return velocityContext;
157     }
158 }