Skip to content

Commit

Permalink
OAP-215 Move oap-mail into the oap project
Browse files Browse the repository at this point in the history
  • Loading branch information
galaxina committed Feb 2, 2024
1 parent b32beb5 commit 788a6ab
Show file tree
Hide file tree
Showing 50 changed files with 2,528 additions and 0 deletions.
18 changes: 18 additions & 0 deletions oap-mail/oap-mail-sendgrid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## OAP SendGrid Mail
Add module to `oap-module.yaml`

dependsOn:
- oap-mail-sendgrid

Go to https://app.sendgrid.com/settings/api_keys and click
`Create API key`.

Enable and configure SendGrid in `application.conf`

profiles = [
oap-mail-sendgrid
]

oap-mail-smtp-transport.parameters.sendGridKey = [NEW KEY]
oap-mail-queue.paramaters.transport = "@service:oap-mail-sendgrid-trasport"

60 changes: 60 additions & 0 deletions oap-mail/oap-mail-sendgrid/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>oap-mail-parent</artifactId>
<groupId>oap</groupId>
<version>${oap.project.version}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>oap-mail-sendgrid</artifactId>

<dependencies>
<dependency>
<groupId>oap</groupId>
<artifactId>oap-mail</artifactId>
<version>${oap.project.version}</version>
</dependency>

<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>${oap.deps.sendgrid.version}</version>
<exclusions>
<exclusion>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${oap.deps.lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* The MIT License (MIT)
*
* Copyright (c) Open Application Platform Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package oap.mail.sendgrid;

import com.sendgrid.Method;
import com.sendgrid.Request;
import com.sendgrid.SendGrid;
import com.sendgrid.helpers.mail.Mail;
import com.sendgrid.helpers.mail.objects.Attachments;
import com.sendgrid.helpers.mail.objects.Content;
import com.sendgrid.helpers.mail.objects.Email;
import lombok.extern.slf4j.Slf4j;
import oap.mail.MailAddress;
import oap.mail.Message;
import oap.mail.Transport;

import javax.mail.Part;

@SuppressWarnings( "unused" )
@Slf4j
public class SendGridTransport implements Transport {
private final String sendGridKey;

public SendGridTransport( String sendGridKey ) {
this.sendGridKey = sendGridKey;
}

@Override
public void send( Message message ) {
Email from = new Email( message.from.toString() );
Content content = new Content( "text/html", message.body );
SendGrid sendGrid = new SendGrid( sendGridKey );
Request request = new Request();
request.setMethod( Method.POST );
request.setEndpoint( "mail/send" );
for( MailAddress address : message.to ) {
Email to = new Email( address.toString() );
Mail mail = new Mail( from, message.subject, to, content );
message.attachments.stream()
.map( this::createAttachments )
.forEach( mail::addAttachments );
try {
request.setBody( mail.build() );
sendGrid.api( request );
} catch( Exception e ) {
log.error( "failed to send {}", message, e );
}
}
}

private Attachments createAttachments( oap.mail.Attachment oapAttachment ) {
Attachments attachments = new Attachments();
attachments.setContent( oapAttachment.getContent() );
attachments.setContentId( oapAttachment.getContentId() );
attachments.setDisposition( Part.ATTACHMENT );
attachments.setFilename( oapAttachment.getFile() );
attachments.setType( oapAttachment.getContentType() );

return attachments;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = oap-mail-sendgrid
dependsOn = oap-mail
services {
oap-mail-sendgrid-transport.implementation = oap.mail.sendgrid.SendGridTransport
}
59 changes: 59 additions & 0 deletions oap-mail/oap-mail-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ The MIT License (MIT)
~
~ Copyright (c) Open Application Platform Authors
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>oap-mail-test</name>
<artifactId>oap-mail-test</artifactId>

<parent>
<groupId>oap</groupId>
<artifactId>oap-mail-parent</artifactId>
<version>${oap.project.version}</version>
</parent>

<dependencies>
<dependency>
<groupId>oap</groupId>
<artifactId>oap-mail</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>oap</groupId>
<artifactId>oap-stdlib-test</artifactId>
<version>${oap.project.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${oap.deps.lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
117 changes: 117 additions & 0 deletions oap-mail/oap-mail-test/src/main/java/oap/mail/test/MailBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* The MIT License (MIT)
*
* Copyright (c) Open Application Platform Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package oap.mail.test;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import oap.mail.MailAddress;
import oap.mail.Message;
import org.jetbrains.annotations.NotNull;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.search.FlagTerm;
import java.io.IOException;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
public class MailBox {

private static final Properties defaultProperties = createDefaultProperties();

@NotNull
private static Properties createDefaultProperties() {
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 ) {
javax.mail.Message[] inboxMessages = inbox.search(
new FlagTerm( new Flags( Flags.Flag.SEEN ), false ) );

return Stream.of( inboxMessages )
.sorted( Comparator.comparing( MailBox::getSentDate ) )
.map( MailBox::convertMessage )
.collect( Collectors.toList() );
}

@SneakyThrows( MessagingException.class )
public static Message getLastSentMessageFromTheBox( Folder inbox ) {
return convertMessage( inbox.getMessage( inbox.getMessageCount() ) );
}

@SneakyThrows( { MessagingException.class, IOException.class } )
public static Message convertMessage( javax.mail.Message source ) {
Message target = new Message( source.getSubject(), source.getContent().toString().trim(), null );
target.from = MailAddress.of( ( InternetAddress ) source.getFrom()[0] );
target.to.addAll( MailAddress.of( ( InternetAddress[] ) source.getRecipients( javax.mail.Message.RecipientType.TO ) ) );
target.cc.addAll( MailAddress.of( ( InternetAddress[] ) source.getRecipients( javax.mail.Message.RecipientType.CC ) ) );
target.bcc.addAll( MailAddress.of( ( InternetAddress[] ) source.getRecipients( javax.mail.Message.RecipientType.BCC ) ) );
return target;
}

@SneakyThrows( { NoSuchProviderException.class, MessagingException.class } )
public static Folder connectToInbox( String mail, String password ) {
Session emailSession = Session.getDefaultInstance( defaultProperties );
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 );

// create the inbox object and open it
inbox = store.getFolder( "Inbox" );
inbox.open( Folder.READ_WRITE );
return inbox;
}

private static Date getSentDate( javax.mail.Message message ) {
try {
return message.getSentDate();
} catch( MessagingException e ) {
log.error( "Can't get date from javax message", e );
throw new RuntimeException( e );
}
}
}
Loading

0 comments on commit 788a6ab

Please sign in to comment.