KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/report/LocateByHistoryReport.java,v 1.8 2004/08/05 14:43:34 dflorey Exp $
3  * $Revision: 1.8 $
4  * $Date: 2004/08/05 14:43:34 $
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.structure.ObjectNode;
41 import org.apache.slide.util.XMLValue;
42 import org.apache.slide.webdav.WebdavServletConfig;
43 import org.apache.slide.webdav.util.DeltavConstants;
44 import org.apache.slide.webdav.util.PreconditionViolationException;
45 import org.apache.slide.webdav.util.UriHandler;
46 import org.apache.slide.webdav.util.ViolatedPrecondition;
47 import org.apache.slide.webdav.util.WebdavStatus;
48 import org.apache.slide.webdav.util.WebdavUtils;
49 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind;
50 import org.apache.slide.webdav.util.resourcekind.ResourceKind;
51 import org.apache.slide.webdav.util.resourcekind.VersionControlled;
52 import org.jdom.Element;
53 import org.jdom.JDOMException;
54
55
56 /**
57  * DAV:locate-by-history report worker.
58  *
59  */

60 public class LocateByHistoryReport extends AbstractReport implements DeltavConstants {
61     
62     protected RequestedProperties requestedProperties = null;
63     protected XMLValue versionHistorySet = null;
64     
65     /**
66      * Constructor
67      *
68      * @param slideToken a SlideToken
69      * @param token a NamespaceAccessToken
70      * @param config a WebdavServletConfig
71      * @param serverUrl a String
72      * @param contextPath a String
73      */

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

86     public void init(String JavaDoc resourcePath, Element locateByHistoryElm) throws PreconditionViolationException {
87         List JavaDoc childrenList = locateByHistoryElm.getChildren();
88         if (childrenList.size() != 2) {
89             throw new PreconditionViolationException(
90                 new ViolatedPrecondition("invalid-locate-by-history",
91                                          WebdavStatus.SC_BAD_REQUEST,
92                                          "DAV:locate-by-history element must have 2 children: DAV:version-history-set and DAV:prop"),
93                 resourcePath
94             );
95         }
96         
97         Element versionHistorySetElm = null;
98         Element propElm = null;
99         if ( E_VERSION_HISTORY_SET.equals(((Element)childrenList.get(0)).getName()) &&
100             E_PROP.equals(((Element)childrenList.get(1)).getName()) ) {
101             versionHistorySetElm = (Element)childrenList.get(0);
102             propElm = (Element)childrenList.get(1);
103         }
104         else if ( E_PROP.equals(((Element)childrenList.get(0)).getName()) &&
105                  E_VERSION_HISTORY_SET.equals(((Element)childrenList.get(1)).getName()) ) {
106             propElm = (Element)childrenList.get(0);
107             versionHistorySetElm = (Element)childrenList.get(1);
108         }
109         else {
110             throw new PreconditionViolationException(
111                 new ViolatedPrecondition("invalid-locate-by-history",
112                                          WebdavStatus.SC_BAD_REQUEST,
113                                          "DAV:locate-by-history element must have 2 children: DAV:version-history-set and DAV:prop"),
114                 resourcePath
115             );
116         }
117         
118         // <locate-by-history> report can only be applied to collection
119
if ( ! WebdavUtils.isCollection(token, slideToken, resourcePath) ) {
120             throw new PreconditionViolationException(
121                 new ViolatedPrecondition("must-be-collection",
122                                          WebdavStatus.SC_BAD_REQUEST,
123                                          "the request-URI must specify a collection"),
124                 resourcePath
125             );
126         }
127         
128         this.versionHistorySet =
129             new XMLValue(versionHistorySetElm.getChildren(E_HREF, DNSP));
130         
131         // check DAV:must-be-version-history
132
UriHandler uriHandler = null;
133         String JavaDoc href = null;
134         boolean isVersionHistory = false;
135         Iterator JavaDoc iterator = versionHistorySet.getHrefStrings().iterator();
136         while (iterator.hasNext()) {
137             href = (String JavaDoc)iterator.next();
138             uriHandler = UriHandler.getUriHandler(WebdavUtils.getSlidePath(href, slideContextPath));
139             isVersionHistory = uriHandler.isHistoryUri();
140             if (!isVersionHistory) {
141                 break;
142             }
143         }
144         if (!isVersionHistory) {
145             throw new PreconditionViolationException(
146                 new ViolatedPrecondition(C_MUST_BE_VERSION_HISTORY,
147                                          WebdavStatus.SC_CONFLICT),
148                 resourcePath
149             );
150         }
151         
152         try {
153             this.requestedProperties = new RequestedPropertiesImpl(propElm);
154         }
155         catch (PropertyParseException e) {
156             throw new PreconditionViolationException(
157                 new ViolatedPrecondition("invalid-prop",
158                                          WebdavStatus.SC_BAD_REQUEST,
159                                          e.getMessage()),
160                 resourcePath
161             );
162         }
163     }
164     
165     /**
166      * Execute report and add results to given multistatus element
167      *
168      * @param resourcePath a String
169      * @param multistatusElm an Element
170      * @param depth an int
171      *
172      * @throws SlideException
173      * @throws IOException
174      */

175     public void execute(String JavaDoc resourcePath, Element multistatusElm, int depth) throws SlideException, IOException JavaDoc {
176         if (depth < 0) {
177             return;
178         }
179         
180         NodeRevisionDescriptor nrd = null;
181         NodeRevisionDescriptors nrds = null;
182         ResourceKind resourceKind = null;
183         String JavaDoc versionHistoryUri = null;
184         ObjectNode collectionNode = structure.retrieve(slideToken, resourcePath);
185         Enumeration JavaDoc children = structure.getChildren(slideToken, collectionNode);
186         while (children.hasMoreElements()) {
187             
188             ObjectNode child = (ObjectNode)children.nextElement();
189             if (child.hasChildren()) {
190                 execute(child.getUri(), multistatusElm, depth-1);
191             }
192             else {
193                 nrds = content.retrieve(slideToken, child.getUri());
194                 nrd = content.retrieve(slideToken, nrds);
195                 resourceKind = AbstractResourceKind.determineResourceKind(token, child.getUri(), nrd);
196                 versionHistoryUri = null;
197                 
198                 if (resourceKind instanceof VersionControlled) {
199                     versionHistoryUri = getHistoryUriOfVCR(nrd);
200                 }
201                 
202                 if (versionHistoryUri != null) {
203                     boolean found = false;
204                     Iterator JavaDoc iterator = versionHistorySet.iterator();
205                     String JavaDoc currentHistoryUri = null;
206                     while ( !found && iterator.hasNext() ) {
207                         currentHistoryUri = ((Element)iterator.next()).getText();
208                         if (currentHistoryUri != null) {
209                             currentHistoryUri = WebdavUtils.getSlidePath(currentHistoryUri, slideContextPath);
210                         }
211                         found = versionHistoryUri.equals(currentHistoryUri);
212                     }
213                     if (found) {
214                         multistatusElm.addContent(getResponseElement(slideToken, child.getUri(), nrd.getRevisionNumber(), requestedProperties));
215                     }
216                 }
217             }
218         }
219     }
220     
221     private String JavaDoc getHistoryUriOfVCR(NodeRevisionDescriptor revisionDescriptor) {
222         
223         String JavaDoc historyUri = null;
224         NodeProperty property = revisionDescriptor.getProperty(P_CHECKED_IN);
225         if (property == null) {
226             property = revisionDescriptor.getProperty(P_CHECKED_OUT);
227         }
228         if ( (property != null) && (property.getValue() != null) ) {
229             try {
230                 XMLValue xmlValue = new XMLValue(property.getValue().toString());
231                 Iterator JavaDoc iterator = xmlValue.iterator();
232                 if (iterator.hasNext()) {
233                     String JavaDoc vrUri = ((Element)iterator.next()).getText();
234                     UriHandler uriHandler = UriHandler.getUriHandler(vrUri);
235                     if (uriHandler.isVersionUri()) {
236                         historyUri = uriHandler.getAssociatedHistoryUri();
237                     }
238                 }
239             }
240             catch (JDOMException e) {}
241         }
242         
243         return historyUri;
244     }
245 }
246
247
Popular Tags