use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.
the class JavaSerializedObjectLexiconPolicyParser_serializeTest method testSerialize_simpleExpression_validateExpression.
public void testSerialize_simpleExpression_validateExpression() throws Exception {
final JavaSerializedObjectLexiconPolicyParser parser = new JavaSerializedObjectLexiconPolicyParser();
// build the expression
final PolicyValue<Boolean> op1 = PolicyValueFactory.getInstance(true);
final LiteralPolicyExpression<Boolean> expr = LiteralPolicyExpressionFactory.getInstance(op1);
final Vector<PolicyExpression> operands = new Vector<PolicyExpression>();
operands.add(expr);
final OperationPolicyExpression oper = OperationPolicyExpressionFactory.getInstance(PolicyOperator.LOGICAL_NOT, operands);
// serialize
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
parser.serialize(oper, outStream);
assertTrue(outStream.size() > 0);
String serialzied = new String(outStream.toByteArray());
System.out.println(serialzied);
// deserialize
final ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
final PolicyExpression deserExpression = parser.parse(inStream);
assertNotNull(deserExpression);
assertEquals(PolicyExpressionType.OPERATION, deserExpression.getExpressionType());
}
use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.
the class WSSmtpAgentConfig method addPolicyToMap.
public void addPolicyToMap(Map<String, Collection<PolicyExpression>> policyMap, String domainName, CertPolicyGroupReltn policyReltn) {
// check to see if the domain is in the map
Collection<PolicyExpression> policyExpressionCollection = policyMap.get(domainName);
if (policyExpressionCollection == null) {
policyExpressionCollection = new ArrayList<PolicyExpression>();
policyMap.put(domainName, policyExpressionCollection);
}
final CertPolicy policy = policyReltn.getCertPolicy();
final PolicyLexicon lexicon;
if (policy.getLexicon().equals(org.nhind.config.PolicyLexicon.JAVA_SER))
lexicon = PolicyLexicon.JAVA_SER;
else if (policy.getLexicon().equals(org.nhind.config.PolicyLexicon.SIMPLE_TEXT_V1))
lexicon = PolicyLexicon.SIMPLE_TEXT_V1;
else
lexicon = PolicyLexicon.XML;
final InputStream inStr = new ByteArrayInputStream(policy.getPolicyData());
try {
// grab a parser and compile this policy
final PolicyLexiconParser parser = PolicyLexiconParserFactory.getInstance(lexicon);
policyExpressionCollection.add(parser.parse(inStr));
} catch (PolicyParseException ex) {
throw new SmtpAgentException(SmtpAgentError.InvalidConfigurationFormat, "Failed parse policy into policy expression: " + ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(inStr);
}
}
use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.
the class TrustModel_isCertPolicyCompliantTest method testIsCertPolicyCompliant_noPolicyExpression_assertTrue.
public void testIsCertPolicyCompliant_noPolicyExpression_assertTrue() throws Exception {
final TrustModel model = new TrustModel();
final PolicyResolver resolver = mock(PolicyResolver.class);
when(resolver.getIncomingPolicy((InternetAddress) any())).thenReturn(new ArrayList<PolicyExpression>());
model.setTrustPolicyResolver(resolver);
final X509Certificate cert = mock(X509Certificate.class);
assertTrue(model.isCertPolicyCompliant(new InternetAddress("me@test.com"), cert));
}
use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.
the class TrustModel_isCertPolicyCompliantTest method testIsCertPolicyCompliant_policyCompliant_assertTrue.
public void testIsCertPolicyCompliant_policyCompliant_assertTrue() throws Exception {
final TrustModel model = new TrustModel();
final PolicyFilter filter = mock(PolicyFilter.class);
when(filter.isCompliant((X509Certificate) any(), (PolicyExpression) any())).thenReturn(true);
final PolicyResolver resolver = mock(PolicyResolver.class);
final PolicyExpression expression = mock(PolicyExpression.class);
when(resolver.getIncomingPolicy((InternetAddress) any())).thenReturn(Arrays.asList(expression));
model.setTrustPolicyResolver(resolver);
model.setPolicyFilter(filter);
final X509Certificate cert = mock(X509Certificate.class);
assertTrue(model.isCertPolicyCompliant(new InternetAddress("me@test.com"), cert));
}
use of org.nhindirect.policy.PolicyExpression in project nhin-d by DirectProject.
the class DefaultNHINDAgent method filterCertificatesByPolicy.
protected Collection<X509Certificate> filterCertificatesByPolicy(InternetAddress sender, PolicyResolver resolver, Collection<X509Certificate> certsToFilter, boolean incoming) {
if (certsToFilter == null || certsToFilter.isEmpty())
return certsToFilter;
final Collection<X509Certificate> filteredCerts;
// apply the policy if it exists
if (resolver != null) {
filteredCerts = new ArrayList<X509Certificate>();
// get the incoming policy based on the sender
final Collection<PolicyExpression> expressions = (incoming) ? resolver.getIncomingPolicy(sender) : resolver.getOutgoingPolicy(sender);
// loop through filters and certs
for (X509Certificate cert : certsToFilter) {
boolean filterCert = false;
for (PolicyExpression expression : expressions) {
try {
// check for compliance
if (!policyFilter.isCompliant(cert, expression)) {
filterCert = true;
break;
}
} catch (PolicyRequiredException requiredException) {
filterCert = true;
break;
} catch (PolicyProcessException processException) {
throw new AgentException(AgentError.InvalidPolicy, processException);
}
}
if (!filterCert)
filteredCerts.add(cert);
}
} else
filteredCerts = certsToFilter;
return filteredCerts;
}
Aggregations