KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > vfs > webdav > methods > HEAD


1 /* ========================================================================== *
2  * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
3  * All rights reserved. *
4  * ========================================================================== *
5  * *
6  * Licensed under the Apache License, Version 2.0 (the "License"). You may *
7  * not use this file except in compliance with the License. You may obtain a *
8  * copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. *
9  * *
10  * Unless required by applicable law or agreed to in writing, software *
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
13  * License for the specific language governing permissions and limitations *
14  * under the License. *
15  * *
16  * ========================================================================== */

17 package com.sslexplorer.vfs.webdav.methods;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Date JavaDoc;
21
22 import com.sslexplorer.boot.Util;
23 import com.sslexplorer.vfs.VFSLockManager;
24 import com.sslexplorer.vfs.VFSResource;
25 import com.sslexplorer.vfs.webdav.DAVMethod;
26 import com.sslexplorer.vfs.webdav.DAVRedirection;
27 import com.sslexplorer.vfs.webdav.DAVTransaction;
28 import com.sslexplorer.vfs.webdav.DAVUtilities;
29 import com.sslexplorer.vfs.webdav.LockedException;
30
31 /**
32  * <p><a HREF="http://www.rfc-editor.org/rfc/rfc2616.txt">HTTP</a>
33  * <code>HEAD</code> metohd implementation.</p>
34  *
35  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
36  */

37 public class HEAD implements DAVMethod {
38     public static final String JavaDoc COLLECTION_MIME_TYPE = "text/html";
39     /**
40      * <p>Create a new {@link HEAD} instance.</p>
41      */

42     public HEAD() {
43         super();
44     }
45
46     /**
47      * <p>Process the <code>HEAD</code> method.</p>
48      */

49     public void process(DAVTransaction transaction, VFSResource resource)
50     throws LockedException, IOException JavaDoc {
51         String JavaDoc handle = VFSLockManager.getNewHandle();
52         VFSLockManager.getInstance().lock(resource, transaction.getSessionInfo(), false, true, handle);
53         try {
54             doHead(transaction, resource);
55         } finally {
56             VFSLockManager.getInstance().unlock(transaction.getSessionInfo(), handle);
57         }
58     }
59     
60     protected void doHead(DAVTransaction transaction, VFSResource resource)
61     throws LockedException, IOException JavaDoc {
62         /* Check if we have to force a resource not found or a redirection */
63         if (resource.isNull()) {
64          
65             /**
66              * LDP - Don't throw a DAVException but set some headers, this is
67              * to make Windows Webfolders work
68              */

69             String JavaDoc mime = COLLECTION_MIME_TYPE + "; charset=\"utf-8\"";
70             transaction.setContentType(mime);
71             Util.noCache(transaction.getResponse());
72             
73             transaction.setStatus(404);
74             
75             return;
76         }
77         if (transaction.isRequiredRootRedirect() || !transaction.isResourcePath(resource.getFullPath())) {
78            throw new DAVRedirection(false, resource);
79         }
80
81         /* Check if this is a conditional (processable only for resources) */
82         Date JavaDoc ifmod = transaction.getIfModifiedSince();
83         Date JavaDoc lsmod = resource.getLastModified();
84         if (resource.isResource() && (ifmod != null) && (lsmod != null)) {
85             /* HTTP doesn't send milliseconds, but Java does, so, reset them */
86             lsmod = new Date JavaDoc(((long)(lsmod.getTime() / 1000)) * 1000);
87            // if (!ifmod.before(lsmod)) throw new DAVNotModified(resource);
88
}
89
90         /* Set the headers of this method */
91         String JavaDoc ctyp = resource.getContentType();
92         String JavaDoc etag = resource.getEntityTag();
93         String JavaDoc lmod = DAVUtilities.format(resource.getLastModified());
94         String JavaDoc clen = DAVUtilities.format(resource.getContentLength());
95         
96         /* Set the normal headers that are required for a GET */
97         if (ctyp != null) transaction.setContentType(ctyp == null ? "application/octet-stream" : ctyp);
98         if (etag != null) transaction.setHeader("ETag", etag);
99         if (lmod != null) transaction.setHeader("Last-Modified", lmod);
100         if (clen != null) transaction.setHeader("Content-Length", clen);
101     }
102 }
103
Popular Tags