Search in sources :

Example 1 with PolicyLexicon

use of org.nhind.config.PolicyLexicon in project nhin-d by DirectProject.

the class PolicyCommands method importPolicy.

@Command(name = "ImportPolicy", usage = IMPORT_POLICY_USAGE)
public void importPolicy(String[] args) {
    final String policyName = StringArrayUtil.getRequiredValue(args, 0);
    final String fileLoc = StringArrayUtil.getRequiredValue(args, 1);
    final String lexicon = StringArrayUtil.getOptionalValue(args, 2, "");
    // check if the policy already exists
    try {
        org.nhind.config.CertPolicy policy = proxy.getPolicyByName(policyName);
        if (policy != null) {
            System.out.println("Policy with name " + policyName + " already exists.");
            return;
        }
    } catch (Exception e) {
        System.out.println("Failed to lookup policy: " + e.getMessage());
        return;
    }
    PolicyLexicon lex;
    if (lexicon.isEmpty())
        lex = PolicyLexicon.SIMPLE_TEXT_V1;
    else {
        try {
            lex = PolicyLexicon.fromString(lexicon);
        } catch (Exception e) {
            System.out.println("Invalid lexicon name.");
            return;
        }
    }
    // validate the policy syntax
    final org.nhindirect.policy.PolicyLexicon parseLexicon;
    if (lex.equals(org.nhind.config.PolicyLexicon.JAVA_SER))
        parseLexicon = org.nhindirect.policy.PolicyLexicon.JAVA_SER;
    else if (lex.equals(org.nhind.config.PolicyLexicon.SIMPLE_TEXT_V1))
        parseLexicon = org.nhindirect.policy.PolicyLexicon.SIMPLE_TEXT_V1;
    else
        parseLexicon = org.nhindirect.policy.PolicyLexicon.XML;
    byte[] policyBytes;
    InputStream inStr = null;
    try {
        policyBytes = FileUtils.readFileToByteArray(new File(fileLoc));
        inStr = new ByteArrayInputStream(policyBytes);
        final PolicyLexiconParser parser = PolicyLexiconParserFactory.getInstance(parseLexicon);
        parser.parse(inStr);
    } catch (PolicyParseException e) {
        System.out.println("Syntax error in policy file " + fileLoc + " : " + e.getMessage());
        return;
    } catch (IOException e) {
        System.out.println("Error reading file " + fileLoc + " : " + e.getMessage());
        return;
    } finally {
        IOUtils.closeQuietly(inStr);
    }
    try {
        org.nhind.config.CertPolicy addPolicy = new org.nhind.config.CertPolicy();
        addPolicy.setPolicyData(policyBytes);
        addPolicy.setPolicyName(policyName);
        addPolicy.setLexicon(lex);
        proxy.addPolicy(addPolicy);
        System.out.println("Successfully imported policy.");
    } catch (IOException e) {
        System.out.println("Error reading file " + fileLoc + " : " + e.getMessage());
        return;
    } catch (Exception e) {
        System.out.println("Error importing certificate " + fileLoc + " : " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) PolicyParseException(org.nhindirect.policy.PolicyParseException) PolicyLexicon(org.nhind.config.PolicyLexicon) ByteArrayInputStream(java.io.ByteArrayInputStream) PolicyLexiconParser(org.nhindirect.policy.PolicyLexiconParser) File(java.io.File) PolicyParseException(org.nhindirect.policy.PolicyParseException) Command(org.nhindirect.dns.tools.utils.Command)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PolicyLexicon (org.nhind.config.PolicyLexicon)1 Command (org.nhindirect.dns.tools.utils.Command)1 PolicyLexiconParser (org.nhindirect.policy.PolicyLexiconParser)1 PolicyParseException (org.nhindirect.policy.PolicyParseException)1