001: // ZipFrame.java
002: // $Id: ZipFrame.java,v 1.15 2007/02/09 22:38:02 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.zip;
007:
008: import java.util.Vector;
009: import java.util.Enumeration;
010: import java.io.InputStream;
011: import java.io.File;
012: import java.net.URLEncoder;
013: import org.w3c.tools.sorter.Sorter;
014: import org.w3c.tools.resources.FramedResource;
015: import org.w3c.tools.resources.FileResource;
016: import org.w3c.tools.resources.ContainerInterface;
017: import org.w3c.tools.resources.ContainerResource;
018: import org.w3c.tools.resources.ProtocolException;
019: import org.w3c.tools.resources.ResourceException;
020: import org.w3c.jigsaw.frames.HTTPFrame;
021: import org.w3c.jigsaw.http.HTTPException;
022: import org.w3c.jigsaw.http.Reply;
023: import org.w3c.jigsaw.http.Request;
024: import org.w3c.www.http.HTTP;
025: import org.w3c.jigsaw.html.HtmlGenerator;
026: import org.w3c.tools.resources.ResourceReference;
027: import org.w3c.tools.resources.InvalidResourceException;
028: import org.w3c.tools.resources.MultipleLockException;
029:
030: import org.w3c.tools.resources.ProtocolException;
031: import org.w3c.tools.resources.ResourceException;
032:
033: /**
034: * @version $Revision: 1.15 $
035: * @author Benoît Mahé (bmahe@w3.org)
036: */
037: public class ZipFrame extends HTTPFrame {
038:
039: protected ZipFileResource zipfresource = null;
040:
041: public void registerResource(FramedResource resource) {
042: super .registerResource(resource);
043: if (resource instanceof ZipFileResource)
044: zipfresource = (ZipFileResource) resource;
045: }
046:
047: /**
048: * Create the reply relative to the given file.
049: * @param request the incomming request.
050: * @return A Reply instance
051: * @exception org.w3c.tools.resources.ProtocolException if processing
052: * the request failed.
053: * @exception org.w3c.tools.resources.ResourceException if the resource
054: * got a fatal error.
055: */
056: protected Reply createFileReply(Request request)
057: throws ProtocolException, ResourceException {
058: Reply reply = null;
059: if (zipfresource == null) {
060: throw new ResourceException(
061: "this frame is not attached to a "
062: + "ZipFileResource. ("
063: + resource.getIdentifier() + ")");
064: }
065: // Default to full reply:
066: reply = createDefaultReply(request, HTTP.OK);
067: InputStream in = zipfresource.getInputStream();
068: if (in != null)
069: reply.setStream(in);
070: return reply;
071: }
072:
073: /**
074: * Get for FileResource
075: * @param request the incomming request.
076: * @return A Reply instance
077: * @exception ProtocolException If processsing the request failed.
078: * @exception ResourceException If the resource got a fatal error.
079: */
080: protected Reply getFileResource(Request request)
081: throws ProtocolException, ResourceException {
082: if (fresource == null)
083: throw new ResourceException(
084: "this frame is not attached to a "
085: + "FileResource. ("
086: + resource.getIdentifier() + ")");
087: Reply reply = null;
088: File file = fresource.getFile();
089: fresource.checkContent();
090: updateCachedHeaders();
091: // Check validators:
092: int cim = checkIfMatch(request);
093: if ((cim == COND_FAILED) || (cim == COND_WEAK)) {
094: reply = request.makeReply(HTTP.PRECONDITION_FAILED);
095: reply.setContent("Pre-conditions failed.");
096: reply.setContentMD5(null);
097: return reply;
098: }
099: if (checkIfUnmodifiedSince(request) == COND_FAILED) {
100: reply = request.makeReply(HTTP.PRECONDITION_FAILED);
101: reply.setContent("Pre-conditions failed.");
102: reply.setContentMD5(null);
103: return reply;
104: }
105: if (checkValidators(request) == COND_FAILED) {
106: return createDefaultReply(request, HTTP.NOT_MODIFIED);
107: }
108: // Does this file really exists, if so send it back
109: if (zipfresource.hasEntry()) {
110: reply = createFileReply(request);
111: if (request.hasState(STATE_CONTENT_LOCATION))
112: reply.setContentLocation(getURL(request)
113: .toExternalForm());
114: return reply;
115: } else {
116: return deleteMe(request);
117: }
118: }
119:
120: /**
121: * Get ContainerResource listing
122: * @param refresh should we refresh the listing?
123: * @return a boolean (true if refreshed)
124: */
125: public synchronized boolean computeContainerListing(boolean refresh) {
126: ContainerResource cresource = (ContainerResource) resource;
127: if ((refresh) || (listing == null)
128: || (cresource.getLastModified() > listing_stamp)
129: || (getLastModified() > listing_stamp)) {
130:
131: Class http_class = null;
132: try {
133: http_class = Class
134: .forName("org.w3c.jigsaw.frames.HTTPFrame");
135: } catch (ClassNotFoundException ex) {
136: http_class = null;
137: }
138:
139: Enumeration e = cresource.enumerateResourceIdentifiers();
140: Vector resources = Sorter.sortStringEnumeration(e);
141: HtmlGenerator g = new HtmlGenerator("Index of "
142: + cresource.getIdentifier());
143: // Add style link
144: addStyleSheet(g);
145: g.append("<h1>" + cresource.getIdentifier() + "</h1>");
146: // Link to the parent, when possible:
147: if (cresource.getParent() != null) {
148: g.append("<p><a href=\"..\">Parent</a><br>");
149: }
150: // List the children:
151: for (int i = 0; i < resources.size(); i++) {
152: String name = (String) resources.elementAt(i);
153: ResourceReference rr = null;
154: long size = -1;
155: rr = cresource.lookup(name);
156: FramedResource resource = null;
157: if (rr != null) {
158: try {
159: resource = (FramedResource) rr.lock();
160: // remove manually deleted FileResources
161: if (resource instanceof ZipFileResource) {
162: ZipFileResource zfr = (ZipFileResource) resource;
163: if (!zfr.hasEntry()) {
164: try {
165: zfr.delete();
166: } catch (MultipleLockException ex) {
167: }
168: ;
169: continue;
170: } else {
171: size = zfr.getEntrySize();
172: }
173: }
174: // remove manually deleted DirectoryResources
175: if (resource instanceof ZipDirectoryResource) {
176: ZipDirectoryResource zdr = (ZipDirectoryResource) resource;
177: if (!zdr.hasEntry()) {
178: try {
179: zdr.delete();
180: } catch (MultipleLockException ex) {
181: }
182: ;
183: continue;
184: }
185: }
186: HTTPFrame itsframe = null;
187: if (http_class != null) {
188: itsframe = (HTTPFrame) resource
189: .getFrame(http_class);
190: }
191: if (itsframe != null) {
192: // Icon first, if available
193: String icon = itsframe.getIcon();
194: if (icon != null)
195: g.append("<img src=\""
196: + getIconDirectory() + "/"
197: + icon + "\" alt=\"" + icon
198: + "\">");
199: // Resource's name with link:
200: if (resource instanceof ContainerInterface)
201: g.append("<a href=\"", URLEncoder
202: .encode(name), "/\">" + name
203: + "</a>");
204: else
205: g.append("<a href=\"", URLEncoder
206: .encode(name), "\">" + name
207: + "</a>");
208: // resource's title, if any:
209: String title = itsframe.getTitle();
210: if (title != null)
211: g.append(" " + title);
212: //size (if any)
213: if (size != -1) {
214: String s = null;
215: if (size > 1023) {
216: s = " [" + (size / 1024) + " Kb]";
217: } else {
218: s = " [" + size + " bytes]";
219: }
220: g.append(s);
221: }
222: g.append("<br>\n");
223: } else {
224: // Resource's name with link:
225: g
226: .append(name
227: + " (<i>Not available via HTTP.</i>)");
228: g.append("<br>\n");
229: }
230: } catch (InvalidResourceException ex) {
231: g
232: .append(name
233: + " cannot be loaded (server misconfigured)");
234: g.append("<br>\n");
235: continue;
236: } finally {
237: rr.unlock();
238: }
239: }
240: }
241: g.close();
242: listing_stamp = getLastModified();
243: listing = g;
244: return true;
245: }
246: return false;
247: }
248:
249: /**
250: * The default PUT method replies with a not implemented.
251: * @param request The request to handle.
252: * @exception ProtocolException Always thrown, to return a NOT_IMPLEMENTED
253: * error.
254: * @exception ResourceException If the resource got a fatal error.
255: */
256:
257: public Reply put(Request request) throws ProtocolException,
258: ResourceException {
259: Reply error = request.makeReply(HTTP.NOT_IMPLEMENTED);
260: error
261: .setContent("Method PUT not implemented for zipped document");
262: throw new HTTPException(error);
263: }
264:
265: }
|