1 23 24 package org.apache.slide.webdav.method; 25 26 import java.io.IOException ; 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 58 public class ReportMethod extends AbstractWebdavMethod implements DeltavConstants, AclConstants, ReadMethod { 59 60 private String resourcePath; 61 private AbstractReport reportWorker; 62 private String servletPath; 63 private VersioningHelper versioningHelper; 64 65 71 public ReportMethod(NamespaceAccessToken token, WebdavServletConfig config) { 72 super(token, config); 73 } 74 75 80 protected void parseRequest() throws WebdavException { 81 Element reportElm; 82 String 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 ("Unknown report "+reportName) ); 129 throw new WebdavException( statusCode ); 130 } 131 } 132 catch (IOException e) { int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR; 134 sendError( statusCode, e ); 135 throw new WebdavException( statusCode ); 136 } 137 catch (JDOMException e) { int statusCode = WebdavStatus.SC_BAD_REQUEST; 139 sendError( statusCode, e ); 140 throw new WebdavException( statusCode ); 141 } 142 catch (PreconditionViolationException e) { try { 144 sendPreconditionViolation(e); 145 throw e; 146 } catch (IOException x) {} 147 throw new WebdavException(e.getStatusCode()); 148 } 149 } 150 151 private AbstractReport getExternalReportWorker(String reportName) throws JDOMException, WebdavException { 152 AbstractReport result = null; 153 String clsName = config.getExternalReport(reportName); 154 if (clsName != null) { 155 try { 156 Class cls = Class.forName(clsName); 157 Class [] parmtypes = {SlideToken.class, 158 NamespaceAccessToken.class, 159 WebdavServletConfig.class, 160 String .class, 161 String .class 162 }; 163 Object [] initargs = {slideToken, 164 token, 165 config, 166 servletPath, 167 getSlideContextPath() 168 }; 169 result = 170 (AbstractReport)cls.getConstructor(parmtypes).newInstance(initargs); 171 } 172 catch( Exception e ) { 173 int statusCode = WebdavStatus.SC_BAD_REQUEST; 174 sendError( statusCode, new Exception ("Cannot create "+reportName+" report worker "+clsName+" ["+e+"]") ); 175 throw new WebdavException( statusCode ); 176 } 177 } 178 return result; 179 } 180 181 186 protected void executeRequest() throws WebdavException, IOException { 187 188 try { 190 if (isLockNull(resourcePath)) { 191 int statusCode = WebdavStatus.SC_NOT_FOUND; 192 sendError( statusCode, "lock-null resource", new Object []{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 e) { 227 int statusCode = getErrorCode( e ); 228 sendError( statusCode, e ); 229 throw new WebdavException( statusCode ); 230 } 231 } 232 233 238 private int getDepth() throws WebdavException { 239 int depth = requestHeaders.getDepth(0); 240 241 if (depth > config.getDepthLimit()) { 243 depth = config.getDepthLimit(); 244 } 245 return depth; 246 } 247 } 248 249 250 251 | Popular Tags |