You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by bd...@apache.org on 2011/02/17 12:17:25 UTC
svn commit: r1071577 - in
/incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http:
Request.java RequestBuilder.java RequestExecutor.java
Author: bdelacretaz
Date: Thu Feb 17 11:17:25 2011
New Revision: 1071577
URL: http://svn.apache.org/viewvc?rev=1071577&view=rev
Log:
STANBOL-19 - query parameters, redirects and improved Entity support, added while working on SLING-1981 where I'm reusing that module
Modified:
incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/Request.java
incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestBuilder.java
incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java
Modified: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/Request.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/Request.java?rev=1071577&r1=1071576&r2=1071577&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/Request.java (original)
+++ incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/Request.java Thu Feb 17 11:17:25 2011
@@ -18,6 +18,7 @@ package org.apache.stanbol.commons.testi
import java.io.UnsupportedEncodingException;
+import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
@@ -29,6 +30,7 @@ public class Request {
private final HttpUriRequest request;
private String username;
private String password;
+ private boolean redirects = true;
Request(HttpUriRequest r) {
request = r;
@@ -48,14 +50,28 @@ public class Request {
this.password = password;
return this;
}
+
+ public Request withRedirects(boolean followRedirectsAutomatically) {
+ redirects = followRedirectsAutomatically;
+ return this;
+ }
- public Request withContent(String content) throws UnsupportedEncodingException {
+ private HttpEntityEnclosingRequestBase getHttpEntityEnclosingRequestBase() {
if(request instanceof HttpEntityEnclosingRequestBase) {
- ((HttpEntityEnclosingRequestBase)request).setEntity(new StringEntity(content, "UTF-8"));
+ return (HttpEntityEnclosingRequestBase)request;
} else {
- throw new IllegalStateException("Cannot add content to request "
- + request.getClass().getName());
+ throw new IllegalStateException(
+ "Request is not an HttpEntityEnclosingRequestBase: "
+ + request.getClass().getName());
}
+ }
+
+ public Request withContent(String content) throws UnsupportedEncodingException {
+ return withEntity(new StringEntity(content, "UTF-8"));
+ }
+
+ public Request withEntity(HttpEntity e) throws UnsupportedEncodingException {
+ getHttpEntityEnclosingRequestBase().setEntity(e);
return this;
}
@@ -66,4 +82,8 @@ public class Request {
public String getPassword() {
return password;
}
+
+ public boolean getRedirects() {
+ return redirects;
+ }
}
Modified: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestBuilder.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestBuilder.java?rev=1071577&r1=1071576&r2=1071577&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestBuilder.java (original)
+++ incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestBuilder.java Thu Feb 17 11:17:25 2011
@@ -16,9 +16,15 @@
*/
package org.apache.stanbol.commons.testing.http;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.message.BasicNameValuePair;
/** Convenience builder for Request objects */
public class RequestBuilder {
@@ -28,15 +34,57 @@ public class RequestBuilder {
this.baseUrl = baseUrl;
}
- public Request buildGetRequest(String path) {
- return new Request(new HttpGet(baseUrl + path));
+ /** Build a GET request to specified path with optional query
+ * parameters. See {@link #buildUrl(String, String...)} for
+ * queryParameters semantics.
+ */
+ public Request buildGetRequest(String path, String...queryParameters) {
+ return new Request(new HttpGet(buildUrl(path, queryParameters)));
}
+ /** Build a POST request to specified path with optional query
+ * parameters. See {@link #buildUrl(String, String...)} for
+ * queryParameters semantics.
+ */
public Request buildPostRequest(String path) {
- return new Request(new HttpPost(baseUrl + path));
+ return new Request(new HttpPost(buildUrl(path)));
}
+ /** Wrap supplied HTTP request */
public Request buildOtherRequest(HttpRequestBase r) {
return new Request(r);
}
+
+ /** Build an URL from our base path, supplied path and optional
+ * query parameters.
+ * @param queryParameters an even number of Strings, each pair
+ * of values represents the key and value of a query parameter.
+ * Keys and values are encoded by this method.
+ */
+ public String buildUrl(String path, String...queryParameters) {
+ final StringBuilder sb = new StringBuilder();
+
+ if(queryParameters == null || queryParameters.length == 0) {
+ sb.append(baseUrl);
+ sb.append(path);
+
+ } else if(queryParameters.length % 2 != 0) {
+ throw new IllegalArgumentException(
+ "Invalid number of queryParameters arguments ("
+ + queryParameters.length
+ + "), must be even"
+ );
+ } else {
+ final List<NameValuePair> p = new ArrayList<NameValuePair>();
+ for(int i=0 ; i < queryParameters.length; i+=2) {
+ p.add(new BasicNameValuePair(queryParameters[i], queryParameters[i+1]));
+ }
+ sb.append(baseUrl);
+ sb.append(path);
+ sb.append("?");
+ sb.append(URLEncodedUtils.format(p, "UTF-8"));
+ }
+
+ return sb.toString();
+ }
}
Modified: incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java?rev=1071577&r1=1071576&r2=1071577&view=diff
==============================================================================
--- incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java (original)
+++ incubator/stanbol/trunk/commons/testing/http/src/main/java/org/apache/stanbol/commons/testing/http/RequestExecutor.java Thu Feb 17 11:17:25 2011
@@ -39,6 +39,7 @@ import org.apache.http.auth.UsernamePass
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
@@ -105,8 +106,8 @@ public class RequestExecutor {
clear();
request = r.getRequest();
+ // Optionally setup for basic authentication
if(r.getUsername() != null) {
- // Setup for basic authentication
httpClient.getCredentialsProvider().setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(r.getUsername(), r.getPassword()));
@@ -118,6 +119,10 @@ public class RequestExecutor {
httpClient.removeRequestInterceptorByClass(PreemptiveAuthInterceptor.class);
}
+ // Setup redirects
+ httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, r.getRedirects());
+
+ // Execute request
response = httpClient.execute(request);
entity = response.getEntity();
if(entity != null) {