[jboss-svn-commits] JBoss Portal SVN: r5606 - in trunk: common common/src/main/org/jboss/portal/common/net common/src/main/org/jboss/portal/test/common server/src/main/org/jboss/portal/server/servlet

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Nov 8 13:06:06 EST 2006


Author: julien at jboss.com
Date: 2006-11-08 13:05:58 -0500 (Wed, 08 Nov 2006)
New Revision: 5606

Added:
   trunk/common/src/main/org/jboss/portal/common/net/FastURLEncoder.java
   trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java
Modified:
   trunk/common/build.xml
   trunk/server/src/main/org/jboss/portal/server/servlet/PortalServlet.java
Log:
added a FastURLEncoder that can use a StringBuffer and uses a table based lookup of char to encode in x-www-formurlencoded form

Modified: trunk/common/build.xml
===================================================================
--- trunk/common/build.xml	2006-11-08 17:59:08 UTC (rev 5605)
+++ trunk/common/build.xml	2006-11-08 18:05:58 UTC (rev 5606)
@@ -202,11 +202,9 @@
 
          <x-test>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.test.InfoTestCase"/>
-<!--
             <test todir="${test.reports}" name="org.jboss.portal.test.common.test.TestParameterValueTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.test.TestParametrizationTestCase"/>
--->
-<!--
+            <test todir="${test.reports}" name="org.jboss.portal.test.common.FastURLEncoderTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.AbstractInvocationContextTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.LocaleInfoTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.ParentChildResourceBundleTestCase"/>
@@ -225,7 +223,6 @@
             <test todir="${test.reports}" name="org.jboss.portal.test.common.ImplodeTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.Application_XWWWFormURLEncodedFormatTestCase"/>
             <test todir="${test.reports}" name="org.jboss.portal.test.common.BufferedStreamTestCase"/>
--->
          </x-test>
          <x-classpath>
             <pathelement location="${build.classes}"/>

Added: trunk/common/src/main/org/jboss/portal/common/net/FastURLEncoder.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/common/net/FastURLEncoder.java	2006-11-08 17:59:08 UTC (rev 5605)
+++ trunk/common/src/main/org/jboss/portal/common/net/FastURLEncoder.java	2006-11-08 18:05:58 UTC (rev 5606)
@@ -0,0 +1,133 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat                                               *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual                    *
+ * contributors as indicated by the @authors tag. See the                     *
+ * copyright.txt in the distribution for a full listing of                    *
+ * individual contributors.                                                   *
+ *                                                                            *
+ * This is free software; you can redistribute it and/or modify it            *
+ * under the terms of the GNU Lesser General Public License as                *
+ * published by the Free Software Foundation; either version 2.1 of           *
+ * the License, or (at your option) any later version.                        *
+ *                                                                            *
+ * This software is distributed in the hope that it will be useful,           *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           *
+ * Lesser General Public License for more details.                            *
+ *                                                                            *
+ * You should have received a copy of the GNU Lesser General Public           *
+ * License along with this software; if not, write to the Free                *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA         *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.                   *
+ ******************************************************************************/
+package org.jboss.portal.common.net;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class FastURLEncoder
+{
+
+   /** . */
+   private String encoding;
+
+   /** . */
+   private char[][] table;
+
+   /** . */
+   private int from;
+
+   /** . */
+   private int to;
+
+   public FastURLEncoder(String encoding, int from, int to) throws UnsupportedEncodingException, IllegalArgumentException
+   {
+      if (encoding == null)
+      {
+         throw new IllegalArgumentException("No encoding provided");
+      }
+      if (to <= from)
+      {
+         throw new IllegalArgumentException("Bad range [" + from + "," + to + "[");
+      }
+      if (from < 0)
+      {
+         throw new IllegalArgumentException("Lower bound cannot be negative " + to);
+      }
+
+      //
+      this.encoding = encoding;
+      this.from = from;
+      this.to = to;
+      this.table = new char[to - from][];
+
+      //
+      for (int c = (to - 1);c >= from;c--)
+      {
+         String v = URLEncoder.encode(Character.toString((char)c), encoding);
+         table[c - from] = v.toCharArray();
+      }
+   }
+
+   public String encode(String s) throws UnsupportedEncodingException, IllegalArgumentException
+   {
+      StringBuffer tmp = new StringBuffer();
+      encode(s, tmp);
+      return tmp.toString();
+   }
+
+   public void encode(String s, StringBuffer out) throws IllegalArgumentException
+   {
+      if (s == null)
+      {
+         throw new IllegalArgumentException();
+      }
+      if (out == null)
+      {
+         throw new IllegalArgumentException();
+      }
+      for (int i = 0;i < s.length();i++)
+      {
+         char c = s.charAt(i);
+         if (c >= from && c < to)
+         {
+            out.append(table[c - from]);
+         }
+         else
+         {
+            try
+            {
+               out.append(URLEncoder.encode(Character.toString(c), encoding));
+            }
+            catch (UnsupportedEncodingException e)
+            {
+               // That should never happen has the encoding validity is check at construction time
+               throw new Error(e);
+            }
+         }
+      }
+   }
+
+   public String toString()
+   {
+      return "FastURLEncoder[" + encoding + ",[" + from + "," + to + "]]";
+   }
+
+   public static FastURLEncoder create(String encoding, int from, int to) throws IllegalArgumentException
+   {
+      try
+      {
+         return new FastURLEncoder(encoding, from, to);
+      }
+      catch (UnsupportedEncodingException e)
+      {
+         IllegalArgumentException iae = new IllegalArgumentException();
+         iae.initCause(e);
+         throw iae;
+      }
+   }
+}

Added: trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java
===================================================================
--- trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java	2006-11-08 17:59:08 UTC (rev 5605)
+++ trunk/common/src/main/org/jboss/portal/test/common/FastURLEncoderTestCase.java	2006-11-08 18:05:58 UTC (rev 5606)
@@ -0,0 +1,177 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat                                               *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual                    *
+ * contributors as indicated by the @authors tag. See the                     *
+ * copyright.txt in the distribution for a full listing of                    *
+ * individual contributors.                                                   *
+ *                                                                            *
+ * This is free software; you can redistribute it and/or modify it            *
+ * under the terms of the GNU Lesser General Public License as                *
+ * published by the Free Software Foundation; either version 2.1 of           *
+ * the License, or (at your option) any later version.                        *
+ *                                                                            *
+ * This software is distributed in the hope that it will be useful,           *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU           *
+ * Lesser General Public License for more details.                            *
+ *                                                                            *
+ * You should have received a copy of the GNU Lesser General Public           *
+ * License along with this software; if not, write to the Free                *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA         *
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.                   *
+ ******************************************************************************/
+package org.jboss.portal.test.common;
+
+import org.jboss.portal.common.net.FastURLEncoder;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
+ * @version $Revision: 1.1 $
+ */
+public class FastURLEncoderTestCase extends TestCase
+{
+
+   public void testConstructor() throws Exception
+   {
+      try
+      {
+         new FastURLEncoder(null, 0, 10);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("UTF8", -1, 10);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("UTF8", -2, -1);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("UTF8", 1, 0);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("UTF8", 0, 0);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("ABCDEF", 0, 10);
+         fail();
+      }
+      catch (UnsupportedEncodingException expected)
+      {
+      }
+   }
+
+   public void testFactory() throws Exception
+   {
+      try
+      {
+         new FastURLEncoder(null, 0, 10);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("UTF8", -1, 10);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("UTF8", -2, -1);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("UTF8", 1, 0);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("UTF8", 0, 0);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         new FastURLEncoder("ABCDEF", 0, 10);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+   }
+
+   public void testEncodeThrowsIAE() throws Exception
+   {
+      FastURLEncoder encoder = new FastURLEncoder("UTF8", 0, 256);
+      try
+      {
+         encoder.encode(null, new StringBuffer());
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+      try
+      {
+         encoder.encode("", null);
+         fail();
+      }
+      catch (IllegalArgumentException expected)
+      {
+      }
+   }
+
+   public void testEncode() throws Exception
+   {
+      FastURLEncoder encoder = new FastURLEncoder("UTF8", 0, 512);
+      StringBuffer tmp = new StringBuffer();
+      for (int i = 0;i < 512;i++)
+      {
+         tmp.append((char)i);
+      }
+      String s = tmp.toString();
+      String u1 = encoder.encode(s);
+      String u2 = URLEncoder.encode(s, "UTF8");
+      assertEquals(u2, u1);
+   }
+}

Modified: trunk/server/src/main/org/jboss/portal/server/servlet/PortalServlet.java
===================================================================
--- trunk/server/src/main/org/jboss/portal/server/servlet/PortalServlet.java	2006-11-08 17:59:08 UTC (rev 5605)
+++ trunk/server/src/main/org/jboss/portal/server/servlet/PortalServlet.java	2006-11-08 18:05:58 UTC (rev 5606)
@@ -29,6 +29,7 @@
 import org.jboss.portal.common.invocation.InvocationException;
 import org.jboss.portal.common.util.Exceptions;
 import org.jboss.portal.common.util.URLTools;
+import org.jboss.portal.common.net.FastURLEncoder;
 import org.jboss.portal.server.PortalConstants;
 import org.jboss.portal.server.RequestController;
 import org.jboss.portal.server.RequestControllerDispatcher;
@@ -86,6 +87,9 @@
    /** Describes a path mapping. */
    private static final int PATH_MAPPING = 2;
 
+   /** The fast url encoder. */
+   private static final FastURLEncoder urlEncoder = FastURLEncoder.create("UTF8", 0, 1024);
+
    /** The logger. */
    protected Logger log = Logger.getLogger(getClass());
 
@@ -442,11 +446,8 @@
 
       private URLFormat format;
 
-      private char[] buffer = new char[256];
+      private StringBuffer buffer = new StringBuffer(256);
 
-      private int len = 0;
-
-
       public Buffer(HttpServletRequest req, HttpServletResponse resp, URLContext context, URLFormat format)
       {
          this.req = req;
@@ -461,9 +462,9 @@
          //
          if (!format.isRelative())
          {
-            append(req.getScheme());
-            append("://");
-            append(req.getServerName());
+            buffer.append(req.getScheme());
+            buffer.append("://");
+            buffer.append(req.getServerName());
 
             //
             int port = req.getServerPort();
@@ -471,41 +472,41 @@
             {
                if (port != 443)
                {
-                  append(":");
-                  append(Integer.toString(port));
+                  buffer.append(":");
+                  buffer.append(Integer.toString(port));
                }
             }
             else if (port != 80)
             {
-               append(":");
-               append(Integer.toString(port));
+               buffer.append(":");
+               buffer.append(Integer.toString(port));
             }
          }
 
          // Append the context path
-         append(req.getContextPath());
+         buffer.append(req.getContextPath());
 
          // Append the servlet path
          if (context.isAuthenticated())
          {
             if (context.getSecure())
             {
-               append("/authsec");
+               buffer.append("/authsec");
             }
             else
             {
-               append("/auth");
+               buffer.append("/auth");
             }
          }
          else
          {
             if (context.getSecure())
             {
-               append("/sec");
+               buffer.append("/sec");
             }
             else
             {
-               append("");
+               buffer.append("");
             }
          }
       }
@@ -513,7 +514,7 @@
       public String toString(ServerURL url)
       {
          // julien : check UTF-8 is ok and should not be dependant on the response charset
-         append(url.getPortalRequestPath());
+         buffer.append(url.getPortalRequestPath());
 
          //
          boolean first = true;
@@ -525,16 +526,16 @@
             for (int j = 0; j < values.length; j++)
             {
                String value = values[j];
-               append(first ? "?" : "&");
-               append(URLTools.encodeXWWWFormURL(name));
-               append("=");
-               append(URLTools.encodeXWWWFormURL(value));
+               buffer.append(first ? "?" : "&");
+               urlEncoder.encode(name, buffer);
+               buffer.append("=");
+               urlEncoder.encode(value, buffer);
                first = false;
             }
          }
 
          //
-         String s = new String(buffer, 0, len);
+         String s = buffer.toString();
 
          //
          if (format.isEncoded())
@@ -545,21 +546,6 @@
          //
          return s;
       }
-
-      private void append(String arg)
-      {
-         int arglen = arg.length();
-         int buflen = buffer.length;
-         while (len + arglen > buflen)
-         {
-            char[] tmp = new char[buflen * 2 + 1];
-            System.arraycopy(buffer, 0, tmp, 0, len);
-            buffer = tmp;
-            buflen = tmp.length;
-         }
-         arg.getChars(0, arglen, buffer, len);
-         len += arglen;
-      }
    }
 
    private URLContext parseURLContext(HttpServletRequest req)




More information about the jboss-svn-commits mailing list