From 421b9b08d9087311cab98367a14520e98a860b2a Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Tue, 14 Nov 2023 15:42:42 +0100 Subject: [PATCH 1/2] Fix URLEncode to be backwards compatible (metafacture-fix#273) Complements c093da1cc27ab5b7dfd14a6c3fb84ff8af95bd4d. --- .../metamorph/functions/URLEncode.java | 8 ++++++-- .../metamorph/functions/URLEncodeTest.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/metamorph/src/main/java/org/metafacture/metamorph/functions/URLEncode.java b/metamorph/src/main/java/org/metafacture/metamorph/functions/URLEncode.java index 2117faef5..ca52b016a 100644 --- a/metamorph/src/main/java/org/metafacture/metamorph/functions/URLEncode.java +++ b/metamorph/src/main/java/org/metafacture/metamorph/functions/URLEncode.java @@ -25,13 +25,15 @@ * Default is to convert a whitespace " "to a plus sign "+". This can be set so that a whitespace " " is escaped to * "%20". * Safe characters for this escaper are the ranges 0..9, a..z and A..Z. These are always safe and should not be - * specified. + * specified. Default safe characters are also ".", "-", "*", and "_", following URLEncoder. + * + * @see java.net.URLEncoder * * @author Markus Michael Geipel * @author Pascal Christoph (dr0i) */ public final class URLEncode extends AbstractSimpleStatelessFunction { - private String safeChars = ""; + private String safeChars = ".-*_"; private Boolean plusForSpace = true; private PercentEscaper percentEscaper = new PercentEscaper(safeChars, plusForSpace); @@ -50,6 +52,8 @@ public String process(final String value) { * Sets a URI escaper with the specified safe characters. The ranges 0..9, a..z and A..Z are always safe * and should not be specified. * + * Default is also ".", "-", "*", and "_" , mimicking {@link java.net.URLEncoder}. + * * @param safeChars the chars which will not be escaped */ public void setSafeChars(final String safeChars) { diff --git a/metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java b/metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java index 470e93aae..9203a2bf7 100644 --- a/metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java +++ b/metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java @@ -16,6 +16,9 @@ package org.metafacture.metamorph.functions; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + import static org.junit.Assert.*; import org.junit.Test; @@ -31,10 +34,15 @@ public final class URLEncodeTest { private static final String CAFE_ENCODED = "caf%C3%A9"; private static final String SOME_CHARS = "/&%\\+"; private static final String SOME_CHARS_ENCODED = "%2F%26%25%5C%2B"; + private static final String SPECIAL_CHARACTERS = ".-*_"; + private static final String URL = + "http://lobid.org/resources/search?q=hasItem.hasItem.heldBy.id:\"http://lobid" + + ".org/organisations/DE-290#!\"&format=json"; private static final String WHITESPACE = " "; private static final String WHITESPACE_AS_PLUS_ENCODED = "+"; private static final String WHITESPACE_PERCENT_ENCODED = "%20"; +URLEncoder urlEncoder; @Test public void testUtf8(){ final URLEncode urlEncode = new URLEncode(); @@ -64,5 +72,15 @@ public void testSafeChars(){ urlEncode.setSafeChars(SOME_CHARS); assertEquals(SOME_CHARS, urlEncode.process(SOME_CHARS)); } + @Test + public void testSpecialChars(){ + final URLEncode urlEncode = new URLEncode(); + assertEquals(SPECIAL_CHARACTERS, urlEncode.process(SPECIAL_CHARACTERS)); + } + @Test + public void testBackwardsCompatibility() throws UnsupportedEncodingException { + final URLEncode urlEncode = new URLEncode(); + assertEquals(urlEncode.process(URL), URLEncoder.encode(URL, "UTF-8")); + } } From 604b0642cf449299b494ccfd72027809aff8e56a Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Thu, 16 Nov 2023 13:53:13 +0100 Subject: [PATCH 2/2] Remove superflous line Complements 421b9b08d9087311cab98367a14520e98a860b2a --- .../java/org/metafacture/metamorph/functions/URLEncodeTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java b/metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java index 9203a2bf7..c104e4446 100644 --- a/metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java +++ b/metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java @@ -42,7 +42,6 @@ public final class URLEncodeTest { private static final String WHITESPACE_AS_PLUS_ENCODED = "+"; private static final String WHITESPACE_PERCENT_ENCODED = "%20"; -URLEncoder urlEncoder; @Test public void testUtf8(){ final URLEncode urlEncode = new URLEncode();