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

EPMRPP-90118 || Improve Email configuration to take into account possible deployment under path #1976

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
Expand All @@ -57,14 +58,18 @@ public class FinishLaunchHandlerImpl implements FinishLaunchHandler {
private final FinishHierarchyHandler<Launch> finishHierarchyHandler;
private final ApplicationEventPublisher eventPublisher;

private final String path;

@Autowired
public FinishLaunchHandlerImpl(LaunchRepository launchRepository,
@Qualifier("finishLaunchHierarchyHandler")
FinishHierarchyHandler<Launch> finishHierarchyHandler,
ApplicationEventPublisher eventPublisher) {
ApplicationEventPublisher eventPublisher,
@Value("${server.servlet.context-path:}") String path) {
this.launchRepository = launchRepository;
this.finishHierarchyHandler = finishHierarchyHandler;
this.eventPublisher = eventPublisher;
this.path = path;
}

@Override
Expand Down Expand Up @@ -107,7 +112,7 @@ public FinishLaunchRS finishLaunch(String launchId, FinishExecutionRQ finishLaun
FinishLaunchRS response = new FinishLaunchRS();
response.setId(launch.getUuid());
response.setNumber(launch.getNumber());
response.setLink(generateLaunchLink(baseUrl, projectDetails.getProjectName(),
response.setLink(generateLaunchLink(baseUrl, path, projectDetails.getProjectName(),
String.valueOf(launch.getId())
));
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ private LinkGenerator() {
//static only
}

public static String generateLaunchLink(String baseUrl, String projectName, String id) {
return StringUtils.isEmpty(baseUrl) ? null : baseUrl + UI_PREFIX + projectName + LAUNCHES + id;
public static String generateLaunchLink(String baseUrl, String path, String projectName, String id) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [reviewdog] <com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck> reported by reviewdog 🐶
Line is longer than 100 characters (found 103).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [reviewdog] <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck> reported by reviewdog 🐶
Missing a Javadoc comment.

path = "/".equals(path) ? "" : path.replace("/api", "");
return StringUtils.isEmpty(baseUrl) ? null
: baseUrl + path + UI_PREFIX + projectName + LAUNCHES + id;
}

public static String composeBaseUrl(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class EmailService extends JavaMailSenderImpl {
private String from;
private String rpHost;

private String path;

public EmailService(Properties javaMailProperties) {
super.setJavaMailProperties(javaMailProperties);
}
Expand Down Expand Up @@ -209,9 +211,14 @@ String mergeFinishLaunchText(String url, Launch launch, Set<ProjectIssueType> pr

private String getUrl(String baseUrl) {
return ofNullable(rpHost).map(rh -> {
final UriComponents rpHostUri = UriComponentsBuilder.fromUriString(rh).build();
return UriComponentsBuilder.fromUriString(baseUrl).scheme(rpHostUri.getScheme())
.host(rpHostUri.getHost()).port(rpHostUri.getPort()).build().toUri().toASCIIString();
String processedPath = "/".equals(path) ? "" : path.replace("/api", "");
final UriComponents rpHostUri = UriComponentsBuilder.fromUriString(rpHost).build();
return UriComponentsBuilder.newInstance()
.scheme(rpHostUri.getScheme())
.host(rpHostUri.getHost())
.port(rpHostUri.getPort())
.path(processedPath)
.build().toUri().toASCIIString() + baseUrl;
}).orElse(baseUrl);
}

Expand Down Expand Up @@ -317,6 +324,10 @@ public void setRpHost(String rpHost) {
this.rpHost = rpHost;
}

public void setPath(String path) {
this.path = path;
}

public void sendCreateUserConfirmationEmail(CreateUserRQFull req, String basicUrl) {
MimeMessagePreparator preparator = mimeMessage -> {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "utf-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -63,14 +64,18 @@ public class MailServiceFactory {
private final IntegrationRepository integrationRepository;
private final IntegrationTypeRepository integrationTypeRepository;

private final String path;

@Autowired
public MailServiceFactory(TemplateEngine templateEngine, BasicTextEncryptor encryptor,
IntegrationRepository integrationRepository,
IntegrationTypeRepository integrationTypeRepository) {
IntegrationTypeRepository integrationTypeRepository,
@Value("${server.servlet.context-path:}") String path) {
this.templateEngine = templateEngine;
this.encryptor = encryptor;
this.integrationRepository = integrationRepository;
this.integrationTypeRepository = integrationTypeRepository;
this.path = path;
}

/**
Expand Down Expand Up @@ -116,6 +121,7 @@ && ofNullable(config.get(EmailSettingsEnum.STAR_TLS_ENABLED.getAttribute())).map
}

EmailService service = new EmailService(javaMailProperties);
service.setPath(path);
service.setTemplateEngine(templateEngine);

EmailSettingsEnum.RP_HOST.getAttribute(config).ifPresent(service::setRpHost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.env.Environment;
import org.springframework.test.util.ReflectionTestUtils;

/**
* @author <a href="mailto:[email protected]">Ihar Kahadouski</a>
Expand All @@ -75,12 +78,21 @@ class FinishLaunchHandlerImplTest {
@Mock
private ApplicationEventPublisher publisher;

@Mock
private Environment env;

@InjectMocks
private FinishLaunchHandlerImpl handler;

@InjectMocks
private StopLaunchHandlerImpl stopLaunchHandler;

@BeforeEach
void setUp() {
when(env.getProperty("server.servlet.context-path")).thenReturn("/");
ReflectionTestUtils.setField(handler, "path", env.getProperty("server.servlet.context-path"));
}

@Test
void finishLaunch() {
FinishExecutionRQ finishExecutionRQ = new FinishExecutionRQ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void setUp() {
basicTextEncryptor = new BasicTextEncryptor();
basicTextEncryptor.setPassword("123");
mailServiceFactory = new MailServiceFactory(templateEngine, basicTextEncryptor,
integrationRepository, integrationTypeRepository);
integrationRepository, integrationTypeRepository, "/");
}

@Test
Expand Down
Loading