01: // PushCacheEntityResource.java
02: // $Id: PushEntityCachedResource.java,v 1.1 2001/10/03 15:00:46 ylafon Exp $
03: // (c) COPYRIGHT MIT, INRIA and Keio, 2001.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.www.protocol.http.cache.push;
07:
08: import org.w3c.www.protocol.http.Request;
09:
10: import java.io.FileInputStream;
11: import java.io.FileOutputStream;
12:
13: import org.w3c.www.protocol.http.cache.CachedResource;
14: import org.w3c.www.protocol.http.cache.EntityCachedResource;
15:
16: /**
17: * PushEntityCachedResource
18: * EntityCachedResource that reads data from the file rather than attempting
19: * to use ActiveStream to tee output to a client that is not there.
20: *
21: * @author Paul Henshaw, The Fantastic Corporation, Paul.Henshaw@fantastic.com
22: * @version $Revision: 1.1 $
23: * $Id: PushEntityCachedResource.java,v 1.1 2001/10/03 15:00:46 ylafon Exp $
24: */
25: public class PushEntityCachedResource extends EntityCachedResource {
26:
27: /**
28: * This constructor required to handle startup when cache already
29: * contains PushEntityCachedResources
30: */
31: public PushEntityCachedResource() {
32: super ();
33: }
34:
35: /**
36: * Construct a PushEntityCachedResource
37: *
38: * Used by the PushCacheManager to actually store a PUSH resource.
39: * Note PushEntityCachedResource are used only when saving
40: * resources. When extracting resources from the cache,
41: * EntityCachedResources are used.
42: *
43: * @param filter the PushCacheFilter that in fact has not done
44: * anything yet, but which knows how to handle a
45: * PUSHed resource
46: * @param req the forged request for a URL
47: * @param rep the forged reply for the URL
48: */
49: protected PushEntityCachedResource(PushCacheFilter filter,
50: Request req, PushReply rep) {
51: try {
52: invalidated = false;
53: setValue(ATTR_IDENTIFIER, req.getURL().toExternalForm());
54:
55: // Keep fast track of the filter:
56: this .filter = filter;
57:
58: // update the headers
59: updateInfo(req, rep);
60:
61: // and do some calculation according to the validator
62: filter.getValidator().updateExpirationInfo(this , req, rep);
63:
64: // Save the content of resource into the content cache:
65: java.io.File outfile = filter.getPushCacheStore()
66: .getNewEntryFile();
67:
68: FileOutputStream os = new FileOutputStream(outfile);
69: FileInputStream is = rep.getStream();
70:
71: byte[] buffer = new byte[4096];
72: int wrote = 0;
73: int wantedsize = rep.getContentLength();
74: int this time;
75: while (wrote < wantedsize) {
76: this time = Math.min(is.available(), 4096);
77: is.read(buffer, 0, this time);
78: os.write(buffer, 0, this time);
79: wrote += this time;
80: }
81:
82: os.close();
83: setFile(outfile);
84: setLoadState(STATE_LOAD_COMPLETE);
85: setCurrentLength(wrote);
86: } catch (Exception e) {
87: e.printStackTrace();
88: }
89: }
90: }
|