Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mail Server: Fix endpoint, support setting all attributes #76

Merged
merged 1 commit into from
May 17, 2024
Merged
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 @@ -2,6 +2,7 @@

import com.atlassian.crowd.manager.mail.MailConfiguration;
import com.atlassian.crowd.util.mail.SMTPServer;
import de.aservo.confapi.commons.exception.BadRequestException;
import de.aservo.confapi.commons.model.MailServerSmtpBean;

import javax.annotation.Nullable;
Expand All @@ -15,38 +16,112 @@ public class MailServerSmtpBeanUtil {
public static MailServerSmtpBean toMailServerSmtpBean(
@Nullable final MailConfiguration mailConfiguration) {

if (mailConfiguration == null) {
if (mailConfiguration == null || mailConfiguration.getSmtpServer() == null) {
return null;
}

return new MailServerSmtpBean(
mailConfiguration.getNotificationEmails() == null || mailConfiguration.getNotificationEmails().isEmpty() ?
null : mailConfiguration.getNotificationEmails().get(0),
mailConfiguration.getSmtpServer() == null || mailConfiguration.getSmtpServer().getFrom() == null ?
null : mailConfiguration.getSmtpServer().getFrom().toString(),
mailConfiguration.getSmtpServer() == null ?
null : mailConfiguration.getSmtpServer().getPrefix(),
mailConfiguration.getSmtpServer() == null ?
null : mailConfiguration.getSmtpServer().getHost()
);
final MailServerSmtpBean mailServerSmtpBean = new MailServerSmtpBean();

if (mailConfiguration.getNotificationEmails() != null && !mailConfiguration.getNotificationEmails().isEmpty()) {
mailServerSmtpBean.setAdminContact(mailConfiguration.getNotificationEmails().get(0));
}

final SMTPServer mailConfigurationSmtp = mailConfiguration.getSmtpServer();

if (mailConfigurationSmtp.getFrom() != null) {
mailServerSmtpBean.setFrom(mailConfigurationSmtp.getFrom().toString());
}

mailServerSmtpBean.setPrefix(mailConfigurationSmtp.getPrefix());
mailServerSmtpBean.setHost(mailConfigurationSmtp.getHost());
mailServerSmtpBean.setPort(mailConfigurationSmtp.getPort());
mailServerSmtpBean.setUsername(mailConfigurationSmtp.getUsername());
// don't return password here
mailServerSmtpBean.setTimeout((long) mailConfigurationSmtp.getTimeout());

// TODO: After the build blocker in ConfAPI commons has been removed,
// implement proper distinguishing between useTls and startTls,
// see https://github.com/aservo/confapi-commons/issues/153

return mailServerSmtpBean;
}

@Nullable
public static MailConfiguration toMailConfiguration(
@Nullable final MailServerSmtpBean mailServerSmtpBean) {

return toMailConfiguration(mailServerSmtpBean, null);
}

@Nullable
public static MailConfiguration toMailConfiguration(
@Nullable final MailServerSmtpBean mailServerSmtpBean) throws AddressException {
@Nullable final MailServerSmtpBean mailServerSmtpBean,
@Nullable final MailConfiguration mailConfiguration) {

if (mailServerSmtpBean == null) {
return null;
}

return MailConfiguration.builder()
.setNotificationEmails(Collections.singletonList(mailServerSmtpBean.getAdminContact()))
.setSmtpServer(SMTPServer.builder()
.setFrom(new InternetAddress(mailServerSmtpBean.getFrom()))
.setPrefix(mailServerSmtpBean.getPrefix())
.setHost(mailServerSmtpBean.getHost())
.build())
.build();
final MailConfiguration.Builder mailConfigurationBuilder;
final SMTPServer.Builder smtpServerBuilder;

if (mailConfiguration != null) {
mailConfigurationBuilder = MailConfiguration.builder(mailConfiguration);

if (mailConfiguration.getSmtpServer() != null) {
smtpServerBuilder = SMTPServer.builder(mailConfiguration.getSmtpServer());
} else {
smtpServerBuilder = SMTPServer.builder();
}
} else {
mailConfigurationBuilder = MailConfiguration.builder();
smtpServerBuilder = SMTPServer.builder();
}

if (mailServerSmtpBean.getAdminContact() != null) {
mailConfigurationBuilder.setNotificationEmails(Collections.singletonList(mailServerSmtpBean.getAdminContact()));
}

if (mailServerSmtpBean.getFrom() != null) {
try {
smtpServerBuilder.setFrom(new InternetAddress(mailServerSmtpBean.getFrom()));
} catch (AddressException e) {
throw new BadRequestException(e.getMessage());
}
}

if (mailServerSmtpBean.getPrefix() != null) {
smtpServerBuilder.setPrefix(mailServerSmtpBean.getPrefix());
}

if (mailServerSmtpBean.getHost() != null) {
smtpServerBuilder.setHost(mailServerSmtpBean.getHost());
}

if (mailServerSmtpBean.getPort() != null) {
smtpServerBuilder.setPort(mailServerSmtpBean.getPort());
}

if (mailServerSmtpBean.getUsername() != null) {
smtpServerBuilder.setUsername(mailServerSmtpBean.getUsername());
}

if (mailServerSmtpBean.getPassword() != null) {
smtpServerBuilder.setPassword(mailServerSmtpBean.getPassword());
}

if (mailServerSmtpBean.getTimeout() != null) {
smtpServerBuilder.setTimeout(mailServerSmtpBean.getTimeout().intValue());
}

// TODO: After the build blocker in ConfAPI commons has been removed,
// implement proper distinguishing between useTls and startTls,
// see https://github.com/aservo/confapi-commons/issues/153
smtpServerBuilder.setStartTLS(mailServerSmtpBean.getUseTls());

mailConfigurationBuilder.setSmtpServer(smtpServerBuilder.build());

return mailConfigurationBuilder.build();
}

private MailServerSmtpBeanUtil() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

import com.atlassian.crowd.manager.mail.MailConfiguration;
import com.atlassian.crowd.manager.mail.MailConfigurationService;
import com.atlassian.crowd.util.mail.SMTPServer;
import com.atlassian.plugin.spring.scanner.annotation.export.ExportAsService;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import de.aservo.confapi.commons.exception.BadRequestException;
import de.aservo.confapi.commons.model.MailServerPopBean;
import de.aservo.confapi.commons.model.MailServerSmtpBean;
import de.aservo.confapi.commons.service.api.MailServerService;
import de.aservo.confapi.crowd.model.util.MailServerSmtpBeanUtil;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.util.Collections;

@Component
@ExportAsService(MailServerService.class)
Expand Down Expand Up @@ -45,48 +40,19 @@ public MailServerSmtpBean setMailServerSmtp(
final MailServerSmtpBean mailServerSmtpBean) {

final MailConfiguration mailConfiguration = mailConfigurationService.getMailConfiguration();
final MailConfiguration.Builder newMailConfigurationBuilder = MailConfiguration.builder(mailConfiguration);

if (mailServerSmtpBean.getAdminContact() != null) {
newMailConfigurationBuilder.setNotificationEmails(Collections.singletonList(mailServerSmtpBean.getAdminContact()));
}

//noinspection ConstantConditions
final SMTPServer.Builder smtpServerBuilder = mailConfiguration != null && mailConfiguration.getSmtpServer() != null
? SMTPServer.builder(mailConfiguration.getSmtpServer()) : SMTPServer.builder();

if (mailServerSmtpBean.getFrom() != null) {
try {
smtpServerBuilder.setFrom(new InternetAddress(mailServerSmtpBean.getFrom()));
} catch (AddressException e) {
throw new BadRequestException(e.getMessage());
}
}

if (mailServerSmtpBean.getPrefix() != null) {
smtpServerBuilder.setPrefix(mailServerSmtpBean.getPrefix());
}

if (mailServerSmtpBean.getHost() != null) {
smtpServerBuilder.setHost(mailServerSmtpBean.getHost());
}

newMailConfigurationBuilder.setSmtpServer(smtpServerBuilder.build());

mailConfigurationService.saveConfiguration(newMailConfigurationBuilder.build());

mailConfigurationService.saveConfiguration(MailServerSmtpBeanUtil.toMailConfiguration(mailServerSmtpBean, mailConfiguration));
return getMailServerSmtp();
}

@Override
public MailServerPopBean getMailServerPop() {
throw new UnsupportedOperationException("Getting POP mail server is not implemented");
throw new UnsupportedOperationException("Getting POP mail server is not supported by Crowd");
}

@Override
public MailServerPopBean setMailServerPop(
final MailServerPopBean mailServerPopBean) {

throw new UnsupportedOperationException("Setting POP mail server is not implemented");
throw new UnsupportedOperationException("Setting POP mail server is not supported by Crowd");
}
}
Loading