Revision: 910
          http://stripes.svn.sourceforge.net/stripes/?rev=910&view=rev
Author:   bengunter
Date:     2008-05-16 08:09:55 -0700 (Fri, 16 May 2008)

Log Message:
-----------
Added StringUtil.encodeUrl(String) and StringUtil.decodeUrl(String), which use 
the UTF-8 charset and catch the UnsupportedEncodingException. This just makes 
things a little cleaner in a lot of cases.

Modified Paths:
--------------
    
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/SecurityFilter.java
    trunk/stripes/src/net/sourceforge/stripes/util/ResolverUtil.java
    trunk/stripes/src/net/sourceforge/stripes/util/StringUtil.java
    trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java

Modified: 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/SecurityFilter.java
===================================================================
--- 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/SecurityFilter.java
    2008-05-16 14:52:27 UTC (rev 909)
+++ 
trunk/examples/src/net/sourceforge/stripes/examples/bugzooky/SecurityFilter.java
    2008-05-16 15:09:55 UTC (rev 910)
@@ -8,8 +8,10 @@
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+
+import net.sourceforge.stripes.util.StringUtil;
+
 import java.io.IOException;
-import java.net.URLEncoder;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -48,7 +50,7 @@
         }
         else {
             // Redirect the user to the login page, noting where they were 
coming from
-            String targetUrl = URLEncoder.encode(request.getServletPath(), 
"UTF-8");
+            String targetUrl = StringUtil.urlEncode(request.getServletPath());
 
             response.sendRedirect(
                     request.getContextPath() + 
"/bugzooky/Login.jsp?targetUrl=" + targetUrl);

Modified: trunk/stripes/src/net/sourceforge/stripes/util/ResolverUtil.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/ResolverUtil.java    
2008-05-16 14:52:27 UTC (rev 909)
+++ trunk/stripes/src/net/sourceforge/stripes/util/ResolverUtil.java    
2008-05-16 15:09:55 UTC (rev 910)
@@ -19,7 +19,6 @@
 import java.io.IOException;
 import java.lang.annotation.Annotation;
 import java.net.URL;
-import java.net.URLDecoder;
 import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.Set;
@@ -215,31 +214,26 @@
         }
 
         while (urls.hasMoreElements()) {
-            try {
-                String urlPath = urls.nextElement().getFile();
-                urlPath = URLDecoder.decode(urlPath, "UTF-8");
+            String urlPath = urls.nextElement().getFile();
+            urlPath = StringUtil.urlDecode(urlPath);
 
-                // If it's a file in a directory, trim the stupid file: spec
-                if ( urlPath.startsWith("file:") ) {
-                    urlPath = urlPath.substring(5);
-                }
+            // If it's a file in a directory, trim the stupid file: spec
+            if ( urlPath.startsWith("file:") ) {
+                urlPath = urlPath.substring(5);
+            }
 
-                // Else it's in a JAR, grab the path to the jar
-                if (urlPath.indexOf('!') > 0) {
-                    urlPath = urlPath.substring(0, urlPath.indexOf('!'));
-                }
+            // Else it's in a JAR, grab the path to the jar
+            if (urlPath.indexOf('!') > 0) {
+                urlPath = urlPath.substring(0, urlPath.indexOf('!'));
+            }
 
-                log.info("Scanning for classes in [", urlPath, "] matching 
criteria: ", test);
-                File file = new File(urlPath);
-                if ( file.isDirectory() ) {
-                    loadImplementationsInDirectory(test, packageName, file);
-                }
-                else {
-                    loadImplementationsInJar(test, packageName, file);
-                }
+            log.info("Scanning for classes in [", urlPath, "] matching 
criteria: ", test);
+            File file = new File(urlPath);
+            if ( file.isDirectory() ) {
+                loadImplementationsInDirectory(test, packageName, file);
             }
-            catch (IOException ioe) {
-                log.warn("could not read entries", ioe);
+            else {
+                loadImplementationsInJar(test, packageName, file);
             }
         }
         

Modified: trunk/stripes/src/net/sourceforge/stripes/util/StringUtil.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/StringUtil.java      
2008-05-16 14:52:27 UTC (rev 909)
+++ trunk/stripes/src/net/sourceforge/stripes/util/StringUtil.java      
2008-05-16 15:09:55 UTC (rev 910)
@@ -1,8 +1,13 @@
 package net.sourceforge.stripes.util;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 import java.util.Arrays;
 import java.util.regex.Pattern;
 
+import net.sourceforge.stripes.exception.StripesRuntimeException;
+
 /**
  * Provides utility methods for manipulating and parsing Strings.
  *
@@ -49,4 +54,34 @@
 
         return builder.toString();
     }
+
+    /**
+     * URL-encodes [EMAIL PROTECTED] value} using the UTF-8 charset. Using 
this method eliminates the need for
+     * a try/catch since UTF-8 is guaranteed to exist.
+     * 
+     * @see URLEncoder#encode(String, String)
+     */
+    public static String urlEncode(String value) {
+        try {
+            return URLEncoder.encode(value, "UTF-8");
+        }
+        catch (UnsupportedEncodingException e) {
+            throw new StripesRuntimeException("Unsupported encoding?  UTF-8?  
That's unpossible.");
+        }
+    }
+
+    /**
+     * URL-decodes [EMAIL PROTECTED] value} using the UTF-8 charset. Using 
this method eliminates the need for
+     * a try/catch since UTF-8 is guaranteed to exist.
+     * 
+     * @see URLDecoder#decode(String, String)
+     */
+    public static String urlDecode(String value) {
+        try {
+            return URLDecoder.decode(value, "UTF-8");
+        }
+        catch (UnsupportedEncodingException e) {
+            throw new StripesRuntimeException("Unsupported encoding?  UTF-8?  
That's unpossible.");
+        }
+    }
 }

Modified: trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java      
2008-05-16 14:52:27 UTC (rev 909)
+++ trunk/stripes/src/net/sourceforge/stripes/util/UrlBuilder.java      
2008-05-16 15:09:55 UTC (rev 910)
@@ -14,8 +14,6 @@
  */
 package net.sourceforge.stripes.util;
 
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -386,51 +384,46 @@
      * Build and return the URL
      */
     protected String build() {
-        try {
+        // special handling for event parameter
+        List<Parameter> parameters = new 
ArrayList<Parameter>(this.parameters.size() + 1);
+        if (this.event != null) {
+            parameters.add(this.event);
+        }
+        parameters.addAll(this.parameters);
+
+        // lookup validation info for the bean class to find encrypted 
properties
+        Map<String, ValidationMetadata> validations = getValidationMetadata();
+
+        StringBuilder buffer = new StringBuilder(256);
+        buffer.append(getBaseURL(this.baseUrl, parameters));
+        boolean seenQuestionMark = buffer.indexOf("?") != -1;
+        for (Parameter param : parameters) {
             // special handling for event parameter
-            List<Parameter> parameters = new 
ArrayList<Parameter>(this.parameters.size() + 1);
-            if (this.event != null) {
-                parameters.add(this.event);
+            if (param == this.event) {
+                if (param.value == null)
+                    continue;
+                else
+                    param = new Parameter((String) this.event.value, "");
             }
-            parameters.addAll(this.parameters);
 
-            // lookup validation info for the bean class to find encrypted 
properties
-            Map<String, ValidationMetadata> validations = 
getValidationMetadata();
-
-            StringBuilder buffer = new StringBuilder(256);
-            buffer.append(getBaseURL(this.baseUrl, parameters));
-            boolean seenQuestionMark = buffer.indexOf("?") != -1;
-            for (Parameter param : parameters) {
-                // special handling for event parameter
-                if (param == this.event) {
-                    if (param.value == null)
-                        continue;
-                    else
-                        param = new Parameter((String) this.event.value, "");
-                }
-
-                // Figure out whether we already have params or not
-                if (!seenQuestionMark) {
-                    buffer.append('?');
-                    seenQuestionMark = true;
-                }
-                else {
-                    buffer.append(getParameterSeparator());
-                }
-                buffer.append(URLEncoder.encode(param.name, 
"UTF-8")).append('=');
-                if (param.value != null) {
-                    ValidationMetadata validation = 
validations.get(param.name);
-                    String formatted = format(param.value);
-                    if (validation != null && validation.encrypted())
-                        formatted = CryptoUtil.encrypt(formatted);
-                    buffer.append(URLEncoder.encode(formatted, "UTF-8"));
-                }
+            // Figure out whether we already have params or not
+            if (!seenQuestionMark) {
+                buffer.append('?');
+                seenQuestionMark = true;
             }
-            return buffer.toString();
+            else {
+                buffer.append(getParameterSeparator());
+            }
+            buffer.append(StringUtil.urlEncode(param.name)).append('=');
+            if (param.value != null) {
+                ValidationMetadata validation = validations.get(param.name);
+                String formatted = format(param.value);
+                if (validation != null && validation.encrypted())
+                    formatted = CryptoUtil.encrypt(formatted);
+                buffer.append(StringUtil.urlEncode(formatted));
+            }
         }
-        catch (UnsupportedEncodingException uee) {
-            throw new StripesRuntimeException("Unsupported encoding?  UTF-8?  
That's unpossible.");
-        }
+        return buffer.toString();
     }
 
     /**


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to