From 34542e5cf165185543ec379ab94a3ecd980e4d98 Mon Sep 17 00:00:00 2001 From: Mohamed Karray Date: Fri, 1 Feb 2019 08:51:07 +0100 Subject: [PATCH 1/4] fix i18n encoding --- scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java b/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java index b074781fec..6224b154a7 100644 --- a/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java +++ b/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java @@ -78,6 +78,8 @@ public class I18nServlet extends HttpServlet { @VisibleForTesting @Override protected void doGet(HttpServletRequest req, HttpServletResponse response) { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); try (PrintWriter out = response.getWriter()) { response.setContentType("application/json"); String path = req.getServletPath(); From 0a29f41835829ef110384b86a90f87525bc7feec Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 1 Feb 2019 09:42:19 +0100 Subject: [PATCH 2/4] verify encoding and content type --- .../src/main/java/sonia/scm/web/i18n/I18nServlet.java | 1 - .../test/java/sonia/scm/web/i18n/I18nServletTest.java | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java b/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java index 6224b154a7..08a4a33493 100644 --- a/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java +++ b/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java @@ -81,7 +81,6 @@ public class I18nServlet extends HttpServlet { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json"); try (PrintWriter out = response.getWriter()) { - response.setContentType("application/json"); String path = req.getServletPath(); Function> jsonFileProvider = usedPath -> Optional.empty(); BiConsumer createdJsonFileConsumer = (usedPath, jsonNode) -> log.debug("A json File is created from the path {}", usedPath); diff --git a/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java b/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java index a912f738e2..4874acab37 100644 --- a/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java +++ b/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java @@ -194,6 +194,8 @@ public class I18nServletTest { assertJson(json); verify(cache).get(path); verify(cache).put(eq(path), any()); + + verifyHeaders(response); } @Test @@ -221,6 +223,8 @@ public class I18nServletTest { verify(cache, never()).put(eq(path), any()); verify(cache).get(path); assertJson(json); + + verifyHeaders(response); } @Test @@ -234,6 +238,11 @@ public class I18nServletTest { assertJson(jsonNodeOptional.orElse(null)); } + private void verifyHeaders(HttpServletResponse response) { + verify(response).setCharacterEncoding("UTF-8"); + verify(response).setContentType("application/json"); + } + public void assertJson(JsonNode actual) throws IOException { assertJson(actual.toString()); } From c249d603d73da8e007a1d43f2859173a1e4b6c3f Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 1 Feb 2019 09:44:25 +0100 Subject: [PATCH 3/4] remove unused shiro rule and fixed some deprecated method calls --- .../sonia/scm/web/i18n/I18nServletTest.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java b/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java index 4874acab37..e028857e2c 100644 --- a/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java +++ b/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java @@ -2,8 +2,6 @@ package sonia.scm.web.i18n; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.sdorra.shiro.ShiroRule; -import com.github.sdorra.shiro.SubjectAware; import com.google.common.base.Charsets; import com.google.common.io.Files; import org.apache.commons.lang3.StringUtils; @@ -42,12 +40,8 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.Silent.class) -@SubjectAware(configuration = "classpath:sonia/scm/shiro-001.ini") public class I18nServletTest { - @Rule - public ShiroRule shiro = new ShiroRule(); - private static final String GIT_PLUGIN_JSON = json( "{", "'scm-git-plugin': {", @@ -88,15 +82,15 @@ public class I18nServletTest { public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Mock - PluginLoader pluginLoader; + private PluginLoader pluginLoader; @Mock - CacheManager cacheManager; + private CacheManager cacheManager; @Mock - ClassLoader classLoader; + private ClassLoader classLoader; - I18nServlet servlet; + private I18nServlet servlet; @Mock private Cache cache; @@ -106,9 +100,9 @@ public class I18nServletTest { @SuppressWarnings("unchecked") public void init() throws IOException { resources = Collections.enumeration(Lists.newArrayList( - createFileFromString(SVN_PLUGIN_JSON).toURL(), - createFileFromString(GIT_PLUGIN_JSON).toURL(), - createFileFromString(HG_PLUGIN_JSON).toURL() + createFileFromString(SVN_PLUGIN_JSON).toURI().toURL(), + createFileFromString(GIT_PLUGIN_JSON).toURI().toURL(), + createFileFromString(HG_PLUGIN_JSON).toURI().toURL() )); when(pluginLoader.getUberClassLoader()).thenReturn(classLoader); when(cacheManager.getCache(I18nServlet.CACHE_NAME)).thenReturn(cache); @@ -247,7 +241,7 @@ public class I18nServletTest { assertJson(actual.toString()); } - public void assertJson(String actual) throws IOException { + private void assertJson(String actual) throws IOException { assertThat(actual) .isNotEmpty() .contains(StringUtils.deleteWhitespace(GIT_PLUGIN_JSON.substring(1, GIT_PLUGIN_JSON.length() - 1))) @@ -255,7 +249,7 @@ public class I18nServletTest { .contains(StringUtils.deleteWhitespace(SVN_PLUGIN_JSON.substring(1, SVN_PLUGIN_JSON.length() - 1))); } - public File createFileFromString(String json) throws IOException { + private File createFileFromString(String json) throws IOException { File file = temporaryFolder.newFile(); Files.write(json.getBytes(Charsets.UTF_8), file); return file; From c5c66ef4ea87568826e69e69bad2c41eb2c9dede Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 1 Feb 2019 09:55:03 +0000 Subject: [PATCH 4/4] Close branch bugfix/encoding_i18n_servlet