KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/CheckoutMethod.java,v 1.33 2004/08/05 14:43:29 dflorey Exp $
3  * $Revision: 1.33 $
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 package org.apache.slide.webdav.method;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Iterator 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.event.EventDispatcher;
32 import org.apache.slide.util.Configuration;
33 import org.apache.slide.webdav.WebdavException;
34 import org.apache.slide.webdav.WebdavServletConfig;
35 import org.apache.slide.webdav.event.WebdavEvent;
36 import org.apache.slide.webdav.util.DeltavConstants;
37 import org.apache.slide.webdav.util.LabeledRevisionNotFoundException;
38 import org.apache.slide.webdav.util.PreconditionViolationException;
39 import org.apache.slide.webdav.util.VersioningHelper;
40 import org.apache.slide.webdav.util.ViolatedPrecondition;
41 import org.apache.slide.webdav.util.WebdavStatus;
42 import org.apache.slide.webdav.util.WebdavUtils;
43 import org.jdom.Element;
44 import org.jdom.JDOMException;
45
46 /**
47  * CHECKOUT method.
48  *
49  */

50 public class CheckoutMethod extends AbstractWebdavMethod implements DeltavConstants, WriteMethod {
51
52     /** Resource to be written. */
53     private String JavaDoc resourcePath;
54
55     /** Marshalling variables */
56     private boolean applyToVersion = false;
57     private boolean forkOk = false;
58
59     /**
60      * The VersioningHelper used by this instance.
61      */

62     protected VersioningHelper versioningHelper = null;
63
64
65     // ----------------------------------------------------------- Constructors
66

67
68     /**
69      * Constructor.
70      *
71      * @param token the token for accessing the namespace
72      * @param config configuration of the WebDAV servlet
73      */

74     public CheckoutMethod(NamespaceAccessToken token,
75                           WebdavServletConfig config) {
76         super(token, config);
77     }
78
79     /**
80      * Parse WebDAV XML query.
81      *
82      * @throws WebdavException
83      */

84     protected void parseRequest() throws WebdavException {
85
86         resourcePath = requestUri;
87         if (resourcePath == null) {
88             resourcePath = "/";
89         }
90
91         versioningHelper =
92             VersioningHelper.getVersioningHelper(slideToken, token, req, resp,
93                                                  config);
94
95         // evaluate "Label" header
96
if (Configuration.useVersionControl()) {
97             try {
98
99                 String JavaDoc labelHeader = WebdavUtils.fixTomcatHeader(requestHeaders.getLabel(), "UTF-8");
100
101                 applyToVersion = (labelHeader != null);
102                 resourcePath = versioningHelper.getLabeledResourceUri(resourcePath,
103                                                                       labelHeader);
104             }
105             catch (LabeledRevisionNotFoundException e) {
106                 ViolatedPrecondition violatedPrecondition =
107                     new ViolatedPrecondition(DeltavConstants.C_MUST_SELECT_VERSION_IN_HISTORY,
108                                              WebdavStatus.SC_CONFLICT);
109                 try {
110                     sendPreconditionViolation(new PreconditionViolationException(violatedPrecondition,
111                                                                                  resourcePath));
112                 } catch (IOException JavaDoc ioe) {}
113                 throw new WebdavException(getErrorCode((Exception JavaDoc)e));
114             }
115             catch (SlideException e) {
116                 int statusCode = getErrorCode( e );
117                 sendError( statusCode, e );
118                 throw new WebdavException( statusCode );
119             }
120         }
121
122
123         if( req.getContentLength() > 0 ) {
124 // readRequestContent();
125
try{
126                 Iterator JavaDoc i = parseRequestContent(E_CHECKOUT).getChildren().iterator();
127                 while( i.hasNext() ) {
128                     Element e = (Element)i.next();
129                     if ( e.getName().equals(E_APPLY_TO_VERSION) ) {
130
131                         String JavaDoc labelHeader = WebdavUtils.fixTomcatHeader(requestHeaders.getLabel(), "UTF-8");
132
133                         if (labelHeader != null) {
134                             ViolatedPrecondition violatedPrecondition =
135                                 new ViolatedPrecondition(DeltavConstants.C_MUST_NOT_HAVE_LABEL_AND_APPLY_TO_VERSION,
136                                                          WebdavStatus.SC_CONFLICT);
137                             sendPreconditionViolation(new PreconditionViolationException(violatedPrecondition,
138                                                                                          resourcePath));
139                             throw new WebdavException(WebdavStatus.SC_CONFLICT);
140                         }
141                         applyToVersion = true;
142                     }
143                     if ( e.getName().equals(E_FORK_OK) ) {
144                         forkOk = true;
145                     }
146                 }
147             }
148             catch (IOException JavaDoc e){
149                 int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
150                 sendError( statusCode, e );
151                 throw new WebdavException( statusCode );
152             }
153             catch (JDOMException e){
154                 int statusCode = WebdavStatus.SC_BAD_REQUEST;
155                 sendError( statusCode, e );
156                 throw new WebdavException( statusCode );
157             }
158         }
159     }
160
161     /**
162      * Execute the request.
163      *
164      * @throws WebdavException
165      * @throws IOException
166      */

167     protected void executeRequest() throws WebdavException, IOException JavaDoc {
168         String JavaDoc locationValue = null;
169
170         // Prevent dirty reads
171
slideToken.setForceStoreEnlistment(true);
172         
173         // check lock-null resources
174
try {
175             if (isLockNull(resourcePath)) {
176                 int statusCode = WebdavStatus.SC_NOT_FOUND;
177                 sendError( statusCode, "lock-null resource", new Object JavaDoc[]{resourcePath} );
178                 throw new WebdavException( statusCode );
179             }
180         }
181         catch (ServiceAccessException e) {
182             int statusCode = getErrorCode( e );
183             sendError( statusCode, e );
184             throw new WebdavException( statusCode );
185         }
186
187         try {
188             if ( WebdavEvent.CHECKOUT.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.CHECKOUT, new WebdavEvent(this));
189
190             locationValue = versioningHelper.checkout( resourcePath, forkOk, applyToVersion );
191         }
192         catch (PreconditionViolationException e) {
193             sendPreconditionViolation(e);
194             throw e;
195         }
196         catch (Exception JavaDoc e) {
197             int statusCode = getErrorCode( e );
198             sendError( statusCode, e );
199             throw new WebdavException( statusCode );
200         }
201         finally {
202             resp.setHeader(H_CACHE_CONTROL, NO_CACHE);
203             if( locationValue != null && locationValue.length() > 0 ) {
204                 locationValue = getFullPath( locationValue );
205                 // the location header is "utf-8" expected
206
resp.setHeader( H_LOCATION, WebdavUtils.encodeURL(locationValue, "utf-8") );
207             }
208         }
209
210     }
211
212
213 }
214
215
216
217
Popular Tags