001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.server.webapp;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.FreeList;
034: import com.caucho.server.dispatch.*;
035:
036: import javax.servlet.ServletException;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039: import java.util.Enumeration;
040: import java.util.Hashtable;
041: import java.util.Iterator;
042: import java.util.logging.Logger;
043:
044: /**
045: * sub-request for a include() page
046: */
047: class IncludeDispatchRequest extends DispatchRequest {
048: protected static final Logger log = Logger
049: .getLogger(IncludeDispatchRequest.class.getName());
050:
051: private static final FreeList<IncludeDispatchRequest> _freeList = new FreeList<IncludeDispatchRequest>(
052: 32);
053:
054: private Hashtable<String, String> _headers;
055:
056: protected IncludeDispatchRequest() {
057: }
058:
059: /**
060: * Creates a dispatch request.
061: */
062: public static IncludeDispatchRequest createDispatch() {
063: IncludeDispatchRequest req = _freeList.allocate();
064: if (req == null)
065: req = new IncludeDispatchRequest();
066:
067: return req;
068: }
069:
070: void init(Invocation invocation, WebApp webApp, WebApp oldWebApp,
071: HttpServletRequest request, HttpServletResponse response,
072: String method, String uri, String servletPath,
073: String pathInfo, String queryString, String addedQuery)
074: throws ServletException {
075: super .init(invocation, webApp, oldWebApp, request, response,
076: method, uri, servletPath, pathInfo, queryString,
077: addedQuery);
078:
079: _headers = null;
080: }
081:
082: /**
083: * Returns the include's view of the header names.
084: */
085: public Enumeration getHeaderNames() {
086: return new HeaderEnumeration(super .getHeaderNames(), _headers);
087: }
088:
089: public String getHeader(String key) {
090: String value = null;
091: if (_headers != null)
092: value = (String) _headers.get(key);
093:
094: if (value != null)
095: return value;
096:
097: // The included file must ignore caching directives from the
098: // original request
099: if (key.equalsIgnoreCase("If-Modified-Since")
100: || key.equalsIgnoreCase("If-None-Match"))
101: return null;
102: else {
103: return super .getHeader(key);
104: }
105: }
106:
107: public void setHeader(String key, String value) {
108: if (_headers == null)
109: _headers = new Hashtable<String, String>();
110: _headers.put(key, value);
111: }
112:
113: /**
114: * Frees the request.
115: */
116: public static void free(IncludeDispatchRequest req) {
117: req.free();
118:
119: _freeList.free(req);
120: }
121:
122: public static class HeaderEnumeration implements Enumeration {
123: private Enumeration _parent;
124:
125: private Hashtable<String, String> _headers;
126: private Iterator<String> _headerIter;
127:
128: private String _nextHeader;
129:
130: HeaderEnumeration(Enumeration parent,
131: Hashtable<String, String> headers) {
132: _parent = parent;
133: _headers = headers;
134:
135: if (headers != null)
136: _headerIter = headers.keySet().iterator();
137: }
138:
139: /**
140: * Returns true if there are more elements.
141: */
142: public boolean hasMoreElements() {
143: if (_nextHeader != null)
144: return true;
145:
146: if (_parent == null && _headerIter == null)
147: return false;
148:
149: if (_parent != null) {
150: while (_parent.hasMoreElements() && _nextHeader == null) {
151: _nextHeader = (String) _parent.nextElement();
152:
153: if (_nextHeader == null) {
154: } else if (_nextHeader
155: .equalsIgnoreCase("If-Modified-Since")
156: || _nextHeader
157: .equalsIgnoreCase("If-None-Match")
158: || _headers != null
159: && _headers.get(_nextHeader) != null) {
160: _nextHeader = null;
161: }
162: }
163:
164: if (_nextHeader == null)
165: _parent = null;
166: else
167: return true;
168: }
169:
170: if (_headerIter != null) {
171: _nextHeader = _headerIter.next();
172: }
173:
174: return _nextHeader != null;
175: }
176:
177: public Object nextElement() {
178: if (!hasMoreElements())
179: return null;
180:
181: Object value = _nextHeader;
182: _nextHeader = null;
183:
184: return value;
185: }
186: }
187: }
|