Skip to content
Snippets Groups Projects
Commit f7f039f9 authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

More translation stuff and URL handling

parent cbc1f998
Branches
Tags
No related merge requests found
target/ target/
classes/ classes/
jbossmodule/ jbossmodule/
/nbproject/
\ No newline at end of file
...@@ -90,8 +90,8 @@ ...@@ -90,8 +90,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> <version>2.3.2</version>
<configuration> <configuration>
<source>1.7</source> <source>1.8</source>
<target>1.7</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
......
/* /*
* Copyright (c) 2014 NIBIO <http://www.nibio.no/>. * Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
* *
* This file is part of VIPSCommon. * This file is part of VIPSCommon.
* VIPSCommon is free software: you can redistribute it and/or modify * VIPSCommon is free software: you can redistribute it and/or modify
...@@ -19,47 +19,40 @@ ...@@ -19,47 +19,40 @@
package no.nibio.vips.i18n; package no.nibio.vips.i18n;
import java.util.HashMap;
import java.util.Map;
import com.ibm.icu.util.ULocale; 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> * @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/ */
public class LanguageUtil { public class LanguageUtil {
public static final ULocale[] AVAILABLE_LOCALES = { private static List<ULocale> distinctLanguages;
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;
/** /**
* *
* @return All available locales with distinct languages * @return All available locales with distinct languages
*
*/ */
public static ULocale[] getAvailableLocalesWithDistinctLanguage() public static List<ULocale> getAvailableLocalesWithDistinctLanguage()
{ {
if(distinctLanguages == null) if(distinctLanguages == null)
{ {
Map<String, ULocale> localeBucket = new HashMap<>(); String[] availableLanguages;
for(ULocale locale:LanguageUtil.AVAILABLE_LOCALES) try{
{ availableLanguages = System.getProperty("no.nibio.vips.logic.AVAILABLE_LANGUAGES").split(",");
localeBucket.put(locale.getLanguage(), locale); }
} catch(NullPointerException ex){
distinctLanguages = localeBucket.values().toArray(new ULocale[0]); availableLanguages = new String[1];
availableLanguages[0] = "en";
}
distinctLanguages = Arrays.asList(availableLanguages).stream()
.map(lang -> new ULocale(lang))
.collect(toList());
} }
return distinctLanguages; return distinctLanguages;
} }
} }
/* /*
* Copyright (c) 2014 NIBIO <http://www.nibio.no/>. * Copyright (c) 2016 NIBIO <http://www.nibio.no/>.
* *
* This file is part of VIPSCommon. * This file is part of VIPSCommon.
* VIPSCommon is free software: you can redistribute it and/or modify * VIPSCommon is free software: you can redistribute it and/or modify
...@@ -19,12 +19,16 @@ ...@@ -19,12 +19,16 @@
package no.nibio.vips.util; package no.nibio.vips.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; 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> * @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/ */
public class ServletUtil { public class ServletUtil {
...@@ -162,4 +166,24 @@ public class ServletUtil { ...@@ -162,4 +166,24 @@ public class ServletUtil {
} }
return null; 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) : "");
}
} }
/*
* 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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment