Interface Lock


public interface Lock
Defines methods that WebDAV Class 2 compliant server hierarchy items must implement.

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.

  • Method Details

    • getActiveLocks

      List<LockInfo> getActiveLocks() throws ServerException
      Gets the array of all locks for this item.

      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.

      Returns:
      Array of locks.
      Throws:
      ServerException - In case of an error.

      The code below is part of Collection Synchronization sample provided with the SDK.
          @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());
          }
      
      
    • lock

      LockResult lock(boolean shared, boolean deep, long timeout, String owner) throws LockedException, MultistatusException, ServerException
      Locks this item.

      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.

      Parameters:
      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.
      Returns:
      Actually applied lock (Server may modify timeout).
      Throws:
      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

      The code below is part of Collection Synchronization sample provided with the SDK.
          @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);
          }
      
      
    • refreshLock

      RefreshLockResult refreshLock(String token, long timeout) throws PreconditionFailedException, ServerException
      Updates lock timeout information on this item.
      Parameters:
      token - The lock token associated with a lock.
      timeout - Lock expiration time in seconds. Negative value means never.
      Returns:
      Actually applied lock (Server may modify timeout).
      Throws:
      PreconditionFailedException - Included lock token was not enforceable on this item.
      ServerException - In case of an error.

      The code below is part of Collection Synchronization sample provided with the SDK.
          @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());
          }
      
      
    • unlock

      void unlock(String lockToken) throws PreconditionFailedException, ServerException
      Removes lock with the specified token from this item.

      If this lock included more than one hierarchy item, the lock is removed from all items included in the lock.

      Parameters:
      lockToken - Lock with this token should be removed from the item.
      Throws:
      PreconditionFailedException - Included lock token was not enforceable on this item.
      ServerException - In case of an error.

      The code below is part of Collection Synchronization sample provided with the SDK.
          @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();
              }
          }