Search in sources :

Example 6 with AuditEvent

use of org.nhindirect.common.audit.AuditEvent in project nhin-d by DirectProject.

the class RDBMSAuditor_writeEventTest method testWriteEvent_noContexts_assertWritten.

@Test
public void testWriteEvent_noContexts_assertWritten() throws Exception {
    final AuditEvent auditEvent = new AuditEvent("name1", "value1");
    this.auditorImpl.audit("testPin", auditEvent, null);
    final CompositeData lastMessage = auditorImpl.getLastEvent();
    assertNotNull(lastMessage);
    assertEquals(auditEvent.getName(), lastMessage.get("Event Name"));
    assertEquals(auditEvent.getType(), lastMessage.get("Event Type"));
    assertTrue(lastMessage.get("Event Id").toString().length() > 0);
    assertTrue(lastMessage.get("Event Time").toString().length() > 0);
    assertNotNull(lastMessage.get("Contexts"));
    String[] contexts = (String[]) lastMessage.get("Contexts");
    assertEquals(1, contexts.length);
}
Also used : CompositeData(javax.management.openmbean.CompositeData) AuditEvent(org.nhindirect.common.audit.AuditEvent) Test(org.junit.Test)

Example 7 with AuditEvent

use of org.nhindirect.common.audit.AuditEvent in project nhin-d by DirectProject.

the class RDBMSAuditor_writeEventTest method testWriteEvent_withContexts_assertWritten.

@Test
public void testWriteEvent_withContexts_assertWritten() throws Exception {
    final AuditEvent auditEvent = new AuditEvent("name1", "value1");
    final DefaultAuditContext context1 = new DefaultAuditContext("name1", "value1");
    final DefaultAuditContext context2 = new DefaultAuditContext("name2", "value2");
    this.auditorImpl.audit("testPin", auditEvent, Arrays.asList(context1, context2));
    final CompositeData lastMessage = auditorImpl.getLastEvent();
    assertNotNull(lastMessage);
    assertEquals(auditEvent.getName(), lastMessage.get("Event Name"));
    assertEquals(auditEvent.getType(), lastMessage.get("Event Type"));
    assertTrue(lastMessage.get("Event Id").toString().length() > 0);
    assertTrue(lastMessage.get("Event Time").toString().length() > 0);
    assertNotNull(lastMessage.get("Contexts"));
    String[] contexts = (String[]) lastMessage.get("Contexts");
    assertEquals(2, contexts.length);
    assertEquals("name1:value1", contexts[0]);
    assertEquals("name2:value2", contexts[1]);
}
Also used : DefaultAuditContext(org.nhindirect.common.audit.DefaultAuditContext) CompositeData(javax.management.openmbean.CompositeData) AuditEvent(org.nhindirect.common.audit.AuditEvent) Test(org.junit.Test)

Example 8 with AuditEvent

use of org.nhindirect.common.audit.AuditEvent in project nhin-d by DirectProject.

the class DefaultSmtpAgent method postProcessIncomingMessage.

private void postProcessIncomingMessage(MessageProcessResult result) {
    this.copyMessage(result.getProcessedMessage().getMessage(), settings.getIncomingMessageSettings());
    // check if we need to create notification messages
    try {
        if (settings.getNotificationProducer() != null) {
            result.setNotificationMessages(settings.getNotificationProducer().produce((IncomingMessage) result.getProcessedMessage()));
            if (result.getNotificationMessages() != null && auditor != null) {
                for (NotificationMessage noteMsg : result.getNotificationMessages()) {
                    Collection<AuditContext> contexts = createContextCollectionFromMessage(noteMsg, Arrays.asList(AuditEvents.MDN_HEADER_CONTEXT));
                    auditor.audit(PRINICPAL, new AuditEvent(AuditEvents.PRODUCE_MDN_NAME, AuditEvents.EVENT_TYPE), contexts);
                }
            }
        }
    } catch (Exception e) {
        // don't bail on the whole process if we can't create notifications messages
        LOGGER.error("Failed to create notification messages.", e);
    }
    // check if this is an incoming MDN message... is so, audit it
    if (NotificationHelper.isMDN(result.getProcessedMessage().getMessage())) {
        Collection<AuditContext> contexts = createContextCollectionFromMessage(result.getProcessedMessage(), Arrays.asList(AuditEvents.MDN_RECEIVED_CONTEXT));
        auditor.audit(PRINICPAL, new AuditEvent(AuditEvents.MDN_RECEIVED_NAME, AuditEvents.EVENT_TYPE), contexts);
    }
}
Also used : NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) IncomingMessage(org.nhindirect.stagent.IncomingMessage) AuditEvent(org.nhindirect.common.audit.AuditEvent) AuditContext(org.nhindirect.common.audit.AuditContext) DefaultAuditContext(org.nhindirect.common.audit.DefaultAuditContext) MessagingException(javax.mail.MessagingException) AgentException(org.nhindirect.stagent.AgentException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 9 with AuditEvent

use of org.nhindirect.common.audit.AuditEvent in project nhin-d by DirectProject.

the class DefaultSmtpAgent method processMessage.

/**
	 * Processes an message from an SMTP stack.  The bridge component between the SMTP stack and the SMTP agent is responsible for
	 * extracting the message, the recipient list, and the sender.  In some cases, the routing headers may have different information than
	 * what is populated in the SMTP MAIL FROM and RCTP TO headers.  In these cases, the SMTP headers should be favored over the routing
	 * headers in the message and passed as the recipient collection and sender to this method.
	 * @param message The message in the SMTP envelope.
	 * @param recipients The recipients of the message.  The RCTP TO headers should be used over the message routing headers.
	 * @param sender The send of the message. The MAIL FROM header should be used over the From: routing header in the message.
	 */
public MessageProcessResult processMessage(MimeMessage message, NHINDAddressCollection recipients, NHINDAddress sender) {
    GatewayState.getInstance().lockForProcessing();
    try {
        LOGGER.trace("Entering processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress");
        MessageProcessResult retVal = null;
        verifyInitialized();
        preProcessMessage(message, sender);
        Collection<NHINDAddress> originalRecipList = new ArrayList<NHINDAddress>(recipients);
        DefaultMessageEnvelope envelopeToProcess = null;
        try {
            envelopeToProcess = new DefaultMessageEnvelope(new Message(message), recipients, sender);
            envelopeToProcess.setAgent(agent);
            // should always result in either a non null object or an exception
            MessageEnvelope processEvn = processEnvelope(envelopeToProcess);
            retVal = new MessageProcessResult(processEvn, null);
            if (retVal.getProcessedMessage() != null)
                postProcessMessage(retVal);
        } catch (SmtpAgentException e) {
            // rethrow
            LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress", e);
            throw e;
        } catch (Exception e) {
            // audit the message rejection
            if (envelopeToProcess != null) {
                Collection<AuditContext> contexts = createContextCollectionFromMessage(envelopeToProcess, Arrays.asList(AuditEvents.DEFAULT_HEADER_CONTEXT));
                if (e instanceof NHINDException) {
                    NHINDException exception = (NHINDException) e;
                    if (exception.getError() != null) {
                        contexts.add(new DefaultAuditContext(AuditEvents.REJECTED_MESSAGE_REASON_CONTEXT, exception.getError().toString()));
                        if (exception.getError() != null && exception.getError() instanceof AgentException && ((AgentException) exception.getError()).getError() == AgentError.NoTrustedRecipients) {
                            StringBuilder rejectedRecips = new StringBuilder();
                            int cnt = 0;
                            for (NHINDAddress address : originalRecipList) {
                                rejectedRecips.append(address.getAddress());
                                if (++cnt < originalRecipList.size())
                                    rejectedRecips.append(", ");
                            }
                            contexts.add(new DefaultAuditContext(AuditEvents.REJECTED_RECIPIENTS_CONTEXT, rejectedRecips.toString()));
                        }
                    }
                }
                auditor.audit(PRINICPAL, new AuditEvent(AuditEvents.REJECTED_MESSAGE_NAME, AuditEvents.EVENT_TYPE), contexts);
            }
            LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress", e);
            throw new SmtpAgentException(SmtpAgentError.Unknown, e);
        }
        LOGGER.trace("Exiting processMessage(MimeMessage, NHINDAddressCollection, NHINDAddress");
        return retVal;
    } finally {
        GatewayState.getInstance().unlockFromProcessing();
    }
}
Also used : DefaultAuditContext(org.nhindirect.common.audit.DefaultAuditContext) NotificationMessage(org.nhindirect.stagent.mail.notifications.NotificationMessage) IncomingMessage(org.nhindirect.stagent.IncomingMessage) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) OutgoingMessage(org.nhindirect.stagent.OutgoingMessage) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) AgentException(org.nhindirect.stagent.AgentException) ArrayList(java.util.ArrayList) NHINDException(org.nhindirect.stagent.NHINDException) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) MessageEnvelope(org.nhindirect.stagent.MessageEnvelope) MessagingException(javax.mail.MessagingException) AgentException(org.nhindirect.stagent.AgentException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NHINDException(org.nhindirect.stagent.NHINDException) NHINDAddress(org.nhindirect.stagent.NHINDAddress) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) Collection(java.util.Collection) AuditEvent(org.nhindirect.common.audit.AuditEvent)

Example 10 with AuditEvent

use of org.nhindirect.common.audit.AuditEvent in project nhin-d by DirectProject.

the class FileAuditorTest method testGetEvents_RequestLessThanAvailable_AssertGotOnlyRecords.

@Test
public void testGetEvents_RequestLessThanAvailable_AssertGotOnlyRecords() {
    FileAuditor auditor = new FileAuditor(auditFile);
    auditor.clear();
    AuditEvent event1 = new AuditEvent("Category" + UUID.randomUUID(), "type");
    AuditEvent event2 = new AuditEvent("Category" + UUID.randomUUID(), "type");
    DefaultAuditContext context1 = new DefaultAuditContext("name1", "value1");
    DefaultAuditContext context2 = new DefaultAuditContext("name2", "value2");
    auditor.audit(PRINCIPAL, event1);
    auditor.audit(PRINCIPAL, event2, Arrays.asList(context1, context2));
    CompositeData[] events = auditor.getEvents(1);
    assertNotNull(events);
    assertEquals(1, events.length);
    CompositeData lastMessage = events[0];
    assertEquals(event2.getName(), lastMessage.get("Event Name"));
    assertEquals(event2.getType(), lastMessage.get("Event Type"));
    assertTrue(lastMessage.get("Event Id").toString().length() > 0);
    assertTrue(lastMessage.get("Event Time").toString().length() > 0);
    assertNotNull(lastMessage.get("Contexts"));
    String[] contexts = (String[]) lastMessage.get("Contexts");
    assertEquals(2, contexts.length);
    assertEquals("name1:value1", contexts[0]);
    assertEquals("name2:value2", contexts[1]);
}
Also used : DefaultAuditContext(org.nhindirect.common.audit.DefaultAuditContext) CompositeData(javax.management.openmbean.CompositeData) AuditEvent(org.nhindirect.common.audit.AuditEvent) Test(org.junit.Test)

Aggregations

AuditEvent (org.nhindirect.common.audit.AuditEvent)21 DefaultAuditContext (org.nhindirect.common.audit.DefaultAuditContext)15 CompositeData (javax.management.openmbean.CompositeData)14 Test (org.junit.Test)14 AuditContext (org.nhindirect.common.audit.AuditContext)5 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 MessagingException (javax.mail.MessagingException)2 AgentException (org.nhindirect.stagent.AgentException)2 DefaultMessageEnvelope (org.nhindirect.stagent.DefaultMessageEnvelope)2 IncomingMessage (org.nhindirect.stagent.IncomingMessage)2 MessageEnvelope (org.nhindirect.stagent.MessageEnvelope)2 NHINDAddress (org.nhindirect.stagent.NHINDAddress)2 NHINDException (org.nhindirect.stagent.NHINDException)2 OutgoingMessage (org.nhindirect.stagent.OutgoingMessage)2 NotificationMessage (org.nhindirect.stagent.mail.notifications.NotificationMessage)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Entry (java.util.Map.Entry)1