KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webdav > AbstractMoveOrCopyMethod


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webdav;
18
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20
21 import org.alfresco.service.cmr.model.FileFolderService;
22 import org.alfresco.service.cmr.model.FileInfo;
23 import org.alfresco.service.cmr.model.FileNotFoundException;
24 import org.alfresco.service.cmr.repository.NodeRef;
25
26 /**
27  * Implements the WebDAV COPY and MOVE methods
28  *
29  * @author Derek Hulley
30  */

31 public abstract class AbstractMoveOrCopyMethod extends HierarchicalMethod
32 {
33     /**
34      * Default constructor
35      */

36     public AbstractMoveOrCopyMethod()
37     {
38     }
39     
40     /**
41      * Implement the move or copy, depending on the implementation
42      *
43      * @param fileFolderService the service to do the work
44      * @param sourceNodeRef the node to copy or move
45      * @param destParentNodeRef the destination parent
46      * @param name the name of the file or folder
47      * @throws Exception
48      */

49     protected abstract void moveOrCopy(
50             FileFolderService fileFolderService,
51             NodeRef sourceNodeRef,
52             NodeRef destParentNodeRef,
53             String JavaDoc name) throws Exception JavaDoc;
54
55     /**
56      * Exceute the request
57      *
58      * @exception WebDAVServerException
59      */

60     protected final void executeImpl() throws WebDAVServerException, Exception JavaDoc
61     {
62         FileFolderService fileFolderService = getFileFolderService();
63
64         NodeRef rootNodeRef = getRootNodeRef();
65         String JavaDoc servletPath = getServletPath();
66
67         // Debug
68
if (logger.isDebugEnabled())
69         {
70             logger.debug("Copy from " + getPath() + " to " + getDestinationPath());
71         }
72
73         // the source must exist
74
String JavaDoc sourcePath = getPath();
75         FileInfo sourceInfo = null;
76         try
77         {
78             sourceInfo = getDAVHelper().getNodeForPath(rootNodeRef, sourcePath, servletPath);
79         }
80         catch (FileNotFoundException e)
81         {
82             throw new WebDAVServerException(HttpServletResponse.SC_NOT_FOUND);
83         }
84
85         // the destination parent must exist
86
String JavaDoc destPath = getDestinationPath();
87         FileInfo destParentInfo = null;
88         try
89         {
90             destParentInfo = getDAVHelper().getParentNodeForPath(rootNodeRef, destPath, servletPath);
91         }
92         catch (FileNotFoundException e)
93         {
94             if (logger.isDebugEnabled())
95             {
96                 logger.debug("Destination parent folder doesn't exist: " + destPath);
97             }
98             throw new WebDAVServerException(HttpServletResponse.SC_NOT_FOUND);
99         }
100         
101         // check for the existence of the destination node
102
FileInfo destInfo = null;
103         try
104         {
105             destInfo = getDAVHelper().getNodeForPath(rootNodeRef, destPath, servletPath);
106             if (!hasOverWrite())
107             {
108                 if (logger.isDebugEnabled())
109                 {
110                     logger.debug("Destination exists but overwrite is not allowed");
111                 }
112                 // it exists and we may not overwrite
113
throw new WebDAVServerException(HttpServletResponse.SC_PRECONDITION_FAILED);
114             }
115             // delete the destination node if it is not the same as the source node
116
if (!destInfo.getNodeRef().equals(sourceInfo.getNodeRef()))
117             {
118                 // attempting to move or copy onto another node
119
fileFolderService.delete(destInfo.getNodeRef());
120             }
121             else
122             {
123                 // it is a copy or move onto itself
124
}
125         }
126         catch (FileNotFoundException e)
127         {
128             // destination doesn't exist
129
}
130
131         NodeRef sourceNodeRef = sourceInfo.getNodeRef();
132         NodeRef destParentNodeRef = destParentInfo.getNodeRef();
133         String JavaDoc name = getDAVHelper().splitPath(destPath)[1];
134         moveOrCopy(fileFolderService, sourceNodeRef, destParentNodeRef, name);
135
136         // Set the response status
137
if (destInfo == null)
138         {
139             m_response.setStatus(HttpServletResponse.SC_CREATED);
140         }
141         else
142         {
143             m_response.setStatus(HttpServletResponse.SC_NO_CONTENT);
144         }
145     }
146 }
147
Popular Tags