email service
This commit is contained in:
parent
3c733ddc8c
commit
7702335893
@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.knecon.fforesight.tenantusermanagement.model.SMTPConfiguration;
|
||||
import com.knecon.fforesight.tenantusermanagement.model.SMTPResponse;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
@ -43,7 +44,7 @@ public interface SMTPConfigurationResource {
|
||||
@PostMapping(value = SMTP_PATH + TEST_PATH, consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||
@Operation(summary = "Test SMTP Settings to KeyCloak")
|
||||
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "SMTP Configuration is valid."), @ApiResponse(responseCode = "400", description = "SMTP test failed.")})
|
||||
void testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfigurationModel, @RequestParam(value = TEST_EMAIL, required = false) String testEmail);
|
||||
SMTPResponse testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfigurationModel, @RequestParam(value = TEST_EMAIL, required = false) String testEmail);
|
||||
|
||||
|
||||
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
||||
|
||||
@ -18,6 +18,7 @@ import com.knecon.fforesight.tenantcommons.TenantContext;
|
||||
import com.knecon.fforesight.tenantusermanagement.api.external.PublicResource;
|
||||
import com.knecon.fforesight.tenantusermanagement.api.external.SMTPConfigurationResource;
|
||||
import com.knecon.fforesight.tenantusermanagement.model.SMTPConfiguration;
|
||||
import com.knecon.fforesight.tenantusermanagement.model.SMTPResponse;
|
||||
import com.knecon.fforesight.tenantusermanagement.service.EmailService;
|
||||
import com.knecon.fforesight.tenantusermanagement.service.RealmService;
|
||||
|
||||
@ -68,7 +69,7 @@ public class SMTPConfigurationController implements SMTPConfigurationResource, P
|
||||
@SneakyThrows
|
||||
@Override
|
||||
@PreAuthorize("hasAuthority('" + WRITE_SMTP_CONFIGURATION + "')")
|
||||
public void testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfiguration, @RequestParam(value = TEST_EMAIL, required = false) String testEmail) {
|
||||
public SMTPResponse testSMTPConfiguration(@RequestBody SMTPConfiguration smtpConfiguration, @RequestParam(value = TEST_EMAIL, required = false) String testEmail) {
|
||||
|
||||
String targetEmail;
|
||||
if (StringUtils.isBlank(testEmail)) {
|
||||
@ -81,7 +82,7 @@ public class SMTPConfigurationController implements SMTPConfigurationResource, P
|
||||
updatePassword(smtpConfiguration);
|
||||
smtpConfiguration.setPassword(encryptionDecryptionService.decrypt(smtpConfiguration.getPassword()));
|
||||
|
||||
emailService.send(convertSMTPConfigurationModelToMap(smtpConfiguration), targetEmail, "Redaction Test message", "This is a test message");
|
||||
return emailService.send(convertSMTPConfigurationModelToMap(smtpConfiguration), targetEmail, "Redaction Test message", "This is a test message");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -0,0 +1,22 @@
|
||||
|
||||
package com.knecon.fforesight.tenantusermanagement.model;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(description = "Object containing a simplified version of the SMTP test connection response.")
|
||||
public class SMTPResponse {
|
||||
|
||||
@Schema(description = "Parameter containing status code of the response.")
|
||||
private int statusCode;
|
||||
@Schema(description = "Parameter containing the reason phrase of the response.")
|
||||
private String reasonPhrase;
|
||||
|
||||
}
|
||||
@ -11,6 +11,8 @@ import javax.ws.rs.BadRequestException;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.knecon.fforesight.tenantusermanagement.model.SMTPResponse;
|
||||
|
||||
import jakarta.mail.Address;
|
||||
import jakarta.mail.Message;
|
||||
import jakarta.mail.MessagingException;
|
||||
@ -32,7 +34,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
public class EmailService {
|
||||
|
||||
|
||||
public void send(Map<String, String> config, String address, String subject, String textBody) {
|
||||
public SMTPResponse send(Map<String, String> config, String address, String subject, String textBody) {
|
||||
|
||||
Transport transport = null;
|
||||
try {
|
||||
@ -112,8 +114,15 @@ public class EmailService {
|
||||
}
|
||||
|
||||
transport.sendMessage(msg, new InternetAddress[]{new InternetAddress(address)});
|
||||
|
||||
return SMTPResponse.builder()
|
||||
.statusCode(200)
|
||||
.build();
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException("Failed to send e-mail", e);
|
||||
return SMTPResponse.builder()
|
||||
.statusCode(400)
|
||||
.reasonPhrase(e.getMessage())
|
||||
.build();
|
||||
} finally {
|
||||
if (transport != null) {
|
||||
try {
|
||||
|
||||
@ -27,14 +27,7 @@ public class SMTPConfigurationTest extends AbstractTenantUserManagementIntegrati
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
SMTPConfiguration newConfig = new SMTPConfiguration();
|
||||
newConfig.setAuth(true);
|
||||
newConfig.setFrom("from@knecon.com");
|
||||
newConfig.setHost("test.knecon.com");
|
||||
newConfig.setPassword("secret");
|
||||
newConfig.setUser("user");
|
||||
newConfig.setStarttls(true);
|
||||
newConfig.setSsl(false);
|
||||
SMTPConfiguration newConfig = provideTestSMTPConfiguration();
|
||||
smtpConfigurationClient.updateSMTPConfiguration(newConfig);
|
||||
|
||||
var currentSMTPConfiguration = smtpConfigurationClient.getCurrentSMTPConfiguration();
|
||||
@ -50,4 +43,34 @@ public class SMTPConfigurationTest extends AbstractTenantUserManagementIntegrati
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSMTPConnection() {
|
||||
|
||||
TenantContext.setTenantId(AbstractTenantUserManagementIntegrationTest.TEST_TENANT_ID);
|
||||
|
||||
SMTPConfiguration smtpConfiguration = provideTestSMTPConfiguration();
|
||||
|
||||
var response = smtpConfigurationClient.testSMTPConfiguration(smtpConfiguration, "");
|
||||
|
||||
// Fails because we are not using a smtp config
|
||||
assertThat(response.getStatusCode()).isEqualTo(400);
|
||||
assertThat(response.getReasonPhrase()).isEqualTo("Couldn't connect to host, port: test.knecon.com, 25; timeout 10000");
|
||||
|
||||
TenantContext.clear();
|
||||
}
|
||||
|
||||
private SMTPConfiguration provideTestSMTPConfiguration() {
|
||||
|
||||
SMTPConfiguration smtpConfiguration = new SMTPConfiguration();
|
||||
smtpConfiguration.setAuth(true);
|
||||
smtpConfiguration.setFrom("from@knecon.com");
|
||||
smtpConfiguration.setHost("test.knecon.com");
|
||||
smtpConfiguration.setPassword("secret");
|
||||
smtpConfiguration.setUser("user");
|
||||
smtpConfiguration.setStarttls(true);
|
||||
smtpConfiguration.setSsl(false);
|
||||
|
||||
return smtpConfiguration;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user