KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.InputStream JavaDoc;
21
22 import com.sslexplorer.security.LogonControllerFactory;
23 import com.sslexplorer.vfs.VFSLockManager;
24 import com.sslexplorer.vfs.VFSOutputStream;
25 import com.sslexplorer.vfs.VFSResource;
26 import com.sslexplorer.vfs.webdav.DAVMethod;
27 import com.sslexplorer.vfs.webdav.DAVTransaction;
28 import com.sslexplorer.vfs.webdav.LockedException;
29
30 /**
31  * <p>
32  * <a HREF="http://www.rfc-editor.org/rfc/rfc2518.txt">WebDAV</a>
33  * <code>PUT</code> metohd implementation.
34  * </p>
35  *
36  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
37  */

38 public class PUT implements DAVMethod {
39
40     /**
41      * <p>
42      * Create a new {@link PUT} instance.
43      * </p>
44      */

45     public PUT() {
46         super();
47     }
48
49     /**
50      * <p>
51      * Process the <code>PUT</code> method.
52      * </p>
53      */

54     public void process(DAVTransaction transaction, VFSResource resource) throws LockedException, IOException JavaDoc {
55         
56         String JavaDoc handle = VFSLockManager.getNewHandle();
57         VFSLockManager.getInstance().lock(resource, transaction.getSessionInfo(), true, true, handle);
58         try {
59             /*
60              * The HTTP status code will depend on the existance of the
61              * resource: if not found: HTTP/1.1 201 Created if existing:
62              * HTTP/1.1 204 No Content
63              */

64             transaction.setStatus(resource.isNull() ? 201 : 204);
65
66             /* Open the streams for reading and writing */
67             InputStream JavaDoc in = transaction.getInputStream();
68             VFSOutputStream out = resource.getOutputStream();
69
70             int sessionTimeoutBlockId = LogonControllerFactory.getInstance().addSessionTimeoutBlock(
71                     transaction.getRequest().getSession(), "DAV Upload");
72         
73             /* Write the content from the PUT to the specified resource */
74             try {
75                 byte buffer[] = new byte[32768];
76                 int k = -1;
77                 while (in!=null && (k = in.read(buffer)) != -1)
78                     out.write(buffer, 0, k);
79                 out.close();
80             } finally {
81                 out.abort();
82                 LogonControllerFactory.getInstance().removeSessionTimeoutBlock(transaction.getRequest().getSession(), sessionTimeoutBlockId);
83             }
84             resource.getMount().resourceUpload(resource, transaction, null);
85         } catch (Exception JavaDoc e) {
86             resource.getMount().resourceUpload(resource, transaction, e);
87             IOException JavaDoc ioe = new IOException JavaDoc(e.getMessage());
88             ioe.initCause(e);
89             throw ioe;
90         } finally {
91             VFSLockManager.getInstance().unlock(transaction.getSessionInfo(), handle);
92         }
93     }
94 }
95
Popular Tags