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);
}
}
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();
}
}
}
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();
}
}
}
Aggregations