KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > IncludeDispatchRequest


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.webapp;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.FreeList;
34
35 import javax.servlet.ServletException JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42 /**
43  * sub-request for a include() page
44  */

45 class IncludeDispatchRequest extends DispatchRequest {
46   protected static final Logger JavaDoc log
47     = Logger.getLogger(IncludeDispatchRequest.class.getName());
48
49   private static final FreeList<IncludeDispatchRequest> _freeList
50     = new FreeList<IncludeDispatchRequest>(32);
51   
52   private Hashtable JavaDoc<String JavaDoc,String JavaDoc> _headers;
53   
54   protected IncludeDispatchRequest()
55   {
56   }
57
58   /**
59    * Creates a dispatch request.
60    */

61   public static IncludeDispatchRequest createDispatch()
62   {
63     IncludeDispatchRequest req = _freeList.allocate();
64     if (req == null)
65       req = new IncludeDispatchRequest();
66
67     return req;
68   }
69
70   void init(WebApp webApp,
71             WebApp oldWebApp,
72             HttpServletRequest JavaDoc request,
73             HttpServletResponse JavaDoc response,
74         String JavaDoc method, String JavaDoc uri,
75         String JavaDoc servletPath, String JavaDoc pathInfo,
76         String JavaDoc queryString, String JavaDoc addedQuery)
77     throws ServletException JavaDoc
78   {
79     super.init(webApp, oldWebApp, request, response,
80            method, uri, servletPath, pathInfo, queryString, addedQuery);
81     
82     _headers = null;
83   }
84
85   /**
86    * Returns the include's view of the header names.
87    */

88   public Enumeration JavaDoc getHeaderNames()
89   {
90     return new HeaderEnumeration(super.getHeaderNames(), _headers);
91   }
92     
93   public String JavaDoc getHeader(String JavaDoc key)
94   {
95     String JavaDoc value = null;
96     if (_headers != null)
97       value = (String JavaDoc) _headers.get(key);
98
99     if (value != null)
100       return value;
101
102     // The included file must ignore caching directives from the
103
// original request
104
if (key.equalsIgnoreCase("If-Modified-Since") ||
105         key.equalsIgnoreCase("If-None-Match"))
106       return null;
107     else {
108       return super.getHeader(key);
109     }
110   }
111
112   public void setHeader(String JavaDoc key, String JavaDoc value)
113   {
114     if (_headers == null)
115       _headers = new Hashtable JavaDoc<String JavaDoc,String JavaDoc>();
116     _headers.put(key, value);
117   }
118
119   /**
120    * Frees the request.
121    */

122   public static void free(IncludeDispatchRequest req)
123   {
124     req.free();
125
126     _freeList.free(req);
127   }
128
129   public static class HeaderEnumeration implements Enumeration JavaDoc {
130     private Enumeration JavaDoc _parent;
131     
132     private Hashtable JavaDoc<String JavaDoc,String JavaDoc> _headers;
133     private Iterator JavaDoc<String JavaDoc> _headerIter;
134
135     private String JavaDoc _nextHeader;
136
137     HeaderEnumeration(Enumeration JavaDoc parent, Hashtable JavaDoc<String JavaDoc,String JavaDoc> headers)
138     {
139       _parent = parent;
140       _headers = headers;
141
142       if (headers != null)
143     _headerIter = headers.keySet().iterator();
144     }
145     
146     /**
147      * Returns true if there are more elements.
148      */

149     public boolean hasMoreElements()
150     {
151       if (_nextHeader != null)
152     return true;
153       
154       if (_parent == null && _headerIter == null)
155     return false;
156
157       if (_parent != null) {
158     while (_parent.hasMoreElements() && _nextHeader == null) {
159       _nextHeader = (String JavaDoc) _parent.nextElement();
160
161       if (_nextHeader == null) {
162       }
163       else if (_nextHeader.equalsIgnoreCase("If-Modified-Since") ||
164            _nextHeader.equalsIgnoreCase("If-None-Match") ||
165            _headers != null && _headers.get(_nextHeader) != null) {
166         _nextHeader = null;
167       }
168     }
169
170     if (_nextHeader == null)
171       _parent = null;
172     else
173       return true;
174       }
175
176       if (_headerIter != null) {
177     _nextHeader = _headerIter.next();
178       }
179
180       return _nextHeader != null;
181     }
182     
183     public Object JavaDoc nextElement()
184     {
185       if (! hasMoreElements())
186     return null;
187
188       Object JavaDoc value = _nextHeader;
189       _nextHeader = null;
190
191       return value;
192     }
193   }
194 }
195
Popular Tags