Search in sources :

Example 6 with LockInfo

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

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

the class RFC4918IfHeaderTest method testPutIfLockToken.

public void testPutIfLockToken() throws IOException, URISyntaxException {
    String testuri = this.root + "iflocktest";
    String locktoken = null;
    HttpRequestBase requestBase = null;
    try {
        requestBase = new HttpPut(testuri);
        ((HttpPut) requestBase).setEntity(new StringEntity("1"));
        int status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 201 || status == 204);
        requestBase.releaseConnection();
        requestBase = new HttpLock(testuri, new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "testcase", 10000, true));
        HttpResponse response = this.client.execute(requestBase, this.context);
        status = response.getStatusLine().getStatusCode();
        assertEquals("status", 200, status);
        locktoken = ((HttpLock) requestBase).getLockToken(response);
        assertNotNull(locktoken);
        requestBase.releaseConnection();
        // try to overwrite without lock token
        requestBase = new HttpPut(testuri);
        ((HttpPut) requestBase).setEntity(new StringEntity("2"));
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertEquals("status: " + status, 423, status);
        requestBase.releaseConnection();
        // try to overwrite using bad lock token
        requestBase = new HttpPut(testuri);
        ((HttpPut) requestBase).setEntity(new StringEntity("2"));
        requestBase.setHeader("If", "(<" + "DAV:foobar" + ">)");
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertEquals("status: " + status, 412, status);
        requestBase.releaseConnection();
        // try to overwrite using correct lock token, using  No-Tag-list format
        requestBase = new HttpPut(testuri);
        ((HttpPut) requestBase).setEntity(new StringEntity("2"));
        requestBase.setHeader("If", "(<" + locktoken + ">)");
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 204);
        requestBase.releaseConnection();
        // try to overwrite using correct lock token, using Tagged-list format
        // and full URI
        requestBase = new HttpPut(testuri);
        ((HttpPut) requestBase).setEntity(new StringEntity("3"));
        requestBase.setHeader("If", "<" + testuri + ">" + "(<" + locktoken + ">)");
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 204);
        requestBase.releaseConnection();
        // try to overwrite using correct lock token, using Tagged-list format
        // and absolute path only
        requestBase = new HttpPut(testuri);
        ((HttpPut) requestBase).setEntity(new StringEntity("4"));
        requestBase.setHeader("If", "<" + new URI(testuri).getRawPath() + ">" + "(<" + locktoken + ">)");
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 204);
        requestBase.releaseConnection();
        // try to overwrite using correct lock token, using Tagged-list format
        // and bad path
        requestBase = new HttpPut(testuri);
        ((HttpPut) requestBase).setEntity(new StringEntity("5"));
        requestBase.setHeader("If", "</foobar>" + "(<" + locktoken + ">)");
        status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 404 || status == 412);
    } finally {
        requestBase.releaseConnection();
        requestBase = new HttpDelete(testuri);
        if (locktoken != null) {
            requestBase.setHeader("If", "(<" + locktoken + ">)");
        }
        int status = this.client.execute(requestBase, this.context).getStatusLine().getStatusCode();
        assertTrue("status: " + status, status == 200 || status == 204 || status == 404);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpResponse(org.apache.http.HttpResponse) LockInfo(org.apache.jackrabbit.webdav.lock.LockInfo) HttpLock(org.apache.jackrabbit.webdav.client.methods.HttpLock) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut)

Example 8 with LockInfo

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

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

the class WebdavRequestImpl method getLockInfo.

/**
 * {@link LockInfo} object encapsulating the information passed with a LOCK
 * request if the LOCK request body was valid. If the request body is
 * missing a 'refresh lock' request is assumed. The {@link LockInfo}
 * then only provides timeout and isDeep property and returns true on
 * {@link org.apache.jackrabbit.webdav.lock.LockInfo#isRefreshLock()}
 *
 * @return lock info object or <code>null</code> if an error occurred while
 *         parsing the request body.
 * @throws DavException throws a 400 (Bad Request) DavException if a request
 * body is present but does not start with a DAV:lockinfo element. Note however,
 * that a non-existing request body is a valid request used to refresh
 * an existing lock.
 * @see DavServletRequest#getLockInfo()
 */
public LockInfo getLockInfo() throws DavException {
    LockInfo lockInfo;
    boolean isDeep = (getDepth(DEPTH_INFINITY) == DEPTH_INFINITY);
    Document requestDocument = getRequestDocument();
    // 'create Lock' request and missing for a 'refresh Lock' request
    if (requestDocument != null) {
        Element root = requestDocument.getDocumentElement();
        if (root.getLocalName().equals(XML_LOCKINFO)) {
            lockInfo = new LockInfo(root, getTimeout(), isDeep);
        } else {
            log.debug("Lock request body must start with a DAV:lockinfo element.");
            throw new DavException(DavServletResponse.SC_BAD_REQUEST);
        }
    } else {
        lockInfo = new LockInfo(null, getTimeout(), isDeep);
    }
    return lockInfo;
}
Also used : Element(org.w3c.dom.Element) LockInfo(org.apache.jackrabbit.webdav.lock.LockInfo) Document(org.w3c.dom.Document)

Example 10 with LockInfo

use of org.apache.jackrabbit.webdav.lock.LockInfo in project archiva by apache.

the class DavResourceTest method testRefreshLockThrowsExceptionIfNoLockIsPresent.

@Test
public void testRefreshLockThrowsExceptionIfNoLockIsPresent() throws Exception {
    LockInfo info = new LockInfo(Scope.EXCLUSIVE, Type.WRITE, "/", 0, false);
    assertEquals(0, resource.getLocks().length);
    try {
        lockManager.refreshLock(info, "notoken", resource);
        fail("Did not throw dav exception");
    } catch (DavException e) {
        assertEquals(DavServletResponse.SC_PRECONDITION_FAILED, e.getErrorCode());
    }
    assertEquals(0, resource.getLocks().length);
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) LockInfo(org.apache.jackrabbit.webdav.lock.LockInfo) Test(org.junit.Test)

Aggregations

LockInfo (org.apache.jackrabbit.webdav.lock.LockInfo)13 Test (org.junit.Test)9 DavException (org.apache.jackrabbit.webdav.DavException)4 HttpLock (org.apache.jackrabbit.webdav.client.methods.HttpLock)3 ActiveLock (org.apache.jackrabbit.webdav.lock.ActiveLock)3 URI (java.net.URI)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 LockDiscovery (org.apache.jackrabbit.webdav.lock.LockDiscovery)2 Element (org.w3c.dom.Element)2 ArrayList (java.util.ArrayList)1 PutMethod (org.apache.commons.httpclient.methods.PutMethod)1 HttpResponse (org.apache.http.HttpResponse)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1 HttpPut (org.apache.http.client.methods.HttpPut)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 StringEntity (org.apache.http.entity.StringEntity)1 MultiStatus (org.apache.jackrabbit.webdav.MultiStatus)1 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)1 DeleteMethod (org.apache.jackrabbit.webdav.client.methods.DeleteMethod)1 HttpPropfind (org.apache.jackrabbit.webdav.client.methods.HttpPropfind)1