// Path: app/models/deadbolt/AccessResult.java
// public enum AccessResult
// {
//     ALLOWED,
//     DENIED,
//     NOT_SPECIFIED
// }
// 
// Path: app/models/deadbolt/ExternalizedRestriction.java
// public interface ExternalizedRestriction
// {
//     /**
//      * Gets the role names required to get by this restriction.
//      *
//      * @return the role names
//      */
//     List<String> getRoleNames();
// }
// 
// Path: app/models/deadbolt/ExternalizedRestrictions.java
// public interface ExternalizedRestrictions
// {
//     /**
//      * Gets all the {@link ExternalizedRestriction}s associated with a target.
//      *
//      * @return
//      */
//     List<ExternalizedRestriction> getExternalisedRestrictions();
// }
// 
// Path: app/models/deadbolt/Role.java
// public interface Role
// {
//     /**
//      * The name of the role.
//      *
//      * @return the role name
//      */
//     String getRoleName();
// }
// 
// Path: app/models/deadbolt/RoleHolder.java
// public interface RoleHolder
// {
//     /**
//      * A list of {@link Role}s held by the role holder.
//      *
//      * @return a list of roles
//      */
//     List<? extends Role> getRoles();
// }


import java.util.List;
import java.util.Map;
import models.deadbolt.AccessResult;
import models.deadbolt.ExternalizedRestriction;
import models.deadbolt.ExternalizedRestrictions;
import models.deadbolt.Role;
import models.deadbolt.RoleHolder;
import play.Logger;
import play.Play;
import play.exceptions.ConfigurationException;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/*
 * Copyright 2010-2011 Steve Chaloner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package controllers.deadbolt;



/**
 * Checks authorisation based on role names.
 *
 * @author Steve Chaloner (steve@objectify.be).
 */
public class Deadbolt extends Controller
{
    private enum RestrictionType { NONE, DYNAMIC, STATIC, BASIC }

    public static final String DEADBOLT_HANDLER_KEY = "deadbolt.handler";

    public static final String CACHE_USER_KEY = "deadbolt.cache-user-per-request";

    public static final String CACHE_PER_REQUEST = "deadbolt.cache-user";

    public static final String DEFAULT_RESPONSE_FORMAT = "deadbolt.default-response-format";

    private static DeadboltHandler DEADBOLT_HANDLER;

    static
    {
        String handlerName = Play.configuration.getProperty(DEADBOLT_HANDLER_KEY);
        if (handlerName == null)
        {
            throw new ConfigurationException("deadbolt.handler must be defined");
        }

        try
        {
            Class<DeadboltHandler> clazz = (Class<DeadboltHandler>)Class.forName(handlerName);
            DEADBOLT_HANDLER = clazz.newInstance();
        }
        catch (Exception e)
        {
            throw new ConfigurationException(String.format("Unable to create DeadboltHandler instance: [%s]",
                                                           e.getMessage()));
        }
    }

// Path: app/models/deadbolt/AccessResult.java
// public enum AccessResult
// {
//     ALLOWED,
//     DENIED,
//     NOT_SPECIFIED
// }
// 
// Path: app/models/deadbolt/ExternalizedRestriction.java
// public interface ExternalizedRestriction
// {
//     /**
//      * Gets the role names required to get by this restriction.
//      *
//      * @return the role names
//      */
//     List<String> getRoleNames();
// }
// 
// Path: app/models/deadbolt/ExternalizedRestrictions.java
// public interface ExternalizedRestrictions
// {
//     /**
//      * Gets all the {@link ExternalizedRestriction}s associated with a target.
//      *
//      * @return
//      */
//     List<ExternalizedRestriction> getExternalisedRestrictions();
// }
// 
// Path: app/models/deadbolt/Role.java
// public interface Role
// {
//     /**
//      * The name of the role.
//      *
//      * @return the role name
//      */
//     String getRoleName();
// }
// 
// Path: app/models/deadbolt/RoleHolder.java
// public interface RoleHolder
// {
//     /**
//      * A list of {@link Role}s held by the role holder.
//      *
//      * @return a list of roles
//      */
//     List<? extends Role> getRoles();
// }


// Path: app/controllers/deadbolt/Deadbolt.java
import java.util.List;
import java.util.Map;
import models.deadbolt.AccessResult;
import models.deadbolt.ExternalizedRestriction;
import models.deadbolt.ExternalizedRestrictions;
import models.deadbolt.Role;
import models.deadbolt.RoleHolder;
import play.Logger;
import play.Play;
import play.exceptions.ConfigurationException;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

/*
 * Copyright 2010-2011 Steve Chaloner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package controllers.deadbolt;



/**
 * Checks authorisation based on role names.
 *
 * @author Steve Chaloner (steve@objectify.be).
 */
public class Deadbolt extends Controller
{
    private enum RestrictionType { NONE, DYNAMIC, STATIC, BASIC }

    public static final String DEADBOLT_HANDLER_KEY = "deadbolt.handler";

    public static final String CACHE_USER_KEY = "deadbolt.cache-user-per-request";

    public static final String CACHE_PER_REQUEST = "deadbolt.cache-user";

    public static final String DEFAULT_RESPONSE_FORMAT = "deadbolt.default-response-format";

    private static DeadboltHandler DEADBOLT_HANDLER;

    static
    {
        String handlerName = Play.configuration.getProperty(DEADBOLT_HANDLER_KEY);
        if (handlerName == null)
        {
            throw new ConfigurationException("deadbolt.handler must be defined");
        }

        try
        {
            Class<DeadboltHandler> clazz = (Class<DeadboltHandler>)Class.forName(handlerName);
            DEADBOLT_HANDLER = clazz.newInstance();
        }
        catch (Exception e)
        {
            throw new ConfigurationException(String.format("Unable to create DeadboltHandler instance: [%s]",
                                                           e.getMessage()));
        }
    }

    private static RoleHolder getRoleHolder()