KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > file > HttpFile


1 package com.quadcap.http.servlets.file;
2
3 /* Copyright 1998 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.FileInputStream JavaDoc;
43 import java.io.FileNotFoundException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.OutputStream JavaDoc;
47
48 import java.net.URL JavaDoc;
49 import java.net.URLConnection JavaDoc;
50
51 import javax.servlet.ServletContext JavaDoc;
52
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55
56 import com.quadcap.util.collections.Cacheable;
57
58 import com.quadcap.io.IO;
59
60 import com.quadcap.io.dir.Directory;
61 import com.quadcap.io.dir.Entry;
62
63 import com.quadcap.util.Debug;
64 import com.quadcap.util.Util;
65
66 /**
67  * This class represents a single cached file.
68  *
69  * @author Stan Bailes
70  */

71 public class HttpFile extends Cacheable {
72     ServletContext JavaDoc context;
73     String JavaDoc contentType = "text/plain";
74     File JavaDoc file;
75     URL JavaDoc url;
76     long lastMod;
77     long lastCheck = -1;
78     byte[] buf = null;
79
80     static int MAX_CACHED = 16 * 1024;
81     
82     /**
83      * Default constructor
84      */

85     public HttpFile() {}
86
87     public void init(Object JavaDoc store, Object JavaDoc key) throws IOException JavaDoc {
88     super.init(store, key);
89         FileServlet fs = (FileServlet)store;
90         this.context = fs.getServletContext();
91         String JavaDoc path = key.toString();
92         this.contentType = context.getMimeType(path);
93         this.file = new File JavaDoc(path);
94         if (!file.exists()) {
95             this.file = null;
96             this.url = fs.getURL(path);
97             if (url == null) {
98                 throw new FileNotFoundException JavaDoc("Not found: " + path);
99             }
100         }
101     compile();
102     }
103
104     public synchronized void compile() throws IOException JavaDoc {
105         if (file != null) {
106             if (!file.exists()) {
107                 throw new FileNotFoundException JavaDoc("Not found: " + file);
108             }
109             if (file.isDirectory()) {
110                 File JavaDoc g = new File JavaDoc(file, "index.html");
111                 if (g.canRead()) {
112                     file = g;
113                     contentType = "text/html";
114                 } else {
115                     throw new FileNotFoundException JavaDoc("Not found: " + file);
116                 }
117             }
118             if (file.canRead() && file.length() < MAX_CACHED) {
119                 buf = new byte[(int)file.length()];
120                 FileInputStream JavaDoc is = new FileInputStream JavaDoc(file);
121                 try {
122                     IO.readFully(is, buf);
123                 } finally {
124                     is.close();
125                 }
126                 this.lastMod = file.lastModified();
127             } else {
128                 buf = null;
129             }
130         } else {
131             URLConnection JavaDoc conn = url.openConnection();
132             long len = conn.getContentLength();
133             if (len < MAX_CACHED) {
134                 buf = new byte[(int)len];
135                 try {
136                     this.lastMod = conn.getLastModified();
137                     InputStream JavaDoc is = conn.getInputStream();
138                     try {
139                         IO.readFully(is, buf);
140                     } finally {
141                         is.close();
142                     }
143                 } catch (IOException JavaDoc e) {
144                     buf = null;
145                     throw e;
146                 }
147             } else {
148                 buf = null;
149             }
150         }
151     }
152
153     public synchronized void service(HttpServletRequest JavaDoc req,
154                      HttpServletResponse JavaDoc res)
155     throws IOException JavaDoc
156     {
157         //Jni j = new Jni("file");
158
res.setContentType(contentType);
159         //j.dump("setContentType");
160
OutputStream JavaDoc os = res.getOutputStream();
161         //j.dump("getOutputStream");
162
if (buf != null) {
163         res.setContentLength(buf.length);
164             //j.dump("setContentLength");
165
os.write(buf);
166             //j.dump("os.write(buf)");
167
} else if (file != null) {
168         res.setContentLength((int)file.length());
169         try {
170         FileInputStream JavaDoc is = new FileInputStream JavaDoc(file);
171                 try {
172                     IO.copyStream(is, os);
173                 } finally {
174                     is.close();
175                 }
176         } catch (FileNotFoundException JavaDoc fe) {
177         res.sendError(res.SC_NOT_FOUND, "Not found: " +
178                               file.getName());
179         }
180     } else {
181             URLConnection JavaDoc conn = url.openConnection();
182             res.setContentLength(conn.getContentLength());
183             InputStream JavaDoc is = conn.getInputStream();
184             try {
185                 IO.copyStream(is, os);
186             } finally {
187                 is.close();
188             }
189         }
190         //j.dump("done");
191
}
192
193     public synchronized void checkModified() throws Exception JavaDoc {
194         long now = System.currentTimeMillis();
195         if (now - lastCheck > 2000) {
196             lastCheck = now;
197             if (file != null) {
198                 if (file.lastModified() > lastMod) {
199                     compile();
200                 }
201             } else {
202                 URLConnection JavaDoc conn = url.openConnection();
203                 if (conn.getLastModified() > lastMod) {
204                     compile();
205                 }
206             }
207         }
208     }
209
210     public Object JavaDoc getData() {
211     return this;
212     }
213
214     public void setData(Object JavaDoc obj) {
215     throw new RuntimeException JavaDoc("not implemented");
216     }
217 }
218
Popular Tags