KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/RebindMethod.java,v 1.16 2004/08/02 16:36:01 unico Exp $
3  * $Revision: 1.16 $
4  * $Date: 2004/08/02 16:36:01 $
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.List 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.event.EventDispatcher;
33 import org.apache.slide.lock.ObjectLockedException;
34 import org.apache.slide.structure.CrossServerBindingException;
35 import org.apache.slide.structure.ObjectNode;
36 import org.apache.slide.webdav.WebdavException;
37 import org.apache.slide.webdav.WebdavServletConfig;
38 import org.apache.slide.webdav.event.WebdavEvent;
39 import org.apache.slide.webdav.util.BindConstants;
40 import org.apache.slide.webdav.util.PreconditionViolationException;
41 import org.apache.slide.webdav.util.UriHandler;
42 import org.apache.slide.webdav.util.ViolatedPrecondition;
43 import org.apache.slide.webdav.util.WebdavStatus;
44 import org.jdom.JDOMException;
45
46 /**
47  * REBIND method.
48  *
49  */

50 public class RebindMethod extends AbstractWebdavMethod implements BindConstants, WriteMethod {
51
52     private String JavaDoc sourceUri;
53     private String JavaDoc sourceSegment;
54     private String JavaDoc sourceParentUri;
55     private String JavaDoc collectionUri;
56     private String JavaDoc segment;
57     private boolean overwrite;
58     private ObjectNode sourceNode = null;
59     private ObjectNode sourceParentNode = null;
60     private ObjectNode collectionNode = null;
61
62
63     /**
64      * Constructor.
65      *
66      * @param token the token for accessing the namespace
67      * @param config configuration of the WebDAV servlet
68      */

69     public RebindMethod(NamespaceAccessToken token, WebdavServletConfig config) {
70         super(token, config);
71     }
72
73     /**
74      * Parse WebDAV XML query.
75      *
76      * @exception WebdavException
77      */

78     protected void parseRequest() throws WebdavException {
79         collectionUri = requestUri;
80         if (collectionUri == null) {
81             collectionUri = "/";
82         }
83
84         List JavaDoc content;
85
86 // readRequestContent();
87
try{
88             content = parseRequestContent(E_REBIND).getChildren();
89             segment = MethodUtil.getChildText(content, E_SEGMENT);
90             sourceUri = parseUri(MethodUtil.getChildUrl(content, E_HREF));
91         }
92         catch (IOException JavaDoc e) { // TODO: merge exception handling into jdom access methods
93
int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
94             sendError( statusCode, e );
95             throw new WebdavException( statusCode );
96         }
97         catch (JDOMException e) { // TODO: merge exception handling into jdom access methods
98
int statusCode = WebdavStatus.SC_BAD_REQUEST;
99             sendError( statusCode, e );
100             throw new WebdavException( statusCode );
101         }
102
103         collectionUri = requestUri;
104         if (collectionUri == null) {
105             collectionUri = "/";
106         }
107
108         overwrite = requestHeaders.getOverwrite(true);
109     }
110
111     private void checkPreconditions() throws PreconditionViolationException, ServiceAccessException {
112         resp.setStatus( WebdavStatus.SC_CREATED );
113         UriHandler sourceUh = UriHandler.getUriHandler(sourceUri);
114         UriHandler sourceParentUh = sourceUh.getParentUriHandler();
115         sourceSegment = sourceUh.getName();
116         if (sourceParentUh != null) {
117             sourceParentUri = sourceUh.getParentUriHandler().toString();
118         }
119
120         try {
121             collectionNode = structure.retrieve( slideToken, collectionUri );
122         }
123         catch( ServiceAccessException e ) {
124             throw e;
125         }
126         catch (SlideException e) {} // ignore silently
127

128         try {
129             sourceNode = structure.retrieve( slideToken, sourceUri );
130         }
131         catch( ServiceAccessException e ) {
132             throw e;
133         }
134         catch (SlideException e) {} // ignore silently
135

136         try {
137             sourceParentNode = structure.retrieve( slideToken, sourceParentUri );
138         }
139         catch( ServiceAccessException e ) {
140             throw e;
141         }
142         catch (SlideException e) {} // ignore silently
143

144         if (collectionNode == null || !isCollection(collectionUri)) {
145             throw new PreconditionViolationException(
146                 new ViolatedPrecondition(C_REBIND_INTO_COLLECTION, WebdavStatus.SC_CONFLICT), collectionUri);
147         }
148         if (!MethodUtil.isValidSegment(segment)) {
149             throw new PreconditionViolationException(
150                 new ViolatedPrecondition(C_NAME_ALLOWED, WebdavStatus.SC_FORBIDDEN), segment);
151         }
152         if (sourceNode == null) {
153             throw new PreconditionViolationException(
154                 new ViolatedPrecondition(C_REBIND_SOURCE_EXISTS, WebdavStatus.SC_CONFLICT), sourceUri);
155         }
156         if (collectionNode.hasBinding(segment)) {
157             if (overwrite) {
158                 resp.setStatus( WebdavStatus.SC_NO_CONTENT );
159             }
160             else {
161                 throw new PreconditionViolationException(
162                     new ViolatedPrecondition(C_CAN_OVERWRITE, WebdavStatus.SC_FORBIDDEN), segment);
163             }
164         }
165         if (isCollection(sourceUri)) {
166             if (isDescendant(collectionNode, sourceNode)) {
167                 throw new PreconditionViolationException(
168                     new ViolatedPrecondition(C_CYCLE_ALLOWED, WebdavStatus.SC_FORBIDDEN), sourceUri);
169             }
170         }
171     }
172
173     /**
174      * Execute the request.
175      *
176      * @exception WebdavException
177      */

178     protected void executeRequest() throws WebdavException, IOException JavaDoc {
179
180         // Prevent dirty reads
181
slideToken.setForceStoreEnlistment(true);
182
183         // check lock-null resources
184
try {
185             if (isLockNull(collectionUri)) {
186                 int statusCode = WebdavStatus.SC_NOT_FOUND;
187                 sendError( statusCode, "lock-null resource", new Object JavaDoc[]{collectionUri} );
188                 throw new WebdavException( statusCode );
189             }
190         }
191         catch (ServiceAccessException e) {
192             int statusCode = getErrorCode((Exception JavaDoc)e);
193             sendError( statusCode, e );
194             throw new WebdavException( statusCode );
195         }
196
197         try {
198             if ( WebdavEvent.REBIND.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.REBIND, new WebdavEvent(this));
199
200             checkPreconditions();
201             structure.addBinding( slideToken, collectionNode, segment, sourceNode );
202             structure.removeBinding( slideToken, sourceParentNode, sourceSegment );
203         }
204         catch (CrossServerBindingException e) {
205             sendPreconditionViolation(
206                 new PreconditionViolationException(
207                                          new ViolatedPrecondition(C_CROSS_SERVER_BINDING, WebdavStatus.SC_FORBIDDEN),
208                                          collectionNode.getUri()
209                                      )
210             );
211         }
212         catch (ObjectLockedException e) {
213             ViolatedPrecondition violatedPrecondition;
214             if (collectionUri.equals(e.getObjectUri())) {
215                 violatedPrecondition =
216                     new ViolatedPrecondition(C_LOCKED_UPDATE_ALLOWED, WebdavStatus.SC_LOCKED);
217             }
218             else if (sourceParentUri.equals(e.getObjectUri())) {
219                 violatedPrecondition =
220                     new ViolatedPrecondition(C_LOCKED_SOURCE_COLLECTION_UPDATE_ALLOWED, WebdavStatus.SC_CONFLICT);
221             }
222             else if (sourceUri.equals(e.getObjectUri())) {
223                 violatedPrecondition =
224                     new ViolatedPrecondition(C_PROTECTED_SOURCE_URL_DELETION_ALLOWED, WebdavStatus.SC_CONFLICT);
225             }
226             else {
227                 violatedPrecondition =
228                     new ViolatedPrecondition(C_PROTECTED_URL_MODIFICATION_ALLOWED, WebdavStatus.SC_CONFLICT);
229             }
230             sendPreconditionViolation(
231                 new PreconditionViolationException(violatedPrecondition, collectionNode.getUri())
232             );
233         }
234         catch (PreconditionViolationException e) {
235             sendPreconditionViolation(e);
236             throw e;
237         }
238         catch (Exception JavaDoc e) {
239             int statusCode = getErrorCode( e );
240             sendError( statusCode, e );
241             throw new WebdavException( statusCode );
242         }
243     }
244
245 }
246
247
Popular Tags