package com.robusta.server.access.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public enum AccessPolicy {
    LOCAL_HOST_POLICY {
        @Override
        boolean allowed(HttpServletRequest request) {
            return new ClientIpRequest(request).isLocal();
        }
    }, SAME_HOST_POLICY {
        @Override
        boolean allowed(HttpServletRequest request) {
            return new ClientIpRequest(request).isSameHost(SERVER_IP_ADDRESS);
        }
    }, AUTH_COOKIE_POLICY {
        @Override
        boolean allowed(HttpServletRequest request) {
            return false;  //To change body of implemented methods use File | Settings | File Templates.
        }
    };
    public static final String SERVER_IP_DETERMINATION_FAILURE = "SERVER_IP_DETERMINATION_FAILURE";
    private static final String SERVER_IP_ADDRESS = serverIpAddress();
    private static final Logger LOGGER = LoggerFactory.getLogger(AccessPolicy.class);

    abstract boolean allowed(HttpServletRequest request);

    private static String serverIpAddress() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            LOGGER.error("Unable to determine the server machine's IP address, SAME_HOST_POLICY will not work.", e);
        }
        return SERVER_IP_DETERMINATION_FAILURE;
    }
}

@@ -218,601 +218,38 @@
 lic 
-enum AccessPolicy %7B%0A    LOCAL_HOST_POLICY %7B%0A        @Override%0A        boolean allowed(HttpServletRequest request) %7B%0A            return new ClientIpRequest(request).isLocal();%0A        %7D%0A    %7D, SAME_HOST_POLICY %7B%0A        @Override%0A        boolean allowed(HttpServletRequest request) %7B%0A            return new ClientIpRequest(request).isSameHost(SERVER_IP_ADDRESS);%0A        %7D%0A    %7D, AUTH_COOKIE_POLICY %7B%0A        @Override%0A        boolean allowed(HttpServletRequest request) %7B%0A            return false;  //To change body of implemented methods use File %7C Settings %7C File Templates.%0A        %7D%0A    %7D;
+abstract class AccessPolicy %7B%0A
 %0A   
@@ -856,16 +856,16 @@
       %7D%0A
-
         
@@ -909,11 +909,736 @@
 E;%0A    %7D
+%0A%0A    public static class AccessPolicies %7B%0A        AccessPolicy LOCAL_HOST_POLICY = new AccessPolicy() %7B%0A            @Override%0A            boolean allowed(HttpServletRequest request) %7B%0A                return new ClientIpRequest(request).isLocal();%0A            %7D%0A        %7D;%0A        AccessPolicy SAME_HOST_POLICY = new AccessPolicy() %7B%0A            @Override%0A            boolean allowed(HttpServletRequest request) %7B%0A                return new ClientIpRequest(request).isSameHost(SERVER_IP_ADDRESS);%0A            %7D%0A        %7D;%0A        AccessPolicy AUTH_COOKIE_POLICY = new AccessPolicy() %7B%0A            @Override%0A            boolean allowed(HttpServletRequest request) %7B%0A                return false;%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D%0A

access policy