package com.bradmcevoy.http.http11;

import com.bradmcevoy.http.*;
import java.util.ArrayList;
import java.util.List;

import com.bradmcevoy.http.Request.Method;
import com.bradmcevoy.http.exceptions.BadRequestException;
import com.bradmcevoy.http.exceptions.ConflictException;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OptionsHandler implements ExistingEntityHandler {

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

    public OptionsHandler( Http11ResponseHandler responseHandler, HandlerHelper handlerHelper ) {
        this.responseHandler = responseHandler;
        this.handlerHelper = handlerHelper;
        this.resourceHandlerHelper = new ResourceHandlerHelper( handlerHelper, responseHandler );
    }

    @Override
    public void process( HttpManager manager, Request request, Response response ) throws NotAuthorizedException, ConflictException, BadRequestException {
        resourceHandlerHelper.process( manager, 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 );
    }

    public void processExistingResource( HttpManager manager, Request request, Response response, Resource resource ) throws NotAuthorizedException, BadRequestException, ConflictException {
        List<String> methodsAllowed = determineMethodsAllowed( manager, resource );
        responseHandler.respondWithOptions( resource, response, request, methodsAllowed );
    }

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

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

    private List<String> determineMethodsAllowed( HttpManager manager, Resource res ) {
        List<String> list = new ArrayList<String>();
        for( Handler f : manager.getAllHandlers() ) {
            if( f.isCompatible( res ) ) {
                for( String m : f.getMethods() ) {
                    list.add( m );
                }
            }
        }
        return list;
    }


}

@@ -447,22 +447,16 @@
 nts 
-ExistingEntity
+Resource
 Hand
@@ -614,56 +614,8 @@
 r;%0D%0A
-    private final HandlerHelper handlerHelper;%0D%0A
     
@@ -828,53 +828,8 @@
 r;%0D%0A
-        this.handlerHelper = handlerHelper;%0D%0A
     
@@ -1288,16 +1288,23 @@
 source r
+esource
  ) throw
@@ -1383,91 +1383,50 @@
     
-resourceHandlerHelper.processResource( manager, request, response, r, this 
+long t = System.currentTimeMillis(
 );%0D%0A    
 %7D%0D%0A%0D
@@ -1425,100 +1425,73 @@
     
-%7D%0D%0A%0D%0A    public void processExistingResource( HttpManager manager, Request request, Response
+    try %7B%0D%0A%0D%0A            manager.onProcessResourceStart( request,
  res
@@ -1492,33 +1492,24 @@
 t, response,
- Resource
  resource ) 
@@ -1511,82 +1511,17 @@
 ce )
- throws NotAuthorizedException, BadRequestException, ConflictException %7B%0D%0A
+;%0D%0A%0D%0A    
     
@@ -1609,16 +1609,20 @@
         
+    
 response
@@ -1692,24 +1692,187 @@
 sAllowed );%0D
+%0A%0D%0A        %7D finally %7B%0D%0A            t = System.currentTimeMillis() - t;%0D%0A            manager.onProcessResourceFinish( request, response, resource, t );%0D%0A        %7D%0D
 %0A    %7D%0D%0A%0D%0A  
@@ -2469,15 +2469,11 @@
 %0A    %7D%0D%0A
-%0D%0A%0D%0A
 %7D%0D%0A

allow OPTIONS to be called without authorisation