Skip to content

Commit

Permalink
Merge pull request Onlineberatung#209 from Onlineberatung/TSYSTEMS-178
Browse files Browse the repository at this point in the history
fix: pass cookie when calling tenantService to render privacy texts w…
  • Loading branch information
tkuzynow authored Aug 1, 2024
2 parents 3a71160 + 48694d9 commit 206e670
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import de.caritas.cob.agencyservice.tenantservice.generated.ApiClient;
import de.caritas.cob.agencyservice.tenantservice.generated.web.TenantControllerApi;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class TenantServiceApiControllerFactory {

private static final String DEFAULT_LANGUAGE = "de";
private static final String LANGUAGE_COOKIE_NAME = "lang";
@Value("${tenant.service.api.url}")
private String tenantServiceApiUrl;

Expand All @@ -18,6 +26,27 @@ public class TenantServiceApiControllerFactory {

public TenantControllerApi createControllerApi() {
var apiClient = new ApiClient(restTemplate).setBasePath(this.tenantServiceApiUrl);
apiClient.addDefaultCookie(LANGUAGE_COOKIE_NAME, getCurrentLanguageContext());
return new TenantControllerApi(apiClient);
}


public String getCurrentLanguageContext() {
HttpServletRequest currentRequest =
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

if (currentRequest.getCookies() != null) {
Optional<Cookie> languageCookie = findLanguageCookie(currentRequest);
if (languageCookie.isPresent()) {
return languageCookie.get().getValue();
}
}
return DEFAULT_LANGUAGE;
}

private static Optional<Cookie> findLanguageCookie(HttpServletRequest currentRequest) {
return Arrays.stream(currentRequest.getCookies())
.filter(cookie -> LANGUAGE_COOKIE_NAME.equals(cookie.getName()))
.findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.caritas.cob.agencyservice.config.apiclient;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import static org.assertj.core.api.Assertions.assertThat;

class TenantServiceApiControllerFactoryTest {

@AfterEach
void tearDown() {
RequestContextHolder.resetRequestAttributes();
}

@Test
void createControllerApi_Should_CreateApiClientWithoutExceptions() {
// given
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
mockRequest.addHeader("Authorization", "Bearer token");
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest));

var tenantServiceApiControllerFactory = new TenantServiceApiControllerFactory();

// when
var tenantControllerApi = tenantServiceApiControllerFactory.createControllerApi();

// then
assertThat(tenantControllerApi).isNotNull();
}
}

0 comments on commit 206e670

Please sign in to comment.