KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > method > MkcolMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/MkcolMethod.java,v 1.37.2.1 2004/09/01 15:40:13 pnever Exp $
3  * $Revision: 1.37.2.1 $
4  * $Date: 2004/09/01 15:40:13 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.method;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Date JavaDoc;
28
29 import org.apache.slide.common.NamespaceAccessToken;
30 import org.apache.slide.common.ServiceAccessException;
31 import org.apache.slide.common.SlideException;
32 import org.apache.slide.content.NodeProperty;
33 import org.apache.slide.content.NodeRevisionDescriptor;
34 import org.apache.slide.event.EventDispatcher;
35 import org.apache.slide.structure.LinkedObjectNotFoundException;
36 import org.apache.slide.structure.ObjectAlreadyExistsException;
37 import org.apache.slide.structure.ObjectNotFoundException;
38 import org.apache.slide.structure.SubjectNode;
39 import org.apache.slide.util.Configuration;
40 import org.apache.slide.webdav.WebdavException;
41 import org.apache.slide.webdav.WebdavServletConfig;
42 import org.apache.slide.webdav.event.WebdavEvent;
43 import org.apache.slide.webdav.util.DeltavConstants;
44 import org.apache.slide.webdav.util.UriHandler;
45 import org.apache.slide.webdav.util.VersioningHelper;
46 import org.apache.slide.webdav.util.WebdavStatus;
47
48 /**
49  * MKCOL method.
50  *
51  */

52 public class MkcolMethod
53     extends AbstractWebdavMethod
54     implements DeltavConstants, WriteMethod {
55     
56     
57     // ----------------------------------------------------- Instance Variables
58

59     
60     /**
61      * Collection name.
62      */

63     protected String JavaDoc colName;
64     
65     
66     /**
67      * Constructor.
68      *
69      * @param token the token for accessing the namespace
70      * @param config configuration of the WebDAV servlet
71      */

72     public MkcolMethod(NamespaceAccessToken token,
73                        WebdavServletConfig config) {
74         super(token, config);
75     }
76     
77     
78     // ------------------------------------------------------ Protected Methods
79

80     
81     /**
82      * Parse XML request.
83      */

84     protected void parseRequest()
85         throws WebdavException {
86         
87         if (req.getContentLength() > 0) {
88             int statusCode = WebdavStatus.SC_UNSUPPORTED_MEDIA_TYPE;
89             sendError( statusCode, getClass().getName()+".requestBodyMustBeEmpty" );
90             throw new WebdavException( statusCode );
91         }
92         
93         colName = requestUri;
94         if (colName == null) {
95             colName = "/";
96         }
97     }
98     
99     
100     /**
101      * Execute request.
102      *
103      * @exception WebdavException Bad request
104      */

105     protected void executeRequest() throws WebdavException, IOException JavaDoc {
106         
107         // Prevent dirty reads
108
slideToken.setForceStoreEnlistment(true);
109         NodeRevisionDescriptor revisionDescriptor = null;
110         
111         // check lock-null resources
112
boolean isLockNull = false;
113         try {
114             revisionDescriptor =
115                 content.retrieve(slideToken, content.retrieve(slideToken, colName));
116             isLockNull = isLockNull( revisionDescriptor );
117         }
118         catch (ServiceAccessException e) {
119             int statusCode = getErrorCode((Exception JavaDoc)e);
120             sendError( statusCode, e );
121             throw new WebdavException( statusCode );
122         }
123         catch (SlideException e) {
124             // ignore silently
125
}
126         
127         if (revisionDescriptor == null) {
128             revisionDescriptor =
129                 new NodeRevisionDescriptor(0);
130         }
131         
132         // check destination URI
133
UriHandler destinationUriHandler = UriHandler.getUriHandler(colName);
134         if (destinationUriHandler.isRestrictedUri()) {
135             int statusCode = WebdavStatus.SC_FORBIDDEN;
136             sendError( statusCode, getClass().getName()+".restrictedDestinationUri", new Object JavaDoc[]{colName} );
137             throw new WebdavException( statusCode );
138         }
139         
140         // Resource type
141
revisionDescriptor.setResourceType(NodeRevisionDescriptor.COLLECTION_TYPE);
142         
143         // Creation date
144
revisionDescriptor.setCreationDate(new Date JavaDoc());
145         
146         // Last modification date
147
revisionDescriptor.setLastModified(new Date JavaDoc());
148         
149         // Content length name
150
revisionDescriptor.setContentLength(0);
151         
152         // Source
153
revisionDescriptor.setSource("");
154         
155         // Owner
156
try {
157             String JavaDoc creator = ((SubjectNode)security.getPrincipal(slideToken)).getPath().lastSegment();
158             revisionDescriptor.setCreationUser(creator);
159             revisionDescriptor.setOwner(creator);
160         } catch (Exception JavaDoc e) {
161             int statusCode = getErrorCode( e );
162             sendError( statusCode, e );
163             throw new WebdavException( statusCode );
164         }
165         
166         // Added for DeltaV --start--
167
if( Configuration.useVersionControl() ) {
168             VersioningHelper vHelp = VersioningHelper.getVersioningHelper(
169                 slideToken, token, req, resp, getConfig() );
170             
171             // Workspace
172
vHelp.setWorkspaceProperty( colName, revisionDescriptor );
173         }
174         // Added for DeltaV --end--
175

176         if (isMsProprietarySupport()) {
177             
178             NodeProperty property = null;
179             
180             // Name
181
/*
182              property = new NodeProperty("name", colName, "ms", "MICROSOFT");
183              properties.setProperty(property);
184              */

185             
186             // ParentName
187
// TODO : Fix this
188
/*
189              property = new NodeProperty("parentname", colName, "ms",
190              "MICROSOFT");
191              properties.setProperty(property);
192              */

193             
194             // Href
195
/*
196              property = new NodeProperty("href", colName, "ms", "MICROSOFT");
197              properties.setProperty(property);
198              */

199             
200             // Is hidden
201
property = new NodeProperty("ishidden", "0", "MICROSOFT");
202             revisionDescriptor.setProperty(property);
203             
204             // Is collection
205
property = new NodeProperty("iscollection", "1", "MICROSOFT");
206             revisionDescriptor.setProperty(property);
207             
208             // Is read only
209
property = new NodeProperty("isreadonly", "0", "MICROSOFT");
210             revisionDescriptor.setProperty(property);
211             
212             // Last accessed
213
property = new NodeProperty("lastaccessed",
214                                             (new Date JavaDoc()).toString(), "MICROSOFT");
215             revisionDescriptor.setProperty(property);
216             
217         }
218         
219         // If everything is ok : 201 - Created / OK
220
resp.setStatus(WebdavStatus.SC_CREATED);
221         
222         try {
223             if ( WebdavEvent.MKCOL.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.MKCOL, new WebdavEvent(this));
224
225             if (!isLockNull) {
226                 SubjectNode collection = new SubjectNode();
227                 structure.create(slideToken, collection, colName);
228                 content.create(slideToken, colName, revisionDescriptor, null);
229             }
230             else {
231                 content.store(slideToken, colName, revisionDescriptor, null);
232             }
233         } catch (Exception JavaDoc e) {
234             int statusCode = getErrorCode( e );
235             sendError( statusCode, e );
236             throw new WebdavException( statusCode );
237         }
238         
239         // 415 - Unsupported Media Type
240
// TODO : Has something to do with the body of the request ...
241
// WebDAV RFC is vague on the subject.
242

243         // 507 - Insufficient storage
244
// Would be returned as a ServiceAccessException, so it would
245
// return an Internal Server Error, which is probably acceptable.
246

247         // TODO : Initialize and create collection's properties.
248

249     }
250     
251     
252     /**
253      * Get return status based on exception type.
254      */

255     protected int getErrorCode(Exception JavaDoc ex) {
256         try {
257             throw ex;
258         } catch (ObjectNotFoundException e) {
259             return WebdavStatus.SC_CONFLICT;
260         } catch (LinkedObjectNotFoundException e) {
261             return WebdavStatus.SC_NOT_FOUND;
262         } catch (ObjectAlreadyExistsException e) {
263             return WebdavStatus.SC_METHOD_NOT_ALLOWED;
264         } catch (Exception JavaDoc e) {
265             return super.getErrorCode(e);
266         }
267         
268     }
269     
270 }
271
272
Popular Tags