You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2021/01/04 17:30:53 UTC

svn commit: r1885114 - in /turbine/core/trunk/src: java/org/apache/turbine/util/uri/TurbineURI.java test/org/apache/turbine/util/uri/TurbineURITest.java

Author: tv
Date: Mon Jan  4 17:30:53 2021
New Revision: 1885114

URL: http://svn.apache.org/viewvc?rev=1885114&view=rev
Log:
Modernize a bit

Modified:
    turbine/core/trunk/src/java/org/apache/turbine/util/uri/TurbineURI.java
    turbine/core/trunk/src/test/org/apache/turbine/util/uri/TurbineURITest.java

Modified: turbine/core/trunk/src/java/org/apache/turbine/util/uri/TurbineURI.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/java/org/apache/turbine/util/uri/TurbineURI.java?rev=1885114&r1=1885113&r2=1885114&view=diff
==============================================================================
--- turbine/core/trunk/src/java/org/apache/turbine/util/uri/TurbineURI.java (original)
+++ turbine/core/trunk/src/java/org/apache/turbine/util/uri/TurbineURI.java Mon Jan  4 17:30:53 2021
@@ -21,10 +21,14 @@ package org.apache.turbine.util.uri;
 
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.fulcrum.parser.ParameterParser;
@@ -60,6 +64,9 @@ public class TurbineURI
     /** Local reference to the parser service for URI parameter folding */
     private ParserService parserService;
 
+    /** URI Parameter encoding as defined by the parser service */
+    private Charset parameterEncoding;
+
     /*
      * ========================================================================
      *
@@ -258,9 +265,18 @@ public class TurbineURI
     private void init()
     {
         dataVectors = new List[2];
-        dataVectors[PATH_INFO]  = new ArrayList<URIParam>();
-        dataVectors[QUERY_DATA] = new ArrayList<URIParam>();
+        dataVectors[PATH_INFO]  = new ArrayList<>();
+        dataVectors[QUERY_DATA] = new ArrayList<>();
         parserService = (ParserService)TurbineServices.getInstance().getService(ParserService.ROLE);
+
+        try
+        {
+            parameterEncoding = Charset.forName(parserService.getParameterEncoding());
+        }
+        catch (IllegalCharsetNameException | UnsupportedCharsetException e)
+        {
+            log.error("Unsupported encoding {}", parserService.getParameterEncoding(), e);
+        }
     }
 
     /**
@@ -675,7 +691,7 @@ public class TurbineURI
     }
 
     /**
-     * Gets the current Query Data List.
+     * Gets the current Path Info List.
      *
      * @return A List which contains all query data keys. The keys
      * are URIParam objects.
@@ -751,7 +767,7 @@ public class TurbineURI
      */
     private void getPathInfoAsString(StringBuilder output)
     {
-        doEncode(output, dataVectors[PATH_INFO], '/', '/');
+        doEncode(output, dataVectors[PATH_INFO], "/", "/");
     }
 
     /**
@@ -761,60 +777,49 @@ public class TurbineURI
      */
     private void getQueryDataAsString(StringBuilder output)
     {
-        doEncode(output, dataVectors[QUERY_DATA], '&', '=');
+        doEncode(output, dataVectors[QUERY_DATA], "&", "=");
+    }
+
+    /**
+     * URL encode the given string, catching possible Exceptions
+     *
+     * @param string the string
+     * @return the encoded string
+     */
+    private String urlEncode(String string)
+    {
+        try
+        {
+            // Java10: return URLEncoder.encode(string, parameterEncoding);
+            return URLEncoder.encode(string, parameterEncoding.name());
+        }
+        catch (UnsupportedEncodingException e)
+        {
+            log.warn("Unsupported encoding {}", parameterEncoding);
+        }
+
+        return StringUtils.EMPTY;
     }
 
     /**
      * Does the actual encoding for pathInfoAsString and queryDataAsString.
      *
-     * @param output The Stringbuffer that should contain the information.
-     * @param list A Collection
+     * @param output The String builder that should contain the information.
+     * @param list A list of key to value pairs
      * @param fieldDelim A char which is used to separate key/value pairs
      * @param valueDelim A char which is used to separate key and value
      */
-    private void doEncode(StringBuilder output, Collection<URIParam> list, char fieldDelim, char valueDelim)
+    private void doEncode(StringBuilder output, Collection<URIParam> list, String fieldDelim, String valueDelim)
     {
         if(!list.isEmpty())
         {
-        	String encoding = parserService.getParameterEncoding();
-
-            for(Iterator<URIParam> it = list.iterator(); it.hasNext();)
-            {
-            	try
-            	{
-					URIParam uriParam = it.next();
-					String key = URLEncoder.encode(uriParam.getKey(), encoding);
-					String val = null == uriParam.getValue()
-					        ? null : String.valueOf(uriParam.getValue());
-
-					output.append(key);
-					output.append(valueDelim);
-
-					if(StringUtils.isEmpty(val))
-					{
-					    if (val == null)
-					    {
-				            log.warn("Found a null value for {}", key);
-					        // For backwards compatibility:
-					        val = "null";
-					    }
-					    output.append(val);
-					}
-					else
-					{
-					    output.append(URLEncoder.encode(val, encoding));
-					}
-
-					if (it.hasNext())
-					{
-					    output.append(fieldDelim);
-					}
-				}
-            	catch (UnsupportedEncodingException e)
-            	{
-            		log.warn("Unsupported encoding {}", encoding);
-				}
-            }
+            output.append(list.stream()
+                    .map(uriParam ->
+                        String.join(
+                                valueDelim,
+                                urlEncode(uriParam.getKey()),
+                                urlEncode(Objects.toString(uriParam.getValue()))))
+                    .collect(Collectors.joining(fieldDelim)));
         }
     }
 
@@ -834,7 +839,6 @@ public class TurbineURI
             String value)
     {
         URIParam uriParam = new URIParam(parserService.convertAndTrim(name), value);
-
         dataVectors[type].add(uriParam); // Code so clean you can eat from...
     }
 
@@ -854,19 +858,17 @@ public class TurbineURI
     protected void add(int type,
             ParameterParser pp)
     {
-        for(Iterator<?> it = pp.keySet().iterator(); it.hasNext();)
+        for(String key : pp.keySet())
         {
-            String key = (String) it.next();
-
             if (!key.equalsIgnoreCase(CGI_ACTION_PARAM) &&
                     !key.equalsIgnoreCase(CGI_SCREEN_PARAM))
             {
                 String[] values = pp.getStrings(key);
                 if(values != null)
                 {
-                    for (int i = 0; i < values.length; i++)
+                    for (String value : values)
                     {
-                        add(type, key, values[i]);
+                        add(type, key, value);
                     }
                 }
                 else
@@ -910,17 +912,8 @@ public class TurbineURI
      */
     protected void remove (int type, String name)
     {
-        Collection<URIParam> c = dataVectors[type];
         String key = parserService.convertAndTrim(name);
 
-        for (Iterator<URIParam> it = c.iterator(); it.hasNext();)
-        {
-            URIParam uriParam = it.next();
-
-            if (key.equals(uriParam.getKey()))
-            {
-                it.remove();
-            }
-        }
+        dataVectors[type].removeIf(uriParam -> key.equals(uriParam.getKey()));
     }
 }

Modified: turbine/core/trunk/src/test/org/apache/turbine/util/uri/TurbineURITest.java
URL: http://svn.apache.org/viewvc/turbine/core/trunk/src/test/org/apache/turbine/util/uri/TurbineURITest.java?rev=1885114&r1=1885113&r2=1885114&view=diff
==============================================================================
--- turbine/core/trunk/src/test/org/apache/turbine/util/uri/TurbineURITest.java (original)
+++ turbine/core/trunk/src/test/org/apache/turbine/util/uri/TurbineURITest.java Mon Jan  4 17:30:53 2021
@@ -20,8 +20,10 @@ package org.apache.turbine.util.uri;
  */
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import javax.servlet.http.Part;
 
 import org.apache.fulcrum.parser.DefaultParameterParser;
 import org.apache.fulcrum.parser.ParameterParser;
@@ -34,6 +36,7 @@ import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 /**
  * Testing of the TurbineURI class
@@ -100,8 +103,6 @@ public class TurbineURITest extends Base
         assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
         assertFalse("TurbineURI must not have a queryData", turi.hasQueryData());
 
-        assertFalse("TurbineURI should not have a queryData", turi.hasQueryData());
-        assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
         turi.addQueryData("test", "x");
         assertTrue("TurbineURI must have a queryData", turi.hasQueryData());
         assertFalse("TurbineURI must not have a pathInfo", turi.hasPathInfo());
@@ -172,24 +173,23 @@ public class TurbineURITest extends Base
         turi.removeQueryData("test");
         assertEquals("/context/servlet/turbine", turi.getRelativeLink());
 
-        // TRB-8
-        //
-        // This is commented out for now as it results in a ClassCastException.
-        // The 2_3 branch parser changes need to be merged into the fulcrum
-        // code.
-        //
-        // pp = new DefaultParameterParser();
-        // DiskFileItemFactory factory = new DiskFileItemFactory(10240, null);
-        // FileItem test = factory.createItem("upload-field",
-        // "application/octet-stream", false, null);
-        // pp.append("upload-field", test);
-        // // The following causes a ClassCastException with or without the
-        // TRB-8 fix.
-        // turi.add(1, pp); // 1 = query data
-        // assertEquals("/context/servlet/turbine?upload-field=",
-        // turi.getRelativeLink());
-        // turi.removeQueryData("upload-field");
-        // assertEquals("/context/servlet/turbine", turi.getRelativeLink());
+        parserService.putParser(pp);
+        pp = parserService.getParser(DefaultParameterParser.class);
+        pp.add("test", "1");
+        pp.add("test", "2");
+        turi.add(1, pp); // 1 = query data
+        assertEquals("/context/servlet/turbine?test=1&test=2", turi.getRelativeLink());
+        turi.removeQueryData("test");
+        assertEquals("/context/servlet/turbine", turi.getRelativeLink());
+
+        parserService.putParser(pp);
+        pp = parserService.getParser(DefaultParameterParser.class);
+        Part part = Mockito.mock(Part.class);
+        pp.add("upload-field", part);
+        turi.add(1, pp); // 1 = query data
+        assertEquals("/context/servlet/turbine?upload-field=", turi.getRelativeLink());
+        turi.removeQueryData("upload-field");
+        assertEquals("/context/servlet/turbine", turi.getRelativeLink());
     }
 
 }