[multimap-ng] moving UrlEncoded over and fixing various other failing tests

This commit is contained in:
Joakim Erdfelt 2012-08-10 14:42:35 -07:00
parent 2ad81fc627
commit b8de9a35a4
5 changed files with 35 additions and 29 deletions

View File

@ -144,7 +144,9 @@ public class ShutdownHandler extends AbstractHandler
private boolean hasCorrectSecurityToken(HttpServletRequest request)
{
return _shutdownToken.equals(request.getParameter("token"));
String tok = request.getParameter("token");
LOG.debug("Token: {}", tok);
return _shutdownToken.equals(tok);
}
private void shutdownServer() throws Exception

View File

@ -217,7 +217,7 @@ public class HttpURITest
try
{
HttpURI huri=new HttpURI(uri);
MultiMap params = new MultiMap();
MultiMap<String> params = new MultiMap<>();
huri.decodeQueryTo(params);
System.err.println(params);
Assert.assertTrue(false);
@ -229,7 +229,7 @@ public class HttpURITest
try
{
HttpURI huri=new HttpURI(uri);
MultiMap params = new MultiMap();
MultiMap<String> params = new MultiMap<>();
huri.decodeQueryTo(params,"UTF-8");
System.err.println(params);
Assert.assertTrue(false);
@ -247,9 +247,9 @@ public class HttpURITest
{
HttpURI uri = new HttpURI("/path?value="+URLEncoder.encode(value,"UTF-8"));
MultiMap parameters = new MultiMap();
MultiMap<String> parameters = new MultiMap<>();
uri.decodeQueryTo(parameters,"UTF-8");
assertEquals(value,parameters.get("value"));
assertEquals(value,parameters.getString("value"));
}
}

View File

@ -18,7 +18,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.jetty.util.Utf8Appendable.NotUtf8Exception;
@ -44,7 +44,8 @@ import org.eclipse.jetty.util.log.Logger;
*
* @see java.net.URLEncoder
*/
public class UrlEncoded extends MultiMap implements Cloneable
@SuppressWarnings("serial")
public class UrlEncoded extends MultiMap<String> implements Cloneable
{
static final Logger LOG = Log.getLogger(UrlEncoded.class);
@ -118,21 +119,24 @@ public class UrlEncoded extends MultiMap implements Cloneable
* @param equalsForNullValue if True, then an '=' is always used, even
* for parameters without a value. e.g. "blah?a=&b=&c=".
*/
public static String encode(MultiMap map, String charset, boolean equalsForNullValue)
public static String encode(MultiMap<String> map, String charset, boolean equalsForNullValue)
{
if (charset==null)
charset=ENCODING;
StringBuilder result = new StringBuilder(128);
Iterator<Map.Entry<String, Object>> iter = map.entrySet().iterator();
while(iter.hasNext())
boolean delim = false;
for(Map.Entry<String, List<String>> entry: map.entrySet())
{
Map.Entry<String, Object> entry = iter.next();
String key = entry.getKey().toString();
Object list = entry.getValue();
int s=LazyList.size(list);
List<String> list = entry.getValue();
int s=list.size();
if (delim)
{
result.append('&');
}
if (s==0)
{
@ -146,7 +150,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
{
if (i>0)
result.append('&');
Object val=LazyList.get(list,i);
String val=list.get(i);
result.append(encodeString(key,charset));
if (val!=null)
@ -164,8 +168,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
result.append('=');
}
}
if (iter.hasNext())
result.append('&');
delim = true;
}
return result.toString();
}
@ -176,7 +179,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
/** Decoded parameters to Map.
* @param content the string containing the encoded parameters
*/
public static void decodeTo(String content, MultiMap map, String charset)
public static void decodeTo(String content, MultiMap<String> map, String charset)
{
decodeTo(content,map,charset,-1);
}
@ -185,7 +188,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
/** Decoded parameters to Map.
* @param content the string containing the encoded parameters
*/
public static void decodeTo(String content, MultiMap map, String charset, int maxKeys)
public static void decodeTo(String content, MultiMap<String> map, String charset, int maxKeys)
{
if (charset==null)
charset=ENCODING;
@ -266,7 +269,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
* @param map the {@link MultiMap} to populate
* @param buffer the buffer to decode into
*/
public static void decodeUtf8To(byte[] raw,int offset, int length, MultiMap map)
public static void decodeUtf8To(byte[] raw,int offset, int length, MultiMap<String> map)
{
Utf8StringBuilder buffer = new Utf8StringBuilder();
synchronized(map)
@ -347,7 +350,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
* @param map MultiMap to add parameters to
* @param maxLength maximum number of keys to read or -1 for no limit
*/
public static void decode88591To(InputStream in, MultiMap map, int maxLength, int maxKeys)
public static void decode88591To(InputStream in, MultiMap<String> map, int maxLength, int maxKeys)
throws IOException
{
synchronized(map)
@ -431,7 +434,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
* @param map MultiMap to add parameters to
* @param maxLength maximum number of keys to read or -1 for no limit
*/
public static void decodeUtf8To(InputStream in, MultiMap map, int maxLength, int maxKeys)
public static void decodeUtf8To(InputStream in, MultiMap<String> map, int maxLength, int maxKeys)
throws IOException
{
synchronized(map)
@ -518,7 +521,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
}
/* -------------------------------------------------------------- */
public static void decodeUtf16To(InputStream in, MultiMap map, int maxLength, int maxKeys) throws IOException
public static void decodeUtf16To(InputStream in, MultiMap<String> map, int maxLength, int maxKeys) throws IOException
{
InputStreamReader input = new InputStreamReader(in,StringUtil.__UTF16);
StringWriter buf = new StringWriter(8192);
@ -531,7 +534,7 @@ public class UrlEncoded extends MultiMap implements Cloneable
/** Decoded parameters to Map.
* @param in the stream containing the encoded parameters
*/
public static void decodeTo(InputStream in, MultiMap map, String charset, int maxLength, int maxKeys)
public static void decodeTo(InputStream in, MultiMap<String> map, String charset, int maxLength, int maxKeys)
throws IOException
{
//no charset present, use the configured default

View File

@ -18,6 +18,7 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import org.junit.Assert;
import org.junit.Test;
@ -177,7 +178,7 @@ public class URLEncodedTest
for (int i=0;i<charsets.length;i++)
{
ByteArrayInputStream in = new ByteArrayInputStream("name\n=value+%30&name1=&name2&n\u00e3me3=value+3".getBytes(charsets[i][0]));
MultiMap m = new MultiMap();
MultiMap<String> m = new MultiMap<>();
UrlEncoded.decodeTo(in, m, charsets[i][1], -1,-1);
assertEquals(i+" stream length",4,m.size());
assertEquals(i+" stream name\\n","value 0",m.getString("name\n"));
@ -190,7 +191,7 @@ public class URLEncodedTest
if (java.nio.charset.Charset.isSupported("Shift_JIS"))
{
ByteArrayInputStream in2 = new ByteArrayInputStream ("name=%83e%83X%83g".getBytes());
MultiMap m2 = new MultiMap();
MultiMap<String> m2 = new MultiMap<>();
UrlEncoded.decodeTo(in2, m2, "Shift_JIS", -1,-1);
assertEquals("stream length",1,m2.size());
assertEquals("stream name","\u30c6\u30b9\u30c8",m2.getString("name"));
@ -232,7 +233,7 @@ public class URLEncodedTest
String hex ="E0B89FE0B8ABE0B881E0B8A7E0B894E0B8B2E0B988E0B881E0B89FE0B8A7E0B8ABE0B8AAE0B894E0B8B2E0B988E0B8ABE0B89FE0B881E0B8A7E0B894E0B8AAE0B8B2E0B89FE0B881E0B8ABE0B8A3E0B894E0B989E0B89FE0B8ABE0B899E0B881E0B8A3E0B894E0B8B5";
String expected = new String(TypeUtil.fromHexString(hex),"utf-8");
assertEquals(expected,url_encoded.get("text"));
Assert.assertEquals(expected,url_encoded.getString("text"));
}
/* -------------------------------------------------------------- */
@ -241,7 +242,7 @@ public class URLEncodedTest
{
String query="name=X%c0%afZ";
MultiMap map = new MultiMap();
MultiMap<String> map = new MultiMap<>();
UrlEncoded.LOG.info("EXPECT 4 Not Valid UTF8 warnings...");
UrlEncoded.decodeUtf8To(query.getBytes(StringUtil.__ISO_8859_1),0,query.length(),map);
assertEquals("X"+Utf8Appendable.REPLACEMENT+Utf8Appendable.REPLACEMENT+"Z",map.getValue("name",0));