KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/ReportMethod.java,v 1.71.2.1 2004/09/25 20:38:43 luetzkendorf Exp $
3  * $Revision: 1.71.2.1 $
4  * $Date: 2004/09/25 20:38:43 $
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
28 import org.apache.slide.common.NamespaceAccessToken;
29 import org.apache.slide.common.ServiceAccessException;
30 import org.apache.slide.common.SlideToken;
31 import org.apache.slide.event.EventDispatcher;
32 import org.apache.slide.webdav.WebdavException;
33 import org.apache.slide.webdav.WebdavServletConfig;
34 import org.apache.slide.webdav.event.WebdavEvent;
35 import org.apache.slide.webdav.method.report.AbstractReport;
36 import org.apache.slide.webdav.method.report.AclPrincipalPropSetReport;
37 import org.apache.slide.webdav.method.report.ExpandPropertyReport;
38 import org.apache.slide.webdav.method.report.LocateByHistoryReport;
39 import org.apache.slide.webdav.method.report.PrincipalMatchReport;
40 import org.apache.slide.webdav.method.report.PrincipalPropertySearchReport;
41 import org.apache.slide.webdav.method.report.PrincipalSearchPropertySetReport;
42 import org.apache.slide.webdav.method.report.VersionTreeReport;
43 import org.apache.slide.webdav.util.AclConstants;
44 import org.apache.slide.webdav.util.DeltavConstants;
45 import org.apache.slide.webdav.util.PreconditionViolationException;
46 import org.apache.slide.webdav.util.VersioningHelper;
47 import org.apache.slide.webdav.util.WebdavStatus;
48 import org.jdom.Document;
49 import org.jdom.Element;
50 import org.jdom.JDOMException;
51 import org.jdom.output.XMLOutputter;
52
53
54 /**
55  * REPORT method.
56  *
57  */

58 public class ReportMethod extends AbstractWebdavMethod implements DeltavConstants, AclConstants, ReadMethod {
59     
60     private String JavaDoc resourcePath;
61     private AbstractReport reportWorker;
62     private String JavaDoc servletPath;
63     private VersioningHelper versioningHelper;
64     
65     /**
66      * Constructor.
67      *
68      * @param token the token for accessing the namespace
69      * @param config configuration of the WebDAV servlet
70      */

71     public ReportMethod(NamespaceAccessToken token, WebdavServletConfig config) {
72         super(token, config);
73     }
74     
75     /**
76      * Parse WebDAV XML request body.
77      *
78      * @exception WebdavException
79      */

80     protected void parseRequest() throws WebdavException {
81         Element reportElm;
82         String JavaDoc reportName;
83         
84         resourcePath = requestUri;
85         if (resourcePath == null) {
86             resourcePath = "/";
87         }
88     servletPath = req.getServletPath();
89         
90         versioningHelper =
91             VersioningHelper.getVersioningHelper(slideToken, token, req, resp, config);
92         
93         try{
94             reportElm = parseRequestContent().getRootElement();
95             reportName = reportElm.getName();
96             reportWorker = getExternalReportWorker(reportName);
97             
98             if (reportWorker == null) {
99                 if (R_EXPAND_PROPERTY.equals(reportName)) {
100                     reportWorker = new ExpandPropertyReport(slideToken, token, config, getSlideContextPath());
101             }
102                 else if (R_VERSION_TREE.equals(reportName)) {
103                     reportWorker = new VersionTreeReport(slideToken, token, config, getSlideContextPath());
104                 ((VersionTreeReport)reportWorker).setVersioningHelper(versioningHelper);
105             }
106                 else if (R_LOCATE_BY_HISTORY.equals(reportName)) {
107                     reportWorker = new LocateByHistoryReport(slideToken, token, config, getSlideContextPath());
108             }
109                 else if (R_ACL_PRINCIPAL_PROP_SET.equals(reportName)) {
110                     reportWorker = new AclPrincipalPropSetReport(slideToken, token, config, getSlideContextPath());
111             }
112                 else if (R_PRINCIPAL_MATCH.equals(reportName)) {
113                     reportWorker = new PrincipalMatchReport(slideToken, token, config, getSlideContextPath());
114             }
115                 else if (R_PRINCIPAL_PROPERTY_SEARCH.equals(reportName)) {
116                     reportWorker = new PrincipalPropertySearchReport(slideToken, token, config, getSlideContextPath());
117             }
118                 else if (R_PRINCIPAL_SEARCH_PROPERTY_SET.equals(reportName)) {
119                     reportWorker = new PrincipalSearchPropertySetReport(slideToken, token, config, getSlideContextPath());
120             }
121             }
122             
123             if (reportWorker != null) {
124                 reportWorker.init(resourcePath, reportElm);
125             }
126             else {
127                 int statusCode = WebdavStatus.SC_BAD_REQUEST;
128                 sendError( statusCode, new Exception JavaDoc("Unknown report "+reportName) );
129                 throw new WebdavException( statusCode );
130             }
131         }
132         catch (IOException JavaDoc e) { // TODO: merge exception handling into jdom access methods
133
int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
134             sendError( statusCode, e );
135             throw new WebdavException( statusCode );
136         }
137         catch (JDOMException e) { // TODO: merge exception handling into jdom access methods
138
int statusCode = WebdavStatus.SC_BAD_REQUEST;
139             sendError( statusCode, e );
140             throw new WebdavException( statusCode );
141         }
142         catch (PreconditionViolationException e) { // TODO: merge exception handling into jdom access methods
143
try {
144                 sendPreconditionViolation(e);
145                 throw e;
146             } catch (IOException JavaDoc x) {}
147             throw new WebdavException(e.getStatusCode());
148         }
149     }
150     
151     private AbstractReport getExternalReportWorker(String JavaDoc reportName) throws JDOMException, WebdavException {
152         AbstractReport result = null;
153         String JavaDoc clsName = config.getExternalReport(reportName);
154         if (clsName != null) {
155                 try {
156                     Class JavaDoc cls = Class.forName(clsName);
157                     Class JavaDoc[] parmtypes = {SlideToken.class,
158                             NamespaceAccessToken.class,
159                             WebdavServletConfig.class,
160                             String JavaDoc.class,
161                             String JavaDoc.class
162                     };
163                     Object JavaDoc[] initargs = {slideToken,
164                             token,
165                             config,
166                             servletPath,
167                             getSlideContextPath()
168                     };
169                     result =
170                         (AbstractReport)cls.getConstructor(parmtypes).newInstance(initargs);
171                 }
172                 catch( Exception JavaDoc e ) {
173                     int statusCode = WebdavStatus.SC_BAD_REQUEST;
174                 sendError( statusCode, new Exception JavaDoc("Cannot create "+reportName+" report worker "+clsName+" ["+e+"]") );
175                     throw new WebdavException( statusCode );
176                 }
177             }
178         return result;
179     }
180     
181     /**
182      * Execute the request.
183      *
184      * @exception WebdavException
185      */

186     protected void executeRequest() throws WebdavException, IOException JavaDoc {
187         
188         // check lock-null resources
189
try {
190             if (isLockNull(resourcePath)) {
191                 int statusCode = WebdavStatus.SC_NOT_FOUND;
192                 sendError( statusCode, "lock-null resource", new Object JavaDoc[]{resourcePath} );
193                 throw new WebdavException( statusCode );
194             }
195         }
196         catch (ServiceAccessException e) {
197             int statusCode = getErrorCode( e );
198             sendError( statusCode, e );
199             throw new WebdavException( statusCode );
200         }
201         
202         try {
203             if ( WebdavEvent.REPORT.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.REPORT, new WebdavEvent(this));
204
205             reportWorker.checkPreconditions(resourcePath, getDepth());
206             Element rootElm;
207             if (reportWorker instanceof PrincipalSearchPropertySetReport) {
208                 rootElm = new Element(E_PRINCIPAL_SEARCH_PROPERTY_SET, DNSP);
209                 reportWorker.execute(resourcePath, rootElm, getDepth());
210                 resp.setStatus(WebdavStatus.SC_OK);
211             }
212             else {
213                 rootElm = new Element(E_MULTISTATUS, DNSP);
214                 reportWorker.execute(resourcePath, rootElm, getDepth());
215                 resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
216             }
217             org.jdom.output.Format format = org.jdom.output.Format.getPrettyFormat();
218             format.setIndent(XML_RESPONSE_INDENT);
219             new XMLOutputter(format).
220                 output(new Document(rootElm), resp.getWriter());
221         }
222         catch (PreconditionViolationException e) {
223             sendPreconditionViolation(e);
224             throw e;
225         }
226         catch (Exception JavaDoc e) {
227             int statusCode = getErrorCode( e );
228             sendError( statusCode, e );
229             throw new WebdavException( statusCode );
230         }
231     }
232     
233     /**
234      * Returns the value of the <code>Depth</code> request header.
235      *
236      * @return the value of the <code>Depth</code> request header.
237      */

238     private int getDepth() throws WebdavException {
239         int depth = requestHeaders.getDepth(0);
240         
241         // limit tree browsing a bit
242
if (depth > config.getDepthLimit()) {
243             depth = config.getDepthLimit();
244         }
245         return depth;
246     }
247 }
248
249
250
251
Popular Tags