package com.oxygenxml.examples.webdav;

import java.net.URL;

import ro.sync.ecss.extensions.api.webapp.plugin.LockHandlerWithContext;
import ro.sync.exml.plugin.lock.LockException;
import ro.sync.net.protocol.http.WebdavLockHelper;

/**
 * Lock handler for the WebDAV protocol.
 * 
 * @author cristi_talau
 */
public class WebdavLockHandler extends LockHandlerWithContext {

  /**
   * @see LockHandlerWithContext#isSaveAllowed(String, URL, int)
   */
  @Override
  public boolean isSaveAllowed(String sessionId, URL url, int timeoutSeconds) {
    url = WebdavUrlStreamHandler.addCredentials(sessionId, url);
    return new WebdavLockHelper().isSaveAllowed(sessionId, url, timeoutSeconds);
  }

  /**
   * @see LockHandlerWithContext#unlock(String, URL)
   */
  @Override
  public void unlock(String sessionId, URL url) throws LockException {
    url = WebdavUrlStreamHandler.addCredentials(sessionId, url);
    new WebdavLockHelper().unlock(sessionId, url);
  }

  /**
   * @see LockHandlerWithContext#updateLock(String, URL, int)
   */
  @Override
  public void updateLock(String sessionId, URL url, int timeoutSeconds) throws LockException {
    url = WebdavUrlStreamHandler.addCredentials(sessionId, url);
    new WebdavLockHelper().updateLock(sessionId, url, timeoutSeconds);
  }

  /**
   * @see LockHandlerWithContext#isLockEnabled()
   */
  @Override
  public boolean isLockEnabled() {
    return true;
  }
}

@@ -33,16 +33,56 @@
 ebdav;%0A%0A
+import java.net.PasswordAuthentication;%0A
 import j
@@ -1248,38 +1248,317 @@
 l);%0A    
-new WebdavLockHelper()
+WebdavLockHelper lockHelper = new WebdavLockHelper();%0A%0A    PasswordAuthentication passwordAuthentication = WebdavUrlStreamHandler.credentials.get(sessionId);%0A    if (passwordAuthentication != null) %7B%0A      lockHelper.setLockOwner(sessionId, passwordAuthentication.getUserName());%0A    %7D%0A%0A    lockHelper
 .updateL

Set the lock owner before locking the document.