KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > method > report > VersionTreeReport


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/report/VersionTreeReport.java,v 1.8.2.1 2004/11/15 09:29:33 luetzkendorf Exp $
3  * $Revision: 1.8.2.1 $
4  * $Date: 2004/11/15 09:29:33 $
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.report;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.slide.common.NamespaceAccessToken;
32 import org.apache.slide.common.PropertyParseException;
33 import org.apache.slide.common.RequestedProperties;
34 import org.apache.slide.common.RequestedPropertiesImpl;
35 import org.apache.slide.common.SlideException;
36 import org.apache.slide.common.SlideToken;
37 import org.apache.slide.content.NodeProperty;
38 import org.apache.slide.content.NodeRevisionDescriptor;
39 import org.apache.slide.content.NodeRevisionDescriptors;
40 import org.apache.slide.content.NodeRevisionNumber;
41 import org.apache.slide.structure.ObjectNode;
42 import org.apache.slide.util.Configuration;
43 import org.apache.slide.util.XMLValue;
44 import org.apache.slide.webdav.WebdavServletConfig;
45 import org.apache.slide.webdav.util.DeltavConstants;
46 import org.apache.slide.webdav.util.PreconditionViolationException;
47 import org.apache.slide.webdav.util.UriHandler;
48 import org.apache.slide.webdav.util.VersioningHelper;
49 import org.apache.slide.webdav.util.ViolatedPrecondition;
50 import org.apache.slide.webdav.util.WebdavStatus;
51 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind;
52 import org.apache.slide.webdav.util.resourcekind.ResourceKind;
53 import org.apache.slide.webdav.util.resourcekind.Version;
54 import org.apache.slide.webdav.util.resourcekind.VersionControlled;
55 import org.apache.slide.webdav.util.resourcekind.VersionHistory;
56 import org.jdom.Element;
57 import org.jdom.JDOMException;
58
59 /**
60  * DAV:version-tree report worker.
61  *
62  */

63 public class VersionTreeReport extends AbstractReport implements DeltavConstants {
64     
65     private RequestedProperties requestedProperties = null;
66     private VersioningHelper versioningHelper = null;
67     
68     /**
69      * Constructor
70      *
71      * @param slideToken a SlideToken
72      * @param token a NamespaceAccessToken
73      * @param config a WebdavServletConfig
74      * @param servletPath a String, the result of HttpRequest.getServletPath()
75      * @param contextPath a String , the result of HttpRequest.getContextPath()
76      */

77     public VersionTreeReport(SlideToken slideToken, NamespaceAccessToken token, WebdavServletConfig config, String JavaDoc slideContextPath) {
78         super(slideToken, token, config, slideContextPath);
79     }
80     
81     /**
82      * Initialize report worker with specified report element
83      *
84      * @param resourcePath a String
85      * @param versionTreeElm an Element
86      *
87      * @throws PreconditionViolationException
88      */

89     public void init(String JavaDoc resourcePath, Element versionTreeElm) throws PreconditionViolationException {
90         List JavaDoc childrenList = versionTreeElm.getChildren(E_PROP, DNSP);
91         if (childrenList.size() == 0) {
92             return;
93         }
94         else if (childrenList.size() > 1) {
95             throw new PreconditionViolationException(
96                 new ViolatedPrecondition("at-most-one-prop-element",
97                                          WebdavStatus.SC_BAD_REQUEST,
98                                          "the DAV:version-tree element must contain at most one DAV:prop element"),
99                 resourcePath
100             );
101         }
102         
103         Element propElm = (Element)childrenList.get(0);
104         try {
105             requestedProperties = new RequestedPropertiesImpl(propElm);
106         }
107         catch (PropertyParseException e) {
108             throw new PreconditionViolationException(
109                 new ViolatedPrecondition("invalid-prop-element",
110                                          WebdavStatus.SC_BAD_REQUEST,
111                                          e.getMessage()),
112                 resourcePath
113             );
114         }
115     }
116     
117     /**
118      * Execute report and add results to given multistatus element
119      *
120      * @param resourcePath a String
121      * @param multistatusElm an Element
122      * @param depth an int
123      *
124      * @throws SlideException
125      * @throws IOException
126      */

127     public void execute(String JavaDoc resourcePath, Element multistatusElm, int depth) throws SlideException, IOException JavaDoc {
128         if (depth < 0) {
129             return;
130         }
131         writeReport(resourcePath, multistatusElm);
132         ObjectNode onode = structure.retrieve(slideToken, resourcePath);
133         Enumeration JavaDoc childrenEnum = structure.getChildren(slideToken, onode);
134         while (childrenEnum.hasMoreElements()) {
135             ObjectNode cnode = (ObjectNode)childrenEnum.nextElement();
136             execute(cnode.getUri(), multistatusElm, depth-1);
137         }
138     }
139     
140     public void setVersioningHelper(VersioningHelper versioningHelper) {
141         this.versioningHelper = versioningHelper;
142     }
143     
144     private void writeReport(String JavaDoc resourcePath, Element multistatusElm) throws SlideException {
145         NodeRevisionDescriptors nrds = content.retrieve(slideToken, resourcePath);
146         NodeRevisionDescriptor nrd = content.retrieve(slideToken, nrds);
147         ResourceKind resourceKind =
148             AbstractResourceKind.determineResourceKind(token, nrds, nrd);
149         
150         if (resourceKind instanceof VersionControlled) {
151             resourcePath = versioningHelper.getUriOfAssociatedVR(resourcePath);
152             nrds = content.retrieve(slideToken, resourcePath);
153             nrd = content.retrieve(slideToken, nrds);
154             resourceKind =
155                 AbstractResourceKind.determineResourceKind(token, nrds, nrd);
156         }
157         else if (resourceKind instanceof VersionHistory) {
158             NodeProperty rootVersion = nrd.getProperty(P_ROOT_VERSION);
159             if (rootVersion != null) {
160                 XMLValue value;
161                 if (rootVersion.getValue() instanceof XMLValue) {
162                     value = (XMLValue)rootVersion.getValue();
163                 } else {
164                     try {
165                         value = new XMLValue(rootVersion.getValue().toString());
166                     } catch (JDOMException e) {
167                         throw new SlideException("Could not parse DAV:root-version: "+nrds.getUri());
168                     }
169                 }
170                 Iterator JavaDoc i = value.iterator();
171                 if (i.hasNext()) {
172                     resourcePath = ((Element)i.next()).getText();
173                 }
174             }
175             resourcePath += "/";
176             resourcePath += nrds.getInitialRevision();
177             nrds = content.retrieve(slideToken, resourcePath);
178             nrd = content.retrieve(slideToken, nrds);
179             resourceKind =
180                     AbstractResourceKind.determineResourceKind(token, nrds, nrd);
181         }
182         
183         if ( Configuration.useVersionControl() && (resourceKind instanceof Version) ) {
184             writeReport(nrds, slideToken, multistatusElm);
185         }
186         else {
187             multistatusElm.addContent(getErrorResponse(resourcePath, WebdavStatus.SC_FORBIDDEN, C_SUPPORTED_REPORT));
188         }
189     }
190     
191     private void writeReport(NodeRevisionDescriptors nrds, SlideToken slideToken, Element multistatusElm) throws SlideException {
192         // this is a versioned resource
193
NodeRevisionDescriptor hNrd = content.retrieve(slideToken, nrds, NodeRevisionNumber.HIDDEN_0_0);
194         NodeProperty versionSetProperty = hNrd.getProperty(P_VERSION_SET);
195         if ( (versionSetProperty != null) && (versionSetProperty.getValue() != null) ) {
196             XMLValue xmlValue;
197             try {
198                 xmlValue = new XMLValue(versionSetProperty.getValue().toString());
199             }
200             catch (JDOMException e) {
201                 throw new SlideException("Could not parse DAV:version-set: "+nrds.getUri());
202             }
203             Iterator JavaDoc hrefIterator = xmlValue.iterator();
204             
205             NodeRevisionNumber currentNrn= null;
206             UriHandler currentUh = null;
207             String JavaDoc currentPath = null;
208             
209             while (hrefIterator.hasNext()) {
210                 currentPath = ((Element)hrefIterator.next()).getText();
211                 currentUh = UriHandler.getUriHandler(currentPath);
212                 currentNrn = new NodeRevisionNumber(currentUh.getVersionName());
213                 multistatusElm.addContent(getResponseElement(slideToken, currentPath, currentNrn, getRequestedVersionTreeProperties()));
214             }
215         }
216     }
217     
218     private RequestedProperties getRequestedVersionTreeProperties() {
219         if (requestedProperties == null) {
220             requestedProperties = new RequestedPropertiesImpl();
221         }
222         return requestedProperties;
223     }
224 }
225
226
Popular Tags