Search in sources :

Example 31 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class AbstractWebdavServlet method doLock.

/**
 * The LOCK method
 *
 * @param request
 * @param response
 * @param resource
 * @throws IOException
 * @throws DavException
 */
protected void doLock(WebdavRequest request, WebdavResponse response, DavResource resource) throws IOException, DavException {
    LockInfo lockInfo = request.getLockInfo();
    if (lockInfo.isRefreshLock()) {
        // refresh any matching existing locks
        ActiveLock[] activeLocks = resource.getLocks();
        List<ActiveLock> lList = new ArrayList<ActiveLock>();
        for (ActiveLock activeLock : activeLocks) {
            // adjust lockinfo with type/scope retrieved from the lock.
            lockInfo.setType(activeLock.getType());
            lockInfo.setScope(activeLock.getScope());
            DavProperty<?> etagProp = resource.getProperty(DavPropertyName.GETETAG);
            String etag = etagProp != null ? String.valueOf(etagProp.getValue()) : "";
            if (request.matchesIfHeader(resource.getHref(), activeLock.getToken(), etag)) {
                lList.add(resource.refreshLock(lockInfo, activeLock.getToken()));
            }
        }
        if (lList.isEmpty()) {
            throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);
        }
        ActiveLock[] refreshedLocks = lList.toArray(new ActiveLock[lList.size()]);
        response.sendRefreshLockResponse(refreshedLocks);
    } else {
        int status = HttpServletResponse.SC_OK;
        if (!resource.exists()) {
            // lock-empty requires status code 201 (Created)
            status = HttpServletResponse.SC_CREATED;
        }
        // create a new lock
        ActiveLock lock = resource.lock(lockInfo);
        CodedUrlHeader header = new CodedUrlHeader(DavConstants.HEADER_LOCK_TOKEN, lock.getToken());
        response.setHeader(header.getHeaderName(), header.getHeaderValue());
        DavPropertySet propSet = new DavPropertySet();
        propSet.add(new LockDiscovery(lock));
        response.sendXmlResponse(propSet, status);
    }
}
Also used : CodedUrlHeader(org.apache.jackrabbit.webdav.header.CodedUrlHeader) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) ActiveLock(org.apache.jackrabbit.webdav.lock.ActiveLock) DavException(org.apache.jackrabbit.webdav.DavException) ArrayList(java.util.ArrayList) LockInfo(org.apache.jackrabbit.webdav.lock.LockInfo) LockDiscovery(org.apache.jackrabbit.webdav.lock.LockDiscovery)

Example 32 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class AbstractWebdavServlet method service.

/**
 * Service the given request.
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    WebdavRequest webdavRequest = new WebdavRequestImpl(request, getLocatorFactory(), isCreateAbsoluteURI());
    // DeltaV requires 'Cache-Control' header for all methods except 'VERSION-CONTROL' and 'REPORT'.
    int methodCode = DavMethods.getMethodCode(request.getMethod());
    boolean noCache = DavMethods.isDeltaVMethod(webdavRequest) && !(DavMethods.DAV_VERSION_CONTROL == methodCode || DavMethods.DAV_REPORT == methodCode);
    WebdavResponse webdavResponse = new WebdavResponseImpl(response, noCache);
    try {
        WebdavRequestContextHolder.setContext(new WebdavRequestContextImpl(webdavRequest));
        // make sure there is a authenticated user
        if (!getDavSessionProvider().attachSession(webdavRequest)) {
            return;
        }
        // perform referrer host checks if CSRF protection is enabled
        if (!csrfUtil.isValidRequest(webdavRequest)) {
            webdavResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        // support it (see JCR-4166)
        if (!(webdavRequest instanceof ContentCodingAwareRequest)) {
            List<String> ces = getContentCodings(request);
            if (!ces.isEmpty()) {
                webdavResponse.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
                webdavResponse.setHeader("Accept-Encoding", "identity");
                webdavResponse.setContentType("text/plain; charset=UTF-8");
                webdavResponse.getWriter().println("Content-Encodings not supported, but received: " + ces);
                webdavResponse.getWriter().flush();
            }
        }
        // check matching if=header for lock-token relevant operations
        DavResource resource = getResourceFactory().createResource(webdavRequest.getRequestLocator(), webdavRequest, webdavResponse);
        if (!isPreconditionValid(webdavRequest, resource)) {
            webdavResponse.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
        if (!execute(webdavRequest, webdavResponse, methodCode, resource)) {
            super.service(request, response);
        }
    } catch (DavException e) {
        handleDavException(webdavRequest, webdavResponse, e);
    } catch (IOException ex) {
        Throwable cause = ex.getCause();
        if (cause instanceof DavException) {
            handleDavException(webdavRequest, webdavResponse, (DavException) cause);
        } else {
            throw ex;
        }
    } finally {
        WebdavRequestContextHolder.clearContext();
        getDavSessionProvider().releaseSession(webdavRequest);
    }
}
Also used : DavResource(org.apache.jackrabbit.webdav.DavResource) WebdavResponseImpl(org.apache.jackrabbit.webdav.WebdavResponseImpl) DavException(org.apache.jackrabbit.webdav.DavException) WebdavRequestImpl(org.apache.jackrabbit.webdav.WebdavRequestImpl) IOException(java.io.IOException) ContentCodingAwareRequest(org.apache.jackrabbit.webdav.ContentCodingAwareRequest) WebdavRequest(org.apache.jackrabbit.webdav.WebdavRequest) WebdavResponse(org.apache.jackrabbit.webdav.WebdavResponse)

Example 33 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class SupportedPrivilege method getSupportedPrivilege.

/**
 * Factory method to create/retrieve a <code>SupportedPrivilege</code> from the given
 * DAV:privilege element.
 *
 * @param privilege
 * @return
 */
static SupportedPrivilege getSupportedPrivilege(Element supportedPrivilege) throws DavException {
    if (!DomUtil.matches(supportedPrivilege, XML_SUPPORTED_PRIVILEGE, SecurityConstants.NAMESPACE)) {
        throw new DavException(DavServletResponse.SC_BAD_REQUEST, "DAV:supported-privilege element expected.");
    }
    boolean isAbstract = false;
    Privilege privilege = null;
    String description = null;
    String descriptionLanguage = null;
    List<SupportedPrivilege> sp = new ArrayList<SupportedPrivilege>();
    ElementIterator children = DomUtil.getChildren(supportedPrivilege);
    while (children.hasNext()) {
        Element child = children.next();
        if (child.getLocalName().equals(XML_ABSTRACT)) {
            isAbstract = true;
        } else if (child.getLocalName().equals(Privilege.XML_PRIVILEGE)) {
            privilege = Privilege.getPrivilege(child);
        } else if (child.getLocalName().equals(XML_DESCRIPTION)) {
            description = child.getLocalName();
            if (child.hasAttribute(descriptionLanguage)) {
                descriptionLanguage = child.getAttribute(descriptionLanguage);
            }
        } else if (child.getLocalName().equals(XML_SUPPORTED_PRIVILEGE)) {
            sp.add(getSupportedPrivilege(child));
        }
    }
    return new SupportedPrivilege(privilege, description, descriptionLanguage, isAbstract, sp.toArray(new SupportedPrivilege[sp.size()]));
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

Example 34 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class LatestActivityVersionReport method init.

/**
 * Check all the preconditions for this report.
 *
 * @throws DavException if a precondition is not met.
 * @see Report#init(DavResource, ReportInfo)
 */
public void init(DavResource resource, ReportInfo info) throws DavException {
    // validate info
    if (!getType().isRequestedReportType(info)) {
        throw new DavException(DavServletResponse.SC_BAD_REQUEST, "DAV:latest-activity-version element expected.");
    }
    // make sure the report is applied to a vh-resource
    if (resource != null && (resource instanceof VersionHistoryResource)) {
        vhResource = (VersionHistoryResource) resource;
    } else {
        throw new DavException(DavServletResponse.SC_BAD_REQUEST, "DAV:latest-activity-version report can only be created for a version history resource.");
    }
    // make sure the DAV:href element inside the request body points to
    // an activity resource (precondition for this report).
    String activityHref = normalizeResourceHref(DomUtil.getText(info.getContentElement(DavConstants.XML_HREF, DavConstants.NAMESPACE)));
    DavResourceLocator vhLocator = resource.getLocator();
    DavResourceLocator activityLocator = vhLocator.getFactory().createResourceLocator(vhLocator.getPrefix(), activityHref);
    activity = resource.getFactory().createResource(activityLocator, resource.getSession());
    if (!(activity instanceof ActivityResource)) {
        throw new DavException(DavServletResponse.SC_BAD_REQUEST, "DAV:latest-activity-version report: The DAV:href in the request body MUST identify an activity.");
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) ActivityResource(org.apache.jackrabbit.webdav.version.ActivityResource) VersionHistoryResource(org.apache.jackrabbit.webdav.version.VersionHistoryResource) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 35 with DavException

use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.

the class ReportType method createReport.

/**
 * Creates a new {@link Report} with this type.
 *
 * @return
 * @throws DavException
 */
public Report createReport(DeltaVResource resource, ReportInfo info) throws DavException {
    try {
        Report report = reportClass.newInstance();
        report.init(resource, info);
        return report;
    } catch (IllegalAccessException e) {
        // should never occur
        throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create new report (" + reportClass.getName() + ") from class: " + e.getMessage());
    } catch (InstantiationException e) {
        // should never occur
        throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to create new report (" + reportClass.getName() + ") from class: " + e.getMessage());
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException)

Aggregations

DavException (org.apache.jackrabbit.webdav.DavException)157 RepositoryException (javax.jcr.RepositoryException)89 IOException (java.io.IOException)61 HttpResponse (org.apache.http.HttpResponse)47 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)34 DavResource (org.apache.jackrabbit.webdav.DavResource)30 Element (org.w3c.dom.Element)29 ArrayList (java.util.ArrayList)25 JcrDavException (org.apache.jackrabbit.webdav.jcr.JcrDavException)25 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)18 Node (javax.jcr.Node)16 DavPropertyNameSet (org.apache.jackrabbit.webdav.property.DavPropertyNameSet)15 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)14 ItemNotFoundException (javax.jcr.ItemNotFoundException)13 HttpPropfind (org.apache.jackrabbit.webdav.client.methods.HttpPropfind)12 ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)12 Path (java.nio.file.Path)11 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)10 Test (org.junit.Test)9 HrefProperty (org.apache.jackrabbit.webdav.property.HrefProperty)8