You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by dg...@apache.org on 2003/03/02 01:22:40 UTC

cvs commit: jakarta-struts/src/share/org/apache/struts/util RequestUtils.java

dgraham     2003/03/01 16:22:40

  Modified:    src/share/org/apache/struts/util RequestUtils.java
  Log:
  Change encodeURL to not use reflection on every call.
  
  Revision  Changes    Path
  1.91      +26 -15    jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java
  
  Index: RequestUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
  retrieving revision 1.90
  retrieving revision 1.91
  diff -u -r1.90 -r1.91
  --- RequestUtils.java	26 Feb 2003 04:48:56 -0000	1.90
  +++ RequestUtils.java	2 Mar 2003 00:22:40 -0000	1.91
  @@ -142,6 +142,24 @@
        * The context attribute under which we store our prefixes list.
        */
       private static final String PREFIXES_KEY = "org.apache.struts.util.PREFIXES";
  +    
  +    /**
  +     * Java 1.4 encode method to use instead of deprecated 1.3 version.
  +     */
  +    private static Method encode = null;
  +    
  +    /**
  +     * Initialize the encode variable with the 1.4 method if available
  +     */
  +    static {
  +        try {
  +            // get version of encode method with two String args 
  +            Class[] args = new Class[] { String.class, String.class };
  +            encode = URLEncoder.class.getMethod("encode", args);
  +        } catch (NoSuchMethodException e) {
  +            log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
  +        }
  +    }
   
       // --------------------------------------------------------- Public Methods
   
  @@ -1904,27 +1922,20 @@
        * @return String - the encoded url.
        */
       public static String encodeURL(String url) {
  -        // default to old version
  -        String encodedURL = URLEncoder.encode(url);
  -        Class encoderClass = URLEncoder.class;
  -
           try {
  -            // get version of encode method with two String args
  -            Class[] args = new Class[] { String.class, String.class };
  -            Method encode = encoderClass.getMethod("encode", args);
   
               // encode url with new 1.4 method and UTF-8 encoding
  -            encodedURL = (String) encode.invoke(null, new Object[] { url, "UTF-8" });
  +            if (encode != null) {
  +                return (String) encode.invoke(null, new Object[] { url, "UTF-8" });
  +            }
   
           } catch (IllegalAccessException e) {
               log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
           } catch (InvocationTargetException e) {
               log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
  -        } catch (NoSuchMethodException e) {
  -            log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
           }
   
  -        return encodedURL;
  +        return URLEncoder.encode(url);
       }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


Re: cvs commit: jakarta-struts/src/share/org/apache/struts/util RequestUtils.java

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sat, 2 Mar 2003 dgraham@apache.org wrote:

> Date: 2 Mar 2003 00:22:40 -0000
> From: dgraham@apache.org
> Reply-To: Struts Developers List <st...@jakarta.apache.org>
> To: jakarta-struts-cvs@apache.org
> Subject: cvs commit: jakarta-struts/src/share/org/apache/struts/util
>     RequestUtils.java
>
> dgraham     2003/03/01 16:22:40
>
>   Modified:    src/share/org/apache/struts/util RequestUtils.java
>   Log:
>   Change encodeURL to not use reflection on every call.
>

Note that this change imposes a 1.4 dependency to build Struts.  That
would be OK with *me* (since I use 1.4 all the time), but may not be OK
with other folks.

Craig


>   Revision  Changes    Path
>   1.91      +26 -15    jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java
>
>   Index: RequestUtils.java
>   ===================================================================
>   RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
>   retrieving revision 1.90
>   retrieving revision 1.91
>   diff -u -r1.90 -r1.91
>   --- RequestUtils.java	26 Feb 2003 04:48:56 -0000	1.90
>   +++ RequestUtils.java	2 Mar 2003 00:22:40 -0000	1.91
>   @@ -142,6 +142,24 @@
>         * The context attribute under which we store our prefixes list.
>         */
>        private static final String PREFIXES_KEY = "org.apache.struts.util.PREFIXES";
>   +
>   +    /**
>   +     * Java 1.4 encode method to use instead of deprecated 1.3 version.
>   +     */
>   +    private static Method encode = null;
>   +
>   +    /**
>   +     * Initialize the encode variable with the 1.4 method if available
>   +     */
>   +    static {
>   +        try {
>   +            // get version of encode method with two String args
>   +            Class[] args = new Class[] { String.class, String.class };
>   +            encode = URLEncoder.class.getMethod("encode", args);
>   +        } catch (NoSuchMethodException e) {
>   +            log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
>   +        }
>   +    }
>
>        // --------------------------------------------------------- Public Methods
>
>   @@ -1904,27 +1922,20 @@
>         * @return String - the encoded url.
>         */
>        public static String encodeURL(String url) {
>   -        // default to old version
>   -        String encodedURL = URLEncoder.encode(url);
>   -        Class encoderClass = URLEncoder.class;
>   -
>            try {
>   -            // get version of encode method with two String args
>   -            Class[] args = new Class[] { String.class, String.class };
>   -            Method encode = encoderClass.getMethod("encode", args);
>
>                // encode url with new 1.4 method and UTF-8 encoding
>   -            encodedURL = (String) encode.invoke(null, new Object[] { url, "UTF-8" });
>   +            if (encode != null) {
>   +                return (String) encode.invoke(null, new Object[] { url, "UTF-8" });
>   +            }
>
>            } catch (IllegalAccessException e) {
>                log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
>            } catch (InvocationTargetException e) {
>                log.debug("Could not find Java 1.4 encode method. Using deprecated version.", e);
>   -        } catch (NoSuchMethodException e) {
>   -            log.debug("Could not find Java 1.4 encode method.  Using deprecated version.", e);
>            }
>
>   -        return encodedURL;
>   +        return URLEncoder.encode(url);
>        }
>
>    }
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org