use of org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock in project jackrabbit by apache.
the class DavResourceImpl method getLock.
/**
* @see DavResource#getLock(Type, Scope)
*/
public ActiveLock getLock(Type type, Scope scope) {
ActiveLock lock = null;
if (exists() && Type.WRITE.equals(type) && Scope.EXCLUSIVE.equals(scope)) {
// try to retrieve the repository lock information first
try {
if (node.isLocked()) {
Lock jcrLock = node.getLock();
if (jcrLock != null && jcrLock.isLive()) {
lock = new JcrActiveLock(jcrLock);
String lockroot = locator.getFactory().createResourceLocator(locator.getPrefix(), locator.getWorkspacePath(), jcrLock.getNode().getPath(), false).getHref(false);
lock.setLockroot(lockroot);
}
}
} catch (RepositoryException e) {
// LockException (no lock applies) >> should never occur
// RepositoryException, AccessDeniedException or another error >> ignore
}
// could not retrieve a jcr-lock. test if a simple webdav lock is present.
if (lock == null) {
lock = lockManager.getLock(type, scope, this);
}
}
return lock;
}
use of org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock in project jackrabbit by apache.
the class LockTimeOutFormatTest method testfmt.
private void testfmt(long jcrtimeout, String expectedString) throws RepositoryException, URISyntaxException, ParserConfigurationException {
Lock l = new TestLock(jcrtimeout);
JcrActiveLock al = new JcrActiveLock(l);
Document d = DomUtil.createDocument();
Element activeLock = al.toXml(d);
assertEquals("activelock", activeLock.getLocalName());
NodeList nl = activeLock.getElementsByTagNameNS("DAV:", "timeout");
if (expectedString == null) {
assertEquals(0, nl.getLength());
} else {
assertEquals(1, nl.getLength());
Element timeout = (Element) nl.item(0);
String t = DomUtil.getText(timeout);
assertEquals(expectedString, t);
}
}
use of org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock in project jackrabbit by apache.
the class DefaultItemCollection method lock.
/**
* Creates a lock on this resource by locking the underlying
* {@link javax.jcr.Node node}. Except for the {@link org.apache.jackrabbit.webdav.lock.LockInfo#isDeep()} }
* all information included in the <code>LockInfo</code> object is ignored.
* Lock timeout is defined by JCR implementation.
*
* @param reqLockInfo
* @return lock object representing the lock created on this resource.
* @throws org.apache.jackrabbit.webdav.DavException
* @see org.apache.jackrabbit.webdav.DavResource#lock(org.apache.jackrabbit.webdav.lock.LockInfo)
* @see Node#lock(boolean, boolean)
*/
@Override
public ActiveLock lock(LockInfo reqLockInfo) throws DavException {
if (!isLockable(reqLockInfo.getType(), reqLockInfo.getScope())) {
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);
}
if (Type.WRITE.equals(reqLockInfo.getType())) {
if (!exists()) {
log.warn("Cannot create a write lock for non-existing JCR node (" + getResourcePath() + ")");
throw new DavException(DavServletResponse.SC_NOT_FOUND);
}
try {
boolean sessionScoped = EXCLUSIVE_SESSION.equals(reqLockInfo.getScope());
long timeout = reqLockInfo.getTimeout();
if (timeout == LockInfo.INFINITE_TIMEOUT) {
timeout = Long.MAX_VALUE;
} else {
timeout = timeout / 1000;
}
javax.jcr.lock.LockManager lockMgr = getRepositorySession().getWorkspace().getLockManager();
Lock jcrLock = lockMgr.lock((item).getPath(), reqLockInfo.isDeep(), sessionScoped, timeout, reqLockInfo.getOwner());
ActiveLock lock = new JcrActiveLock(jcrLock);
// add reference to DAVSession for this lock
getSession().addReference(lock.getToken());
return lock;
} catch (RepositoryException e) {
// UnsupportedRepositoryOperationException should not occur...
throw new JcrDavException(e);
}
} else {
return super.lock(reqLockInfo);
}
}
use of org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock in project jackrabbit by apache.
the class DavResourceImpl method lock.
/**
* @see DavResource#lock(LockInfo)
*/
public ActiveLock lock(LockInfo lockInfo) throws DavException {
ActiveLock lock = null;
if (isLockable(lockInfo.getType(), lockInfo.getScope())) {
// TODO: deal with existing locks, that may have been created, before the node was jcr-lockable...
if (isJcrLockable()) {
try {
javax.jcr.lock.LockManager lockMgr = node.getSession().getWorkspace().getLockManager();
long timeout = lockInfo.getTimeout();
if (timeout == LockInfo.INFINITE_TIMEOUT) {
timeout = Long.MAX_VALUE;
} else {
timeout = timeout / 1000;
}
// try to execute the lock operation
Lock jcrLock = lockMgr.lock(node.getPath(), lockInfo.isDeep(), false, timeout, lockInfo.getOwner());
if (jcrLock != null) {
lock = new JcrActiveLock(jcrLock);
}
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
} else {
// create a new webdav lock
lock = lockManager.createLock(lockInfo, this);
}
} else {
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "Unsupported lock type or scope.");
}
return lock;
}
use of org.apache.jackrabbit.webdav.jcr.lock.JcrActiveLock in project jackrabbit by apache.
the class DefaultItemCollection method refreshLock.
/**
* Refreshes the lock on this resource. With this implementation the
* {@link javax.jcr.lock lock} present on the underlying {@link javax.jcr.Node node}
* is refreshed. The timeout indicated by the <code>LockInfo</code>
* object is ignored.
*
* @param reqLockInfo LockInfo as build from the request.
* @param lockToken
* @return the updated lock info object.
* @throws org.apache.jackrabbit.webdav.DavException in case the lock could not be refreshed.
* @see org.apache.jackrabbit.webdav.DavResource#refreshLock(org.apache.jackrabbit.webdav.lock.LockInfo, String)
* @see javax.jcr.lock.Lock#refresh()
*/
@Override
public ActiveLock refreshLock(LockInfo reqLockInfo, String lockToken) throws DavException {
if (lockToken == null) {
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED);
}
ActiveLock lock = getLock(reqLockInfo.getType(), reqLockInfo.getScope());
if (lock == null) {
throw new DavException(DavServletResponse.SC_PRECONDITION_FAILED, "No lock with the given scope/type present on this resource.");
}
if (Type.WRITE.equals(lock.getType())) {
try {
Lock jcrLock = ((Node) item).getLock();
jcrLock.refresh();
return new JcrActiveLock(jcrLock);
} catch (RepositoryException e) {
/*
NOTE: LockException is only thrown by Lock.refresh()
the lock exception thrown by Node.getLock() was circumvented
by the init test if there is a lock applied...
NOTE: UnsupportedRepositoryOperationException should not occur
*/
throw new JcrDavException(e);
}
} else {
return super.refreshLock(reqLockInfo, lockToken);
}
}
Aggregations