Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public void send( Message message ) {
request.setBody( mail.build() );
sendGrid.api( request );
} catch( Exception e ) {
log.error( "failed to send {}", message );
log.error( e.getMessage(), e );
log.error( "failed to send {}", message, e );
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions oap-mail/src/main/java/oap/mail/MailAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public static MailAddress of( InternetAddress address ) {
public static List<MailAddress> of( InternetAddress[] addresses ) {
if( ArrayUtils.isEmpty( addresses ) ) {
return Collections.emptyList();
} else return Stream.of( addresses ).map( MailAddress::of ).toList();
}
return Stream.of( addresses ).map( MailAddress::of ).toList();
}

public static MailAddress of( String personal, String address ) {
Expand All @@ -72,7 +73,8 @@ public static MailAddress of( String personal, String address ) {

@SneakyThrows
public InternetAddress toInternetAddress() {
return personal == null ? new InternetAddress( mail )
return personal == null
? new InternetAddress( mail )
: new InternetAddress( mail, personal, UTF_8.toString() );
}

Expand Down
2 changes: 1 addition & 1 deletion oap-mail/src/main/java/oap/mail/Mailman.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void run() {
transport.send( message );
return true;
} catch( Exception e ) {
log.error( e.getMessage(), e );
log.error( "Could not send message: {}", message, e );
return false;
}
} );
Expand Down
10 changes: 7 additions & 3 deletions oap-mail/src/main/java/oap/mail/SmtpTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class SmtpTransport implements oap.mail.Transport {

private static final Pattern CID_PATTERN = Pattern.compile( "[\"']cid:(.+)[\"']" );

static {
MailcapCommandMap mc = ( MailcapCommandMap ) CommandMap.getDefaultCommandMap();
mc.addMailcap( "text/html;; x-java-content-handler=com.sun.mail.handlers.text_html" );
Expand All @@ -59,7 +63,7 @@ public class SmtpTransport implements oap.mail.Transport {
public final int port;
public final boolean tls;
public final Authenticator authenticator;
private Properties properties = new Properties();
private final Properties properties = new Properties();

public SmtpTransport( String host, int port, boolean tls, Authenticator authenticator ) {
this( host, port, tls, authenticator, "TLSv1.2" );
Expand Down Expand Up @@ -96,12 +100,12 @@ public void send( Message message ) {
if( message.attachments.isEmpty() ) {
mimeMessage.setContent( message.body, message.contentType + "; charset=UTF-8" );
} else {
HashSet<String> cidIds = new HashSet<>();
Set<String> cidIds = new HashSet<>();
MimeMultipart multipart;
if( "text/html".equalsIgnoreCase( message.contentType ) ) {
multipart = new Attachments.HtmlMimeMultipart();
try {
Matcher m = Pattern.compile( "[\"']cid:(.+)[\"']" ).matcher( message.body );
Matcher m = CID_PATTERN.matcher( message.body );
while( m.find() )
cidIds.add( m.group( 1 ) );
} catch( Exception e ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package oap.mail.message.xml;

import lombok.extern.slf4j.Slf4j;
import oap.mail.MailException;
import oap.mail.Message;
import oap.mail.message.MessageParser;
Expand All @@ -36,6 +37,8 @@

import static java.nio.charset.StandardCharsets.UTF_8;


@Slf4j
public class XmlMessageParser implements MessageParser {
public Message parse( String content ) {
try {
Expand All @@ -44,6 +47,7 @@ public Message parse( String content ) {
parser.parse( new ByteArrayInputStream( content.getBytes( UTF_8 ) ), handler );
return handler.getMessage();
} catch( ParserConfigurationException | SAXException | IOException e ) {
log.trace( "Could not deserialize {}", content, e );
throw new MailException( e );
}
}
Expand Down
22 changes: 13 additions & 9 deletions oap-mail/src/main/java/oap/mail/test/MailBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@

@Slf4j
public class MailBox {
private static Properties constructGmailProperties() {
Properties properties = new Properties();

properties.put( "mail.imap.port", "993" );
properties.put( "mail.imap.host", "imap.gmail.com" );
properties.put( "mail.imap.ssl.trust", "imap.gmail.com" );
properties.put( "mail.imap.ssl.protocols", "TLSv1.2" );
properties.put( "mail.imap.starttls.enable", "true" );
properties.put( "mail.imap.starttls.required", "true" );
return properties;
}

@SneakyThrows( MessagingException.class )
public static List<Message> getMessagesFromBox( Folder inbox ) {
Expand Down Expand Up @@ -76,22 +87,15 @@ public static Message convertMessage( javax.mail.Message source ) {

@SneakyThrows( { NoSuchProviderException.class, MessagingException.class } )
public static Folder connectToInbox( String mail, String password ) {
Properties properties = new Properties();

properties.put( "mail.imap.port", "993" );
properties.put( "mail.imap.host", "imap.gmail.com" );
properties.put( "mail.imap.ssl.trust", "imap.gmail.com" );
properties.put( "mail.imap.ssl.protocols", "TLSv1.2" );
properties.put( "mail.imap.starttls.enable", "true" );
properties.put( "mail.imap.starttls.required", "true" );
Properties properties = constructGmailProperties();

Session emailSession = Session.getDefaultInstance( properties );
Folder inbox = null;

// create the imap store object and connect to the imap server
Store store = emailSession.getStore( "imaps" );

store.connect( "imap.gmail.com", mail, password );
store.connect( properties.getProperty( "mail.imap.host" ), mail, password );

// create the inbox object and open it
inbox = store.getFolder( "Inbox" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class VelocityTemplateTransformer {

public VelocityTemplateTransformer() {
try {
// note: engine supports many values per single key
engine.addProperty( "userdirective", XPathDirective.class.getName() );
engine.addProperty( "userdirective", FormatDateDirective.class.getName() );
engine.addProperty( UBERSPECT_CLASSNAME, Uberspector.class.getName() );
Expand All @@ -62,14 +63,16 @@ public VelocityTemplateTransformer() {
}
}

public synchronized String transform( Template template ) {
public String transform( Template template ) {
try {
VelocityContext context = new VelocityContext();
Map<String, Object> parameters = template.getParameters();
for( String key : parameters.keySet() )
context.put( key, parameters.get( key ) );
StringWriter writer = new StringWriter();
engine.evaluate( context, writer, "mail", template.getContent() );
synchronized( this ) {
engine.evaluate( context, writer, "mail", template.getContent() );
}
writer.close();
return writer.toString();
} catch( ParseErrorException | MethodInvocationException | ResourceNotFoundException | IOException e ) {
Expand Down
6 changes: 3 additions & 3 deletions oap-mail/src/main/java/oap/mail/velocity/XPathDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Set;

import static org.w3c.dom.Node.ATTRIBUTE_NODE;
import static org.w3c.dom.Node.CDATA_SECTION_NODE;
Expand All @@ -47,7 +47,7 @@

@Slf4j
public class XPathDirective extends Directive {
private static final List<Short> TEXT_NODES = List.of( TEXT_NODE, ATTRIBUTE_NODE, COMMENT_NODE, CDATA_SECTION_NODE, PROCESSING_INSTRUCTION_NODE );
private static final Set<Short> TEXT_NODES = Set.of( TEXT_NODE, ATTRIBUTE_NODE, COMMENT_NODE, CDATA_SECTION_NODE, PROCESSING_INSTRUCTION_NODE );

public String getName() {
return "xpath";
Expand All @@ -70,7 +70,7 @@ public boolean render( InternalContextAdapter context, Writer writer, org.apache
else context.put( var, element );
else log.warn( "for " + xpath + " nothing found" );
} catch( XPathExpressionException e ) {
throw new IOException( "cannot evaluate xpath: " + e.getMessage() );
throw new IOException( "cannot evaluate xpath: " + xpath, e );
}
return true;
}
Expand Down