Author: sjlee
Date: Wed Jun 24 22:29:33 2009
New Revision: 788199
URL: http://svn.apache.org/viewvc?rev=788199&view=rev
Log:
ASYNCWEB-31
added support for multiple values for a given query parameter.
Modified:
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestEncoder.java
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestMessage.java
Modified:
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestEncoder.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestEncoder.java?rev=788199&r1=788198&r2=788199&view=diff
==============================================================================
---
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestEncoder.java
(original)
+++
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestEncoder.java
Wed Jun 24 22:29:33 2009
@@ -21,6 +21,7 @@
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@@ -100,16 +101,21 @@
StringBuilder sb = new StringBuilder(1024);
//If we have content, lets create the query string
- int attrCount = msg.getParameters().size();
String urlAttrs = "";
- if (attrCount > 0) {
- NameValuePair attrs[] = new NameValuePair[attrCount];
- Set<Map.Entry<String, String>> set =
msg.getParameters().entrySet();
- int i = 0;
- for (Map.Entry<String, String> entry : set) {
- attrs[i++] = new NameValuePair(entry.getKey(),
entry.getValue());
+ Map<String, List<String>> parameters = msg.getParameters();
+ if (!parameters.isEmpty()) {
+ List<NameValuePair> attrs = new ArrayList<NameValuePair>();
+ for (Map.Entry<String, List<String>> entry :
parameters.entrySet()) {
+ List<String> values = entry.getValue();
+ if (values != null) {
+ for (String value : values) {
+ attrs.add(new NameValuePair(entry.getKey(), value));
+ }
+ }
}
- urlAttrs = EncodingUtil.formUrlEncode(attrs,
msg.getUrlEncodingCharset());
+ if (!attrs.isEmpty()) {
+ urlAttrs = EncodingUtil.formUrlEncode(attrs.toArray(new
NameValuePair[] {}), msg.getUrlEncodingCharset());
+ }
}
String method = msg.getRequestMethod();
@@ -120,10 +126,14 @@
if (msg.isProxyEnabled() &&
!msg.getProtocol().toLowerCase().equals("https")) {
sb.append(msg.getUrl().toString());
} else {
- sb.append(msg.getUrl().getFile());
+ String file = msg.getUrl().getFile();
+ if (file.equals("")) {
+ file = "/"; // default path
+ }
+ sb.append(file);
}
//If its a GET, append the attributes
- if (method.equals(HttpRequestMessage.REQUEST_GET) && attrCount >
0) {
+ if (method.equals(HttpRequestMessage.REQUEST_GET) &&
urlAttrs.length() > 0) {
//If there is not already a ? in the query, append one,
otherwise append a &
if (!msg.getUrl().getFile().contains("?")) {
sb.append('?');
@@ -158,7 +168,7 @@
// If this is a POST and parameters are provided, this is a form
// post; any existing content is an error and will be ignored and
// the content type will be set accordingly
- if (method.equals(HttpRequestMessage.REQUEST_POST) && attrCount > 0) {
+ if (method.equals(HttpRequestMessage.REQUEST_POST) &&
urlAttrs.length() > 0) {
content = urlAttrs.getBytes();
// these override any headers that might already be in the set
Modified:
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestMessage.java
URL:
http://svn.apache.org/viewvc/mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestMessage.java?rev=788199&r1=788198&r2=788199&view=diff
==============================================================================
---
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestMessage.java
(original)
+++
mina/asyncweb/branches/1.0-mina1/client/src/main/java/org/apache/asyncweb/client/codec/HttpRequestMessage.java
Wed Jun 24 22:29:33 2009
@@ -24,7 +24,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
@@ -102,7 +101,7 @@
/**
* The parameters.
*/
- private Map<String, String> parameters = new HashMap<String, String>();
+ private Map<String, List<String>> parameters = new HashMap<String,
List<String>>();
/**
* The user agent.
@@ -346,21 +345,27 @@
}
/**
- * Gets a parameter from the parameter map.
+ * Gets a parameter from the parameter map. Call this method <b>only
+ * if</b> you are certain there is one value for the name, as it returns
+ * the first value.
*
* @param name the parameter name
* @return the parameter value
*/
public String getParameter(String name) {
- return parameters.get(name);
+ List<String> values = parameters.get(name);
+ if (values == null || values.isEmpty()) {
+ return null;
+ }
+ return values.get(0);
}
-
+
/**
* Gets the parameter map.
*
* @return the parameter map
*/
- public Map<String, String> getParameters() {
+ public Map<String, List<String>> getParameters() {
return parameters;
}
@@ -369,18 +374,24 @@
*
* @param parameters the parameter map
*/
- public void setParameters(Map<String, String> parameters) {
+ public void setParameters(Map<String, List<String>> parameters) {
this.parameters.putAll(parameters);
}
/**
- * Sets a single parameter.
+ * Sets a single parameter. If a value already exists for the key, the
+ * new value is added to the list.
*
* @param name the parameter name
* @param value the value of parameter
*/
public void setParameter(String name, String value) {
- parameters.put(name, value);
+ List<String> values = parameters.get(name);
+ if (values == null) {
+ values = new ArrayList<String>();
+ parameters.put(name, values);
+ }
+ values.add(value);
}
/**
@@ -484,17 +495,15 @@
return matchCredentials(credentials, scope);
}
- private static Credentials matchCredentials(final HashMap map, final
AuthScope authscope) {
+ private static Credentials matchCredentials(final
HashMap<AuthScope,Credentials> map, final AuthScope authscope) {
// see if we get a direct hit
- Credentials creds = (Credentials)map.get(authscope);
+ Credentials creds = map.get(authscope);
if (creds == null) {
// Nope.
// Do a full scan
int bestMatchFactor = -1;
AuthScope bestMatch = null;
- Iterator items = map.keySet().iterator();
- while (items.hasNext()) {
- AuthScope current = (AuthScope)items.next();
+ for (AuthScope current : map.keySet()) {
int factor = authscope.match(current);
if (factor > bestMatchFactor) {
bestMatchFactor = factor;
@@ -502,7 +511,7 @@
}
}
if (bestMatch != null) {
- creds = (Credentials)map.get(bestMatch);
+ creds = map.get(bestMatch);
}
}
return creds;