From 71aeea336e770ed8d23e3dd0cddbcca774790398 Mon Sep 17 00:00:00 2001 From: hns Date: Wed, 15 Sep 2004 11:15:44 +0000 Subject: [PATCH] =?UTF-8?q?UrlEncoded=20fix=20from=20J=C3=83=C2=BCrg=20Leh?= =?UTF-8?q?ni=20to=20use=20JDK=201.4=20methods=20if=20available=20and=20sw?= =?UTF-8?q?itch=20back=20to=20JDK=201.3=20methods=20otherwise.=20Methods?= =?UTF-8?q?=20renamed=20to=20encode()=20and=20decode(),=20respectively.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helma/framework/core/Application.java | 4 +- src/helma/framework/core/RequestPath.java | 4 +- src/helma/framework/core/Skin.java | 2 +- src/helma/servlet/AbstractServletClient.java | 2 +- src/helma/util/UrlEncoded.java | 119 ++++++++++++++----- 5 files changed, 97 insertions(+), 34 deletions(-) diff --git a/src/helma/framework/core/Application.java b/src/helma/framework/core/Application.java index 9b4f3c39..6f8ee08a 100644 --- a/src/helma/framework/core/Application.java +++ b/src/helma/framework/core/Application.java @@ -1070,7 +1070,7 @@ public final class Application implements IPathElement, Runnable { composeHref(elem, b, 0); if (actionName != null) { - b.append(UrlEncoded.smartEncode(actionName, charset)); + b.append(UrlEncoded.encode(actionName, charset)); } return b.toString(); @@ -1098,7 +1098,7 @@ public final class Application implements IPathElement, Runnable { // append ourselves String ename = getElementName(elem); if (ename != null) { - b.append(UrlEncoded.smartEncode(ename, charset)); + b.append(UrlEncoded.encode(ename, charset)); b.append("/"); } } diff --git a/src/helma/framework/core/RequestPath.java b/src/helma/framework/core/RequestPath.java index 3298d5f0..ed7dadcf 100644 --- a/src/helma/framework/core/RequestPath.java +++ b/src/helma/framework/core/RequestPath.java @@ -123,12 +123,12 @@ public class RequestPath { } for (int i=start; i= 'a') && (c <= 'z')) + || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')))) { + if (encode != null) { + try { + return (String) encode.invoke(null, new Object[] { str, + encoding }); + } catch (IllegalAccessException e) { + // don't keep trying if we get one of these + encode = null; + + System.err.println("UrlEncoded: Can't access JDK 1.4 encode method (" + + e + "). Using deprecated version from now on."); + } catch (InvocationTargetException e) { + // this can only be a UnsupportedEncodingException: + Throwable ex = e.getTargetException(); + if (ex instanceof UnsupportedEncodingException) + throw (UnsupportedEncodingException) ex; + } + } + return URLEncoder.encode(str); } } @@ -64,20 +105,42 @@ public final class UrlEncoded extends URLCodec { } /** - * URL-decode a string using the given encoding, - * or return it unchanged if no encoding was necessary. - * - * @param str The string to be URL-decoded + * URL-decode a string using the given encoding, or return it unchanged if + * no encoding was necessary. + * This method uses the new URLDecoder.decode() method from java 1.4 if + * available, otherwise the old deprecated version is used. Reflection is + * used to find the appropriate method; if the reflection operations throw + * exceptions other than UnsupportedEncodingException, it returns the url + * decoded with the old URLDecoder.decode() method. + * + * @param str The string to be URL-decoded * @param encoding the encoding to use * @return the URL-decoded string, or str if no decoding necessary */ - public static String smartDecode(String str, String encoding) - throws DecoderException, UnsupportedEncodingException { + public static String decode(String str, String encoding) + throws UnsupportedEncodingException { if ((str.indexOf('+') == -1) && (str.indexOf('%') == -1)) { return str; } else { - return new URLCodec().decode(str, encoding); + if (decode != null) { + try { + return (String) decode.invoke(null, new Object[] { str, + encoding }); + } catch (IllegalAccessException e) { + // don't keep trying if we get one of these + decode = null; + + System.err.println("UrlEncoded: Can't access JDK 1.4 decode method (" + + e + "). Using deprecated version from now on."); + } catch (InvocationTargetException e) { + // this can only be a UnsupportedEncodingException: + Throwable ex = e.getTargetException(); + if (ex instanceof UnsupportedEncodingException) + throw (UnsupportedEncodingException) ex; + } + } + return URLDecoder.decode(str); } } -} +} \ No newline at end of file