KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/BindMethod.java,v 1.18 2004/08/02 16:36:02 unico Exp $
3  * $Revision: 1.18 $
4  * $Date: 2004/08/02 16:36:02 $
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.ViolatedPrecondition;
42 import org.apache.slide.webdav.util.WebdavStatus;
43 import org.jdom.JDOMException;
44
45
46 /**
47  * BIND method.
48  *
49  */

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

66     public BindMethod(NamespaceAccessToken token, WebdavServletConfig config) {
67         super(token, config);
68     }
69
70     /**
71      * Parse WebDAV XML query.
72      *
73      * @exception WebdavException
74      */

75     protected void parseRequest() throws WebdavException {
76         List JavaDoc content;
77
78 // readRequestContent();
79
try{
80             content = parseRequestContent(E_BIND).getChildren();
81             segment = MethodUtil.getChildText(content, E_SEGMENT);
82             sourceUri = parseUri(MethodUtil.getChildUrl(content, E_HREF));
83         }
84         catch (IOException JavaDoc e) { // TODO: merge exception handling into jdom access methods
85
int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
86             sendError( statusCode, e );
87             throw new WebdavException( statusCode );
88         }
89         catch (JDOMException e) { // TODO: merge exception handling into jdom access methods
90
int statusCode = WebdavStatus.SC_BAD_REQUEST;
91             sendError( statusCode, e );
92             throw new WebdavException( statusCode );
93         }
94
95         collectionUri = requestUri;
96         if (collectionUri == null) {
97             collectionUri = "/";
98         }
99
100         overwrite = requestHeaders.getOverwrite(true);
101     }
102
103     /**
104      * Method checkPreconditions
105      *
106      * @throws PreconditionViolationException
107      *
108      */

109     private void checkPreconditions() throws PreconditionViolationException, ServiceAccessException {
110         resp.setStatus( WebdavStatus.SC_CREATED );
111
112         try {
113             collectionNode = structure.retrieve( slideToken, collectionUri );
114         }
115         catch( ServiceAccessException e ) {
116             throw e;
117         }
118         catch (SlideException e) {} // ignore silently
119

120         try {
121             sourceNode = structure.retrieve( slideToken, sourceUri );
122         }
123         catch( ServiceAccessException e ) {
124             throw e;
125         }
126         catch (Exception JavaDoc e) {} // ignore silently
127

128         if (collectionNode == null || !isCollection(collectionUri)) {
129             throw new PreconditionViolationException(
130                 new ViolatedPrecondition(C_BIND_INTO_COLLECTION, WebdavStatus.SC_CONFLICT), collectionUri);
131         }
132         if (!MethodUtil.isValidSegment(segment)) {
133             throw new PreconditionViolationException(
134                 new ViolatedPrecondition(C_NAME_ALLOWED, WebdavStatus.SC_FORBIDDEN), segment);
135         }
136         if (sourceNode == null) {
137             throw new PreconditionViolationException(
138                 new ViolatedPrecondition(C_BIND_SOURCE_EXISTS, WebdavStatus.SC_CONFLICT), sourceUri);
139         }
140         if (collectionNode.hasBinding(segment)) {
141             if (overwrite) {
142                 resp.setStatus( WebdavStatus.SC_NO_CONTENT );
143             }
144             else {
145                 throw new PreconditionViolationException(
146                     new ViolatedPrecondition(C_CAN_OVERWRITE, WebdavStatus.SC_FORBIDDEN), segment);
147             }
148         }
149         if (isCollection(sourceUri)) {
150             if (isDescendant(collectionNode, sourceNode)) {
151                 throw new PreconditionViolationException(
152                     new ViolatedPrecondition(C_CYCLE_ALLOWED, WebdavStatus.SC_FORBIDDEN), sourceUri);
153             }
154         }
155     }
156
157     /**
158      * Execute the request.
159      *
160      * @exception WebdavException
161      */

162     protected void executeRequest() throws WebdavException, IOException JavaDoc {
163
164         // Prevent dirty reads
165
slideToken.setForceStoreEnlistment(true);
166
167         // check lock-null resources
168
try {
169             if (isLockNull(collectionUri)) {
170                 int statusCode = WebdavStatus.SC_NOT_FOUND;
171                 sendError( statusCode, "lock-null resource", new Object JavaDoc[]{collectionUri} );
172                 throw new WebdavException( statusCode );
173             }
174         }
175         catch (ServiceAccessException e) {
176             int statusCode = getErrorCode( e );
177             sendError( statusCode, e );
178             throw new WebdavException( statusCode );
179         }
180
181         try {
182             checkPreconditions();
183
184             if ( WebdavEvent.BIND.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.BIND, new WebdavEvent(this));
185
186             structure.addBinding( slideToken, collectionNode, segment, sourceNode );
187         }
188         catch (ObjectLockedException e) {
189             ViolatedPrecondition violatedPrecondition;
190             if (collectionUri.equals(e.getObjectUri())) {
191                 violatedPrecondition =
192                     new ViolatedPrecondition(C_LOCKED_UPDATE_ALLOWED, WebdavStatus.SC_LOCKED);
193             }
194             else {
195                 violatedPrecondition =
196                     new ViolatedPrecondition(C_LOCKED_OVERWRITE_ALLOWED, WebdavStatus.SC_CONFLICT);
197             }
198             sendPreconditionViolation(
199                 new PreconditionViolationException(violatedPrecondition, collectionNode.getUri())
200             );
201         }
202         catch (CrossServerBindingException e) {
203             sendPreconditionViolation(
204                 new PreconditionViolationException(
205                                          new ViolatedPrecondition(C_CROSS_SERVER_BINDING, WebdavStatus.SC_FORBIDDEN),
206                                          collectionNode.getUri()
207                                      )
208             );
209         }
210         catch (PreconditionViolationException e) {
211             sendPreconditionViolation(e);
212             throw e;
213         }
214         catch (Exception JavaDoc e) {
215             int statusCode = getErrorCode( e );
216             sendError( statusCode, e );
217             throw new WebdavException( statusCode );
218         }
219     }
220
221 }
222
223
Popular Tags