class PolicyUpdater {

    static String getEtagForExistingPolicy(ZTSClient zts, PolicyUpdaterConfiguration configuration,
            String domain) {
        
        if (domain == null) {
            throw new IllegalArgumentException("getEtagForExistingPolicy: null parameters are not valid arguments");
        }

        String policyDir = configuration.getPolicyFileDir();
        if (policyDir == null) {
            throw new IllegalArgumentException("getEtagForExistingPolicy: Invalid configuration: no policy directory path");
        }
        
        String policyDirPath;
        if (policyDir.length() - 1 != policyDir.lastIndexOf(File.separator)) {
            policyDirPath = policyDir + File.separator;
        } else {
            policyDirPath = policyDir;
        }
        
        String etag = null;
        String policyFile = policyDirPath + domain + POLICY_FILE_EXTENSION;

        LOG.info("Decoding " + policyFile + " to retrieve eTag from policy file.");
        File file = new File(policyFile);

        if (file.exists() == false) {
            LOG.info("Policy file not found.");
            return etag;
        }

        DomainSignedPolicyData domainSignedPolicyData = null;
        try {
            domainSignedPolicyData = JSON.fromBytes(Files.readAllBytes(file.toPath()),
                    DomainSignedPolicyData.class);
        } catch (Exception ex) {
            LOG.info("Unable to parse domain signed policy file: " + policyFile);
            return etag;
        }
        
        // validate the signature before checking for expiration
        
        if (validateSignedPolicies(zts, configuration, domainSignedPolicyData, domain) == false) {
            LOG.info("Unable to validate domain signed policy file: " + policyFile);
            return etag;
        }
        
        // Check expiration of policies and if its less than the configured interval defined by user
        // to get updated policy then return null so that the policies are updated
        
        LOG.info("Checking expiration time for: " + domain);
        long now = System.currentTimeMillis() / 1000;

        Timestamp expires = domainSignedPolicyData.getSignedPolicyData().getExpires();
 
        long startupDelayInterval = configuration.getStartupDelayIntervalInSecs();
        
        LOG.info("Expiration time for " + domain + " is: " + (expires.millis() / 1000));
        LOG.info("Startup delay: " + startupDelayInterval);
        LOG.info("Current time: " + now);

        if (((expires.millis() / 1000) - now) <  (startupDelayInterval)) {
            LOG.info("Signed policies for domain:" + domain + " are expired, returning null.");
            return null;
        }

        if (domainSignedPolicyData.getSignedPolicyData().getModified() != null) {
            
            // ETags are quoted-strings based on the HTTP RFC
            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.11
            // so we're going to quote our modified timestamp
            
            etag = "\"" + domainSignedPolicyData.getSignedPolicyData().getModified().toString() + "\"";
            LOG.info("ETag: " + etag);
        } else {
            LOG.info("No ETag found.");
        }

        return etag;
    }

}