KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/report/PrincipalPropertySearchReport.java,v 1.10 2004/08/05 14:43:34 dflorey Exp $
3  * $Revision: 1.10 $
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.ArrayList 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.RequestedProperty;
36 import org.apache.slide.common.ServiceAccessException;
37 import org.apache.slide.common.SlideException;
38 import org.apache.slide.common.SlideToken;
39 import org.apache.slide.content.NodeRevisionNumber;
40 import org.apache.slide.search.RequestedResource;
41 import org.apache.slide.search.SearchQuery;
42 import org.apache.slide.search.basic.Literals;
43 import org.apache.slide.structure.SubjectNode;
44 import org.apache.slide.webdav.WebdavServletConfig;
45 import org.apache.slide.webdav.util.AclConstants;
46 import org.apache.slide.webdav.util.PreconditionViolationException;
47 import org.apache.slide.webdav.util.ViolatedPrecondition;
48 import org.apache.slide.webdav.util.WebdavStatus;
49 import org.apache.slide.webdav.util.WebdavUtils;
50 import org.jdom.Element;
51 import org.jdom.Namespace;
52 import org.jdom.output.XMLOutputter;
53
54 /**
55  * DAV:principal-property-search report worker.
56  *
57  */

58 public class PrincipalPropertySearchReport extends AbstractReport implements AclConstants {
59     
60     private RequestedProperties requestedProperties = null;
61     private List JavaDoc conditionList = 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 PrincipalPropertySearchReport(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 principalPropertySearchElm an Element
81      *
82      * @throws PreconditionViolationException
83      */

84     public void init(String JavaDoc resourcePath, Element principalPropertySearchElm) throws PreconditionViolationException {
85         if (principalPropertySearchElm.getChildren().size() == 0) {
86             throw new PreconditionViolationException(
87                 new ViolatedPrecondition("at-least-one-child-element",
88                                          WebdavStatus.SC_BAD_REQUEST,
89                                          "DAV:principal-property-search element must have at least one child"),
90                 resourcePath
91             );
92         }
93         List JavaDoc propertySearchElmL = principalPropertySearchElm.getChildren(E_PROPERTY_SEARCH, DNSP);
94         if (propertySearchElmL.size() == 0) {
95             throw new PreconditionViolationException(
96                 new ViolatedPrecondition("at-least-one-property-search-element",
97                                          WebdavStatus.SC_BAD_REQUEST,
98                                          "DAV:principal-property-search element must contain at least one DAV:property-search element"),
99                 resourcePath
100             );
101         }
102         Iterator JavaDoc ps = propertySearchElmL.iterator();
103         this.conditionList = new ArrayList JavaDoc();
104         while (ps.hasNext()) {
105             Element propertySearchElm = (Element)ps.next();
106             Element matchElm = propertySearchElm.getChild(E_MATCH, DNSP);
107             if (matchElm == null) {
108                 continue;
109             }
110             Element propElm = propertySearchElm.getChild(E_PROP, DNSP);
111             Iterator JavaDoc pElms = propElm.getChildren().iterator();
112             while (pElms.hasNext()) {
113                 Element pElm = (Element)pElms.next();
114                 Element newpropElm = new Element(E_PROP, DNSP);
115                 newpropElm.addContent(new Element(pElm.getName(), pElm.getNamespace()));
116                 Element propcontainsElm = new Element("propcontains", Namespace.getNamespace("http://jakarta.apache.org/slide/"));
117                 Element literalElm = new Element(Literals.LITERAL, DNSP);
118                 literalElm.addContent(matchElm.getTextTrim());
119                 propcontainsElm.addContent(newpropElm);
120                 propcontainsElm.addContent(literalElm);
121                 conditionList.add(propcontainsElm);
122             }
123         }
124         List JavaDoc propElmL = principalPropertySearchElm.getChildren(E_PROP, DNSP);
125         if (propElmL.size() > 1) {
126             throw new PreconditionViolationException(
127                 new ViolatedPrecondition("at-most-one-prop-element",
128                                          WebdavStatus.SC_BAD_REQUEST,
129                                          "DAV:principal-property-search element must have at most one DAV:prop child"),
130                 resourcePath
131             );
132         }
133         
134         if (propElmL.size() == 1) {
135             Element propElm = (Element)propElmL.get(0);
136             try {
137                 this.requestedProperties = new RequestedPropertiesImpl(propElm);
138             }
139             catch (PropertyParseException e) {
140                 throw new PreconditionViolationException(
141                     new ViolatedPrecondition("invalid-prop",
142                                              WebdavStatus.SC_BAD_REQUEST,
143                                              e.getMessage()),
144                     resourcePath
145                 );
146             }
147         }
148     }
149     
150     /**
151      * Execute report and add results to given multistatus element
152      *
153      * @param resourcePath a String
154      * @param multistatusElm an Element
155      * @param depth an int
156      *
157      * @throws SlideException
158      * @throws IOException
159      */

160     public void execute(String JavaDoc resourcePath, Element multistatusElm, int depth) throws SlideException, IOException JavaDoc {
161         SubjectNode currentUserNode = (SubjectNode)security.getPrincipal(slideToken);
162         Element queryElm = getQueryElement(resourcePath, currentUserNode);
163         new XMLOutputter(org.jdom.output.Format.getPrettyFormat()).output(queryElm, System.out);
164         
165         String JavaDoc absUri = WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config);
166         
167         SearchQuery query =
168             search.createSearchQuery(queryElm.getNamespaceURI(),
169                                      queryElm,
170                                      slideToken,
171                                      config.getDepthLimit(),
172                                      absUri);
173         
174         Iterator JavaDoc result = query.execute().iterator();
175         while (result.hasNext()) {
176             RequestedResource r = (RequestedResource)result.next();
177             multistatusElm.addContent(getResponseElement(slideToken, r.getUri(), new NodeRevisionNumber(), requestedProperties));
178         }
179         
180     }
181     
182     private Element getQueryElement(String JavaDoc resourcePath, SubjectNode currentUserNode) {
183         Element result = new Element(Literals.BASICSEARCH, DNSP);
184         // select
185
Element selectElm = new Element(Literals.SELECT, DNSP);
186         result.addContent(selectElm);
187         Element propElm = new Element(E_PROP, DNSP);
188         selectElm.addContent(propElm);
189         Iterator JavaDoc props = requestedProperties.getRequestedProperties();
190         while (props.hasNext()) {
191             RequestedProperty p = (RequestedProperty)props.next();
192             Namespace nsp = (DNSP.getURI().equals(p.getNamespace()))
193                 ? DNSP
194                 : Namespace.getNamespace(p.getNamespace());
195             propElm.addContent(new Element(p.getName(), nsp));
196         }
197         // from
198
Element fromElm = new Element(Literals.FROM, DNSP);
199         result.addContent(fromElm);
200         Element scopeElm = new Element(Literals.SCOPE, DNSP);
201         fromElm.addContent(scopeElm);
202         Element hrefElm = new Element(E_HREF, DNSP);
203         hrefElm.setText(WebdavUtils.getAbsolutePath (resourcePath, slideContextPath, config));
204         
205         scopeElm.addContent(hrefElm);
206         // where
207
if (conditionList.size() > 0) {
208             Element whereElm = new Element(Literals.WHERE, DNSP);
209             result.addContent(whereElm);
210             if (conditionList.size() == 1) {
211                 whereElm.addContent((Element)conditionList.get(0));
212             }
213             else {
214                 Element andElm = new Element(Literals.AND, DNSP);
215                 whereElm.addContent(andElm);
216                 Iterator JavaDoc conditions = conditionList.iterator();
217                 while (conditions.hasNext()) {
218                     andElm.addContent((Element)conditions.next());
219                 }
220             }
221         }
222         return result;
223     }
224     
225     /**
226      * Method checkPreconditions
227      * @param resourcePath a String
228      * @param depth an int
229      * @throws PreconditionViolationException
230      * @throws ServiceAccessException
231      */

232     public void checkPreconditions(String JavaDoc resourcePath, int depth) throws PreconditionViolationException, ServiceAccessException {
233         if (depth != 0) {
234             throw new PreconditionViolationException(
235                 new ViolatedPrecondition("depth-must-be-zero",
236                                          WebdavStatus.SC_BAD_REQUEST,
237                                          "This report is only defined for depth=0."),
238                 resourcePath
239             );
240         }
241     }
242 }
243
Popular Tags