[Yanel-commits] rev 40358 - in public/yanel/trunk/src:
core/java/org/wyona/yanel/core/util
test/junit/org/wyona/yanel/core/util
simon at wyona.com
simon at wyona.com
Tue Dec 9 11:25:45 CET 2008
Author: simon
Date: 2008-12-09 11:25:44 +0100 (Tue, 09 Dec 2008)
New Revision: 40358
Added:
public/yanel/trunk/src/test/junit/org/wyona/yanel/core/util/HttpServletRequestHelperTest.java
Modified:
public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/HttpServletRequestHelper.java
Log:
see bug http://bugzilla.wyona.com/cgi-bin/bugzilla/show_bug.cgi?id=6849. thanks to guillaume
Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/HttpServletRequestHelper.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/HttpServletRequestHelper.java 2008-12-09 10:14:12 UTC (rev 40357)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/HttpServletRequestHelper.java 2008-12-09 10:25:44 UTC (rev 40358)
@@ -1,5 +1,5 @@
/*
- * Copyright 2006 Wyona
+ * Copyright 2006,2008 Wyona
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,9 @@
package org.wyona.yanel.core.util;
import javax.servlet.http.HttpServletRequest;
+
import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
/**
* TODO:
@@ -59,4 +61,25 @@
throw new RuntimeException(uee.toString(), uee);
}
}
+
+ /**
+ * Decode an URI that was part of an URL path.
+ *
+ * Algorithm: If the 1st character of the URI is the escape character,
+ * replace all occurrences of this character by '%' and decode as Percent-Encoded URI.
+ */
+ public static String decodeURIinURLpath(final char escapeCharacter, final String URLpathPart) {
+ final char firstCharacter = URLpathPart.charAt(0);
+ if (firstCharacter != escapeCharacter) {
+ return URLpathPart;
+ }
+ final String decodedURLpathPart = URLpathPart.substring(1).replace(escapeCharacter, '%');
+ final String encodedURL = decodedURLpathPart.replaceAll("\\+", "%2B");
+ try {
+ return URLDecoder.decode(encodedURL, "UTF-8");
+ } catch (UnsupportedEncodingException uee) {
+ throw new RuntimeException(uee); // this should never happen, as UTF-8 encoding should be always available
+ }
+ }
+
}
Added: public/yanel/trunk/src/test/junit/org/wyona/yanel/core/util/HttpServletRequestHelperTest.java
===================================================================
--- public/yanel/trunk/src/test/junit/org/wyona/yanel/core/util/HttpServletRequestHelperTest.java (rev 0)
+++ public/yanel/trunk/src/test/junit/org/wyona/yanel/core/util/HttpServletRequestHelperTest.java 2008-12-09 10:25:44 UTC (rev 40358)
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2008 Wyona
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.wyona.org/licenses/APACHE-LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wyona.yanel.core.util;
+
+import junit.framework.TestCase;
+
+public class HttpServletRequestHelperTest extends TestCase {
+
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ public void testDecodeURIinURLpath() {
+ assertEquals(
+ "http://www.wyona.org/yanel/resource/1.0::foo/bar",
+ HttpServletRequestHelper.decodeURIinURLpath('^', "^http^3a^2f^2fwww.wyona.org^2fyanel^2fresource^2f1.0^3a^3afoo/bar")
+ );
+ assertEquals(
+ "http://example.com/foo^bar^baz",
+ HttpServletRequestHelper.decodeURIinURLpath('^', "^http://example.com/foo^5ebar^5ebaz")
+ );
+ assertEquals(
+ "http://example.com/foo+bar+baz",
+ HttpServletRequestHelper.decodeURIinURLpath('^', "^http://example.com/foo+bar+baz")
+ );
+ assertEquals(
+ "http://example.com/foo/bar/baz",
+ HttpServletRequestHelper.decodeURIinURLpath('^', "http://example.com/foo/bar/baz")
+ );
+ }
+
+}
More information about the Yanel-commits
mailing list