KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URI JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import com.sslexplorer.vfs.VFSLockManager;
26 import com.sslexplorer.vfs.VFSResource;
27 import com.sslexplorer.vfs.webdav.DAVException;
28 import com.sslexplorer.vfs.webdav.DAVMethod;
29 import com.sslexplorer.vfs.webdav.DAVMultiStatus;
30 import com.sslexplorer.vfs.webdav.DAVProcessor;
31 import com.sslexplorer.vfs.webdav.DAVServlet;
32 import com.sslexplorer.vfs.webdav.DAVTransaction;
33 import com.sslexplorer.vfs.webdav.LockedException;
34
35 /**
36  * <p>
37  * <a HREF="http://www.rfc-editor.org/rfc/rfc2518.txt">WebDAV</a>
38  * <code>COPY</code> metohd implementation.
39  * </p>
40  *
41  * @author <a HREF="http://www.betaversion.org/~pier/">Pier Fumagalli</a>
42  */

43 public class COPY implements DAVMethod {
44
45     final static Log log = LogFactory.getLog(COPY.class);
46
47     /**
48      * <p>
49      * Create a new {@link COPY} instance.
50      * </p>
51      */

52     public COPY() {
53         super();
54     }
55
56     /**
57      * <p>
58      * Process the <code>COPY</code> method.
59      * </p>
60      *
61      * @throws IOException
62      */

63     public void process(DAVTransaction transaction, VFSResource resource) throws LockedException, IOException JavaDoc {
64         
65         String JavaDoc handle = VFSLockManager.getNewHandle();
66         VFSLockManager.getInstance().lock(resource, transaction.getSessionInfo(), false, true, handle);
67    
68         String JavaDoc action = (String JavaDoc) transaction.getRequest().getMethod();
69
70         VFSResource dest = null;
71         
72         try {
73             
74             URI JavaDoc target = transaction.getDestination();
75
76             if (target == null)
77                 throw new DAVException(412, "No destination");
78             if(log.isDebugEnabled())
79                 log.debug("Target " + target.getPath());
80             
81             try {
82                 DAVProcessor processor = DAVServlet.getDAVProcessor(transaction.getRequest());
83                 dest = processor.getRepository().getResource(resource.getLaunchSession(), target.getPath(), transaction.getCredentials()/*, transaction*/);
84                 VFSLockManager.getInstance().lock(dest, transaction.getSessionInfo(), true, false, handle);
85
86             } catch (Exception JavaDoc ex) {
87                 log.error("Failed to get resource. ", ex);
88                 transaction.setStatus(500);
89                 return;
90             }
91             
92             int depth = transaction.getDepth();
93             boolean recursive = false;
94             if (depth == 0) {
95                 recursive = false;
96             } else if (depth == DAVTransaction.INFINITY) {
97                 recursive = true;
98             } else {
99                 throw new DAVException(412, "Invalid Depth specified");
100             }
101             try {
102                 resource.copy(dest, transaction.getOverwrite(), recursive);
103                 transaction.setStatus(204);
104             } catch (DAVMultiStatus multistatus) {
105                 multistatus.write(transaction);
106             }
107             if (action.equals("COPY")) {
108                 resource.getMount().resourceCopy(resource, dest, transaction, null);
109             }
110         } catch (Exception JavaDoc e) {
111             if (action.equals("COPY")) {
112                 resource.getMount().resourceCopy(resource, dest, transaction, e);
113             }
114             IOException JavaDoc ioe = new IOException JavaDoc(e.getMessage());
115             ioe.initCause(e);
116             throw ioe;
117         } finally {
118             VFSLockManager.getInstance().unlock(transaction.getSessionInfo(), handle);
119         }
120         
121     }
122 }
123
Popular Tags