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