KickJava   Java API By Example, From Geeks To Geeks.

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


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.model.ContentModel;
22 import org.alfresco.service.cmr.model.FileFolderService;
23 import org.alfresco.service.cmr.model.FileInfo;
24 import org.alfresco.service.cmr.model.FileNotFoundException;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.w3c.dom.Document JavaDoc;
27
28 /**
29  * Implements the WebDAV MKCOL method
30  *
31  * @author gavinc
32  */

33 public class MkcolMethod extends WebDAVMethod
34 {
35     /**
36      * Default constructor
37      */

38     public MkcolMethod()
39     {
40     }
41
42     /**
43      * Parse the request headers
44      *
45      * @Exception WebDAVServerException
46      */

47     protected void parseRequestHeaders() throws WebDAVServerException
48     {
49         // Nothing to do in this method
50
}
51
52     /**
53      * Parse the request body
54      *
55      * @exception WebDAVServerException
56      */

57     protected void parseRequestBody() throws WebDAVServerException
58     {
59         // There should not be a body with the MKCOL request
60

61         Document JavaDoc body = getRequestBodyAsDocument();
62
63         if (body != null)
64         {
65             throw new WebDAVServerException(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
66         }
67     }
68
69     /**
70      * Exceute the request
71      *
72      * @exception WebDAVServerException
73      */

74     protected void executeImpl() throws WebDAVServerException, Exception JavaDoc
75     {
76         FileFolderService fileFolderService = getFileFolderService();
77
78         // see if it exists
79
try
80         {
81             getDAVHelper().getNodeForPath(getRootNodeRef(), getPath(), getServletPath());
82             // already exists
83
throw new WebDAVServerException(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
84         }
85         catch (FileNotFoundException e)
86         {
87             // it doesn't exist
88
}
89         
90         // Trim the last path component and check if the parent path exists
91
String JavaDoc parentPath = getPath();
92         int lastPos = parentPath.lastIndexOf(WebDAVHelper.PathSeperator);
93         
94         NodeRef parentNodeRef = null;
95
96         if ( lastPos == 0)
97         {
98             // Create new folder at root
99

100             parentPath = WebDAVHelper.PathSeperator;
101             parentNodeRef = getRootNodeRef();
102         }
103         else if (lastPos != -1)
104         {
105             // Trim the last path component
106
parentPath = parentPath.substring(0, lastPos + 1);
107             try
108             {
109                 FileInfo parentFileInfo = getDAVHelper().getNodeForPath(getRootNodeRef(), parentPath, m_request.getServletPath());
110                 parentNodeRef = parentFileInfo.getNodeRef();
111             }
112             catch (FileNotFoundException e)
113             {
114                 // parent path is missing
115
throw new WebDAVServerException(HttpServletResponse.SC_NOT_FOUND);
116             }
117         }
118         else
119         {
120             // Looks like a bad path
121
throw new WebDAVServerException(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
122         }
123
124         // Get the new folder name
125
String JavaDoc folderName = getPath().substring(lastPos + 1);
126
127         // Create the new folder node
128
fileFolderService.create(parentNodeRef, folderName, ContentModel.TYPE_FOLDER);
129
130         // Return a success status
131
m_response.setStatus(HttpServletResponse.SC_CREATED);
132     }
133 }
134
Popular Tags