Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package br.com.algartelecom.algarcrm.notification.service.impl;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.List;
- import java.util.Properties;
- import javax.mail.Flags;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.Session;
- import javax.mail.Store;
- import javax.mail.internet.MimeBodyPart;
- import org.apache.commons.lang3.ArrayUtils;
- import org.apache.log4j.Logger;
- import org.springframework.stereotype.Service;
- import br.com.algartelecom.algarcrm.notification.model.Email;
- import br.com.algartelecom.algarcrm.notification.service.MessageReaderService;
- import br.com.algartelecom.algarcrm.notification.service.exception.EmailReadException;
- import br.com.algartelecom.algarcrm.notification.util.PropertyUtils;
- /**
- * @author oldamar
- *
- */
- @Service
- public class MessageReaderEmailServiceImpl implements MessageReaderService< Email > {
- private static final Logger LOG = Logger.getLogger( MessageReaderEmailServiceImpl.class );
- private final static String KEY_PROTOCOL = "mail.store.protocol";
- private final static String PROTOCOL = "message.reader.email.protocol";
- private final static String HOST = "message.reader.email.host";
- private final static String USERNAME = "message.reader.email.username";
- private final static String PASSWORD = "message.reader.email.password";
- private final static String FOLDER = "message.reader.email.folder";
- private static final String HTML_CONTENT = "text/html";
- private static final String PLAIN_CONTENT = "text/plain";
- private static final String REVERSE_ORDER_MESG = "message.reader.email.reverse";
- @Override
- public Collection< Email > execute()
- throws EmailReadException {
- return this.execute( DEFAULT_CONFIG );
- }
- @Override
- public Collection< Email > execute( String configPath )
- throws EmailReadException {
- Properties prop = null;
- try {
- prop = PropertyUtils.getProperties( configPath );
- } catch ( IOException e ) {
- LOG.error( String.format( "Error loading email configuration file: %s - errorMsg: %s", configPath, e.getMessage() ) );
- }
- Properties configProp = System.getProperties();
- configProp.setProperty( KEY_PROTOCOL, prop.getProperty( PROTOCOL ) );
- return this.execute(
- configProp,
- prop.getProperty( HOST ),
- prop.getProperty( USERNAME ),
- prop.getProperty( PASSWORD ),
- prop.getProperty( FOLDER ),
- prop.getProperty( REVERSE_ORDER_MESG ) );
- }
- @Override
- public Collection< Email > execute( Properties configProperties, String host, String username, String password, String folderName, String reverse )
- throws EmailReadException {
- Folder folder = null;
- Store store = null;
- Collection< Email > result = null;
- try {
- Session session = Session.getDefaultInstance( configProperties, null );
- store = session.getStore( PROTOCOL );
- store.connect( host, username, password );
- folder = store.getFolder( folderName );
- folder.open( Folder.READ_WRITE );
- LOG.info( String.format( "Nr of Messages for %s : %s", username, folder.getMessageCount() ) );
- if ( folder.getMessageCount() > 0 ) {
- result = new ArrayList< Email >();
- Message messages[] = folder.getMessages();
- if ( "yes".equals( reverse ) ) {
- ArrayUtils.reverse( messages );
- }
- List< Message > msgs = Arrays.asList();
- for ( Message msg : msgs ) {
- Email email = new Email();
- email.setSubject( msg.getSubject() );
- email.setFrom( this.getFrom( msg ) );
- this.setContent( msg.getContent(), email ); // pegar sim ou
- // nao pelo
- // texto, tera
- // que ser
- // script
- // gambiarra,
- // orientar a
- // reposta
- // pegar campo hidden - fazer essas 2 coisas no
- // notification, aqui somente ler em htmlk e o texto
- // testei com gmail e outlook
- result.add( email );
- // criar log o email, setar no proprio instance
- msg.setFlag( Flags.Flag.SEEN, true );
- msg.setFlag( Flags.Flag.DELETED, true );
- }
- }
- } catch ( Exception e ) {
- LOG.error( String.format( "Error in read email message: %s", e.getMessage() ), e );
- throw new EmailReadException( e.getMessage(), e );
- } finally {
- try {
- if ( folder != null ) {
- folder.close( true );
- }
- if ( store != null ) {
- store.close();
- }
- } catch ( MessagingException e ) {
- LOG.error( String.format( "Error closing email connection: %s", e.getMessage() ), e );
- }
- }
- return result;
- }
- private void setContent( Object content, Email email )
- throws MessagingException,
- IOException {
- if ( content instanceof Multipart ) {
- Multipart multi = ( (Multipart) content );
- int parts = multi.getCount();
- for ( int i = 0; i < parts; ++i ) {
- MimeBodyPart part = (MimeBodyPart) multi.getBodyPart( i );
- if ( part.getContent() instanceof Multipart ) {
- setContent( part.getContent(), email );
- } else {
- if ( part.isMimeType( HTML_CONTENT ) ) {
- LOG.info( String.format( "Process Html content email for $s : $s", email, content ) );
- email.setHtml( part.getContent().toString() );
- } else if ( part.isMimeType( PLAIN_CONTENT ) ) {
- LOG.info( String.format( "Process Message content email for $s : $s", email, content ) );
- email.setMessage( part.getContent().toString() );
- } else {
- // TODO verificar se precisa e anexo, provavel que nao
- // extension = part.getDataHandler().getName();
- }
- }
- }
- } else {
- LOG.warn( String.format( "It was not possible to read the email %s, it is not coming as Multipart.", email.getSubject() ) );
- }
- }
- private String getFrom( Message msg )
- throws MessagingException {
- if ( msg.getReplyTo() != null && msg.getReplyTo().length >= 1 ) {
- return msg.getReplyTo()[ 0 ].toString();
- } else if ( msg.getFrom().length >= 1 ) {
- return msg.getFrom()[ 0 ].toString();
- }
- return "unknown";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement