KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/VersionControlMethod.java,v 1.31 2004/08/05 14:43:29 dflorey Exp $
3  * $Revision: 1.31 $
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 import java.util.Iterator JavaDoc;
28
29 import org.apache.slide.common.Domain;
30 import org.apache.slide.common.NamespaceAccessToken;
31 import org.apache.slide.common.ServiceAccessException;
32 import org.apache.slide.event.EventDispatcher;
33 import org.apache.slide.macro.ForbiddenException;
34 import org.apache.slide.webdav.WebdavException;
35 import org.apache.slide.webdav.WebdavServletConfig;
36 import org.apache.slide.webdav.event.WebdavEvent;
37 import org.apache.slide.webdav.util.DeltavConstants;
38 import org.apache.slide.webdav.util.PreconditionViolationException;
39 import org.apache.slide.webdav.util.UriHandler;
40 import org.apache.slide.webdav.util.VersioningHelper;
41 import org.apache.slide.webdav.util.WebdavStatus;
42 import org.jdom.Element;
43 import org.jdom.JDOMException;
44 /**
45  * VERSION-CONTROL method.
46  *
47  */

48 public class VersionControlMethod extends AbstractWebdavMethod
49     implements DeltavConstants, WriteMethod {
50     
51     /**
52      * Resource to be written.
53      */

54     private String JavaDoc resourcePath;
55     
56     /**
57      * The VERSION-CONTROL request can be used to create a new VCR for an existing
58      * version history (see 6.7 of RFC3253)
59      */

60     private String JavaDoc existingVersionPath;
61     
62     
63     // ----------------------------------------------------------- Constructors
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 VersionControlMethod(NamespaceAccessToken token,
73                                 WebdavServletConfig config) {
74         super(token, config);
75     }
76     
77     /**
78      * Parse WebDAV XML query.
79      *
80      * @exception WebdavException
81      */

82     protected void parseRequest() throws WebdavException {
83         // readRequestContent();
84

85         resourcePath = requestUri;
86         if (resourcePath == null) {
87             resourcePath = "/";
88         }
89         
90         if( req.getContentLength() > 0 ) {
91             try{
92                 Element ve = null;
93                 Iterator JavaDoc i = parseRequestContent(E_VERSION_CONTROL).getChildren().iterator();
94                 while( i.hasNext() ) {
95                     Element e = (Element)i.next();
96                     if( e.getName().equals(E_VERSION) ) {
97                         // version element found
98
ve = e;
99                         // get the href element
100
try {
101                             Element hre = (Element)ve.getChildren().get(0);
102                             if( hre == null || !hre.getName().equals(E_HREF) )
103                                 throw new Exception JavaDoc();
104                             existingVersionPath = getSlidePath( hre.getText() );
105                         }
106                         catch( Exception JavaDoc x ) {
107                             Domain.warn( E_VERSION+" element must contain "+E_HREF+" element" );
108                             throw new JDOMException("<"+E_VERSION+"> element must contain <"+E_HREF+"> element" );
109                         }
110                         break;
111                     }
112                 }
113             }
114             catch (JDOMException e){
115                 int statusCode = WebdavStatus.SC_BAD_REQUEST;
116                 sendError( statusCode, e );
117                 throw new WebdavException( statusCode );
118             }
119             catch( IOException JavaDoc e ){
120                 int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
121                 sendError( statusCode, e );
122                 throw new WebdavException( statusCode );
123             }
124         }
125     }
126     
127     /**
128      * Execute the request.
129      *
130      * @exception WebdavException
131      */

132     protected void executeRequest() throws WebdavException, IOException JavaDoc {
133         
134         // Prevent dirty reads
135
slideToken.setForceStoreEnlistment(true);
136         
137         // check lock-null resources
138
try {
139             if (isLockNull(resourcePath)) {
140                 int statusCode = WebdavStatus.SC_NOT_FOUND;
141                 sendError( statusCode, "lock-null resource", new Object JavaDoc[]{resourcePath} );
142                 throw new WebdavException( statusCode );
143             }
144         }
145         catch (ServiceAccessException e) {
146             int statusCode = getErrorCode((Exception JavaDoc)e);
147             sendError( statusCode, e );
148             throw new WebdavException( statusCode );
149         }
150         
151         try {
152             UriHandler rUh = UriHandler.getUriHandler( resourcePath );
153             if (isExcludedForVersionControl(resourcePath)) {
154                 throw new ForbiddenException(
155                     resourcePath,
156                     new Exception JavaDoc("The resource path has been excluded from version-control") );
157             }
158             if ( WebdavEvent.VERSION_CONTROL.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.VERSION_CONTROL, new WebdavEvent(this));
159
160             VersioningHelper vh = VersioningHelper.getVersioningHelper(
161                 slideToken, token, req, resp, getConfig() );
162             if( existingVersionPath == null )
163                 vh.versionControl( resourcePath );
164             else
165                 vh.versionControl( resourcePath, existingVersionPath );
166         }
167         catch (PreconditionViolationException e) {
168             sendPreconditionViolation(e);
169             throw e;
170         }
171         catch (Exception JavaDoc e) {
172             int statusCode = getErrorCode( e );
173             sendError( statusCode, e );
174             throw new WebdavException( statusCode );
175         }
176     }
177 }
178
179
180
181
Popular Tags