Package org.apache.slide.webdav.method

Source Code of org.apache.slide.webdav.method.ReportMethod

/*
* $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/ReportMethod.java,v 1.61.2.2 2004/03/14 18:14:56 mholz Exp $
* $Revision: 1.61.2.2 $
* $Date: 2004/03/14 18:14:56 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.slide.webdav.method;

import java.io.IOException;
import java.util.Iterator;
import org.apache.slide.common.NamespaceAccessToken;
import org.apache.slide.common.ServiceAccessException;
import org.apache.slide.common.SlideToken;
import org.apache.slide.util.XMLValue;
import org.apache.slide.webdav.WebdavException;
import org.apache.slide.webdav.WebdavServletConfig;
import org.apache.slide.webdav.method.report.AbstractReport;
import org.apache.slide.webdav.method.report.AclPrincipalPropSetReport;
import org.apache.slide.webdav.method.report.ExpandPropertyReport;
import org.apache.slide.webdav.method.report.LocateByHistoryReport;
import org.apache.slide.webdav.method.report.PrincipalMatchReport;
import org.apache.slide.webdav.method.report.PrincipalPropertySearchReport;
import org.apache.slide.webdav.method.report.PrincipalSearchPropertySetReport;
import org.apache.slide.webdav.method.report.VersionTreeReport;
import org.apache.slide.webdav.util.AclConstants;
import org.apache.slide.webdav.util.DeltavConstants;
import org.apache.slide.webdav.util.PreconditionViolationException;
import org.apache.slide.webdav.util.VersioningHelper;
import org.apache.util.WebdavStatus;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;


/**
* REPORT method.
*
* @author <a href="mailto:peter.nevermann@softwareag.com">Peter Nevermann</a>
*/
public class ReportMethod extends AbstractWebdavMethod implements DeltavConstants, AclConstants {
   
    private String resourcePath;
    private AbstractReport reportWorker;
    private String servletPath;
    private VersioningHelper versioningHelper;
   
    /**
     * Constructor.
     *
     * @param token     the token for accessing the namespace
     * @param config    configuration of the WebDAV servlet
     */
    public ReportMethod(NamespaceAccessToken token, WebdavServletConfig config) {
        super(token, config);
    }
   
    /**
     * Parse WebDAV XML request body.
     *
     * @exception WebdavException
     */
    protected void parseRequest() throws WebdavException {
        Element reportElm;
       
        resourcePath = requestUri;
        if (resourcePath == null) {
            resourcePath = "/";
        }
  servletPath = req.getServletPath();
       
        versioningHelper =
            VersioningHelper.getVersioningHelper(slideToken, token, req, resp, config);
       
        try{
            reportElm = parseRequestContent().getRootElement();
            if (R_EXPAND_PROPERTY.equals(reportElm.getName())) {
                reportWorker = new ExpandPropertyReport(slideToken, token, config, servletPath, req.getContextPath());
            }
            else if (R_VERSION_TREE.equals(reportElm.getName())) {
                reportWorker = new VersionTreeReport(slideToken, token, config, servletPath, req.getContextPath());
                ((VersionTreeReport)reportWorker).setVersioningHelper(versioningHelper);
            }
            else if (R_LOCATE_BY_HISTORY.equals(reportElm.getName())) {
                reportWorker = new LocateByHistoryReport(slideToken, token, config, servletPath, req.getContextPath());
            }
            else if (R_ACL_PRINCIPAL_PROP_SET.equals(reportElm.getName())) {
                reportWorker = new AclPrincipalPropSetReport(slideToken, token, config, servletPath, req.getContextPath());
            }
            else if (R_PRINCIPAL_MATCH.equals(reportElm.getName())) {
                reportWorker = new PrincipalMatchReport(slideToken, token, config, servletPath, req.getContextPath());
            }
            else if (R_PRINCIPAL_PROPERTY_SEARCH.equals(reportElm.getName())) {
                reportWorker = new PrincipalPropertySearchReport(slideToken, token, config, servletPath, req.getContextPath());
            }
            else if (R_PRINCIPAL_SEARCH_PROPERTY_SET.equals(reportElm.getName())) {
                reportWorker = new PrincipalSearchPropertySetReport(slideToken, token, config, servletPath, req.getContextPath());
            }
            else {
                // check additional reports
                reportWorker = getExternalReportWorker(reportElm);
            }
           
            if (reportWorker != null) {
                reportWorker.init(resourcePath, reportElm);
            }
            else {
                int statusCode = WebdavStatus.SC_BAD_REQUEST;
                sendError( statusCode, new Exception("Unknown report "+reportElm.getName()) );
                throw new WebdavException( statusCode );
            }
        }
        catch (IOException e) {  // TODO: merge exception handling into jdom access methods
            int statusCode = WebdavStatus.SC_INTERNAL_SERVER_ERROR;
            sendError( statusCode, e );
            throw new WebdavException( statusCode );
        }
        catch (JDOMException e) {  // TODO: merge exception handling into jdom access methods
            int statusCode = WebdavStatus.SC_BAD_REQUEST;
            sendError( statusCode, e );
            throw new WebdavException( statusCode );
        }
        catch (PreconditionViolationException e) {  // TODO: merge exception handling into jdom access methods
            try {
                sendPreconditionViolation(e);
                throw e;
            } catch (IOException x) {}
            throw new WebdavException(e.getStatusCode());
        }
    }
   
    private AbstractReport getExternalReportWorker(Element reportElm) throws JDOMException, WebdavException {
        AbstractReport result = null;
        XMLValue xv = new XMLValue(config.getInitParameter("external-reports"));
        Iterator i = xv.iterator();
        while (i.hasNext()) {
            Object r = i.next();
            if (r instanceof Element
                && "report".equals(((Element)r).getName())
                && reportElm.getName().equals(((Element)r).getAttributeValue("name"))
               ) {
               
                String clsName = ((Element)r).getTextTrim();
                try {
                    Class cls = Class.forName(clsName);
                    Class[] parmtypes = {SlideToken.class,
                            NamespaceAccessToken.class,
                            WebdavServletConfig.class,
                            String.class,
                            String.class
                    };
                    Object[] initargs = {slideToken,
                            token,
                            config,
                            servletPath,
                            req.getContextPath()
                    };
                    result =
                        (AbstractReport)cls.getConstructor(parmtypes).newInstance(initargs);
                }
                catch( Exception e ) {
                    int statusCode = WebdavStatus.SC_BAD_REQUEST;
                    sendError( statusCode, new Exception("Cannot create report worker "+clsName) );
                    throw new WebdavException( statusCode );
                }
            }
        }
        return result;
    }
   
    /**
     * Execute the request.
     *
     * @exception WebdavException
     */
    protected void executeRequest() throws WebdavException, IOException {
       
        // check lock-null resources
        try {
            if (isLockNull(resourcePath)) {
                int statusCode = WebdavStatus.SC_NOT_FOUND;
                sendError( statusCode, "lock-null resource", new Object[]{resourcePath} );
                throw new WebdavException( statusCode );
            }
        }
        catch (ServiceAccessException e) {
            int statusCode = getErrorCode( e );
            sendError( statusCode, e );
            throw new WebdavException( statusCode );
        }
       
        try {
            reportWorker.checkPreconditions(resourcePath, getDepth());
            Element rootElm;
            if (reportWorker instanceof PrincipalSearchPropertySetReport) {
                rootElm = new Element(E_PRINCIPAL_SEARCH_PROPERTY_SET, DNSP);
                reportWorker.execute(resourcePath, rootElm, getDepth());
                resp.setStatus(WebdavStatus.SC_OK);
            }
            else {
                rootElm = new Element(E_MULTISTATUS, DNSP);
                reportWorker.execute(resourcePath, rootElm, getDepth());
                resp.setStatus(WebdavStatus.SC_MULTI_STATUS);
            }
            new XMLOutputter(XML_REPONSE_INDENT, true).
                output(new Document(rootElm), resp.getWriter());
        }
        catch (PreconditionViolationException e) {
            sendPreconditionViolation(e);
            throw e;
        }
        catch (Exception e) {
            int statusCode = getErrorCode( e );
            sendError( statusCode, e );
            throw new WebdavException( statusCode );
        }
    }
   
    /**
     * Returns the value of the <code>Depth</code> request header.
     *
     * @return     the value of the <code>Depth</code> request header.
     */
    private int getDepth() throws WebdavException {
        int depth = requestHeaders.getDepth(0);
       
        // limit tree browsing a bit
        if (depth > config.getDepthLimit()) {
            depth = config.getDepthLimit();
        }
        return depth;
    }
}



TOP

Related Classes of org.apache.slide.webdav.method.ReportMethod

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.