Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package br.com.algartelecom.algarcrm.notification.service.impl;
  5.  
  6.  
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.Collection;
  11. import java.util.List;
  12. import java.util.Properties;
  13.  
  14. import javax.mail.Flags;
  15. import javax.mail.Folder;
  16. import javax.mail.Message;
  17. import javax.mail.MessagingException;
  18. import javax.mail.Multipart;
  19. import javax.mail.Session;
  20. import javax.mail.Store;
  21. import javax.mail.internet.MimeBodyPart;
  22.  
  23. import org.apache.commons.lang3.ArrayUtils;
  24. import org.apache.log4j.Logger;
  25. import org.springframework.stereotype.Service;
  26.  
  27. import br.com.algartelecom.algarcrm.notification.model.Email;
  28. import br.com.algartelecom.algarcrm.notification.service.MessageReaderService;
  29. import br.com.algartelecom.algarcrm.notification.service.exception.EmailReadException;
  30. import br.com.algartelecom.algarcrm.notification.util.PropertyUtils;
  31.  
  32.  
  33. /**
  34. * @author oldamar
  35. *
  36. */
  37. @Service
  38. public class MessageReaderEmailServiceImpl implements MessageReaderService< Email > {
  39.  
  40. private static final Logger LOG = Logger.getLogger( MessageReaderEmailServiceImpl.class );
  41.  
  42. private final static String KEY_PROTOCOL = "mail.store.protocol";
  43.  
  44. private final static String PROTOCOL = "message.reader.email.protocol";
  45.  
  46. private final static String HOST = "message.reader.email.host";
  47.  
  48. private final static String USERNAME = "message.reader.email.username";
  49.  
  50. private final static String PASSWORD = "message.reader.email.password";
  51.  
  52. private final static String FOLDER = "message.reader.email.folder";
  53.  
  54. private static final String HTML_CONTENT = "text/html";
  55.  
  56. private static final String PLAIN_CONTENT = "text/plain";
  57.  
  58. private static final String REVERSE_ORDER_MESG = "message.reader.email.reverse";
  59.  
  60.  
  61. @Override
  62. public Collection< Email > execute()
  63. throws EmailReadException {
  64.  
  65. return this.execute( DEFAULT_CONFIG );
  66. }
  67.  
  68.  
  69. @Override
  70. public Collection< Email > execute( String configPath )
  71. throws EmailReadException {
  72.  
  73. Properties prop = null;
  74. try {
  75. prop = PropertyUtils.getProperties( configPath );
  76. } catch ( IOException e ) {
  77. LOG.error( String.format( "Error loading email configuration file: %s - errorMsg: %s", configPath, e.getMessage() ) );
  78. }
  79.  
  80. Properties configProp = System.getProperties();
  81. configProp.setProperty( KEY_PROTOCOL, prop.getProperty( PROTOCOL ) );
  82.  
  83. return this.execute(
  84. configProp,
  85. prop.getProperty( HOST ),
  86. prop.getProperty( USERNAME ),
  87. prop.getProperty( PASSWORD ),
  88. prop.getProperty( FOLDER ),
  89. prop.getProperty( REVERSE_ORDER_MESG ) );
  90. }
  91.  
  92.  
  93. @Override
  94. public Collection< Email > execute( Properties configProperties, String host, String username, String password, String folderName, String reverse )
  95. throws EmailReadException {
  96.  
  97. Folder folder = null;
  98. Store store = null;
  99. Collection< Email > result = null;
  100. try {
  101.  
  102. Session session = Session.getDefaultInstance( configProperties, null );
  103.  
  104. store = session.getStore( PROTOCOL );
  105. store.connect( host, username, password );
  106. folder = store.getFolder( folderName );
  107.  
  108. folder.open( Folder.READ_WRITE );
  109.  
  110. LOG.info( String.format( "Nr of Messages for %s : %s", username, folder.getMessageCount() ) );
  111.  
  112. if ( folder.getMessageCount() > 0 ) {
  113.  
  114. result = new ArrayList< Email >();
  115.  
  116. Message messages[] = folder.getMessages();
  117.  
  118. if ( "yes".equals( reverse ) ) {
  119. ArrayUtils.reverse( messages );
  120. }
  121.  
  122. List< Message > msgs = Arrays.asList();
  123.  
  124. for ( Message msg : msgs ) {
  125.  
  126. Email email = new Email();
  127. email.setSubject( msg.getSubject() );
  128. email.setFrom( this.getFrom( msg ) );
  129. this.setContent( msg.getContent(), email ); // pegar sim ou
  130. // nao pelo
  131. // texto, tera
  132. // que ser
  133. // script
  134. // gambiarra,
  135. // orientar a
  136. // reposta
  137. // pegar campo hidden - fazer essas 2 coisas no
  138. // notification, aqui somente ler em htmlk e o texto
  139. // testei com gmail e outlook
  140. result.add( email );
  141.  
  142. // criar log o email, setar no proprio instance
  143.  
  144. msg.setFlag( Flags.Flag.SEEN, true );
  145. msg.setFlag( Flags.Flag.DELETED, true );
  146. }
  147. }
  148. } catch ( Exception e ) {
  149. LOG.error( String.format( "Error in read email message: %s", e.getMessage() ), e );
  150. throw new EmailReadException( e.getMessage(), e );
  151. } finally {
  152.  
  153. try {
  154. if ( folder != null ) {
  155. folder.close( true );
  156. }
  157. if ( store != null ) {
  158. store.close();
  159. }
  160. } catch ( MessagingException e ) {
  161. LOG.error( String.format( "Error closing email connection: %s", e.getMessage() ), e );
  162. }
  163.  
  164. }
  165.  
  166. return result;
  167. }
  168.  
  169.  
  170. private void setContent( Object content, Email email )
  171. throws MessagingException,
  172. IOException {
  173.  
  174. if ( content instanceof Multipart ) {
  175.  
  176. Multipart multi = ( (Multipart) content );
  177. int parts = multi.getCount();
  178.  
  179. for ( int i = 0; i < parts; ++i ) {
  180.  
  181. MimeBodyPart part = (MimeBodyPart) multi.getBodyPart( i );
  182.  
  183. if ( part.getContent() instanceof Multipart ) {
  184.  
  185. setContent( part.getContent(), email );
  186.  
  187. } else {
  188.  
  189. if ( part.isMimeType( HTML_CONTENT ) ) {
  190. LOG.info( String.format( "Process Html content email for $s : $s", email, content ) );
  191. email.setHtml( part.getContent().toString() );
  192. } else if ( part.isMimeType( PLAIN_CONTENT ) ) {
  193. LOG.info( String.format( "Process Message content email for $s : $s", email, content ) );
  194. email.setMessage( part.getContent().toString() );
  195. } else {
  196. // TODO verificar se precisa e anexo, provavel que nao
  197. // extension = part.getDataHandler().getName();
  198. }
  199.  
  200. }
  201. }
  202. } else {
  203. LOG.warn( String.format( "It was not possible to read the email %s, it is not coming as Multipart.", email.getSubject() ) );
  204. }
  205.  
  206. }
  207.  
  208.  
  209. private String getFrom( Message msg )
  210. throws MessagingException {
  211.  
  212. if ( msg.getReplyTo() != null && msg.getReplyTo().length >= 1 ) {
  213. return msg.getReplyTo()[ 0 ].toString();
  214. } else if ( msg.getFrom().length >= 1 ) {
  215. return msg.getFrom()[ 0 ].toString();
  216. }
  217.  
  218. return "unknown";
  219. }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement