Search in sources :

Example 1 with LockDiscovery

use of org.apache.jackrabbit.webdav.lock.LockDiscovery in project nuxeo-filesystem-connectors by nuxeo.

the class WebDavClientTest method testPropFindOnLockedFile.

@Test
public void testPropFindOnLockedFile() throws Exception {
    String fileUri = ROOT_URI + "quality.jpg";
    HttpLock request = new HttpLock(fileUri, new LockInfo(Scope.EXCLUSIVE, Type.WRITE, USERNAME, 10000L, false));
    try (CloseableHttpResponse response = client.execute(request, context)) {
        request.checkSuccess(response);
    }
    HttpPropfind request2 = new HttpPropfind(fileUri, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
    try (CloseableHttpResponse response = client.execute(request2, context)) {
        MultiStatus multiStatus = request2.getResponseBodyAsMultiStatus(response);
        MultiStatusResponse[] responses = multiStatus.getResponses();
        assertEquals(1L, responses.length);
        MultiStatusResponse resp = responses[0];
        DavProperty<?> pLockDiscovery = resp.getProperties(200).get(DavConstants.PROPERTY_LOCKDISCOVERY);
        Element eLockDiscovery = (Element) ((Element) pLockDiscovery.getValue()).getParentNode();
        LockDiscovery lockDiscovery = LockDiscovery.createFromXml(eLockDiscovery);
        assertEquals(USERNAME, lockDiscovery.getValue().get(0).getOwner());
    }
}
Also used : HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) Element(org.w3c.dom.Element) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) MultiStatus(org.apache.jackrabbit.webdav.MultiStatus) LockInfo(org.apache.jackrabbit.webdav.lock.LockInfo) HttpLock(org.apache.jackrabbit.webdav.client.methods.HttpLock) LockDiscovery(org.apache.jackrabbit.webdav.lock.LockDiscovery) Test(org.junit.Test)

Example 2 with LockDiscovery

use of org.apache.jackrabbit.webdav.lock.LockDiscovery in project jackrabbit by apache.

the class DavResourceImpl method initProperties.

/**
 * Fill the set of properties
 */
protected void initProperties() {
    if (!exists() || propsInitialized) {
        return;
    }
    try {
        config.getPropertyManager().exportProperties(getPropertyExportContext(), isCollection());
    } catch (RepositoryException e) {
        log.warn("Error while accessing resource properties", e);
    }
    // set (or reset) fundamental properties
    if (getDisplayName() != null) {
        properties.add(new DefaultDavProperty<String>(DavPropertyName.DISPLAYNAME, getDisplayName()));
    }
    if (isCollection()) {
        properties.add(new ResourceType(ResourceType.COLLECTION));
        // Windows XP support
        properties.add(new DefaultDavProperty<String>(DavPropertyName.ISCOLLECTION, "1"));
    } else {
        properties.add(new ResourceType(ResourceType.DEFAULT_RESOURCE));
        // Windows XP support
        properties.add(new DefaultDavProperty<String>(DavPropertyName.ISCOLLECTION, "0"));
    }
    if (rfc4122Uri != null) {
        properties.add(new HrefProperty(BindConstants.RESOURCEID, rfc4122Uri, true));
    }
    Set<ParentElement> parentElements = getParentElements();
    if (!parentElements.isEmpty()) {
        properties.add(new ParentSet(parentElements));
    }
    /* set current lock information. If no lock is set to this resource,
        an empty lock discovery will be returned in the response. */
    properties.add(new LockDiscovery(getLock(Type.WRITE, Scope.EXCLUSIVE)));
    /* lock support information: all locks are lockable. */
    SupportedLock supportedLock = new SupportedLock();
    supportedLock.addEntry(Type.WRITE, Scope.EXCLUSIVE);
    properties.add(supportedLock);
    propsInitialized = true;
}
Also used : ParentSet(org.apache.jackrabbit.webdav.bind.ParentSet) HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) SupportedLock(org.apache.jackrabbit.webdav.lock.SupportedLock) RepositoryException(javax.jcr.RepositoryException) ResourceType(org.apache.jackrabbit.webdav.property.ResourceType) ParentElement(org.apache.jackrabbit.webdav.bind.ParentElement) LockDiscovery(org.apache.jackrabbit.webdav.lock.LockDiscovery)

Example 3 with LockDiscovery

use of org.apache.jackrabbit.webdav.lock.LockDiscovery 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 4 with LockDiscovery

use of org.apache.jackrabbit.webdav.lock.LockDiscovery in project jackrabbit by apache.

the class RepositoryServiceImpl method getLockInfo.

@Override
public LockInfo getLockInfo(SessionInfo sessionInfo, NodeId nodeId) throws RepositoryException {
    // set of Dav-properties to be retrieved
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(DavPropertyName.LOCKDISCOVERY);
    nameSet.add(JcrRemotingConstants.JCR_PARENT_LN, ItemResourceConstants.NAMESPACE);
    HttpPropfind request = null;
    try {
        String uri = getItemUri(nodeId, sessionInfo);
        request = new HttpPropfind(uri, nameSet, DEPTH_0);
        initMethod(request, sessionInfo, false);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        MultiStatusResponse[] mresponses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (mresponses.length != 1) {
            throw new ItemNotFoundException("Unable to retrieve the LockInfo. No such node " + saveGetIdString(nodeId, sessionInfo));
        }
        DavPropertySet ps = mresponses[0].getProperties(DavServletResponse.SC_OK);
        if (ps.contains(DavPropertyName.LOCKDISCOVERY)) {
            DavProperty<?> p = ps.get(DavPropertyName.LOCKDISCOVERY);
            LockDiscovery ld = LockDiscovery.createFromXml(p.toXml(DomUtil.createDocument()));
            NodeId parentId = getParentId(uri, ps, sessionInfo);
            return retrieveLockInfo(ld, sessionInfo, nodeId, parentId);
        } else {
            // no lock present
            log.debug("No Lock present on node with id " + saveGetIdString(nodeId, sessionInfo));
            return null;
        }
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (ParserConfigurationException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) LockDiscovery(org.apache.jackrabbit.webdav.lock.LockDiscovery) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) NodeId(org.apache.jackrabbit.spi.NodeId) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 5 with LockDiscovery

use of org.apache.jackrabbit.webdav.lock.LockDiscovery in project jackrabbit by apache.

the class WebdavResponseImpl method sendRefreshLockResponse.

/**
 * Send response body for a lock request that was intended to refresh one
 * or several locks.
 *
 * @param locks
 * @throws java.io.IOException
 * @see DavServletResponse#sendRefreshLockResponse(org.apache.jackrabbit.webdav.lock.ActiveLock[])
 */
public void sendRefreshLockResponse(ActiveLock[] locks) throws IOException {
    DavPropertySet propSet = new DavPropertySet();
    propSet.add(new LockDiscovery(locks));
    sendXmlResponse(propSet, SC_OK);
}
Also used : DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) LockDiscovery(org.apache.jackrabbit.webdav.lock.LockDiscovery)

Aggregations

LockDiscovery (org.apache.jackrabbit.webdav.lock.LockDiscovery)7 RepositoryException (javax.jcr.RepositoryException)3 DavException (org.apache.jackrabbit.webdav.DavException)3 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)3 IOException (java.io.IOException)2 HttpResponse (org.apache.http.HttpResponse)2 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)2 HttpLock (org.apache.jackrabbit.webdav.client.methods.HttpLock)2 HttpPropfind (org.apache.jackrabbit.webdav.client.methods.HttpPropfind)2 LockInfo (org.apache.jackrabbit.webdav.lock.LockInfo)2 ResourceType (org.apache.jackrabbit.webdav.property.ResourceType)2 ArrayList (java.util.ArrayList)1 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 AuthScope (org.apache.http.auth.AuthScope)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 LockInfo (org.apache.jackrabbit.spi.LockInfo)1 NodeId (org.apache.jackrabbit.spi.NodeId)1 MultiStatus (org.apache.jackrabbit.webdav.MultiStatus)1 ParentElement (org.apache.jackrabbit.webdav.bind.ParentElement)1