From bd186b60ef43983486d731ea7f6f1e7809f0e8da Mon Sep 17 00:00:00 2001 From: Jan Stastny Date: Mon, 19 Oct 2015 10:41:17 +0200 Subject: [PATCH] FIX: Correctly escape parameters in URL. --- src/main/java/de/meggsimum/w3w/What3Words.java | 13 +++++++------ src/test/java/de/meggsimum/w3w/What3WordsTest.java | 10 ++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/meggsimum/w3w/What3Words.java b/src/main/java/de/meggsimum/w3w/What3Words.java index 0c925d1..62e75aa 100644 --- a/src/main/java/de/meggsimum/w3w/What3Words.java +++ b/src/main/java/de/meggsimum/w3w/What3Words.java @@ -8,6 +8,7 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLEncoder; import org.json.JSONArray; import org.json.JSONObject; @@ -15,7 +16,7 @@ /** * A Java wrapper for the What3Words Web-API. * - * @see http://developer.what3words.com/api/ + * @see http://developer.what3words.com/api/ * * @author Christian Mayer, meggsimum */ @@ -83,14 +84,14 @@ public double[] wordsToPosition(String[] words) throws IOException, */ public double[] wordsToPosition(String[] words, String language) throws IOException, What3WordsException { - String url = String.format("%sw3w?key=%s&string=%s.%s.%s&lang=%s" ,this.baseUrl, this.apiKey, words[0], words[1], words[2], language); + String url = String.format("%sw3w?key=%s&string=%s.%s.%s&lang=%s" ,this.baseUrl, URLEncoder.encode(this.apiKey, "UTF-8"), URLEncoder.encode(words[0], "UTF-8"), URLEncoder.encode(words[1], "UTF-8"), URLEncoder.encode(words[2], "UTF-8"), language); String response = this.doHttpGet(url); try { // parse the coordinates out of the JSON JSONObject json = new JSONObject(response); - if (json.has("position") == true) { + if (json.has("position")) { JSONArray jsonCoords = (JSONArray) json.get("position"); double[] coords = new double[2]; coords[0] = jsonCoords.getDouble(0); @@ -141,7 +142,7 @@ public String[] positionToWords(double[] coords) */ public String[] positionToWords(double[] coords, String language) throws What3WordsException, IOException { - String url = String.format("%sposition?key=%s&position=%s,%s&lang=%s" ,this.baseUrl, this.apiKey, coords[0], coords[1], language); + String url = String.format("%sposition?key=%s&position=%s,%s&lang=%s" ,this.baseUrl, URLEncoder.encode(this.apiKey, "UTF-8"), coords[0], coords[1], language); String response = this.doHttpGet(url); @@ -149,7 +150,7 @@ public String[] positionToWords(double[] coords, String language) // parse the words out of the JSON JSONObject json = new JSONObject(response); - if (json.has("words") == true) { + if (json.has("words")) { JSONArray jsonWords = (JSONArray) json.get("words"); String[] words = new String[3]; words[0] = jsonWords.getString(0); @@ -191,7 +192,7 @@ private String doHttpGet(String url) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader( con.getInputStream())); String inputLine; - StringBuffer response = new StringBuffer(); + StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); diff --git a/src/test/java/de/meggsimum/w3w/What3WordsTest.java b/src/test/java/de/meggsimum/w3w/What3WordsTest.java index c22beae..8cef69b 100644 --- a/src/test/java/de/meggsimum/w3w/What3WordsTest.java +++ b/src/test/java/de/meggsimum/w3w/What3WordsTest.java @@ -104,4 +104,14 @@ public void testWhat3WordsException() { assertTrue(thrown); } + /** + * Test translating non-ascii words to position. + * Correct URL encoding is required for this to pass. + */ + public void testNonAsciiCharacters() throws Exception { + What3Words w3w = new What3Words(API_KEY, "de"); + String[] words = {"winkel", "artenschutz","fängen"}; + w3w.wordsToPosition(words); + } + }