package com.bradmcevoy.http.webdav;

import com.bradmcevoy.common.Path;
import com.bradmcevoy.http.*;
import com.bradmcevoy.http.Request.Method;
import com.bradmcevoy.http.Response.Status;
import com.bradmcevoy.http.exceptions.BadRequestException;
import com.bradmcevoy.http.exceptions.ConflictException;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import com.bradmcevoy.http.http11.Http11ResponseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MkColHandler implements Handler {

    private static final Logger log = LoggerFactory.getLogger( MkColHandler.class );
    private final Http11ResponseHandler responseHandler;
    private final HandlerHelper handlerHelper;

    public MkColHandler( Http11ResponseHandler responseHandler, HandlerHelper handlerHelper ) {
        this.responseHandler = responseHandler;
        this.handlerHelper = handlerHelper;
    }

    public String[] getMethods() {
        return new String[]{Method.MKCOL.code};
    }

    @Override
    public boolean isCompatible( Resource handler ) {
        return ( handler instanceof MakeCollectionableResource );
    }

    public void process( HttpManager manager, Request request, Response response ) throws ConflictException, NotAuthorizedException, BadRequestException {
        if( !handlerHelper.checkExpects( responseHandler, request, response ) ) {
            return;
        }
        String host = request.getHostHeader();
        String finalurl = HttpManager.decodeUrl( request.getAbsolutePath() );
        String name;
        log.debug( "process request: host: " + host + " url: " + finalurl );

        Path finalpath = Path.path( finalurl ); //this is the parent collection it goes in
        name = finalpath.getName();
        Path parent = finalpath.getParent();
        String parenturl = parent.toString();

        Resource parentcol = manager.getResourceFactory().getResource( host, parenturl );
        if( parentcol != null ) {
            log.debug( "process: resource: " + parentcol.getClass().getName() );

            if( handlerHelper.isLockedOut( request, parentcol ) ) {
                response.setStatus( Status.SC_LOCKED );
                return;
            }
            Resource dest = manager.getResourceFactory().getResource( host, finalpath.toString() );

            if( dest != null && handlerHelper.isLockedOut( request, dest ) ) {
                response.setStatus( Status.SC_LOCKED ); //notowner_modify wants this code here
                return;
            } else if( handlerHelper.missingLock( request, parentcol ) ) {
                response.setStatus( Status.SC_PRECONDITION_FAILED ); //notowner_modify wants this code here
                return;
            }

            if( parentcol instanceof CollectionResource ) {
                CollectionResource col = (CollectionResource) parentcol;
                processMakeCol( manager, request, response, col, name );
            } else {
                responseHandler.respondConflict( parentcol, response, request, "not a collection" );
            }
            
        } else {
            manager.getResponseHandler().respondConflict( parentcol, response, request, name );
        }
    }

    protected void processMakeCol( HttpManager milton, Request request, Response response, CollectionResource resource, String newName ) throws ConflictException, NotAuthorizedException {
        MakeCollectionableResource existingCol = (MakeCollectionableResource) resource;
        try {
            //For litmus test and RFC support
            if( request.getInputStream().read() > -1 ) //This should be empty
            {
                response.setStatus( Response.Status.SC_UNSUPPORTED_MEDIA_TYPE );
                return;
            }
        } catch( Exception ex ) {
            ex.printStackTrace();
        }
        Resource existingChild = existingCol.child( newName );
        if( existingChild != null ) {
            log.debug( "item already exists: " + existingChild.getName() );
            throw new ConflictException( existingChild );
        }
        CollectionResource made = existingCol.createCollection( newName );
        if( made == null ) {
            response.setStatus( Response.Status.SC_METHOD_NOT_ALLOWED );
        } else {
            response.setStatus( Response.Status.SC_CREATED );
        }
    }
}

@@ -2167,32 +2167,74 @@
 arentcol ) ) %7B%0D%0A
+                log.warn(%22isLockedOut%22);%0D%0A
                 
@@ -2270,24 +2270,24 @@
 _LOCKED );%0D%0A
-
             
@@ -3394,13 +3394,14 @@
 er m
-ilton
+anager
 , Re
@@ -3524,32 +3524,228 @@
 zedException %7B%0D%0A
+        if( !handlerHelper.checkAuthorisation( manager, resource, request ) ) %7B%0D%0A            responseHandler.respondUnauthorised( resource, response, request );%0D%0A            return;%0D%0A        %7D%0D%0A%0D%0A
         MakeColl
@@ -4295,21 +4295,20 @@
     log.
-debug
+warn
 ( %22item 
@@ -4493,32 +4493,32 @@
 on( newName );%0D%0A
-
         if( made
@@ -4523,32 +4523,136 @@
 de == null ) %7B%0D%0A
+            log.warn( %22createCollection returned null. In resource class: %22 + existingCol.getClass());%0D%0A
             resp

add authorisation to processMakeCol