
package com.bradmcevoy.http.webdav;

import com.bradmcevoy.http.*;
import com.bradmcevoy.http.exceptions.PreConditionFailedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bradmcevoy.http.Request.Method;
import com.bradmcevoy.http.exceptions.BadRequestException;
import com.bradmcevoy.http.exceptions.ConflictException;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;

public class UnlockHandler implements ExistingEntityHandler {

    private Logger log = LoggerFactory.getLogger(UnlockHandler.class);

    private final ResourceHandlerHelper resourceHandlerHelper;

    private final WebDavResponseHandler responseHandler;

    public UnlockHandler( ResourceHandlerHelper resourceHandlerHelper, WebDavResponseHandler responseHandler ) {
        this.resourceHandlerHelper = resourceHandlerHelper;
        this.responseHandler = responseHandler;
    }


    public void process( HttpManager httpManager, Request request, Response response ) throws ConflictException, NotAuthorizedException, BadRequestException {
        resourceHandlerHelper.process( httpManager, request, response, this );
    }

    public void processResource( HttpManager manager, Request request, Response response, Resource r ) throws NotAuthorizedException, ConflictException, BadRequestException {
        resourceHandlerHelper.processResource( manager, request, response, r, this );
    }

	@Override
    public void processExistingResource( HttpManager manager, Request request, Response response, Resource resource ) throws NotAuthorizedException, BadRequestException, ConflictException {
        LockableResource r = (LockableResource) resource;
        String sToken = request.getLockTokenHeader();        
        sToken = LockHandler.parseToken(sToken);
        
        // this should be checked in processResource now
        
//       	if( r.getCurrentLock() != null &&
//       			!sToken.equals( r.getCurrentLock().tokenId) &&
//       			isLockedOut( request, resource ))
//    	{
//       		//Should this be unlocked easily? With other tokens?
//    		response.setStatus(Status.SC_LOCKED);
//    	    log.info("cant unlock with token: " + sToken);
//    		return;
//    	}

        
        log.debug("unlocking token: " + sToken);
        try {
            r.unlock( sToken );
            responseHandler.respondNoContent( resource, response, request );
        } catch( PreConditionFailedException ex ) {
            responseHandler.respondPreconditionFailed( request, response, resource );
        }
    }
    
    public String[] getMethods() {
        return new String[]{Method.UNLOCK.code};
    }
    
    @Override
    public boolean isCompatible( Resource handler ) {
        return handler instanceof LockableResource;
    }

    
}

@@ -238,16 +238,61 @@
 ethod;%0D%0A
+import com.bradmcevoy.http.Response.Status;%0D%0A
 import c
@@ -962,24 +962,36 @@
 %0A    %7D%0D%0A%0D%0A%0D%0A
+%09@Override%0D%0A
     public v
@@ -1218,23 +1218,655 @@
 his );%0D%0A
-    %7D%0D%0A
+%09%09%0D%0A        String host = request.getHostHeader();%0D%0A        String url = HttpManager.decodeUrl( request.getAbsolutePath() );%0D%0A%0D%0A        // Find a resource if it exists%0D%0A        Resource r = httpManager.getResourceFactory().getResource( host, url );%0D%0A        if( r != null ) %7B%0D%0A            log.debug( %22locking existing resource: %22 + r.getName() );%0D%0A            processExistingResource( httpManager, request, response, r );%0D%0A        %7D else %7B%0D%0A//            log.debug( %22lock target doesnt exist, attempting lock null..%22 );%0D%0A//            processNonExistingResource( httpManager, request, response, host, url );%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A%09@Override
 %0D%0A    pu
@@ -2574,26 +2574,16 @@
 ce now%0D%0A
-        %0D%0A
 //      
@@ -2678,57 +2678,8 @@
 nId)
- &&%0D%0A//       %09%09%09isLockedOut( request, resource )
 )%0D%0A/
@@ -2687,18 +2687,16 @@
     %09%7B%0D%0A
-//
        %09
@@ -3231,24 +3231,36 @@
    %7D%0D%0A    %0D%0A
+%09@Override%0D%0A
     public S

Implement unlock