Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

restclient: add query string parameter handling to urlbuilder and test

case.
  • Loading branch information...
commit e7bf55602dcd268944a1f159091a325d889b3fd2 1 parent cde44cf
@kgilmer kgilmer authored
View
71 org.touge.restclient/src/org/touge/restclient/ReSTClient.java
@@ -13,6 +13,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
@@ -108,6 +109,7 @@
/**
* @return URL as a String with scheme
*/
+ @Override
String toString();
/**
@@ -120,6 +122,15 @@
* @return A new instance of URLBuilder with same path and scheme as parent, with segment appended.
*/
URLBuilder copy(String ... segments);
+
+ /**
+ * Add a query string parameter to the URL.
+ *
+ * @param key key name. Same key name can be used multiple times.
+ * @param value value of parameter.
+ * @return URLBuilder
+ */
+ URLBuilder addParameter(String key, String value);
}
/**
@@ -364,7 +375,7 @@ public String getName() {
private ConnectionProvider connectionProvider;
- private List<ConnectionInitializer> connectionInitializers;
+ private final List<ConnectionInitializer> connectionInitializers;
private ErrorHandler errorHandler;
private PrintWriter debugStream;
@@ -682,7 +693,7 @@ public T getContent() throws IOException {
errorHandler.handleError(getCode(), serverMessage);
if (deserializer != null)
- return (T) deserializer.deserialize(connection.getErrorStream(), connection.getResponseCode(),
+ return deserializer.deserialize(connection.getErrorStream(), connection.getResponseCode(),
connection.getHeaderFields());
return null;
@@ -694,14 +705,14 @@ public T getContent() throws IOException {
done = true;
if (responseBuffer != null) {
- debugMid(responseBuffer, ((T)response).toString());
+ debugMid(responseBuffer, response.toString());
debugEnd(responseBuffer);
}
- return (T) response;
+ return response;
}
- T response = (T) deserializer.deserialize(connection.getInputStream(),
+ T response = deserializer.deserialize(connection.getInputStream(),
connection.getResponseCode(), connection.getHeaderFields());
done = true;
@@ -1143,7 +1154,7 @@ public static String propertyString(Map<String, String> props) throws IOExceptio
String key = i.next();
sb.append(URLEncoder.encode(key, "UTF-8"));
sb.append("=");
- sb.append(URLEncoder.encode((String) props.get(key), "UTF-8"));
+ sb.append(URLEncoder.encode(props.get(key), "UTF-8"));
if (i.hasNext()) {
sb.append("&");
@@ -1271,6 +1282,7 @@ private static void validateArguments(Object ... args) {
/* (non-Javadoc)
* @see com.buglabs.util.http.RestClient.ConnectionProvider#getConnection(java.lang.String)
*/
+ @Override
public HttpURLConnection getConnection(String urlStr) throws IOException {
URL url = new URL(urlStr);
return (HttpURLConnection) url.openConnection();
@@ -1301,6 +1313,7 @@ private void writeRequestBody(HttpURLConnection connection, byte[] content) thro
private final class URLBuilderImpl implements URLBuilder {
private final List<String> segments;
private boolean httpsScheme;
+ private List<Map.Entry<String, String>> parameters;
/**
*
@@ -1375,6 +1388,22 @@ public String toString() {
if (i.hasNext())
sb.append('/');
}
+
+ try {
+ if (parameters != null) {
+ sb.append('?');
+ for (Map.Entry<String, String> parameter : parameters) {
+ sb.append(URLEncoder.encode(parameter.getKey(), "UTF-8"));
+ sb.append('=');
+ sb.append(URLEncoder.encode(parameter.getValue(), "UTF-8"));
+
+ if (parameters.indexOf(parameter) < (parameters.size() - 1))
+ sb.append('&');
+ }
+ }
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
return sb.toString();
}
@@ -1414,5 +1443,35 @@ public URLBuilder copy(String ... segments) {
return this.copy().append(segments);
}
+
+ @Override
+ public URLBuilder addParameter(final String key, final String value) {
+ validateArguments(key, value);
+
+ if (parameters == null) {
+ parameters = new ArrayList<Map.Entry<String,String>>();
+ }
+
+ parameters.add(new Map.Entry<String, String>() {
+
+ @Override
+ public String setValue(String arg0) {
+ //Unimplemented
+ return null;
+ }
+
+ @Override
+ public String getValue() {
+ return key;
+ }
+
+ @Override
+ public String getKey() {
+ return value;
+ }
+ });
+
+ return this;
+ }
}
}
View
9 org.touge.restclient/test/org/touge/restclient/test/ReSTClient_usage.java
@@ -10,7 +10,6 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
@@ -225,6 +224,14 @@ public void handleError(int code, String message) throws IOException {
pl(origurl);
// Child URL: https://myhost.net/homepage/asdf/adf/reqotwoetiywer
pl(newurl);
+
+ URLBuilder purl = restClient.buildURL("myhost.net/","/homepage");
+
+ purl.addParameter("p1", "v1");
+ purl.addParameter("p1", "v1-2");
+ purl.addParameter("p2", "v2");
+
+ pl(purl);
}
private static void pl(Object message) {
View
61 org.touge.restclient/test/org/touge/restclient/test/URLBuilderTests.java
@@ -0,0 +1,61 @@
+package org.touge.restclient.test;
+
+import junit.framework.TestCase;
+
+import org.touge.restclient.ReSTClient;
+import org.touge.restclient.ReSTClient.URLBuilder;
+
+public class URLBuilderTests extends TestCase {
+
+ public void testURLBuilder() {
+ ReSTClient restClient = new ReSTClient();
+
+ //This URLBuilder builds https://citibank.com/secureme/halp
+ String url = restClient.buildURL("htTPS://citibank.com/secureme/").append("/halp").toString();
+ assertTrue(url.equals("https://citibank.com/secureme/halp"));
+
+ // Builds http://yahoo.com/a/mystore/toodles?index=5
+ url = restClient.buildURL("yahoo.com")
+ .append("a")
+ .append("mystore/")
+ .append("toodles?index=5").toString();
+ assertTrue(url.equals("http://yahoo.com/a/mystore/toodles?index=5"));
+
+ // Builds http://me.com/you/andi/like/each/ohter
+ url =
+ restClient.buildURL("me.com/")
+ .append("/you/")
+ .append("/andi/")
+ .append("like/each/ohter/").toString();
+ assertTrue(url.equals("http://me.com/you/andi/like/each/ohter"));
+
+ // Builds https://myhost.com/first/second/third/forth/fith/mypage.asp?i=1&b=2&c=3
+ url = restClient.buildURL(
+ "myhost.com",
+ "first/",
+ "//second",
+ "third/forth/fith",
+ "mypage.asp?i=1&b=2&c=3").setHttps(true).toString();
+ assertTrue(url.equals("https://myhost.com/first/second/third/forth/fith/mypage.asp?i=1&b=2&c=3"));
+
+ // Create child URLs from base URLs
+ URLBuilder origurl = restClient.buildURL("myhost.net/","/homepage");
+ URLBuilder newurl = origurl
+ .copy()
+ .append("asdf/adf/reqotwoetiywer")
+ .setHttps(true);
+
+ // Original URL: http://myhost.net/homepage
+ assertTrue(origurl.toString().equals("http://myhost.net/homepage"));
+ // Child URL: https://myhost.net/homepage/asdf/adf/reqotwoetiywer
+ assertTrue(newurl.toString().equals("https://myhost.net/homepage/asdf/adf/reqotwoetiywer"));
+
+ URLBuilder purl = restClient.buildURL("myhost.net/","/homepage");
+
+ purl.addParameter("p1", "v1");
+ purl.addParameter("p1", "v1-2");
+ purl.addParameter("p2", "v2");
+
+ assertTrue(purl.toString().equals("http://myhost.net/homepage?v1=p1&v1-2=p1&v2=p2"));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.