Search in sources :

Example 1 with HttpLock

use of org.apache.jackrabbit.webdav.client.methods.HttpLock in project jackrabbit by apache.

the class RFC4918IfHeaderTest method testPutIfLockToken.

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

use of org.apache.jackrabbit.webdav.client.methods.HttpLock in project jackrabbit by apache.

the class RepositoryServiceImpl method refreshLock.

@Override
public void refreshLock(SessionInfo sessionInfo, NodeId nodeId) throws RepositoryException {
    checkSessionInfo(sessionInfo);
    String uri = getItemUri(nodeId, sessionInfo);
    // since sessionInfo does not allow to retrieve token by NodeId,
    // pass all available lock tokens to the LOCK method (TODO: correct?)
    Set<String> allLockTokens = ((SessionInfoImpl) sessionInfo).getAllLockTokens();
    String[] locktokens = allLockTokens.toArray(new String[allLockTokens.size()]);
    HttpLock httpLock = null;
    try {
        httpLock = new HttpLock(uri, INFINITE_TIMEOUT, locktokens);
        execute(httpLock, sessionInfo);
    } finally {
        if (httpLock != null) {
            httpLock.releaseConnection();
        }
    }
}
Also used : HttpLock(org.apache.jackrabbit.webdav.client.methods.HttpLock)

Example 3 with HttpLock

use of org.apache.jackrabbit.webdav.client.methods.HttpLock in project jackrabbit by apache.

the class RepositoryServiceImpl method lock.

@Override
public LockInfo lock(SessionInfo sessionInfo, NodeId nodeId, boolean deep, boolean sessionScoped, long timeoutHint, String ownerHint) throws RepositoryException {
    HttpLock request = null;
    try {
        checkSessionInfo(sessionInfo);
        long davTimeout = (timeoutHint == Long.MAX_VALUE) ? INFINITE_TIMEOUT : timeoutHint * 1000;
        String ownerInfo = (ownerHint == null) ? sessionInfo.getUserID() : ownerHint;
        String uri = getItemUri(nodeId, sessionInfo);
        Scope scope = (sessionScoped) ? ItemResourceConstants.EXCLUSIVE_SESSION : Scope.EXCLUSIVE;
        request = new HttpLock(uri, new org.apache.jackrabbit.webdav.lock.LockInfo(scope, Type.WRITE, ownerInfo, davTimeout, deep));
        HttpResponse response = execute(request, sessionInfo);
        String lockToken = request.getLockToken(response);
        ((SessionInfoImpl) sessionInfo).addLockToken(lockToken, sessionScoped);
        LockDiscovery disc = request.getResponseBodyAsLockDiscovery(response);
        return retrieveLockInfo(disc, sessionInfo, nodeId, null);
    } catch (IOException 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) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) LockDiscovery(org.apache.jackrabbit.webdav.lock.LockDiscovery) Scope(org.apache.jackrabbit.webdav.lock.Scope) LockInfo(org.apache.jackrabbit.spi.LockInfo) HttpLock(org.apache.jackrabbit.webdav.client.methods.HttpLock)

Aggregations

HttpLock (org.apache.jackrabbit.webdav.client.methods.HttpLock)3 HttpResponse (org.apache.http.HttpResponse)2 IOException (java.io.IOException)1 URI (java.net.URI)1 RepositoryException (javax.jcr.RepositoryException)1 HttpDelete (org.apache.http.client.methods.HttpDelete)1 HttpPut (org.apache.http.client.methods.HttpPut)1 StringEntity (org.apache.http.entity.StringEntity)1 LockInfo (org.apache.jackrabbit.spi.LockInfo)1 DavException (org.apache.jackrabbit.webdav.DavException)1 LockDiscovery (org.apache.jackrabbit.webdav.lock.LockDiscovery)1 LockInfo (org.apache.jackrabbit.webdav.lock.LockInfo)1 Scope (org.apache.jackrabbit.webdav.lock.Scope)1