KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > StaticPage


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.vfs.Depend;
33 import com.caucho.vfs.Path;
34 import com.caucho.vfs.PersistentDependency;
35 import com.caucho.vfs.ReadStream;
36 import com.caucho.vfs.TempCharBuffer;
37 import com.caucho.vfs.WriteStream;
38
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.ServletRequest JavaDoc;
41 import javax.servlet.ServletResponse JavaDoc;
42 import javax.servlet.http.HttpServletRequest JavaDoc;
43 import javax.servlet.http.HttpServletResponse JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.PrintWriter JavaDoc;
46 import java.util.ArrayList JavaDoc;
47
48 /**
49  * A static page is a page that's just a static file.
50  */

51 public class StaticPage extends Page {
52   private Path _cacheEntry;
53   private long _lastModified;
54   private int _contentLength;
55   private boolean _hasSession;
56
57   /**
58    * Create a new Static page.
59    *
60    * @param path the underlying file
61    * @param hasSession if true, create a new session
62    */

63   StaticPage(Path path, boolean hasSession)
64     throws IOException JavaDoc
65   {
66     _cacheEntry = path;
67     _contentLength = (int) _cacheEntry.getLength();
68     _hasSession = hasSession;
69
70     _caucho_setCacheable();
71   }
72
73   public void init(Path path)
74     throws ServletException JavaDoc
75   {
76   }
77
78   /**
79    * Returns true if the source has modified for this page.
80    */

81   public boolean _caucho_isModified()
82   {
83     return ! _cacheEntry.exists() || super._caucho_isModified();
84   }
85
86   void _caucho_setUncacheable()
87   {
88     _lastModified = 0;
89   }
90
91   public long getLastModified(HttpServletRequest JavaDoc request)
92   {
93     return _caucho_lastModified();
94   }
95
96   /**
97    * Executes the JSP Page
98    */

99   public void service(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
100     throws IOException JavaDoc, ServletException JavaDoc
101   {
102     HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
103     HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) response;
104     
105     _caucho_init(req, res);
106
107     if (_hasSession) {
108       req.getSession();
109       res.setHeader("Cache-Control", "private");
110     }
111
112     // res.setContentLength(_contentLength);
113

114     TempCharBuffer buf = TempCharBuffer.allocate();
115     char []cBuf = buf.getBuffer();
116     int len;
117
118     PrintWriter JavaDoc out = response.getWriter();
119     
120     ReadStream rs = _cacheEntry.openRead();
121     rs.setEncoding("UTF-8");
122     try {
123       while ((len = rs.read(cBuf, 0, cBuf.length)) > 0) {
124     out.write(cBuf, 0, len);
125       }
126     } finally {
127       rs.close();
128     }
129
130     TempCharBuffer.free(buf);
131   }
132
133   public boolean disableLog()
134   {
135     return true;
136   }
137
138   public static void writeDepend(Path dependPath,
139                  ArrayList JavaDoc<PersistentDependency> dependList)
140     throws IOException JavaDoc
141   {
142     WriteStream os = dependPath.openWrite();
143     try {
144       for (int i = 0; i < dependList.size(); i++) {
145     PersistentDependency dependency = dependList.get(i);
146
147     if (dependency instanceof Depend) {
148       Depend depend = (Depend) dependency;
149
150       os.print('"');
151       os.print(depend.getPath().getNativePath());
152       os.print("\" \"");
153       os.print(depend.getDigest());
154       os.println("\"");
155     }
156       }
157     } finally {
158       os.close();
159     }
160   }
161
162   static ArrayList JavaDoc<Depend> parseDepend(Path dependPath)
163     throws IOException JavaDoc
164   {
165     ReadStream is = dependPath.openRead();
166     try {
167       ArrayList JavaDoc<Depend> dependList = new ArrayList JavaDoc<Depend>();
168       
169       String JavaDoc name;
170
171       while ((name = parseName(is)) != null) {
172     long digest = Long.parseLong(parseName(is));
173
174     Depend depend = new Depend(dependPath.lookup(name), digest);
175
176     dependList.add(depend);
177       }
178
179       return dependList;
180     } finally {
181       is.close();
182     }
183   }
184
185   private static String JavaDoc parseName(ReadStream is)
186     throws IOException JavaDoc
187   {
188     int ch;
189     
190     for (ch = is.read(); ch > 0 && ch != '"'; ch = is.read()) {
191     }
192
193     if (ch < 0)
194       return null;
195
196     CharBuffer cb = new CharBuffer();
197     
198     for (ch = is.read(); ch > 0 && ch != '"'; ch = is.read()) {
199       cb.append((char) ch);
200     }
201
202     if (ch < 0)
203       return null;
204
205     return cb.toString();
206   }
207   
208   public void destroy()
209   {
210     /*
211     try {
212       _cacheEntry.remove();
213     } catch (IOException e) {
214     }
215     */

216   }
217
218   /**
219    * Returns a printable version of the static page object.
220    */

221   public String JavaDoc toString()
222   {
223     return "StaticPage[" + _cacheEntry + "]";
224   }
225 }
226
Popular Tags