Permalink
Browse files

restclient: urlbuilder can optionally not emit the scheme and domain

parts of a URL.
  • Loading branch information...
1 parent e7bf556 commit 9dad32038ce8124fc5715eb4a60bc6e7ca2d8720 @kgilmer kgilmer committed Nov 4, 2011
Showing with 43 additions and 6 deletions.
  1. +43 −6 org.touge.restclient/src/org/touge/restclient/ReSTClient.java
@@ -131,6 +131,19 @@
* @return URLBuilder
*/
URLBuilder addParameter(String key, String value);
+
+ /**
+ *
+ * @param value if true, scheme will be present in toString(), otherwise will be omitted.
+ * @return URLBuilder
+ */
+ URLBuilder emitScheme(boolean value);
+
+ /**
+ * @param value if true, domain is emitted in toString(), false means it will not be emitted.
+ * @return URLBuilder
+ */
+ URLBuilder emitDomain(boolean value);
}
/**
@@ -1314,6 +1327,8 @@ private void writeRequestBody(HttpURLConnection connection, byte[] content) thro
private final List<String> segments;
private boolean httpsScheme;
private List<Map.Entry<String, String>> parameters;
+ private boolean emitScheme = true;
+ private boolean emitDomain = true;
/**
*
@@ -1377,12 +1392,22 @@ else if (segment.toUpperCase().startsWith("HTTPS:")) {
public String toString() {
StringBuilder sb = new StringBuilder();
- if (httpsScheme)
- sb.append("https://");
- else
- sb.append("http://");
+ if (emitScheme) {
+ if (httpsScheme)
+ sb.append("https://");
+ else
+ sb.append("http://");
+ }
+ int count = 0;
for (Iterator<String> i = segments.iterator(); i.hasNext();) {
+ count++;
+ if (!emitDomain && count == 1) {
+ i.next();
+ sb.append('/');
+ continue;
+ }
+
sb.append(i.next());
if (i.hasNext())
@@ -1462,16 +1487,28 @@ public String setValue(String arg0) {
@Override
public String getValue() {
- return key;
+ return value;
}
@Override
public String getKey() {
- return value;
+ return key;
}
});
return this;
}
+
+ @Override
+ public URLBuilder emitScheme(boolean value) {
+ this.emitScheme = value;
+ return this;
+ }
+
+ @Override
+ public URLBuilder emitDomain(boolean value) {
+ this.emitDomain = value;
+ return this;
+ }
}
}

0 comments on commit 9dad320

Please sign in to comment.