Skip to content

Commit

Permalink
Merge branch 'Onlineberatung:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
web-mi authored Nov 2, 2023
2 parents dad4e7b + c36e97e commit 458abc0
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
@RequiredArgsConstructor
Expand All @@ -26,10 +24,7 @@ public void addTenantHeader(HttpHeaders headers, Optional<Long> tenantId) {
}
}

public Optional<Long> getTenantFromHeader() {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();
public Optional<Long> getTenantFromHeader(HttpServletRequest request) {
try {
return Optional.of(Long.parseLong(request.getHeader("tenantId")));
} catch (NumberFormatException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class CustomHeaderTenantResolver implements TenantResolver {

@Override
public Optional<Long> resolve(HttpServletRequest request) {
return tenantHeaderSupplier.getTenantFromHeader();
return tenantHeaderSupplier.getTenantFromHeader(request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public class SubdomainTenantResolver implements TenantResolver {

@Override
public Optional<Long> resolve(HttpServletRequest request) {
return resolveTenantFromSubdomain();
return resolveTenantFromSubdomain(request);
}

private Optional<Long> resolveTenantFromSubdomain() {
Optional<String> currentSubdomain = subdomainExtractor.getCurrentSubdomain();
private Optional<Long> resolveTenantFromSubdomain(HttpServletRequest request) {
Optional<String> currentSubdomain = subdomainExtractor.getCurrentSubdomain(request);
if (currentSubdomain.isPresent()) {
return of(getTenantIdBySubdomain(currentSubdomain.get()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes;

import com.google.common.net.InternetDomainName;
import de.caritas.cob.uploadservice.api.service.helper.HttpUrlUtils;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
@Setter
@Slf4j
@AllArgsConstructor
public class SubdomainExtractor {

public Optional<String> getCurrentSubdomain() {
var request = ((ServletRequestAttributes) currentRequestAttributes()).getRequest();
public Optional<String> getCurrentSubdomain(HttpServletRequest request) {
String url = HttpUrlUtils.removeHttpPrefix(request.getServerName());
return getSubdomain(url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CustomHeaderTenantResolverTest {
@Test
void resolve_Should_ResolveTenantId_When_SupplierCanResolveTenantId() {
// given
when(tenantHeaderSupplier.getTenantFromHeader()).thenReturn(Optional.of(2L));
when(tenantHeaderSupplier.getTenantFromHeader(request)).thenReturn(Optional.of(2L));
var resolved = customHeaderTenantResolver.resolve(request);
// then
assertThat(resolved).isEqualTo(Optional.of(2L));
Expand All @@ -34,7 +34,7 @@ void resolve_Should_ResolveTenantId_When_SupplierCanResolveTenantId() {
@Test
void resolve_Should_NotResolveTenantId_When_SupplierCannotResolveTenantId() {
// given
when(tenantHeaderSupplier.getTenantFromHeader()).thenReturn(Optional.empty());
when(tenantHeaderSupplier.getTenantFromHeader(request)).thenReturn(Optional.empty());
var resolved = customHeaderTenantResolver.resolve(request);
// then
assertThat(resolved).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SubdomainTenantResolverTest {
@Test
void resolve_should_resolveTenantId_When_SubdomainCouldBeDetermined() {
// given
when(subdomainExtractor.getCurrentSubdomain()).thenReturn(Optional.of("mucoviscidose"));
when(subdomainExtractor.getCurrentSubdomain(httpServletRequest)).thenReturn(Optional.of("mucoviscidose"));
when(tenantService.getRestrictedTenantDataBySubdomain("mucoviscidose")).thenReturn(
new de.caritas.cob.uploadservice.tenantservice.generated.web.model.RestrictedTenantDTO()
.id(1L));
Expand All @@ -46,7 +46,7 @@ void resolve_should_resolveTenantId_When_SubdomainCouldBeDetermined() {
@Test
void resolve_should_NotResolve_When_SubdomainIsEmpty() {
// given
when(subdomainExtractor.getCurrentSubdomain()).thenReturn(Optional.empty());
when(subdomainExtractor.getCurrentSubdomain(httpServletRequest)).thenReturn(Optional.empty());

// when
Optional<Long> resolve = subdomainTenantResolver.resolve(httpServletRequest);
Expand Down

0 comments on commit 458abc0

Please sign in to comment.