From f7f039f9dea2cf64c68ef46b368735191c69ca73 Mon Sep 17 00:00:00 2001 From: Tor-Einar Skog <tor-einar.skog@bioforsk.no> Date: Tue, 1 Nov 2016 13:47:12 -0700 Subject: [PATCH] More translation stuff and URL handling --- .gitignore | 1 + pom.xml | 4 +- .../java/no/nibio/vips/i18n/LanguageUtil.java | 45 ++++++------ .../java/no/nibio/vips/util/ServletUtil.java | 28 +++++++- .../no/nibio/vips/util/ServletUtilTest.java | 68 +++++++++++++++++++ 5 files changed, 116 insertions(+), 30 deletions(-) create mode 100644 src/test/java/no/nibio/vips/util/ServletUtilTest.java diff --git a/.gitignore b/.gitignore index a4a8548..9cab156 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target/ classes/ jbossmodule/ +/nbproject/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index 22f0557..ae28fa4 100644 --- a/pom.xml +++ b/pom.xml @@ -90,8 +90,8 @@ <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> - <source>1.7</source> - <target>1.7</target> + <source>1.8</source> + <target>1.8</target> </configuration> </plugin> </plugins> diff --git a/src/main/java/no/nibio/vips/i18n/LanguageUtil.java b/src/main/java/no/nibio/vips/i18n/LanguageUtil.java index 1034175..12862ae 100644 --- a/src/main/java/no/nibio/vips/i18n/LanguageUtil.java +++ b/src/main/java/no/nibio/vips/i18n/LanguageUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 NIBIO <http://www.nibio.no/>. + * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. * * This file is part of VIPSCommon. * VIPSCommon is free software: you can redistribute it and/or modify @@ -19,47 +19,40 @@ package no.nibio.vips.i18n; -import java.util.HashMap; -import java.util.Map; import com.ibm.icu.util.ULocale; +import java.util.Arrays; +import java.util.List; +import static java.util.stream.Collectors.toList; /** - * @copyright 2014 <a href="http://www.nibio.no/">NIBIO</a> + * @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a> * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ public class LanguageUtil { - public static final ULocale[] AVAILABLE_LOCALES = { - new ULocale("en"), // English - new ULocale("nb"), // Norwegian bokmål - new ULocale("bs"), // Bosnian - new ULocale("hr"), // Croatian - new ULocale("sr"), // Serbian - new ULocale("fi"), // Finnish - new ULocale("fr"), // French - new ULocale("de"), // German - new ULocale("lv") // Latvian - }; - - private static ULocale[] distinctLanguages; + private static List<ULocale> distinctLanguages; /** * * @return All available locales with distinct languages + * */ - public static ULocale[] getAvailableLocalesWithDistinctLanguage() + public static List<ULocale> getAvailableLocalesWithDistinctLanguage() { - if(distinctLanguages == null) { - Map<String, ULocale> localeBucket = new HashMap<>(); - for(ULocale locale:LanguageUtil.AVAILABLE_LOCALES) - { - localeBucket.put(locale.getLanguage(), locale); - } - distinctLanguages = localeBucket.values().toArray(new ULocale[0]); + String[] availableLanguages; + try{ + availableLanguages = System.getProperty("no.nibio.vips.logic.AVAILABLE_LANGUAGES").split(","); + } + catch(NullPointerException ex){ + availableLanguages = new String[1]; + availableLanguages[0] = "en"; + } + distinctLanguages = Arrays.asList(availableLanguages).stream() + .map(lang -> new ULocale(lang)) + .collect(toList()); } - return distinctLanguages; } } diff --git a/src/main/java/no/nibio/vips/util/ServletUtil.java b/src/main/java/no/nibio/vips/util/ServletUtil.java index 9e00953..c39a912 100644 --- a/src/main/java/no/nibio/vips/util/ServletUtil.java +++ b/src/main/java/no/nibio/vips/util/ServletUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 NIBIO <http://www.nibio.no/>. + * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. * * This file is part of VIPSCommon. * VIPSCommon is free software: you can redistribute it and/or modify @@ -19,12 +19,16 @@ package no.nibio.vips.util; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Enumeration; +import java.util.List; +import java.util.stream.Collectors; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; /** - * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> + * @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a> * @author Tor-Einar Skog <tor-einar.skog@nibio.no> */ public class ServletUtil { @@ -162,4 +166,24 @@ public class ServletUtil { } return null; } + + /** + * Removes the given parameters from a query string + * @param parametersToRemove + * @return + */ + public static String getCleanedQueryString(String servletPathWithQueryString, String parameterToRemove) + { + List<String> whatsLeft; + String servletPath = servletPathWithQueryString.split("\\?").length > 1 ? + servletPathWithQueryString.split("\\?")[0] + : ""; + whatsLeft = (servletPathWithQueryString.split("\\?").length > 1 ? + Arrays.asList(servletPathWithQueryString.split("\\?")[1].split("&")) : + new ArrayList<String>()) + .stream().filter( + paramStr -> ! paramStr.contains(parameterToRemove) + ).collect(Collectors.toList()); + return servletPath + (!whatsLeft.isEmpty() ? "?" + String.join("&", whatsLeft) : ""); + } } diff --git a/src/test/java/no/nibio/vips/util/ServletUtilTest.java b/src/test/java/no/nibio/vips/util/ServletUtilTest.java new file mode 100644 index 0000000..af57c21 --- /dev/null +++ b/src/test/java/no/nibio/vips/util/ServletUtilTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSCommon. + * VIPSCommon is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSCommon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSCommon. If not, see <http://www.nibio.no/licenses/>. + * + */ +package no.nibio.vips.util; + +import javax.servlet.http.HttpServletRequest; +import junit.framework.TestCase; + +/** + * + * @author treinar + */ +public class ServletUtilTest extends TestCase { + + public ServletUtilTest(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + + /** + * Test of getCleanedQueryString method, of class ServletUtil. + */ + public void testGetCleanedQueryString() { + System.out.println("getCleanedQueryString"); + String servletPathWithQueryString = "/blabla?removeParam=77&keepParam=66&keepAlso=uyt"; + String parameterToRemove = "removeParam"; + String expResult = "/blabla?keepParam=66&keepAlso=uyt"; + String result = ServletUtil.getCleanedQueryString(servletPathWithQueryString, parameterToRemove); + System.out.println("result=" + result); + assertEquals(expResult, result); + servletPathWithQueryString = "/blabla?keepParam=66&removeParam=77&keepAlso=uyt"; + result = ServletUtil.getCleanedQueryString(servletPathWithQueryString, parameterToRemove); + assertEquals(expResult, result); + servletPathWithQueryString = "/blabla?keepParam=66&keepAlso=uyt&removeParam=77"; + result = ServletUtil.getCleanedQueryString(servletPathWithQueryString, parameterToRemove); + assertEquals(expResult, result); + servletPathWithQueryString = "/blabla?removeParam=77"; + expResult = "/blabla"; + result = ServletUtil.getCleanedQueryString(servletPathWithQueryString, parameterToRemove); + assertEquals(expResult, result); + } + +} -- GitLab