-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
659 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...m-it/belgif-rest-problem-it-common/src/main/resources/com/acme/custom/Messages.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CustomProblem.detail=Custom detail |
1 change: 1 addition & 0 deletions
1
...t/belgif-rest-problem-it-common/src/main/resources/com/acme/custom/Messages_de.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CustomProblem.detail=DE detail |
1 change: 1 addition & 0 deletions
1
...t/belgif-rest-problem-it-common/src/main/resources/com/acme/custom/Messages_fr.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CustomProblem.detail=FR detail |
1 change: 1 addition & 0 deletions
1
...t/belgif-rest-problem-it-common/src/main/resources/com/acme/custom/Messages_nl.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CustomProblem.detail=NL detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...m-java-ee/src/main/java/io/github/belgif/rest/problem/jaxrs/I18NAcceptLanguageFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.github.belgif.rest.problem.jaxrs; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.servlet.ServletContext; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.container.ContainerResponseContext; | ||
import javax.ws.rs.container.ContainerResponseFilter; | ||
import javax.ws.rs.container.PreMatching; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import io.github.belgif.rest.problem.i18n.I18N; | ||
|
||
/** | ||
* Filter that registers the requested locale, as specified in Accept-Language HTTP header, | ||
* with the I18N helper (and clears it afterward). | ||
*/ | ||
@PreMatching | ||
@Provider | ||
public class I18NAcceptLanguageFilter implements ContainerRequestFilter, ContainerResponseFilter { | ||
|
||
public static final String I18N_FLAG = "io.github.belgif.rest.problem.i18n"; | ||
|
||
@Context | ||
private ServletContext servletContext; | ||
|
||
private boolean enabled = true; | ||
|
||
@PostConstruct | ||
public void initialize() { | ||
if (servletContext.getInitParameter(I18N_FLAG) != null) { | ||
enabled = Boolean.parseBoolean(servletContext.getInitParameter(I18N_FLAG)); | ||
} else if (System.getProperty(I18N_FLAG) != null) { | ||
enabled = Boolean.parseBoolean(System.getProperty(I18N_FLAG)); | ||
} else if (System.getenv(I18N_FLAG) != null) { | ||
enabled = Boolean.parseBoolean(System.getenv(I18N_FLAG)); | ||
} else { | ||
enabled = true; | ||
} | ||
} | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext) { | ||
if (enabled && !requestContext.getAcceptableLanguages().isEmpty()) { | ||
I18N.setRequestLocale(requestContext.getAcceptableLanguages().get(0)); | ||
} | ||
} | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) { | ||
if (enabled) { | ||
I18N.clearRequestLocale(); | ||
} | ||
} | ||
|
||
protected void setServletContext(ServletContext servletContext) { | ||
this.servletContext = servletContext; | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...va-ee/src/test/java/io/github/belgif/rest/problem/jaxrs/I18NAcceptLanguageFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package io.github.belgif.rest.problem.jaxrs; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.Collections; | ||
import java.util.Locale; | ||
|
||
import javax.servlet.ServletContext; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerResponseContext; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junitpioneer.jupiter.SetEnvironmentVariable; | ||
import org.junitpioneer.jupiter.SetSystemProperty; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import io.github.belgif.rest.problem.i18n.I18N; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class I18NAcceptLanguageFilterTest { | ||
|
||
@InjectMocks | ||
private I18NAcceptLanguageFilter filter; | ||
|
||
@Mock | ||
private ContainerRequestContext requestContext; | ||
|
||
@Mock | ||
private ContainerResponseContext responseContext; | ||
|
||
@Mock | ||
private ServletContext servletContext; | ||
|
||
@AfterEach | ||
void cleanup() { | ||
I18N.clearRequestLocale(); | ||
} | ||
|
||
@Test | ||
void languageRequested() { | ||
when(requestContext.getAcceptableLanguages()).thenReturn(Collections.singletonList(new Locale("nl", "BE"))); | ||
filter.filter(requestContext); | ||
assertThat(I18N.getRequestLocale()).isEqualTo(new Locale("nl", "BE")); | ||
} | ||
|
||
@Test | ||
void noLanguageRequested() { | ||
when(requestContext.getAcceptableLanguages()).thenReturn(Collections.emptyList()); | ||
filter.filter(requestContext); | ||
assertThat(I18N.getRequestLocale()).isEqualTo(new Locale("en")); | ||
} | ||
|
||
@Test | ||
void clearLocaleAfterResponse() { | ||
I18N.setRequestLocale(new Locale("nl", "BE")); | ||
filter.filter(requestContext, responseContext); | ||
assertThat(I18N.getRequestLocale()).isEqualTo(new Locale("en")); | ||
} | ||
|
||
@Test | ||
void disabledViaInitParam() { | ||
when(servletContext.getInitParameter(I18NAcceptLanguageFilter.I18N_FLAG)).thenReturn("false"); | ||
filter.initialize(); | ||
filter.filter(requestContext); | ||
verifyNoInteractions(requestContext); | ||
} | ||
|
||
@Test | ||
@SetSystemProperty(key = I18NAcceptLanguageFilter.I18N_FLAG, value = "false") | ||
void disabledViaSystemProperty() { | ||
when(servletContext.getInitParameter(I18NAcceptLanguageFilter.I18N_FLAG)).thenReturn(null); | ||
filter.initialize(); | ||
filter.filter(requestContext); | ||
verifyNoInteractions(requestContext); | ||
} | ||
|
||
@Test | ||
@SetEnvironmentVariable(key = I18NAcceptLanguageFilter.I18N_FLAG, value = "false") | ||
void disabledViaEnvironmentVariable() { | ||
when(servletContext.getInitParameter(I18NAcceptLanguageFilter.I18N_FLAG)).thenReturn(null); | ||
filter.initialize(); | ||
filter.filter(requestContext); | ||
verifyNoInteractions(requestContext); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...m-spring/src/main/java/io/github/belgif/rest/problem/spring/I18NAcceptLanguageFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.github.belgif.rest.problem.spring; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.support.RequestContextUtils; | ||
|
||
import io.github.belgif.rest.problem.i18n.I18N; | ||
|
||
/** | ||
* Filter that registers the requested locale, as specified in Accept-Language HTTP header, | ||
* with the I18N helper (and clears it afterward). | ||
*/ | ||
@Component | ||
@ConditionalOnProperty(prefix = "io.github.belgif.rest.problem", name = "i18n", havingValue = "true", | ||
matchIfMissing = true) | ||
public class I18NAcceptLanguageFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) | ||
throws IOException, ServletException { | ||
I18N.setRequestLocale(RequestContextUtils.getLocale((HttpServletRequest) servletRequest)); | ||
try { | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
} finally { | ||
I18N.clearRequestLocale(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ring/src/test/java/io/github/belgif/rest/problem/spring/I18NAcceptLanguageFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.github.belgif.rest.problem.spring; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
|
||
import io.github.belgif.rest.problem.i18n.I18N; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class I18NAcceptLanguageFilterTest { | ||
|
||
private final I18NAcceptLanguageFilter filter = new I18NAcceptLanguageFilter(); | ||
|
||
@Mock | ||
private FilterChain filterChain; | ||
|
||
@Test | ||
void languageRequested() throws ServletException, IOException { | ||
MockHttpServletRequest servletRequest = new MockHttpServletRequest(); | ||
MockHttpServletResponse servletResponse = new MockHttpServletResponse(); | ||
servletRequest.addHeader("Accept-Language", "nl-BE"); | ||
AtomicReference<Locale> locale = new AtomicReference<>(); | ||
Mockito.doAnswer(invocationOnMock -> { | ||
locale.set(I18N.getRequestLocale()); | ||
return null; | ||
}).when(filterChain).doFilter(servletRequest, servletResponse); | ||
filter.doFilter(servletRequest, servletResponse, filterChain); | ||
// during the request processing, locale should be nl-BE | ||
assertThat(locale).hasValue(new Locale("nl", "BE")); | ||
// after the request processing, locale should be restored to default = en | ||
assertThat(I18N.getRequestLocale()).isEqualTo(new Locale("en")); | ||
} | ||
|
||
@Test | ||
void noLanguageRequested() throws ServletException, IOException { | ||
MockHttpServletRequest servletRequest = new MockHttpServletRequest(); | ||
MockHttpServletResponse servletResponse = new MockHttpServletResponse(); | ||
AtomicReference<Locale> locale = new AtomicReference<>(); | ||
Mockito.doAnswer(invocationOnMock -> { | ||
locale.set(I18N.getRequestLocale()); | ||
return null; | ||
}).when(filterChain).doFilter(servletRequest, servletResponse); | ||
filter.doFilter(servletRequest, servletResponse, filterChain); | ||
// during the request processing, locale should be default = en | ||
assertThat(locale).hasValue(new Locale("en")); | ||
// after the request processing, locale should be restored to default = en | ||
assertThat(I18N.getRequestLocale()).isEqualTo(new Locale("en")); | ||
} | ||
|
||
} |
Oops, something went wrong.