use of org.nhindirect.policy.PolicyGrammarException in project nhin-d by DirectProject.
the class SimpleTextV1LexiconPolicyParser_buildExpressionTest method testBuildExpression_toManyClosingParenthesis_assertGrammarException.
public void testBuildExpression_toManyClosingParenthesis_assertGrammarException() throws Exception {
final SimpleTextV1LexiconPolicyParser parser = new SimpleTextV1LexiconPolicyParser();
final InputStream stream = IOUtils.toInputStream("(2 = 1) != true)");
final Vector<SimpleTextV1LexiconPolicyParser.TokenTypeAssociation> tokens = parser.parseToTokens(stream);
boolean exceptionOccured = false;
try {
parser.buildExpression(tokens.iterator());
} catch (PolicyGrammarException e) {
exceptionOccured = true;
}
assertTrue(exceptionOccured);
stream.close();
}
use of org.nhindirect.policy.PolicyGrammarException in project nhin-d by DirectProject.
the class SimpleTextV1LexiconPolicyParser_parseTest method testParse_unclosedGroup_assertGrammarException.
public void testParse_unclosedGroup_assertGrammarException() throws Exception {
final SimpleTextV1LexiconPolicyParser parser = new SimpleTextV1LexiconPolicyParser();
final InputStream stream = IOUtils.toInputStream("(1 = 1");
boolean exceptionOccured = false;
try {
parser.parse(stream);
} catch (PolicyGrammarException e) {
exceptionOccured = true;
}
assertTrue(exceptionOccured);
}
use of org.nhindirect.policy.PolicyGrammarException in project nhin-d by DirectProject.
the class SimpleTextV1LexiconPolicyParser_parseTest method testParse_noOperator_assertGrammarException.
public void testParse_noOperator_assertGrammarException() throws Exception {
final SimpleTextV1LexiconPolicyParser parser = new SimpleTextV1LexiconPolicyParser();
final InputStream stream = IOUtils.toInputStream("1");
boolean exceptionOccured = false;
try {
parser.parse(stream);
} catch (PolicyGrammarException e) {
exceptionOccured = true;
}
assertTrue(exceptionOccured);
}
use of org.nhindirect.policy.PolicyGrammarException in project nhin-d by DirectProject.
the class SimpleTextV1LexiconPolicyParser method parse.
/**
* {@inheritDoc}
*/
@Override
public PolicyExpression parse(InputStream stream) throws PolicyParseException {
final Vector<TokenTypeAssociation> tokens = parseToTokens(stream);
resetLevel();
final PolicyExpression retExpression = buildExpression(tokens.iterator());
if (getLevel() != 0)
throw new PolicyGrammarException("Group not closed.");
if (retExpression.getExpressionType() != PolicyExpressionType.OPERATION)
throw new PolicyGrammarException("Expression must evaluate to an operation");
return retExpression;
}
use of org.nhindirect.policy.PolicyGrammarException in project nhin-d by DirectProject.
the class ValidatePanel method validateCert.
private void validateCert() {
reportText.setText("");
final File certFile = certFileField.getFile();
final File policyFile = policyFileField.getFile();
if (!certFile.exists()) {
JOptionPane.showMessageDialog(this, "Certificate file does not exist or cannot be found.", "Invalid Cert File", JOptionPane.ERROR_MESSAGE);
return;
}
InputStream policyInput = null;
if (!feedMode) {
if (!policyFile.exists()) {
JOptionPane.showMessageDialog(this, "Policy file does not exist or cannot be found.", "Invalid Policy File", JOptionPane.ERROR_MESSAGE);
return;
}
try {
// load the policy as an input stream
policyInput = FileUtils.openInputStream(policyFile);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Could not load policy from file: " + e.getMessage(), "Invalid Policy File", JOptionPane.ERROR_MESSAGE);
return;
}
} else {
try {
final int length = feed.getLength();
policyInput = IOUtils.toInputStream(feed.getText(0, length));
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Could not load policy: " + e.getMessage(), "Invalid Policy", JOptionPane.ERROR_MESSAGE);
return;
}
}
// load the certificate
X509Certificate cert = null;
try {
cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(FileUtils.openInputStream(certFile));
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Could not load certificate from file: " + e.getMessage(), "Invalid Cert File", JOptionPane.ERROR_MESSAGE);
return;
}
final DateFormat dateFormat = new SimpleDateFormat("EEE, MMM d yyyy HH:mm:ss", Locale.getDefault());
final StringBuilder reportTextBuilder = new StringBuilder("Validation run at " + dateFormat.format(Calendar.getInstance(Locale.getDefault()).getTime()) + "\r\n\r\n");
try {
final PolicyLexiconParser parser = (feedMode) ? PolicyLexiconParserFactory.getInstance(feedLexicon) : PolicyLexiconParserFactory.getInstance(PolicyLexicon.XML);
final PolicyExpression policyExpression = parser.parse(policyInput);
final org.nhindirect.policy.Compiler compiler = new StackMachineCompiler();
compiler.setReportModeEnabled(true);
final PolicyFilter filter = PolicyFilterFactory.getInstance(compiler);
if (filter.isCompliant(cert, policyExpression) && compiler.getCompilationReport().isEmpty())
reportTextBuilder.append("Certificate is compliant with the provided policy.");
else {
reportTextBuilder.append("Certificate is NOT compliant with the provided policy.\r\n\r\n");
final Collection<String> report = compiler.getCompilationReport();
if (!report.isEmpty()) {
for (String reportEntry : report) reportTextBuilder.append(reportEntry + "\r\n");
}
}
} catch (PolicyRequiredException e) {
reportTextBuilder.append("Validation Successful\r\nCertificate is missing a required field\r\n\t" + e.getMessage());
} catch (PolicyGrammarException e) {
reportTextBuilder.append("Validation Failed\r\nError compiling policy\r\n\t" + e.getMessage());
} catch (Exception e) {
final ByteArrayOutputStream str = new ByteArrayOutputStream();
final PrintStream printStr = new PrintStream(str);
e.printStackTrace();
e.printStackTrace(printStr);
final String stackTrace = new String(str.toByteArray());
reportTextBuilder.append("Validation Failed\r\nError compiling or proccessing policy\r\n\t" + e.getMessage() + "\r\n" + stackTrace);
} finally {
reportText.setText(reportTextBuilder.toString());
IOUtils.closeQuietly(policyInput);
}
}
Aggregations