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