Skip to content

Commit a8aec30

Browse files
authored
Fix #12578 pretty print getParameterMap (#12585)
Fix #12578 pretty print getParameterMap + Added test for parameters + Added TypeUtil method
1 parent 8e810a7 commit a8aec30

File tree

5 files changed

+94
-32
lines changed

5 files changed

+94
-32
lines changed

jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/Fields.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,14 @@ public Iterator<Field> iterator()
329329
*/
330330
public Map<String, String[]> toStringArrayMap()
331331
{
332-
Map<String, String[]> result = new LinkedHashMap<>();
332+
Map<String, String[]> result = new LinkedHashMap<>()
333+
{
334+
@Override
335+
public String toString()
336+
{
337+
return TypeUtil.toString(this);
338+
}
339+
};
333340
fields.forEach((k, f) -> result.put(f.getName(), f.getValues().toArray(new String[0])));
334341
return result;
335342
}

jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/MultiMap.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import java.util.ArrayList;
1717
import java.util.Arrays;
18-
import java.util.Iterator;
1918
import java.util.LinkedHashMap;
2019
import java.util.List;
2120
import java.util.Map;
@@ -330,33 +329,7 @@ public boolean containsSimpleValue(V value)
330329
@Override
331330
public String toString()
332331
{
333-
Iterator<Map.Entry<String, List<V>>> iter = entrySet().iterator();
334-
StringBuilder sb = new StringBuilder();
335-
sb.append('{');
336-
boolean delim = false;
337-
while (iter.hasNext())
338-
{
339-
Map.Entry<String, List<V>> e = iter.next();
340-
if (delim)
341-
{
342-
sb.append(", ");
343-
}
344-
String key = e.getKey();
345-
List<V> vals = e.getValue();
346-
sb.append(key);
347-
sb.append('=');
348-
if (vals.size() == 1)
349-
{
350-
sb.append(vals.get(0));
351-
}
352-
else
353-
{
354-
sb.append(vals);
355-
}
356-
delim = true;
357-
}
358-
sb.append('}');
359-
return sb.toString();
332+
return TypeUtil.toString(this);
360333
}
361334

362335
/**

jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/TypeUtil.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Iterator;
3636
import java.util.List;
3737
import java.util.ListIterator;
38+
import java.util.Map;
3839
import java.util.Objects;
3940
import java.util.Optional;
4041
import java.util.ServiceConfigurationError;
@@ -864,4 +865,39 @@ public static boolean isDeclaredMethodOn(Object object, String methodName, Class
864865
return false;
865866
}
866867
}
868+
869+
/**
870+
* Pretty print a map. Specifically expanding Array values.
871+
* @param map The map to render as a String
872+
* @return A String representation of the map
873+
*/
874+
public static String toString(Map<?, ?> map)
875+
{
876+
if (map.isEmpty())
877+
return "{}";
878+
879+
StringBuilder sb = new StringBuilder();
880+
sb.append('{');
881+
for (Iterator<? extends Map.Entry<?, ?>> i = map.entrySet().iterator(); i.hasNext();)
882+
{
883+
Map.Entry<?, ?> e = i.next();
884+
Object key = e.getKey();
885+
sb.append(key);
886+
sb.append('=');
887+
888+
Object value = e.getValue();
889+
890+
if (value == null)
891+
sb.append("null");
892+
else if (value.getClass().isArray())
893+
sb.append(Arrays.asList((Object[])value));
894+
else
895+
sb.append(value);
896+
if (i.hasNext())
897+
sb.append(',');
898+
}
899+
sb.append('}');
900+
return sb.toString();
901+
}
902+
867903
}

jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/MultiMapTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,12 @@ public void testToString()
462462
MultiMap<String> mm = new MultiMap<>();
463463
mm.put("color", "red");
464464

465-
assertEquals("{color=red}", mm.toString());
465+
assertEquals("{color=[red]}", mm.toString());
466466

467467
mm.putValues("food", "apple", "cherry", "raspberry");
468468

469-
String expected1 = "{color=red, food=[apple, cherry, raspberry]}";
470-
String expected2 = "{food=[apple, cherry, raspberry], color=red}";
469+
String expected1 = "{color=[red],food=[apple, cherry, raspberry]}";
470+
String expected2 = "{food=[apple, cherry, raspberry],color=[red]}";
471471
String actual = mm.toString();
472472
assertTrue(actual.equals(expected1) || actual.equals(expected2));
473473
}

jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/RequestTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.IOException;
1717
import java.io.InputStream;
1818
import java.io.OutputStream;
19+
import java.io.PrintWriter;
1920
import java.io.UnsupportedEncodingException;
2021
import java.net.InetAddress;
2122
import java.net.Socket;
@@ -626,4 +627,49 @@ protected void service(HttpServletRequest request, HttpServletResponse resp) thr
626627
assertThat(date, containsString(":"));
627628
}
628629
}
630+
631+
@Test
632+
public void testParameters() throws Exception
633+
{
634+
final AtomicReference<String> parameterMap = new AtomicReference<>();
635+
636+
startServer(new HttpServlet()
637+
{
638+
@Override
639+
protected void service(HttpServletRequest request, HttpServletResponse resp) throws IOException
640+
{
641+
parameterMap.set(request.getParameterMap().toString());
642+
PrintWriter out = resp.getWriter();
643+
out.println(request.getParameter("a"));
644+
out.println(request.getParameterValues("a")[1]);
645+
out.println(request.getParameterValues("a")[2]);
646+
out.println(Arrays.asList(request.getParameterValues("b")));
647+
out.println(Arrays.asList(request.getParameterValues("c")));
648+
out.println(Arrays.asList(request.getParameterValues("d")));
649+
650+
}
651+
});
652+
653+
String rawResponse = _connector.getResponse(
654+
"""
655+
POST /test/parameters?a=1&a=2&b=one&c= HTTP/1.1\r
656+
Host: localhost\r
657+
Connection: close\r
658+
Content-Type: application/x-www-form-urlencoded\r
659+
Content-Length: 23\r
660+
\r
661+
a=3&b=two&b=three&d=xyz\r
662+
""");
663+
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
664+
assertThat(response.getStatus(), is(HttpStatus.OK_200));
665+
assertThat(parameterMap.get(), is("{a=[1, 2, 3],b=[one, two, three],c=[],d=[xyz]}"));
666+
assertThat(response.getContent().replaceAll("\r\n", "\n"), is("""
667+
1
668+
2
669+
3
670+
[one, two, three]
671+
[]
672+
[xyz]
673+
"""));
674+
}
629675
}

0 commit comments

Comments
 (0)