public interface Lock
Classes that implement this interface represent WebDAV Class 2 hierarchy items. To create WebDAV Class 2 compliant server you must implement this interface on your file items and folder items.
Modifier and Type | Method and Description |
---|---|
List<LockInfo> |
getActiveLocks()
Gets the array of all locks for this item.
|
LockResult |
lock(boolean shared,
boolean deep,
long timeout,
String owner)
Locks this item.
|
RefreshLockResult |
refreshLock(String token,
long timeout)
Updates lock timeout information on this item.
|
void |
unlock(String lockToken)
Removes lock with the specified token from this item.
|
List<LockInfo> getActiveLocks() throws ServerException
This method must return all locks for the item including deep locks on any of the parent folders.
All fields of each LockInfo
structure in the array must be set.
ServerException
- In case of an error.
@Override public List<LockInfo> getActiveLocks() throws ServerException { if (activeLocks == null) { String activeLocksJson = ExtendedAttributesExtension.getExtendedAttribute(getFullPath().toString(), activeLocksAttribute); activeLocks = new ArrayList<>(SerializationUtils.deserializeList(LockInfo.class, activeLocksJson)); } else { activeLocks = new LinkedList<>(); } return activeLocks.stream() .filter(x -> System.currentTimeMillis() < x.getTimeout()) .map(lock -> new LockInfo( lock.isShared(), lock.isDeep(), lock.getToken(), (lock.getTimeout() < 0 || lock.getTimeout() == Long.MAX_VALUE) ? lock.getTimeout() : (lock.getTimeout() - System.currentTimeMillis()) / 1000, lock.getOwner())) .collect(Collectors.toList()); }
LockResult lock(boolean shared, boolean deep, long timeout, String owner) throws LockedException, MultistatusException, ServerException
In your Lock
implementation you must generate lock token and create LockResult
class instance.
You must also associate generated token with the hierarchy item in your repository during this call.
The token is sent to the WebDAV client.
shared
- Indicates whether a lock is shared or exclusive.deep
- Indicates whether a lock is enforceable on the subtree.timeout
- Lock expiration time in seconds. Negative value means never.owner
- Provides information about the principal taking out a lock.LockedException
- The item is locked, so the method has been rejected.MultistatusException
- Errors have occured during processing of the subtree.ServerException
- In case of an error
@Override public LockResult lock(boolean shared, boolean deep, long timeout, String owner) throws LockedException, MultistatusException, ServerException { if (hasLock(shared)) { throw new LockedException(); } String token = UUID.randomUUID().toString(); if (timeout < 0 || timeout == Long.MAX_VALUE) { // If timeout is absent or infinity timeout requested, // grant 5 minute lock. timeout = 300; } long expires = System.currentTimeMillis() + timeout * 1000; LockInfo lockInfo = new LockInfo(shared, deep, token, expires, owner); activeLocks.add(lockInfo); ExtendedAttributesExtension.setExtendedAttribute(getFullPath().toString(), activeLocksAttribute, SerializationUtils.serialize(activeLocks)); getEngine().getWebSocketServer().notifyLocked(getPath(), getWebSocketID()); return new LockResult(token, timeout); }
RefreshLockResult refreshLock(String token, long timeout) throws PreconditionFailedException, ServerException
token
- The lock token associated with a lock.timeout
- Lock expiration time in seconds. Negative value means never.PreconditionFailedException
- Included lock token was not enforceable on this item.ServerException
- In case of an error.
@Override public RefreshLockResult refreshLock(String token, long timeout) throws PreconditionFailedException, ServerException { getActiveLocks(); LockInfo lockInfo = activeLocks.stream().filter(x -> x.getToken().equals(token)).findFirst().orElse(null); if (lockInfo == null) { throw new PreconditionFailedException(); } if (timeout < 0 || timeout == Long.MAX_VALUE) { // If timeout is absent or infinity timeout requested, // grant 5 minute lock. timeout = 300; } long expires = System.currentTimeMillis() + timeout * 1000; lockInfo.setTimeout(expires); ExtendedAttributesExtension.setExtendedAttribute(getFullPath().toString(), activeLocksAttribute, SerializationUtils.serialize(activeLocks)); getEngine().getWebSocketServer().notifyLocked(getPath(), getWebSocketID()); return new RefreshLockResult(lockInfo.isShared(), lockInfo.isDeep(), timeout, lockInfo.getOwner()); }
void unlock(String lockToken) throws PreconditionFailedException, ServerException
If this lock included more than one hierarchy item, the lock is removed from all items included in the lock.
lockToken
- Lock with this token should be removed from the item.PreconditionFailedException
- Included lock token was not enforceable on this item.ServerException
- In case of an error.
@Override public void unlock(String lockToken) throws PreconditionFailedException, ServerException { getActiveLocks(); LockInfo lock = activeLocks.stream().filter(x -> x.getToken().equals(lockToken)).findFirst().orElse(null); if (lock != null) { activeLocks.remove(lock); if (!activeLocks.isEmpty()) { ExtendedAttributesExtension.setExtendedAttribute(getFullPath().toString(), activeLocksAttribute, SerializationUtils.serialize(activeLocks)); } else { ExtendedAttributesExtension.deleteExtendedAttribute(getFullPath().toString(), activeLocksAttribute); } getEngine().getWebSocketServer().notifyUnlocked(getPath(), getWebSocketID()); } else { throw new PreconditionFailedException(); } }
Copyright © ITHit. All Rights Reserved.