KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/report/PrincipalMatchReport.java,v 1.11 2004/08/05 15:44:57 unico Exp $
3  * $Revision: 1.11 $
4  * $Date: 2004/08/05 15:44:57 $
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.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.slide.common.NamespaceAccessToken;
31 import org.apache.slide.common.PropertyParseException;
32 import org.apache.slide.common.RequestedProperties;
33 import org.apache.slide.common.RequestedPropertiesImpl;
34 import org.apache.slide.common.RequestedProperty;
35 import org.apache.slide.common.ServiceAccessException;
36 import org.apache.slide.common.SlideException;
37 import org.apache.slide.common.SlideToken;
38 import org.apache.slide.content.NodeRevisionNumber;
39 import org.apache.slide.search.RequestedResource;
40 import org.apache.slide.search.SearchQuery;
41 import org.apache.slide.search.basic.Literals;
42 import org.apache.slide.structure.SubjectNode;
43 import org.apache.slide.webdav.WebdavServletConfig;
44 import org.apache.slide.webdav.util.AclConstants;
45 import org.apache.slide.webdav.util.PreconditionViolationException;
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.jdom.Element;
50 import org.jdom.Namespace;
51 import org.jdom.output.XMLOutputter;
52
53
54 /**
55  * DAV:principal-match report worker.
56  *
57  */

58 public class PrincipalMatchReport extends AbstractReport implements AclConstants {
59     
60     private RequestedProperties requestedProperties = null;
61     private SubjectNode matchSubjectNode = null;
62     
63     /**
64      * Constructor
65      *
66      * @param slideToken a SlideToken
67      * @param token a NamespaceAccessToken
68      * @param config a WebdavServletConfig
69      * @param servletPath a String, the result of HttpRequest.getServletPath()
70      * @param contextPath a String , the result of HttpRequest.getContextPath()
71      */

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

84     public void init(String JavaDoc resourcePath, Element principalMatchElm) throws PreconditionViolationException {
85         if (principalMatchElm.getChildren().size() == 0) {
86             throw new PreconditionViolationException(
87                 new ViolatedPrecondition("at-least-one-child-element",
88                                          WebdavStatus.SC_BAD_REQUEST,
89                                          "DAV:principal-match element must have at least one child"),
90                 resourcePath
91             );
92         }
93         List JavaDoc principalPropertyElmL = principalMatchElm.getChildren(E_PRINCIPAL_PROPERTY, DNSP);
94         List JavaDoc selfElmL = principalMatchElm.getChildren(E_SELF, DNSP);
95         if (principalPropertyElmL.size()+selfElmL.size() != 1) {
96             throw new PreconditionViolationException(
97                 new ViolatedPrecondition("either-one-principal-property-or-one-self-element",
98                                          WebdavStatus.SC_BAD_REQUEST,
99                                          "DAV:principal-match element must contain either a DAV:principal-property or a DAV:self element"),
100                 resourcePath
101             );
102         }
103         if (principalPropertyElmL.size() == 1) {
104             Element principalPropertyElm = (Element)principalPropertyElmL.get(0);
105             if (principalPropertyElm.getChildren(E_OWNER, DNSP).size() > 0) {
106                 matchSubjectNode = SubjectNode.OWNER;
107             }
108             else {
109                 // TODO: support other properties
110
}
111         }
112         else {
113             matchSubjectNode = SubjectNode.SELF;
114         }
115         List JavaDoc propElmL = principalMatchElm.getChildren(E_PROP, DNSP);
116         if (propElmL.size() > 1) {
117             throw new PreconditionViolationException(
118                 new ViolatedPrecondition("at-most-one-prop-element",
119                                          WebdavStatus.SC_BAD_REQUEST,
120                                          "DAV:principal-match element must have at most one DAV:prop child"),
121                 resourcePath
122             );
123         }
124         
125         if (propElmL.size() == 1) {
126             Element propElm = (Element)propElmL.get(0);
127             try {
128                 this.requestedProperties = new RequestedPropertiesImpl(propElm);
129             }
130             catch (PropertyParseException e) {
131                 throw new PreconditionViolationException(
132                     new ViolatedPrecondition("invalid-prop",
133                                              WebdavStatus.SC_BAD_REQUEST,
134                                              e.getMessage()),
135                     resourcePath
136                 );
137             }
138         }
139     }
140     
141     /**
142      * Execute report and add results to given multistatus element
143      *
144      * @param resourcePath a String
145      * @param multistatusElm an Element
146      * @param depth an int
147      *
148      * @throws SlideException
149      * @throws IOException
150      */

151     public void execute(String JavaDoc resourcePath, Element multistatusElm, int depth) throws SlideException, IOException JavaDoc {
152         SubjectNode currentUserNode = (SubjectNode)security.getPrincipal(slideToken);
153         Element queryElm = getQueryElement(resourcePath, currentUserNode);
154         new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).output(queryElm, System.out);
155         
156         String JavaDoc absUri = WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config);
157         
158         SearchQuery query =
159             search.createSearchQuery(queryElm.getNamespaceURI(),
160                                      queryElm,
161                                      slideToken,
162                                      config.getDepthLimit(),
163                                      absUri);
164         
165         Iterator JavaDoc result = query.execute().iterator();
166         while (result.hasNext()) {
167             RequestedResource r = (RequestedResource)result.next();
168             multistatusElm.addContent(getResponseElement(slideToken, r.getUri(), new NodeRevisionNumber(), requestedProperties));
169         }
170         
171     }
172     
173     private Element getQueryElement(String JavaDoc resourcePath, SubjectNode currentUserNode) throws SlideException {
174         if (matchSubjectNode.equals(SubjectNode.OWNER)) {
175             return getOwnerQueryElement(resourcePath, currentUserNode);
176         }
177         else if (matchSubjectNode.equals(SubjectNode.SELF)) {
178             return getSelfQueryElement(resourcePath, currentUserNode);
179         }
180         else {
181             // TODO: other properties
182
return null;
183         }
184     }
185     
186     private Element getSelfQueryElement(String JavaDoc resourcePath, SubjectNode currentUserNode) {
187         Element result = new Element(Literals.BASICSEARCH, DNSP);
188         // select
189
Element selectElm = new Element(Literals.SELECT, DNSP);
190         result.addContent(selectElm);
191         Element propElm = new Element(E_PROP, DNSP);
192         selectElm.addContent(propElm);
193         Iterator JavaDoc props = requestedProperties.getRequestedProperties();
194         while (props.hasNext()) {
195             RequestedProperty p = (RequestedProperty)props.next();
196             Namespace nsp = (DNSP.getURI().equals(p.getNamespace()))
197                 ? DNSP
198                 : Namespace.getNamespace(p.getNamespace());
199             propElm.addContent(new Element(p.getName(), nsp));
200         }
201         // from
202
Element fromElm = new Element(Literals.FROM, DNSP);
203         result.addContent(fromElm);
204         Element scopeElm = new Element(Literals.SCOPE, DNSP);
205         fromElm.addContent(scopeElm);
206         Element hrefElm = new Element(E_HREF, DNSP);
207         hrefElm.setText(WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config));
208         
209         scopeElm.addContent(hrefElm);
210         // where
211
Element whereElm = new Element(Literals.WHERE, DNSP);
212         result.addContent(whereElm);
213         Element propcontainsElm = new Element("propcontains", Namespace.getNamespace("http://jakarta.apache.org/slide/"));
214         whereElm.addContent(propcontainsElm);
215         propElm = new Element(E_PROP, DNSP);
216         propElm.addContent(new Element(P_GROUP_MEMBER_SET, DNSP));
217         Element literalElm = new Element(Literals.LITERAL, DNSP);
218         literalElm.addContent("/"+currentUserNode.getPath().lastSegment()+"<");
219         propcontainsElm.addContent(propElm);
220         propcontainsElm.addContent(literalElm);
221         return result;
222     }
223     
224     private Element getOwnerQueryElement(String JavaDoc resourcePath, SubjectNode currentUserNode) {
225         Element result = new Element(Literals.BASICSEARCH, DNSP);
226         // select
227
Element selectElm = new Element(Literals.SELECT, DNSP);
228         result.addContent(selectElm);
229         Element propElm = new Element(E_PROP, DNSP);
230         selectElm.addContent(propElm);
231         Iterator JavaDoc props = requestedProperties.getRequestedProperties();
232         while (props.hasNext()) {
233             RequestedProperty p = (RequestedProperty)props.next();
234             Namespace nsp = (DNSP.getURI().equals(p.getNamespace()))
235                 ? DNSP
236                 : Namespace.getNamespace(p.getNamespace());
237             propElm.addContent(new Element(p.getName(), nsp));
238         }
239         // from
240
Element fromElm = new Element(Literals.FROM, DNSP);
241         result.addContent(fromElm);
242         Element scopeElm = new Element(Literals.SCOPE, DNSP);
243         fromElm.addContent(scopeElm);
244         Element hrefElm = new Element(E_HREF, DNSP);
245         hrefElm.setText(WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config));
246         
247         scopeElm.addContent(hrefElm);
248         // where
249
Element whereElm = new Element(Literals.WHERE, DNSP);
250         result.addContent(whereElm);
251         Element eqElm = new Element(Literals.EQ, DNSP);
252         whereElm.addContent(eqElm);
253         propElm = new Element(E_PROP, DNSP);
254         propElm.addContent(new Element(P_OWNER, DNSP));
255         Element literalElm = new Element(Literals.LITERAL, DNSP);
256         literalElm.addContent(currentUserNode.getPath().lastSegment());
257         eqElm.addContent(propElm);
258         eqElm.addContent(literalElm);
259         return result;
260     }
261     
262     /**
263      * Method checkPreconditions
264      * @param resourcePath a String
265      * @param depth an int
266      * @throws PreconditionViolationException
267      * @throws ServiceAccessException
268      */

269     public void checkPreconditions(String JavaDoc resourcePath, int depth) throws PreconditionViolationException, ServiceAccessException {
270         if (depth != 0) {
271             throw new PreconditionViolationException(
272                 new ViolatedPrecondition("depth-must-be-zero",
273                                          WebdavStatus.SC_BAD_REQUEST,
274                                          "This report is only defined for depth=0."),
275                 resourcePath
276             );
277         }
278     }
279 }
280
Popular Tags