KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/DeleteMethod.java,v 1.37 2004/08/05 14:43:29 dflorey Exp $
3  * $Revision: 1.37 $
4  * $Date: 2004/08/05 14:43:29 $
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
28 import org.apache.slide.common.NamespaceAccessToken;
29 import org.apache.slide.common.ServiceAccessException;
30 import org.apache.slide.common.SlideException;
31 import org.apache.slide.content.NodeRevisionDescriptor;
32 import org.apache.slide.content.NodeRevisionDescriptors;
33 import org.apache.slide.content.RevisionDescriptorNotFoundException;
34 import org.apache.slide.event.EventDispatcher;
35 import org.apache.slide.macro.DeleteListener;
36 import org.apache.slide.macro.DeleteMacroException;
37 import org.apache.slide.structure.ObjectNotFoundException;
38 import org.apache.slide.util.Configuration;
39 import org.apache.slide.webdav.WebdavException;
40 import org.apache.slide.webdav.WebdavServletConfig;
41 import org.apache.slide.webdav.event.WebdavEvent;
42 import org.apache.slide.webdav.util.DeltavConstants;
43 import org.apache.slide.webdav.util.PreconditionViolationException;
44 import org.apache.slide.webdav.util.PropertyHelper;
45 import org.apache.slide.webdav.util.UriHandler;
46 import org.apache.slide.webdav.util.VersioningHelper;
47 import org.apache.slide.webdav.util.ViolatedPrecondition;
48 import org.apache.slide.webdav.util.WebdavStatus;
49 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind;
50 import org.apache.slide.webdav.util.resourcekind.CheckedOutVersionControlled;
51 import org.apache.slide.webdav.util.resourcekind.ResourceKind;
52 import org.apache.slide.webdav.util.resourcekind.Working;
53 import org.jdom.JDOMException;
54
55 /**
56  * DELETE method.
57  *
58  */

59 public class DeleteMethod extends AbstractMultistatusResponseMethod implements DeltavConstants, DeleteListener, WriteMethod {
60     
61     
62     // ----------------------------------------------------- Instance Variables
63

64     
65     /**
66      * Id of the resosurce or collection which is to be deleted.
67      */

68     private String JavaDoc toDelete;
69     
70     /**
71      * The NodeRevisionDescriptors of the history of the deleted version.
72      */

73     protected NodeRevisionDescriptors historyNrds = null;
74     
75     /**
76      * Indicates if the resource to delete is a collection.
77      */

78     protected boolean isCollection = false;
79     
80     /**
81      * The UriHandler used by method {@link #beforeDelete beforeDelete()} and
82      * {@link #afterDelete afterDelete()}.
83      */

84     protected UriHandler uriHandler = null;
85     
86     /**
87      * The VersioningHelper used by this instance.
88      */

89     protected VersioningHelper versioningHelper = null;
90     
91     
92     // ----------------------------------------------------------- Constructors
93

94     
95     /**
96      * Constructor.
97      *
98      * @param token the token for accessing the namespace
99      * @param config configuration of the WebDAV servlet
100      */

101     public DeleteMethod(NamespaceAccessToken token,
102                         WebdavServletConfig config) {
103         super(token, config);
104     }
105     
106     
107     // ------------------------------------------------------ Protected Methods
108

109     
110     /**
111      * Parse request.
112      *
113      * @exception WebdavException Does not happen
114      */

115     protected void parseRequest()
116         throws WebdavException {
117         versioningHelper =
118             VersioningHelper.getVersioningHelper(slideToken, token, req, resp,
119                                                  config);
120         toDelete = requestUri;
121         if (toDelete == null) {
122             toDelete = "/";
123         }
124     }
125     
126     
127     /**
128      * Execute request.
129      *
130      * @exception WebdavException Unrecoverable error while deleting
131      */

132     protected void executeRequest()
133         throws WebdavException {
134         
135         // Prevent dirty reads
136
slideToken.setForceStoreEnlistment(true);
137         
138         // check lock-null resources
139
try {
140             if (isLockNull(toDelete)) {
141                 int statusCode = WebdavStatus.SC_NOT_FOUND;
142                 sendError( statusCode, "lock-null resource", new Object JavaDoc[]{toDelete} );
143                 throw new WebdavException( statusCode );
144             }
145         }
146         catch (ServiceAccessException e) {
147             int statusCode = getErrorCode( e );
148             sendError( statusCode, e );
149             throw new WebdavException( statusCode );
150         }
151         
152         isCollection = isCollection(toDelete);
153         try {
154             if ( WebdavEvent.DELETE.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.DELETE, new WebdavEvent(this));
155
156             macro.delete(slideToken, toDelete, null, this);
157             resp.setStatus(WebdavStatus.SC_NO_CONTENT);
158         } catch (DeleteMacroException dme) {
159             // If it's not a collection, we don't want to give a 207,
160
// because it's silly, and it confuses many clients (such as
161
// MS Web Folders).
162
if (generateMultiStatusResponse(isCollection, dme, requestUri)) {
163                 String JavaDoc errorMessage = generateErrorMessage(dme);
164                 // Write it on the servlet writer
165
resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
166                 try {
167                     resp.setContentType(TEXT_XML_UTF_8);
168                     resp.getWriter().write(errorMessage);
169                 } catch(IOException JavaDoc ex) {
170                     // Critical error ... Servlet container is dead or something
171
int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
172                     sendError( statusCode, ex );
173                     throw new WebdavException( statusCode );
174                 }
175             } else {
176                 // Returning 207 on non-collection requests is generally
177
// considered bad. So let's not do it, since this way
178
// makes clients generally behave better.
179
SlideException exception = (SlideException)dme.enumerateExceptions().nextElement();
180                 if (exception instanceof PreconditionViolationException) {
181                     try {
182                         sendPreconditionViolation((PreconditionViolationException)exception);
183                     } catch(IOException JavaDoc ex) {
184                         // Critical error ... Servlet container is dead or something
185
int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
186                         sendError( statusCode, ex );
187                         throw new WebdavException( statusCode );
188                     }
189                 }
190                 else {
191                     int statusCode = getErrorCode( exception );
192                     sendError( statusCode, exception );
193                     throw new WebdavException( statusCode );
194                 }
195             }
196             //
197
// make sure the transaction is aborted
198
// throw any WebDAV exception to indicate the transaction wants to be aborted
199
//
200
throw new WebdavException(WebdavStatus.SC_ACCEPTED, false);
201         } catch( SlideException x ) {
202             int statusCode = getErrorCode((SlideException)x);
203             sendError( statusCode, x );
204             throw new WebdavException( statusCode );
205         }
206     }
207     
208     // ------------------------------------------------------ Interface DeleteListener
209

210     /**
211      * This method is called prior to deleting the resource associated by
212      * the given <code>targetUri</code>. The deletion can be prohibited by
213      * throwing a SlideException.
214      *
215      * @param targetUri the Uri of the resource that will be deleted.
216      *
217      * @throws SlideException this Exception will be passed to the caller
218      * of the Macro helper (contained in the
219      * MacroDeleteException.
220      */

221     public void beforeDelete(String JavaDoc targetUri) throws SlideException {
222         
223         if(Configuration.useVersionControl()) {
224             
225             uriHandler = UriHandler.getUriHandler(targetUri);
226             if (uriHandler.isVersionUri() && !isCollection) {
227                 // delete of version is only allowed if the history collection is deleted
228
throw new PreconditionViolationException(new ViolatedPrecondition(C_NO_VERSION_DELETE,
229                                                                                   WebdavStatus.SC_FORBIDDEN),
230                                                          targetUri);
231             }
232             
233             // if resource being removed is a checked-out VCR or a WR,
234
// remove its URI in the DAV:checkout-set property of the VR
235
NodeRevisionDescriptors nrds = content.retrieve(slideToken, targetUri);
236             NodeRevisionDescriptor nrd = content.retrieve(slideToken, nrds);
237             ResourceKind rk = AbstractResourceKind.determineResourceKind(token, nrds, nrd);
238             if(rk instanceof CheckedOutVersionControlled || rk instanceof Working) {
239                 String JavaDoc vrUri =
240                     VersioningHelper.getUriOfAssociatedVR(token, slideToken, content, targetUri);
241                 try {
242                     NodeRevisionDescriptors vrNrds = content.retrieve(slideToken, vrUri);
243                     NodeRevisionDescriptor vrNrd = content.retrieve(slideToken, vrNrds);
244                     PropertyHelper.removeHrefFromProperty(vrNrd, P_CHECKOUT_SET, targetUri);
245                     content.store(slideToken, vrNrds.getUri(), vrNrd, null);
246                 }
247                 catch( ObjectNotFoundException e ) {
248                     // Nothing to do if the VR is gone, e.g. because the VHR was deleted.
249
}
250                 catch( RevisionDescriptorNotFoundException e ) {
251                     // Nothing to do if the VR is gone, e.g. because the VHR was deleted.
252
}
253                 catch( JDOMException e ) {
254                     throw new SlideException(
255                         "Unable to update DAV:checkout-set of "+vrUri+": "+e.getMessage() );
256                 }
257             }
258         }
259     }
260     
261     
262     /**
263      * This method is called after deleting the resource associated by
264      * the given <code>targetUri</code>.
265      *
266      * @param targetUri the Uri of the resource that will be deleted.
267      *
268      * @throws SlideException this Exception will be passed to the caller
269      * of the Macro helper (contained in the
270      * MacroDeleteException.
271      */

272     public void afterDelete(String JavaDoc targetUri) throws SlideException {
273     }
274     
275     
276     
277 }
278
279
Popular Tags