Writing a Sink

To write a custom Sink follow the steps below:

  1. Inherit the Sink from the AbstractSimpleSink class.
  2. If necessary override init method to provide the Sink-specific initialization (if there’s any) – this method is called once when the Sink is initialized. The Sink-specific properties would have been entered by the user in the Configuration Tool (see also section 4.3)
  3. You must override the following method to provide your own handling of messages sent from AwareIM:
    public void processMessage (Message msg) 
    throws PipelineException;


    You can translate messages from AwareIM using the following techniques:

    1. get the array of DataObject’s from the message:
      DataObject [] dobs = msg.peekDataObjects ();
    2. use MessageInterpreter class to interpret the message (see also 4.1.3):
      MessageInterpreter mi = new MessageInterpreter (dobs);
      INotification notif = mi.getNotification (); /**or**/
      String serviceName  = mi.getServiceName ();

As an example you can see the code snippet below that implements the Sink that sends outgoing e-mails from AwareIM. Note that this code is for illustration purposes only and certain details such as e-mail construction and sending and error handling are omitted.

  1. public class EmailSink extends AbstractSimpleSink
  2. {
  3. private Stringm_mailHost= null ;
  4. private Sessionm_session= null ;
  5. private Stringm_fromAddress= null ;
  6. /** Initializethesink. Mail host and 'from address' properties are specified in the user interface */
  7. public void init(Stringname,Propertiesprops,Stringprefix,
  8. Controllercontroller)
  9. throws IbafException
  10. {
  11. try
  12. {
  13. super .init(name,props,prefix,controller);
  14. m_mailHost=props.getProperty(prefix+"."+"MAIL_HOST");
  15. if (m_mailHost== null )
  16. throw new IbafException("Mail host property for outgoing e-mails is not specified.");
  17. PropertiessessionProps=System.getProperties();
  18. sessionProps.put("mail.smtp.host",m_mailHost);
  19. m_fromAddress=props.getProperty(prefix+"."+"MAIL_FROM_ADDRESS");
  20. m_session=Session.getDefaultInstance(sessionProps,null);
  21. }
  22. catch (Exceptione)
  23. {
  24. e.printStackTrace();
  25. throw new IbafException(e.toString());
  26. }
  27. }
  28. /** Message handling method – must be provided */
  29. public void processMessage(Message msg)
  30. throws PipelineException
  31. {
  32. Stringsubject= null ,toAddress= null ;
  33. StringsentDateStr= null ;
  34. try
  35. {
  36. DataObject[]dobs=msg.peekDataObjects();
  37. // get all message parameters from data objects
  38. MessageInterpretermi= new MessageInterpreter(dobs);
  39. // get e-mail address from channel values
  40. DataObjectchvDob=mis.getChannelValues();
  41. if (chvDob== null )
  42. return ; // ignore the message
  43. try
  44. {
  45. toAddress=(String)chvDob.getAttributeValue(EMAIL_ADDRESS);
  46. }
  47. catch (InvalidParameterExceptionipe)
  48. {
  49.  
  50. }
  51. if (toAddress== null ) return ;//** ignore the message
  52. INotificationnotif=mis.getNotification();
  53. if (notif== null ) return ; // ignore the message
  54. // we expect notification of a certain structure – it must have "BODY" and SUBJECT attributes defined
  55. Stringbody= null ;
  56. try
  57. {
  58. body=(String)notif.getAttributeValue (EMAIL_BODY_ATTR);
  59. }
  60. catch (InvalidParameterExceptionipe)
  61. {
  62.  
  63. }
  64. if (body== null )
  65. return ; // ignore the message// subject is optional
  66. try
  67. {
  68. subject=(String)notif.getAttributeValue (EMAIL_SUBJECT_ATTR);
  69. }
  70. catch (Exceptione)
  71. {
  72. }
  73. if (subject== null ) subject="";
  74. // create MAPI e-mail message
  75. MimeMessageemail=createEmail(body,subject, toAddress);
  76. if (email!= null )
  77. {
  78. // send e-mail using MAPI
  79. sendEmail(email,toAddress,new Date ());
  80. }
  81. }
  82. catch (Exceptione)
  83. {
  84. }
  85. }
  86.  
  87. /** Send the e-mail using MAPI. A lot of details are omitted **/
  88. private boolean sendEmail(MimeMessageemail,StringtoAddress, StringsentDate)
  89. {
  90. try
  91. {
  92. Transport.send(email);
  93. }
  94. catch (Exceptione)
  95. {
  96. }
  97. return true;
  98. }
  99.  
  100. /** Create e-mailmessage using MAPI. Details are omitted */
  101. private MimeMessagecreateEmail(Stringbody,Stringsubject,String toAddress,DatesentDate)
  102. throws Exception
  103. {
  104. MimeMessagemailMessage= new MimeMessage(m_session);
  105. if (m_fromAddress!= null ) mailMessage.setFrom(InternetAddress.parse mailMessage.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(toAddress, false ));
  106. mailMessage.setSubject (subject);
  107. mailMessage.setSentDate(sentDate);
  108. return mailMessage;
  109. }