Skip to content

Commit

Permalink
Fix mail api (#4138)
Browse files Browse the repository at this point in the history
* remove ROLE_ from system roles + refactoring

Signed-off-by: Iliyan Velichkov <[email protected]>

* Fix user task assignment UI

Signed-off-by: Iliyan Velichkov <[email protected]>

* fix inbox apis authentication + refactoring

Signed-off-by: Iliyan Velichkov <[email protected]>

* protect processing tasks for other users

Signed-off-by: Iliyan Velichkov <[email protected]>

* put path in user tasks form key instead of full url

Signed-off-by: Iliyan Velichkov <[email protected]>

* support ant path patterns for access constraints

Signed-off-by: Iliyan Velichkov <[email protected]>

* apply code formatter

Signed-off-by: Iliyan Velichkov <[email protected]>

* fix mail API

Signed-off-by: Iliyan Velichkov <[email protected]>

* remove TestMail.java

Signed-off-by: Iliyan Velichkov <[email protected]>

* format MailService.ts

Signed-off-by: Iliyan Velichkov <[email protected]>

* change port

Signed-off-by: Iliyan Velichkov <[email protected]>

* use free random port

Signed-off-by: Iliyan Velichkov <[email protected]>

* format the code

Signed-off-by: Iliyan Velichkov <[email protected]>

* update smtp configs

Signed-off-by: Iliyan Velichkov <[email protected]>

* explicitly add localhost

Signed-off-by: Iliyan Velichkov <[email protected]>

* update connect

Signed-off-by: Iliyan Velichkov <[email protected]>

* add logs

Signed-off-by: Iliyan Velichkov <[email protected]>

* add logs

Signed-off-by: Iliyan Velichkov <[email protected]>

* add configs using the API

Signed-off-by: Iliyan Velichkov <[email protected]>

---------

Signed-off-by: Iliyan Velichkov <[email protected]>
  • Loading branch information
iliyan-velichkov authored Jul 11, 2024
1 parent fb57f64 commit cb8ffaf
Show file tree
Hide file tree
Showing 16 changed files with 299 additions and 232 deletions.
7 changes: 4 additions & 3 deletions components/api/api-mail/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>

Expand Down Expand Up @@ -50,4 +50,5 @@
<parent.pom.folder>../../../</parent.pom.folder>
</properties>

</project>
</project>

Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
*
* SPDX-FileCopyrightText: Eclipse Dirigible contributors SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.dirigible.mail.env;

import java.util.Properties;
package org.eclipse.dirigible.components.api.mail;

import org.eclipse.dirigible.commons.config.Configuration;
import org.eclipse.dirigible.components.api.mail.MailConfigurationProvider;
import org.springframework.stereotype.Component;

import java.util.Properties;

/**
* The Class EnvMailConfigProvider.
*/

@Component
public class EnvMailConfigProvider implements MailConfigurationProvider {

/** The Constant MAIL_USER. */
Expand Down Expand Up @@ -125,7 +127,7 @@ public Properties getProperties() {
* @param key the key
* @param envKey the env key
*/
private static void addValue(Properties properties, String key, String envKey) {
private void addValue(Properties properties, String key, String envKey) {
addValue(properties, key, envKey, null);
}

Expand All @@ -137,7 +139,7 @@ private static void addValue(Properties properties, String key, String envKey) {
* @param envKey the env key
* @param defaultValue the default value
*/
private static void addValue(Properties properties, String key, String envKey, String defaultValue) {
private void addValue(Properties properties, String key, String envKey, String defaultValue) {
String value = Configuration.get(envKey);
if (value != null) {
properties.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,38 @@
*/
package org.eclipse.dirigible.components.api.mail;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.eclipse.angus.mail.smtp.SMTPSSLTransport;
import org.eclipse.angus.mail.smtp.SMTPTransport;
import com.google.gson.Gson;
import jakarta.activation.DataHandler;
import jakarta.mail.Authenticator;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;
import jakarta.mail.internet.ContentType;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;
import jakarta.mail.*;
import jakarta.mail.internet.*;
import jakarta.mail.util.ByteArrayDataSource;
import org.eclipse.angus.mail.smtp.SMTPSSLTransport;
import org.eclipse.angus.mail.smtp.SMTPTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.*;

/**
* The Class MailClient.
*/
public class MailClient {

private static final Logger LOGGER = LoggerFactory.getLogger(MailClient.class);

/** The Constant MAIL_USER. */
// Mail properties
private static final String MAIL_USER = "mail.user";

private static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
/** The Constant MAIL_PASSWORD. */
private static final String MAIL_PASSWORD = "mail.password";

/** The Constant SMTP_TRANSPORT. */
private static final String SMTP_TRANSPORT = "smtp";

/** The Constant SMTPS_TRANSPORT. */
private static final String SMTPS_TRANSPORT = "smtps";

/** The properties. */
private final Properties properties;

Expand All @@ -73,48 +64,55 @@ public MailClient(Properties properties) {
* @param parts the mail parts
* @return the map
* @throws MessagingException the messaging exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public Map send(String from, String[] to, String[] cc, String[] bcc, String subject, List<Map> parts)
throws MessagingException, IOException {
Session session = getSession(this.properties);
SMTPTransport transport;
String transportProperty = properties.getProperty("mail.transport.protocol")
.toLowerCase();

transport = switch (transportProperty) {
case SMTP_TRANSPORT -> (SMTPTransport) session.getTransport();
case SMTPS_TRANSPORT -> (SMTPSSLTransport) session.getTransport();
default -> throw new IllegalStateException("Unexpected transport property: " + transportProperty);
};

public Map send(String from, String[] to, String[] cc, String[] bcc, String subject, List<Map> parts) throws MessagingException {
try {
String proxyType = this.properties.getProperty("ProxyType");
if (proxyType != null && proxyType.equals("OnPremise")) {
Socket socket = new ConnectivitySocks5ProxySocket(getTransportProperty(transportProperty, "socks.host"),
getTransportProperty(transportProperty, "socks.port"), getTransportProperty(transportProperty, "proxy.user"),
getTransportProperty(transportProperty, "proxy.password", " "));

socket.connect(new InetSocketAddress(getTransportProperty(transportProperty, "host"),
Integer.parseInt(getTransportProperty(transportProperty, "port"))));

transport.connect(socket);
} else {
transport.connect(this.properties.getProperty(MAIL_USER), this.properties.getProperty(MAIL_PASSWORD));
Session session = getSession(this.properties);
SMTPTransport transport;
String protocol = properties.getProperty(MAIL_TRANSPORT_PROTOCOL);
if (null == protocol) {
throw new IllegalStateException("Missing property " + MAIL_TRANSPORT_PROTOCOL);
}

MimeMessage mimeMessage = createMimeMessage(session, from, to, cc, bcc, subject, parts);
mimeMessage.saveChanges();
String messageId = mimeMessage.getMessageID();
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
String finalReply = transport.getLastServerResponse();
Map mailResult = new HashMap();
mailResult.put("messageId", messageId);
mailResult.put("finalReply", finalReply);

return mailResult;
} finally {
transport.close();
String transportProperty = protocol.toLowerCase();

transport = switch (transportProperty) {
case SMTP_TRANSPORT -> (SMTPTransport) session.getTransport();
case SMTPS_TRANSPORT -> (SMTPSSLTransport) session.getTransport();
default -> throw new IllegalStateException("Unexpected transport property: " + transportProperty);
};

try {
String proxyType = this.properties.getProperty("ProxyType");
if (proxyType != null && proxyType.equals("OnPremise")) {
Socket socket = new ConnectivitySocks5ProxySocket(getTransportProperty(transportProperty, "socks.host"),
getTransportProperty(transportProperty, "socks.port"), getTransportProperty(transportProperty, "proxy.user"),
getTransportProperty(transportProperty, "proxy.password", " "));

socket.connect(new InetSocketAddress(getTransportProperty(transportProperty, "host"),
Integer.parseInt(getTransportProperty(transportProperty, "port"))));

transport.connect(socket);
} else {
transport.connect(this.properties.getProperty(MAIL_USER), this.properties.getProperty(MAIL_PASSWORD));
}

MimeMessage mimeMessage = createMimeMessage(session, from, to, cc, bcc, subject, parts);
mimeMessage.saveChanges();
String messageId = mimeMessage.getMessageID();
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
String finalReply = transport.getLastServerResponse();
Map mailResult = new HashMap();
mailResult.put("messageId", messageId);
mailResult.put("finalReply", finalReply);

return mailResult;
} finally {
transport.close();
}
} catch (MessagingException | IOException | RuntimeException ex) {
String message = "Failed to send email from [" + from + "] to " + Arrays.toString(to);
LOGGER.error(message, ex); // log the message since the js may not log it properly
throw new MessagingException(message, ex);
}

}
Expand Down Expand Up @@ -150,7 +148,7 @@ protected PasswordAuthentication getPasswordAuthentication() {
* @return the mime message
* @throws MessagingException the messaging exception
*/
private static MimeMessage createMimeMessage(Session smtpSession, String from, String to[], String cc[], String bcc[],
private static MimeMessage createMimeMessage(Session smtpSession, String from, String[] to, String[] cc, String[] bcc,
String subjectText, List<Map> parts) throws MessagingException {

MimeMessage mimeMessage = new MimeMessage(smtpSession);
Expand Down Expand Up @@ -261,4 +259,5 @@ private String getTransportProperty(String transport, String prop) {
private String getTransportProperty(String transport, String prop, String defaultValue) {
return this.properties.getProperty("mail." + transport + "." + prop, defaultValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@
*/
package org.eclipse.dirigible.components.api.mail;

import java.util.Properties;
import java.util.ServiceLoader;

import org.eclipse.dirigible.commons.config.Configuration;
import org.eclipse.dirigible.components.base.spring.BeanProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Optional;
import java.util.Properties;

/**
* The Class MailFacade.
*/
public class MailFacade {

private static final Logger LOGGER = LoggerFactory.getLogger(MailFacade.class);

/** The Constant DIRIGIBLE_MAIL_CONFIG_PROVIDER. */
// Dirigible mail properties
private static final String DIRIGIBLE_MAIL_CONFIG_PROVIDER = "DIRIGIBLE_MAIL_CONFIG_PROVIDER";
Expand All @@ -27,9 +33,6 @@ public class MailFacade {
// Default values
private static final String DEFAULT_PROVIDER_NAME = "environment";

/** The Constant MAIL_PROVIDERS. */
private static final ServiceLoader<MailConfigurationProvider> MAIL_PROVIDERS = ServiceLoader.load(MailConfigurationProvider.class);

/**
* Get MailClient with configuration options from the chosen mail configuration provider.
*
Expand All @@ -38,11 +41,18 @@ public class MailFacade {
public static MailClient getInstance() {
Properties properties = new Properties();
String providerName = Configuration.get(DIRIGIBLE_MAIL_CONFIG_PROVIDER, DEFAULT_PROVIDER_NAME);
for (MailConfigurationProvider next : MAIL_PROVIDERS) {
if (providerName.equals(next.getName())) {
properties.putAll(next.getProperties());
break;
}

Collection<MailConfigurationProvider> providers = BeanProvider.getBeans(MailConfigurationProvider.class);
Optional<MailConfigurationProvider> matchedProvider = providers.stream()
.filter(p -> providerName.equals(p.getName()))
.findFirst();
if (matchedProvider.isPresent()) {
MailConfigurationProvider provider = matchedProvider.get();
LOGGER.info("Will load properties from [{}]", provider);
properties.putAll(provider.getProperties());
} else {
LOGGER.info("Properties will not be loaded from any provider since provider with name [{}] was not found in [{}]", providerName,
providers);
}
return getInstance(properties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Optional;

/**
Expand Down Expand Up @@ -92,4 +93,12 @@ public static <T> Optional<T> getOptionalBean(Class<T> clazz) {
return Optional.empty();
}
}

public static <T> Collection<T> getBeans(Class<T> clazz) {
if (!isInitialzed()) {
throw new IllegalStateException("Spring is not initialized yet.");
}
return context.getBeansOfType(clazz)
.values();
}
}
4 changes: 4 additions & 0 deletions components/engine/engine-bpm-flowable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
<artifactId>liquibase-core</artifactId>
</exclusion>
-->
<exclusion>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
Loading

0 comments on commit cb8ffaf

Please sign in to comment.