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());
}
}
Aggregations