You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2011/11/12 05:25:25 UTC
svn commit: r1201185 [1/2] - in /abdera/abdera2:
activities/src/main/java/org/apache/abdera2/activities/client/
activities/src/main/java/org/apache/abdera2/activities/extra/
client/src/main/java/org/apache/abdera2/protocol/client/
common/src/main/java/...
Author: jmsnell
Date: Sat Nov 12 04:25:22 2011
New Revision: 1201185
URL: http://svn.apache.org/viewvc?rev=1201185&view=rev
Log: (empty)
Added:
abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreExecutors2.java (with props)
abdera/abdera2/core/src/main/java/org/apache/abdera2/protocol/client/
abdera/abdera2/core/src/main/java/org/apache/abdera2/protocol/client/AbderaBody.java (props changed)
- copied unchanged from r1174408, abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaBody.java
abdera/abdera2/core/src/main/java/org/apache/abdera2/protocol/client/AbderaClient.java (props changed)
- copied unchanged from r1174408, abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaClient.java
abdera/abdera2/core/src/main/java/org/apache/abdera2/protocol/client/AbderaClientResponse.java (props changed)
- copied unchanged from r1201010, abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaClientResponse.java
abdera/abdera2/core/src/main/java/org/apache/abdera2/protocol/client/AbderaClientResponseImpl.java (props changed)
- copied unchanged from r1187599, abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaClientResponseImpl.java
abdera/abdera2/core/src/main/java/org/apache/abdera2/protocol/client/AbderaEntity.java (props changed)
- copied unchanged from r1174408, abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaEntity.java
abdera/abdera2/core/src/main/java/org/apache/abdera2/protocol/client/AbderaResponseHandler.java (props changed)
- copied unchanged from r1174408, abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaResponseHandler.java
abdera/abdera2/core/src/main/java/org/apache/abdera2/protocol/client/AbderaSession.java (contents, props changed)
- copied, changed from r1187599, abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaSession.java
Removed:
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaBody.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaClient.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaClientResponse.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaClientResponseImpl.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaEntity.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaResponseHandler.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/AbderaSession.java
Modified:
abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClient.java
abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java
abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesSession.java
abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/OAuth2Helper.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseImpl.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseWrapper.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestHelper.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestOptions.java
abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/Session.java
abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/AbstractMessage.java
abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/AbstractResponseContext.java
abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/BaseRequestContextWrapper.java
abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/BaseResponseContextWrapper.java
abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/Message.java
abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/ResponseContext.java
abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/GooglePlusExample.java
abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/history/FeedPagingHelper.java
abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/json/JSONServlet.java
abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/AppTest.java
abdera/abdera2/test/src/main/java/org/apache/abdera2/test/client/CacheTest.java
abdera/abdera2/test/src/main/java/org/apache/abdera2/test/server/basic/BasicTest.java
abdera/abdera2/test/src/main/java/org/apache/abdera2/test/server/custom/CustomProviderTest.java
abdera/abdera2/test/src/main/java/org/apache/abdera2/test/server/customer/CustomerAdapterTest.java
Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClient.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClient.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClient.java (original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClient.java Sat Nov 12 04:25:22 2011
@@ -49,14 +49,14 @@ public class ActivitiesClient
return (T)new ActivitiesSession(this);
}
- public <T extends Collection<?>>ASDocument<T> getCollection(String uri) {
+ public <M extends ASObject,T extends Collection<M>>ASDocument<T> getCollection(String uri) {
ActivitiesSession session = newSession();
- return session.<T>getCollection(uri);
+ return session.<M,T>getCollection(uri);
}
- public <T extends Collection<?>>ASDocument<T> getCollection(String uri, RequestOptions options) {
+ public <M extends ASObject,T extends Collection<M>>ASDocument<T> getCollection(String uri, RequestOptions options) {
ActivitiesSession session = newSession();
- return session.<T>getCollection(uri,options);
+ return session.<M,T>getCollection(uri,options);
}
public <T extends Activity>ASDocument<T> getActivity(String uri) {
Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java (original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesClientPusher.java Sat Nov 12 04:25:22 2011
@@ -56,7 +56,7 @@ public class ActivitiesClientPusher<T ex
public ActivitiesClientPusher(
String iri,
ActivitiesSession session) {
- this(iri,session,session.getDefaultRequestOptions());
+ this(iri,session,session.getDefaultRequestOptions().get());
}
public ActivitiesClientPusher(
Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesSession.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesSession.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesSession.java (original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/client/ActivitiesSession.java Sat Nov 12 04:25:22 2011
@@ -17,6 +17,10 @@
*/
package org.apache.abdera2.activities.client;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+
import javax.activation.MimeType;
import org.apache.abdera2.activities.model.ASBase;
@@ -29,6 +33,7 @@ import org.apache.abdera2.common.http.En
import org.apache.abdera2.common.protocol.ProtocolException;
import org.apache.abdera2.protocol.client.Client;
import org.apache.abdera2.protocol.client.ClientResponse;
+import org.apache.abdera2.protocol.client.RequestHelper;
import org.apache.abdera2.protocol.client.RequestOptions;
import org.apache.abdera2.protocol.client.Session;
import org.joda.time.DateTime;
@@ -41,11 +46,10 @@ import org.joda.time.DateTime;
public class ActivitiesSession
extends Session {
- private final IO io;
+ private static final IO io = IO.get();
protected ActivitiesSession(Client client) {
super(client);
- this.io = IO.get();
}
public IO getIO() {
@@ -56,21 +60,63 @@ public class ActivitiesSession
return (ActivitiesClient) client;
}
- public <T extends Collection<?>>ASDocument<T> getCollection(String uri) {
- return this.<T>getCollection(uri, this.getDefaultRequestOptions());
+ public <M extends ASObject,T extends Collection<M>>ASDocument<T> getCollection(String uri) {
+ return this.<M,T>getCollection(uri, this.getDefaultRequestOptions().get());
}
public <T extends ClientResponse>T post(String uri, ASBase base) {
- return this.<T>post(uri,base, this.getDefaultRequestOptions());
+ return this.<T>post(uri,base, this.getDefaultRequestOptions().get());
+ }
+
+ public <T extends ClientResponse>Callable<T> postTask(
+ final String uri,
+ final ASBase base) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return post(uri,base);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> postTask(
+ final String uri,
+ final ASBase base,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return post(uri,base,options);
+ }
+ };
}
public <T extends ClientResponse>T post(String uri, ASBase base, RequestOptions options) {
ActivityEntity entity = new ActivityEntity(base);
return this.<T>post(uri, entity, options);
}
+
+ public <T extends ClientResponse>Callable<T> putTask(
+ final String uri,
+ final ASBase base) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return put(uri,base);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> putTask(
+ final String uri,
+ final ASBase base,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return put(uri,base,options);
+ }
+ };
+ }
public <T extends ClientResponse>T put(String uri, ASBase base) {
- return this.<T>put(uri,base, this.getDefaultRequestOptions());
+ return this.<T>put(uri,base, this.getDefaultRequestOptions().get());
}
public <T extends ClientResponse>T put(String uri, ASBase base, RequestOptions options) {
@@ -78,8 +124,31 @@ public class ActivitiesSession
return this.<T>put(uri, entity, options);
}
- public <T extends Collection<?>>ASDocument<T> getCollection(String uri, RequestOptions options) {
- ClientResponse cr = get(uri, options);
+ public <M extends ASObject,T extends Collection<M>>Callable<ASDocument<T>> getCollectionTask(final String uri) {
+ final ActivitiesSession session = this;
+ return new Callable<ASDocument<T>>() {
+ public ASDocument<T> call() throws Exception {
+ return session.<M,T>getCollection(uri);
+ }
+ };
+ }
+
+ public <M extends ASObject, T extends Collection<M>>Callable<ASDocument<T>> getCollectionTask(
+ final String uri,
+ final RequestOptions options) {
+ final ActivitiesSession session = this;
+ return new Callable<ASDocument<T>>() {
+ public ASDocument<T> call() throws Exception {
+ return session.<M,T>getCollection(uri,options);
+ }
+ };
+ }
+
+ public <M extends ASObject, T extends Collection<M>>ASDocument<T> getCollection(String uri, RequestOptions options) {
+ return ActivitiesSession.<M,T>getCollectionFromResp(get(uri, options));
+ }
+
+ static <M extends ASObject, T extends Collection<M>>ASDocument<T> getCollectionFromResp(ClientResponse cr) {
try {
if (cr != null) {
switch(cr.getType()) {
@@ -101,12 +170,33 @@ public class ActivitiesSession
}
}
+ public <T extends Activity>Callable<ASDocument<T>> getActivityTask(final String uri) {
+ return new Callable<ASDocument<T>>() {
+ public ASDocument<T> call() throws Exception {
+ return getActivity(uri);
+ }
+ };
+ }
+
+ public <T extends Activity>Callable<ASDocument<T>> getActivityTask(
+ final String uri,
+ final RequestOptions options) {
+ return new Callable<ASDocument<T>>() {
+ public ASDocument<T> call() throws Exception {
+ return getActivity(uri,options);
+ }
+ };
+ }
+
public <T extends Activity>ASDocument<T> getActivity(String uri) {
- return this.<T>getActivity(uri,this.getDefaultRequestOptions());
+ return this.<T>getActivity(uri,this.getDefaultRequestOptions().get());
}
public <T extends Activity>ASDocument<T> getActivity(String uri, RequestOptions options) {
- ClientResponse cr = get(uri, options);
+ return getActivityFromResponse(get(uri, options));
+ }
+
+ static <T extends Activity>ASDocument<T> getActivityFromResponse(ClientResponse cr) {
try {
if (cr != null) {
switch(cr.getType()) {
@@ -128,12 +218,33 @@ public class ActivitiesSession
}
}
+ public <T extends ASObject>Callable<ASDocument<T>> getObjectTask(final String uri) {
+ return new Callable<ASDocument<T>>() {
+ public ASDocument<T> call() throws Exception {
+ return getObject(uri);
+ }
+ };
+ }
+
+ public <T extends ASObject>Callable<ASDocument<T>> getObjectTask(
+ final String uri,
+ final RequestOptions options) {
+ return new Callable<ASDocument<T>>() {
+ public ASDocument<T> call() throws Exception {
+ return getObject(uri,options);
+ }
+ };
+ }
+
public <T extends ASObject>ASDocument<T> getObject(String uri) {
- return this.<T>getObject(uri, this.getDefaultRequestOptions());
+ return this.<T>getObject(uri, this.getDefaultRequestOptions().get());
}
public <T extends ASObject>ASDocument<T> getObject(String uri, RequestOptions options) {
- ClientResponse cr = get(uri, options);
+ return getObjectFromResponse(get(uri, options));
+ }
+
+ static <T extends ASObject>ASDocument<T> getObjectFromResponse(ClientResponse cr) {
try {
if (cr != null) {
switch(cr.getType()) {
@@ -155,7 +266,7 @@ public class ActivitiesSession
}
}
- private <T extends ASBase>ASDocument<T> getDoc(ClientResponse resp, T base) {
+ private static <T extends ASBase>ASDocument<T> getDoc(ClientResponse resp, T base) {
ASDocument.Builder<T> builder =
ASDocument.make(base);
EntityTag etag = resp.getEntityTag();
@@ -175,4 +286,110 @@ public class ActivitiesSession
builder.slug(slug);
return builder.get();
}
+
+ public RequestOptions.Builder getDefaultRequestOptions() {
+ return RequestHelper.createActivitiesDefaultRequestOptions();
+ }
+
+ public <T extends ClientResponse>void post(
+ String uri,
+ ASBase base,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>postTask(uri,base),listener);
+ }
+
+ public <T extends ClientResponse>void post(
+ String uri,
+ ASBase base,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>postTask(uri,base,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> post(
+ String uri,
+ ASBase base,
+ ExecutorService exec) {
+ return process(exec,this.<T>postTask(uri,base));
+ }
+
+ public <T extends ClientResponse>Future<T> post(
+ String uri,
+ ASBase base,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>postTask(uri,base,options));
+ }
+
+ public <T extends ClientResponse>void put(
+ String uri,
+ ASBase base,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>putTask(uri,base),listener);
+ }
+
+ public <T extends ClientResponse>void put(
+ String uri,
+ ASBase base,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>putTask(uri,base,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> put(
+ String uri,
+ ASBase base,
+ ExecutorService exec) {
+ return process(exec,this.<T>putTask(uri,base));
+ }
+
+ public <T extends ClientResponse>Future<T> put(
+ String uri,
+ ASBase base,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>putTask(uri,base,options));
+ }
+
+ static abstract class ASListener<T extends ClientResponse, X extends ASBase>
+ implements Listener<T> {
+ protected abstract void onResponse(ASDocument<X> doc);
+ }
+
+ public static abstract class CollectionListener<T extends ClientResponse,X extends ASObject>
+ extends ASListener<T,Collection<X>> {
+ public void onResponse(T resp) {
+ onResponse(ActivitiesSession.<X,Collection<X>>getCollectionFromResp(resp));
+ }
+ }
+
+ public static abstract class SimpleActivityCollectionListener
+ extends CollectionListener<ClientResponse,Activity>{}
+
+ public static abstract class SimpleObjectCollectionListener
+ extends CollectionListener<ClientResponse,ASObject>{}
+
+ public static abstract class SimpleActivityListener
+ extends ActivityListener<ClientResponse,Activity>{}
+
+ public static abstract class SimpleObjectListener
+ extends ObjectListener<ClientResponse,ASObject>{}
+
+ public static abstract class ActivityListener<T extends ClientResponse,X extends Activity>
+ extends ASListener<T,X> {
+ public void onResponse(T resp) {
+ onResponse(ActivitiesSession.<X>getActivityFromResponse(resp));
+ }
+ }
+
+ public static abstract class ObjectListener<T extends ClientResponse,X extends ASObject>
+ extends ASListener<T,X> {
+ public void onResponse(T resp) {
+ onResponse(ActivitiesSession.<X>getObjectFromResponse(resp));
+ }
+ }
}
Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/OAuth2Helper.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/OAuth2Helper.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/OAuth2Helper.java (original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/OAuth2Helper.java Sat Nov 12 04:25:22 2011
@@ -138,8 +138,8 @@ public final class OAuth2Helper {
public static ASBase getToken(Session session, String uri, String payload) throws IOException {
ASBase token = null;
RequestOptions options =
- session.getDefaultRequestOptions();
- options.setContentType("application/x-www-form-urlencoded");
+ session.getDefaultRequestOptions()
+ .contentType("application/x-www-form-urlencoded").get();
ClientResponse resp =
session.post(
uri,
Modified: abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseImpl.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseImpl.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseImpl.java (original)
+++ abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseImpl.java Sat Nov 12 04:25:22 2011
@@ -299,17 +299,6 @@ class ClientResponseImpl
}
return links;
}
-
- public Iterable<Preference> getPreferApplied() {
- List<Preference> links = new ArrayList<Preference>();
- Iterable<Object> headers = this.getHeaders("Preference-Applied");
- for (Object obj : headers) {
- Iterable<Preference> list = Preference.parse(obj.toString());
- for (Preference link : list)
- links.add(link);
- }
- return links;
- }
public <T> T getHeader(String name, Function<String, T> transform) {
checkNotNull(transform);
Modified: abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseWrapper.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseWrapper.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseWrapper.java (original)
+++ abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/ClientResponseWrapper.java Sat Nov 12 04:25:22 2011
@@ -141,10 +141,6 @@ public class ClientResponseWrapper
return internal.getPrefer();
}
- public Iterable<Preference> getPreferApplied() {
- return internal.getPreferApplied();
- }
-
public Method getMethod() {
return internal.getMethod();
}
Modified: abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestHelper.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestHelper.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestHelper.java (original)
+++ abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestHelper.java Sat Nov 12 04:25:22 2011
@@ -47,10 +47,21 @@ import org.apache.http.client.params.Cli
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
+import com.google.common.collect.ImmutableSet;
+
public final class RequestHelper {
private RequestHelper() {}
-
+
+ // the set of methods we never use post-override for
+ private static final ImmutableSet<Method> nopostoveride =
+ ImmutableSet.of(
+ Method.GET,
+ Method.POST,
+ Method.OPTIONS,
+ Method.HEAD,
+ Method.TRACE);
+
public static HttpUriRequest createRequest(
String method,
String uri,
@@ -59,22 +70,18 @@ public final class RequestHelper {
if (method == null)
return null;
if (options == null)
- options = createDefaultRequestOptions();
+ options = createAtomDefaultRequestOptions().get();
Method m = Method.get(method);
Method actual = null;
HttpUriRequest httpMethod = null;
- if (options.isUsePostOverride()) {
- if (m.equals(Method.PUT)) {
- actual = m;
- } else if (m.equals(Method.DELETE)) {
- actual = m;
- }
- if (actual != null)
- m = Method.POST;
+ if (options.isUsePostOverride() &&
+ !nopostoveride.contains(m)) {
+ actual = m;
+ m = Method.POST;
}
- if (m == GET) {
+ if (m == GET)
httpMethod = new HttpGet(uri);
- } else if (m == POST) {
+ else if (m == POST) {
httpMethod = new HttpPost(uri);
if (entity != null)
((HttpPost)httpMethod).setEntity(entity);
@@ -82,19 +89,18 @@ public final class RequestHelper {
httpMethod = new HttpPut(uri);
if (entity != null)
((HttpPut)httpMethod).setEntity(entity);
- } else if (m == DELETE) {
+ } else if (m == DELETE)
httpMethod = new HttpDelete(uri);
- } else if (m == HEAD) {
+ else if (m == HEAD)
httpMethod = new HttpHead(uri);
- } else if (m == OPTIONS) {
+ else if (m == OPTIONS)
httpMethod = new HttpOptions(uri);
- } else if (m == TRACE) {
+ else if (m == TRACE)
httpMethod = new HttpTrace(uri);
- } else if (m == PATCH) {
+ else if (m == PATCH)
httpMethod = new ExtensionRequest(m.name(),uri,entity);
- } else {
+ else
httpMethod = new ExtensionRequest(m.name(),uri,entity);
- }
if (actual != null) {
httpMethod.addHeader("X-HTTP-Method-Override", actual.name());
}
@@ -103,10 +109,11 @@ public final class RequestHelper {
HttpParams params = httpMethod.getParams();
if (!options.isUseExpectContinue())
params.setBooleanParameter(
- CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
+ CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
if (!(httpMethod instanceof HttpEntityEnclosingRequest))
params.setBooleanParameter(
- ClientPNames.HANDLE_REDIRECTS, options.isFollowRedirects());
+ ClientPNames.HANDLE_REDIRECTS,
+ options.isFollowRedirects());
return httpMethod;
}
@@ -114,9 +121,8 @@ public final class RequestHelper {
Iterable<String> headers = options.getHeaderNames();
for (String header : headers) {
Iterable<Object> values = options.getHeaders(header);
- for (Object value : values) {
+ for (Object value : values)
request.addHeader(header, value.toString());
- }
}
CacheControl cc = options.getCacheControl();
if (cc != null) {
@@ -148,18 +154,29 @@ public final class RequestHelper {
}
- public static RequestOptions createDefaultRequestOptions() {
- RequestOptions options = new RequestOptions();
- options.setAcceptEncoding("gzip", "deflate");
- options.setAccept("application/atom+xml;type=entry",
- "application/atom+xml;type=feed",
- "application/atom+xml",
- "application/atomsvc+xml",
- "application/atomcat+xml",
- "application/xml;q=0.5",
- "text/xml;q=0.5",
- "*/*;q=0.01");
- options.setAcceptCharset("utf-8", "*;q=0.5");
- return options;
+ public static RequestOptions.Builder createAtomDefaultRequestOptions() {
+ return RequestOptions.make()
+ .acceptEncoding("gzip", "deflate")
+ .accept(
+ "application/atom+xml;type=entry",
+ "application/atom+xml;type=feed",
+ "application/atom+xml",
+ "application/atomsvc+xml",
+ "application/atomcat+xml",
+ "application/xml;q=0.5",
+ "text/xml;q=0.5",
+ "*/*;q=0.01")
+ .acceptCharset("utf-8", "*;q=0.5");
+ }
+
+ public static RequestOptions.Builder createActivitiesDefaultRequestOptions() {
+ return RequestOptions.make()
+ .acceptEncoding("gzip", "deflate")
+ .accept(
+ "application/json",
+ "*/*;q=0.01")
+ .acceptCharset(
+ "utf-8",
+ "*;q=0.5");
}
}
Modified: abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestOptions.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestOptions.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestOptions.java (original)
+++ abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/RequestOptions.java Sat Nov 12 04:25:22 2011
@@ -17,21 +17,26 @@
*/
package org.apache.abdera2.protocol.client;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
import javax.activation.MimeType;
import org.apache.abdera2.common.Localizer;
import org.apache.abdera2.common.lang.Lang;
+import org.apache.abdera2.common.misc.ExceptionHelper;
+import org.apache.abdera2.common.misc.MoreFunctions;
import org.apache.abdera2.common.protocol.AbstractRequest;
import org.apache.abdera2.common.protocol.Request;
+import org.apache.abdera2.common.selector.AbstractSelector;
+import org.apache.abdera2.common.selector.Selector;
import org.apache.abdera2.common.text.Codec;
import org.apache.abdera2.common.text.UrlEncoding;
import org.apache.abdera2.common.text.CharUtils.Profile;
+import org.apache.abdera2.common.date.DateTimes;
+import org.apache.abdera2.common.http.Authentication;
import org.apache.abdera2.common.http.CacheControl;
import org.apache.abdera2.common.http.EntityTag;
import org.apache.abdera2.common.http.Preference;
@@ -40,508 +45,501 @@ import org.apache.http.impl.cookie.DateP
import org.apache.http.impl.cookie.DateUtils;
import org.joda.time.DateTime;
+import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+
/**
* The RequestOptions class allows a variety of options affecting the execution of the request to be modified.
*/
public class RequestOptions extends AbstractRequest implements Request {
- private boolean noLocalCache = false;
- private boolean revalidateAuth = false;
- private boolean useChunked = false;
- private boolean usePostOverride = false;
- private boolean requestException4xx = false;
- private boolean requestException5xx = false;
- private boolean useExpectContinue = true;
- private boolean useConditional = true;
- private boolean followRedirects = true;
- private CacheControl cacheControl = null;
-
- private final Map<String, Set<String>> headers =
- new HashMap<String, Set<String>>();
-
- public RequestOptions() {
- }
-
- /**
- * Create the RequestOptions object with the specified If-Modified-Since header value
- *
- * @param ifModifiedSince
- */
- public RequestOptions(DateTime ifModifiedSince) {
- this();
- setIfModifiedSince(ifModifiedSince);
- }
-
- /**
- * Create the RequestOptions object with the specified If-None-Match header value
- *
- * @param IfNoneMatch
- */
- public RequestOptions(String ifNoneMatch) {
- this();
- setIfNoneMatch(ifNoneMatch);
- }
-
- /**
- * Create the RequestOptions object with the specified If-None-Match header value
- *
- * @param IfNoneMatch
- */
- public RequestOptions(String etag, String... ifNoneMatch) {
- this();
- setIfNoneMatch(etag, ifNoneMatch);
- }
-
- /**
- * Create the RequestOptions object with the specified If-Modified-Since and If-None-Match header values
- *
- * @param ifModifiedSince
- * @param IfNoneMatch
- */
- public RequestOptions(DateTime ifModifiedSince, String ifNoneMatch) {
- this();
- setIfModifiedSince(ifModifiedSince);
- setIfNoneMatch(ifNoneMatch);
- }
-
- /**
- * Create the RequestOptions object with the specified If-Modified-Since and If-None-Match header values
- *
- * @param ifModifiedSince
- * @param IfNoneMatch
- */
- public RequestOptions(DateTime ifModifiedSince, String etag, String... ifNoneMatch) {
- this();
- setIfModifiedSince(ifModifiedSince);
- setIfNoneMatch(etag, ifNoneMatch);
- }
-
- /**
- * Create the RequestOptions object
- *
- * @param no_cache True if the request will indicate that cached responses should not be returned
- */
- public RequestOptions(boolean no_cache) {
- this();
- setCacheControl(CacheControl.NOCACHE());
- noLocalCache = true;
- }
-
- private Map<String, Set<String>> getHeaders() {
- return headers;
+ public static Builder make() {
+ return new Builder();
+ }
+
+ public static Builder make(DateTime ifModifiedSince) {
+ return make().ifModifiedSince(ifModifiedSince);
+ }
+
+ public static Builder make(String ifNoneMatch) {
+ return make().ifNoneMatch(ifNoneMatch);
+ }
+
+ public static Builder make(String etag, String... ifNoneMatch) {
+ return make().ifNoneMatch(etag, ifNoneMatch);
+ }
+
+ public static Builder make(DateTime ifModifiedSince, String ifNoneMatch) {
+ return make()
+ .ifModifiedSince(ifModifiedSince)
+ .ifNoneMatch(ifNoneMatch);
+ }
+
+ public static Builder make(DateTime ifModifiedSince, String etag, String... ifNoneMatch) {
+ return make()
+ .ifModifiedSince(ifModifiedSince)
+ .ifNoneMatch(etag, ifNoneMatch);
+ }
+
+ public static Builder make(boolean no_cache) {
+ return make().cacheControl(CacheControl.NOCACHE());
+ }
+
+ public Builder template() {
+ return new Builder(this);
+ }
+
+ public Builder template(Selector<Map.Entry<String, Set<String>>> filter) {
+ return new Builder(this,filter);
+ }
+
+ public static Selector<Map.Entry<String, Set<String>>> withAllHeaders() {
+ return new AbstractSelector<Map.Entry<String, Set<String>>>() {
+ public boolean select(Object item) {
+ return true;
+ }
+ };
+ }
+
+ public static Selector<Map.Entry<String, Set<String>>> withNoHeaders() {
+ return new AbstractSelector<Map.Entry<String, Set<String>>>() {
+ public boolean select(Object item) {
+ return false;
+ }
+ };
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Selector<Map.Entry<String, Set<String>>> withHeaders(String... names) {
+ final ImmutableSet<String> set = ImmutableSet.copyOf(names);
+ return new AbstractSelector<Map.Entry<String, Set<String>>>() {
+ public boolean select(Object item) {
+ Map.Entry<String, Set<String>> entry =
+ (Entry<String, Set<String>>) item;
+ return set.contains(entry.getKey());
+ }
+ };
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Selector<Map.Entry<String, Set<String>>> withoutHeaders(String... names) {
+ final ImmutableSet<String> set = ImmutableSet.copyOf(names);
+ return new AbstractSelector<Map.Entry<String, Set<String>>>() {
+ public boolean select(Object item) {
+ Map.Entry<String, Set<String>> entry =
+ (Entry<String, Set<String>>) item;
+ return !set.contains(entry.getKey());
+ }
+ };
+ }
+
+
+ public static class Builder implements Supplier<RequestOptions> {
+
+ boolean revalidateAuth = false;
+ boolean useChunked = false;
+ boolean usePostOverride = false;
+ boolean requestException4xx = false;
+ boolean requestException5xx = false;
+ boolean useExpectContinue = true;
+ boolean useConditional = true;
+ boolean followRedirects = true;
+ CacheControl cacheControl = null;
+
+ final Map<String, ImmutableSet.Builder<String>> headers =
+ Maps.newHashMap();
+
+ public Builder() {}
+
+ Builder(RequestOptions template) {
+ this(template,null);
}
-
- private String combine(String... values) {
- StringBuilder v = new StringBuilder();
- for (String val : values) {
- if (v.length() > 0)
- v.append(", ");
- v.append(val);
+
+ Builder(RequestOptions template,Selector<Map.Entry<String, Set<String>>> filter) {
+ this.revalidateAuth = template.revalidateAuth;
+ this.followRedirects = template.followRedirects;
+ this.cacheControl = template.cacheControl;
+ this.useChunked = template.useChunked;
+ this.usePostOverride = template.usePostOverride;
+ this.requestException4xx = template.requestException4xx;
+ this.requestException5xx = template.requestException5xx;
+ this.useExpectContinue = template.useExpectContinue;
+ this.useConditional = template.useConditional;
+ for (Map.Entry<String, Set<String>> header : template.headers.entrySet()) {
+ if (filter == null || filter.apply(header)) {
+ ImmutableSet.Builder<String> builder = ImmutableSet.builder();
+ builder.addAll(header.getValue());
+ this.headers.put(header.getKey(),builder);
}
- return v.toString();
+ }
}
-
- /**
- * The difference between this and getNoCache is that this only disables the local cache without affecting the
- * Cache-Control header.
- */
- public boolean getUseLocalCache() {
- return !noLocalCache;
- }
-
- /**
- * True if the local client cache should be used
- */
- public RequestOptions setUseLocalCache(boolean use_cache) {
- this.noLocalCache = !use_cache;
- return this;
- }
-
- /**
- * Set the value of the HTTP Content-Type header
- */
- public RequestOptions setContentType(String value) {
- return setHeader("Content-Type", value);
- }
-
- public RequestOptions setContentLocation(String iri) {
- return setHeader("Content-Location", iri);
+
+ public Builder revalidateAuth() {
+ this.revalidateAuth = true;
+ return this;
}
-
- /**
- * Set the value of the HTTP Content-Type header
- */
- public RequestOptions setContentType(MimeType value) {
- return setHeader("Content-Type", value.toString());
+
+ public Builder useChunked() {
+ this.useChunked = true;
+ return this;
}
-
- /**
- * Set the value of the HTTP Authorization header
- */
- public RequestOptions setAuthorization(String auth) {
- return setHeader("Authorization", auth);
+
+ public Builder usePostOverride() {
+ this.usePostOverride = true;
+ return this;
}
-
- /**
- * Set the value of a header using proper encoding of non-ascii characters
- */
- public RequestOptions setEncodedHeader(String header, String charset, String value) {
- return setHeader(header, Codec.encode(value, charset));
+
+ public Builder requestException4xx() {
+ this.requestException4xx = true;
+ return this;
}
-
- /**
- * Set the values of a header using proper encoding of non-ascii characters
- */
- public RequestOptions setEncodedHeader(String header, String charset, String... values) {
- if (values != null && values.length > 0) {
- Set<String> vals = new HashSet<String>();
- for (String value : values)
- vals.add(Codec.encode(value,charset));
- getHeaders().put(header, vals);
- } else {
- removeHeaders(header);
- }
- return this;
+
+ public Builder requestException5xx() {
+ this.requestException5xx = true;
+ return this;
}
-
- /**
- * Set the value of the specified HTTP header
- */
- public RequestOptions setHeader(String header, String value) {
- return value != null ? setHeader(header, new String[] {value}) : removeHeaders(header);
+
+ public Builder doNotUseExpectContinue() {
+ this.useExpectContinue = false;
+ return this;
}
-
- /**
- * Set the value of the specified HTTP header
- */
- public RequestOptions setHeader(String header, String... values) {
- if (values != null && values.length > 0) {
- Set<String> vals = new HashSet<String>();
- vals.add(combine(values));
- getHeaders().put(header, vals);
- } else {
- removeHeaders(header);
- }
- return this;
+
+ public Builder doNotFollowRedirects() {
+ this.followRedirects = false;
+ return this;
}
-
- /**
- * Set the date value of the specified HTTP header
- */
- public RequestOptions setDateHeader(String header, DateTime value) {
- return value != null ? setHeader(
- header,
- DateUtils.formatDate(value.toDate())) :
- removeHeaders(header);
+
+ public Builder contentType(String value) {
+ return header("Content-Type", value);
}
- /**
- * Similar to setEncodedHeader, but allows for multiple instances of the specified header
- */
- public RequestOptions addEncodedHeader(String header, String charset, String value) {
- return addHeader(header, Codec.encode(value, charset));
+ public Builder contentType(MimeType value) {
+ return header("Content-Type", value.toString());
}
-
- /**
- * Similar to setEncodedHeader, but allows for multiple instances of the specified header
- */
- public RequestOptions addEncodedHeader(String header, String charset, String... values) {
- if (values == null || values.length == 0)
- return this;
- for (int n = 0; n < values.length; n++) {
- values[n] = Codec.encode(values[n], charset);
- }
- Set<String> list = getHeaders().get(header);
- String value = combine(values);
- if (list != null)
- list.add(value);
- else
- setHeader(header, new String[] {value});
- return this;
+
+ public Builder contentLocation(String iri) {
+ return header("Content-Location", iri);
}
- /**
- * Similar to setHeader but allows for multiple instances of the specified header
- */
- public RequestOptions addHeader(String header, String value) {
- if (value != null)
- addHeader(header, new String[] {value});
- return this;
+ public Builder setAuthorization(String auth) {
+ return header("Authorization", auth);
}
- /**
- * Similar to setHeader but allows for multiple instances of the specified header
- */
- public RequestOptions addHeader(String header, String... values) {
- if (values == null || values.length == 0)
- return this;
- Set<String> headers = getHeaders().get(header);
- String value = combine(values);
- if (headers != null)
- headers.add(value);
- else
- setHeader(header, new String[] {value});
- return this;
+ public Builder setAuthorization(Authentication auth) {
+ return header("Authorization",auth.toString());
}
-
- /**
- * Similar to setDateHeader but allows for multiple instances of the specified header
- */
- public RequestOptions addDateHeader(String header, DateTime value) {
- if (value == null)
- return this;
- return addHeader(header, DateUtils.formatDate(value.toDate()));
+
+ public Builder encodedHeader(String header, String charset, String value) {
+ return header(header, Codec.encode(value, charset));
}
- /**
- * Returns the text value of the specified header
- */
- public String getHeader(String header) {
- Set<String> list = getHeaders().get(header);
- return list.size() > 0 ? list.iterator().next() : null;
+ public Builder encodedHeader(String header, String charset, String... values) {
+ if (values != null && values.length > 0) {
+ ImmutableSet.Builder<String> vals = headers.get(header);
+ if (vals == null) {
+ vals = ImmutableSet.builder();
+ headers.put(header, vals);
+ }
+ for (String value : values)
+ vals.add(Codec.encode(value,charset));
+ }
+ return this;
}
- /**
- * Return a listing of text values for the specified header
- */
- public Iterable<Object> getHeaders(String header) {
- Set<String> headers = getHeaders().get(header);
- return new HashSet<Object>(headers);
+ public Builder header(String header, String value) {
+ if (value != null)
+ header(header, MoreFunctions.array(value));
+ return this;
}
- /**
- * Returns the date value of the specified header
- */
- public DateTime getDateHeader(String header) {
- String val = getHeader(header);
- try {
- return (val != null) ? new DateTime(DateUtils.parseDate(val)) : null;
- } catch (DateParseException e) {
- throw new RuntimeException(e);
+ public Builder header(String header, String... values) {
+ if (values != null && values.length > 0) {
+ ImmutableSet.Builder<String> vals = headers.get(header);
+ if (vals == null) {
+ vals = ImmutableSet.builder();
+ headers.put(header, vals);
}
+ vals.add(combine(values));
+ }
+ return this;
}
- /**
- * Returns a listing of header names
- */
- public Iterable<String> getHeaderNames() {
- return getHeaders().keySet();
+ public Builder dateHeader(
+ String header,
+ DateTime value) {
+ if (value != null)
+ header(header, DateUtils.formatDate(value.toDate()));
+ return this;
}
- /**
- * Sets the value of the HTTP If-Match header
- */
- public RequestOptions setIfMatch(String entity_tag) {
- return setIfMatch(new EntityTag(entity_tag));
+ private String combine(String... values) {
+ StringBuilder v = new StringBuilder();
+ for (String val : values) {
+ if (v.length() > 0)
+ v.append(", ");
+ v.append(val);
+ }
+ return v.toString();
}
- /**
- * Sets the value of the HTTP If-Match header
- */
- public RequestOptions setIfMatch(EntityTag entity_tag) {
- return setHeader("If-Match", entity_tag.toString());
+ public Builder ifMatch(EntityTag entity_tag) {
+ return header("If-Match", entity_tag.toString());
}
/**
* Sets the value of the HTTP If-Match header
*/
- public RequestOptions setIfMatch(EntityTag tag, EntityTag... entity_tags) {
- return setHeader("If-Match", EntityTag.toString(tag,entity_tags));
+ public Builder ifMatch(EntityTag tag, EntityTag... entity_tags) {
+ return header("If-Match", EntityTag.toString(tag,entity_tags));
}
/**
* Sets the value of the HTTP If-Match header
*/
- public RequestOptions setIfMatch(String etag, String... entity_tags) {
- return setHeader("If-Match", EntityTag.toString(etag, entity_tags));
+ public Builder ifMatch(String etag, String... entity_tags) {
+ return header("If-Match", EntityTag.toString(etag, entity_tags));
}
/**
* Sets the value of the HTTP If-None-Match header
*/
- public RequestOptions setIfNoneMatch(String entity_tag) {
- return setIfNoneMatch(new EntityTag(entity_tag));
+ public Builder ifNoneMatch(String entity_tag) {
+ return ifNoneMatch(new EntityTag(entity_tag));
}
/**
* Sets the value of the HTTP If-None-Match header
*/
- public RequestOptions setIfNoneMatch(EntityTag entity_tag) {
- return setHeader("If-None-Match", entity_tag.toString());
+ public Builder ifNoneMatch(EntityTag entity_tag) {
+ return header("If-None-Match", entity_tag.toString());
}
/**
* Sets the value of the HTTP If-None-Match header
*/
- public RequestOptions setIfNoneMatch(EntityTag etag, EntityTag... entity_tags) {
- return setHeader("If-None-Match", EntityTag.toString(etag, entity_tags));
+ public Builder ifNoneMatch(EntityTag etag, EntityTag... entity_tags) {
+ return header("If-None-Match", EntityTag.toString(etag, entity_tags));
}
/**
* Sets the value of the HTTP If-None-Match header
*/
- public RequestOptions setIfNoneMatch(String etag, String... entity_tags) {
- return setHeader("If-None-Match", EntityTag.toString(etag, entity_tags));
+ public Builder ifNoneMatch(String etag, String... entity_tags) {
+ return header("If-None-Match", EntityTag.toString(etag, entity_tags));
}
/**
* Sets the value of the HTTP If-Modified-Since header
*/
- public RequestOptions setIfModifiedSince(DateTime date) {
- return setDateHeader("If-Modified-Since", date);
+ public Builder ifModifiedSince(DateTime date) {
+ return dateHeader("If-Modified-Since", date);
+ }
+
+ public Builder ifModifiedSinceNow() {
+ return ifModifiedSince(DateTimes.now());
}
/**
* Sets the value of the HTTP If-Unmodified-Since header
*/
- public RequestOptions setIfUnmodifiedSince(DateTime date) {
- return setDateHeader("If-Unmodified-Since", date);
+ public Builder ifUnmodifiedSince(DateTime date) {
+ return dateHeader("If-Unmodified-Since", date);
}
/**
* Sets the value of the HTTP Accept header
*/
- public RequestOptions setAccept(String accept) {
- return setAccept(new String[] {accept});
+ public Builder accept(String accept) {
+ return accept(new String[] {accept});
}
/**
* Sets the value of the HTTP Accept header
*/
- public RequestOptions setAccept(String... accept) {
- return setHeader("Accept", combine(accept));
+ public Builder accept(String... accept) {
+ return header("Accept", combine(accept));
}
- public RequestOptions setAcceptLanguage(Locale locale) {
- return setAcceptLanguage(Lang.fromLocale(locale));
+ public Builder acceptLanguage(Locale locale) {
+ return acceptLanguage(Lang.fromLocale(locale));
}
- public RequestOptions setAcceptLanguage(Locale... locales) {
- String[] langs = new String[locales.length];
- for (int n = 0; n < locales.length; n++)
- langs[n] = Lang.fromLocale(locales[n]);
- setAcceptLanguage(langs);
- return this;
+ public Builder acceptLanguage(Locale... locales) {
+ String[] langs = new String[locales.length];
+ for (int n = 0; n < locales.length; n++)
+ langs[n] = Lang.fromLocale(locales[n]);
+ acceptLanguage(langs);
+ return this;
}
/**
* Sets the value of the HTTP Accept-Language header
*/
- public RequestOptions setAcceptLanguage(String accept) {
- return setAcceptLanguage(new String[] {accept});
+ public Builder acceptLanguage(String accept) {
+ return acceptLanguage(new String[] {accept});
}
/**
* Sets the value of the HTTP Accept-Language header
*/
- public RequestOptions setAcceptLanguage(String... accept) {
- return setHeader("Accept-Language", combine(accept));
+ public Builder acceptLanguage(String... accept) {
+ return header("Accept-Language", combine(accept));
}
/**
* Sets the value of the HTTP Accept-Charset header
*/
- public RequestOptions setAcceptCharset(String accept) {
- return setAcceptCharset(new String[] {accept});
+ public Builder acceptCharset(String accept) {
+ return acceptCharset(new String[] {accept});
}
/**
* Sets the value of the HTTP Accept-Charset header
*/
- public RequestOptions setAcceptCharset(String... accept) {
- return setHeader("Accept-Charset", combine(accept));
+ public Builder acceptCharset(String... accept) {
+ return header("Accept-Charset", combine(accept));
}
/**
* Sets the value of the HTTP Accept-Encoding header
*/
- public RequestOptions setAcceptEncoding(String accept) {
- return setAcceptEncoding(new String[] {accept});
+ public Builder acceptEncoding(String accept) {
+ return acceptEncoding(new String[] {accept});
}
/**
* Sets the value of the HTTP Accept-Encoding header
*/
- public RequestOptions setAcceptEncoding(String... accept) {
- return setHeader("Accept-Encoding", combine(accept));
+ public Builder acceptEncoding(String... accept) {
+ return header("Accept-Encoding", combine(accept));
}
/**
* Sets the value of the Atom Publishing Protocol Slug header
*/
- public RequestOptions setSlug(String slug) {
+ public Builder slug(String slug) {
if (slug.indexOf((char)10) > -1 || slug.indexOf((char)13) > -1)
throw new IllegalArgumentException(Localizer.get("SLUG.BAD.CHARACTERS"));
- return setHeader("Slug", UrlEncoding.encode(slug, Profile.PATHNODELIMS));
+ return header("Slug", UrlEncoding.encode(slug, Profile.PATHNODELIMS));
}
- /**
- * Sets the value of the HTTP Cache-Control header
- */
- public RequestOptions setCacheControl(String cc) {
- this.cacheControl = CacheControl.parse(cc);
- return this;
+ public Builder cacheControl(String cc) {
+ this.cacheControl = CacheControl.parse(cc);
+ return this;
}
-
- /**
- * Sets the value of the HTTP Cache-Control header
- */
- public RequestOptions setCacheControl(CacheControl cc) {
+
+ public Builder cacheControl(CacheControl cc) {
this.cacheControl = cc;
return this;
}
+
+ public Builder ifMatch(String entity_tag) {
+ return ifMatch(new EntityTag(entity_tag));
+ }
+
+ public Builder webLinks(WebLink weblink, WebLink... links) {
+ header("Link", WebLink.toString(weblink,links));
+ return this;
+ }
+
+ public Builder prefer(Preference pref, Preference... prefs) {
+ header("Prefer", Preference.toString(pref, prefs));
+ return this;
+ }
+
+ public RequestOptions get() {
+ ImmutableMap.Builder<String,Set<String>> actuals =
+ ImmutableMap.builder();
+ for (Map.Entry<String, ImmutableSet.Builder<String>> header : headers.entrySet())
+ actuals.put(header.getKey(), header.getValue().build());
+ return new RequestOptions(this,actuals.build());
+ }
+
+ }
+
+ final boolean revalidateAuth;
+ final boolean useChunked;
+ final boolean usePostOverride;
+ final boolean requestException4xx;
+ final boolean requestException5xx;
+ final boolean useExpectContinue;
+ final boolean useConditional;
+ final boolean followRedirects;
+ final CacheControl cacheControl;
+ final ImmutableMap<String, Set<String>> headers;
+
+ RequestOptions(Builder builder,ImmutableMap<String,Set<String>> headers) {
+ this.revalidateAuth = builder.revalidateAuth;
+ this.useChunked = builder.useChunked;
+ this.usePostOverride = builder.usePostOverride;
+ this.requestException4xx = builder.requestException4xx;
+ this.requestException5xx = builder.requestException5xx;
+ this.useConditional = builder.useConditional;
+ this.useExpectContinue = builder.useExpectContinue;
+ this.followRedirects = builder.followRedirects;
+ this.cacheControl = builder.cacheControl;
+ this.headers = headers;
+ }
+
+ private Map<String, Set<String>> getHeaders() {
+ return headers;
+ }
/**
- * Remove the specified HTTP header
+ * Returns the text value of the specified header
*/
- public RequestOptions removeHeaders(String name) {
- getHeaders().remove(name);
- return this;
+ public String getHeader(String header) {
+ Set<String> list = getHeaders().get(header);
+ return list.size() > 0 ? list.iterator().next() : null;
}
/**
- * Return the value of the Cache-Control header
+ * Return a listing of text values for the specified header
*/
- public CacheControl getCacheControl() {
- return cacheControl;
+ public Iterable<Object> getHeaders(String header) {
+ return ImmutableSet.<Object>copyOf(getHeaders().get(header));
}
/**
- * Configure the AbderaClient Side cache to revalidate when using Authorization
+ * Returns the date value of the specified header
*/
- public boolean getRevalidateWithAuth() {
- return revalidateAuth;
+ public DateTime getDateHeader(String header) {
+ String val = getHeader(header);
+ try {
+ return (val != null) ? new DateTime(DateUtils.parseDate(val)) : null;
+ } catch (DateParseException e) {
+ throw ExceptionHelper.propogate(e);
+ }
}
/**
- * Configure the AbderaClient Side cache to revalidate when using Authorization
+ * Returns a listing of header names
*/
- public RequestOptions setRevalidateWithAuth(boolean revalidateAuth) {
- this.revalidateAuth = revalidateAuth;
- return this;
+ public Iterable<String> getHeaderNames() {
+ return getHeaders().keySet();
}
/**
- * Should the request use chunked encoding?
+ * Return the value of the Cache-Control header
*/
- public boolean isUseChunked() {
- return useChunked;
+ public CacheControl getCacheControl() {
+ return cacheControl;
}
/**
- * Set whether the request should use chunked encoding.
+ * Configure the AbderaClient Side cache to revalidate when using Authorization
*/
- public RequestOptions setUseChunked(boolean useChunked) {
- this.useChunked = useChunked;
- return this;
+ public boolean getRevalidateWithAuth() {
+ return revalidateAuth;
}
/**
- * Set whether the request should use the X-HTTP-Method-Override option
+ * Should the request use chunked encoding?
*/
- public RequestOptions setUsePostOverride(boolean useOverride) {
- this.usePostOverride = useOverride;
- return this;
+ public boolean isUseChunked() {
+ return useChunked;
}
/**
@@ -552,14 +550,6 @@ public class RequestOptions extends Abst
}
/**
- * Set whether or not to throw a RequestExeption on 4xx responses
- */
- public RequestOptions set4xxRequestException(boolean v) {
- this.requestException4xx = v;
- return this;
- }
-
- /**
* Return true if a RequestException should be thrown on 4xx responses
*/
public boolean is4xxRequestException() {
@@ -567,14 +557,6 @@ public class RequestOptions extends Abst
}
/**
- * Set whether or not to throw a RequestExeption on 5xx responses
- */
- public RequestOptions set5xxRequestException(boolean v) {
- this.requestException5xx = v;
- return this;
- }
-
- /**
* Return true if a RequestException should be thrown on 5xx responses
*/
public boolean is5xxRequestException() {
@@ -582,14 +564,6 @@ public class RequestOptions extends Abst
}
/**
- * Set whether or not to use the HTTP Expect-Continue mechanism (enabled by default)
- */
- public RequestOptions setUseExpectContinue(boolean useExpect) {
- this.useExpectContinue = useExpect;
- return this;
- }
-
- /**
* Return true if Expect-Continue should be used
*/
public boolean isUseExpectContinue() {
@@ -605,41 +579,13 @@ public class RequestOptions extends Abst
}
/**
- * True if HTTP Conditinal Request should be used automatically. This only has an effect when putting a Document
- * that has an ETag or Last-Modified date present
- */
- public RequestOptions setConditionalPut(boolean conditional) {
- this.useConditional = conditional;
- return this;
- }
-
- /**
* True if the client should follow redirects automatically
*/
public boolean isFollowRedirects() {
return followRedirects;
}
-
- /**
- * True if the client should follow redirects automatically
- */
- public RequestOptions setFollowRedirects(boolean followredirects) {
- this.followRedirects = followredirects;
- return this;
- }
- public RequestOptions setWebLinks(WebLink weblink, WebLink... links) {
- setHeader("Link", WebLink.toString(weblink,links));
- return this;
- }
-
- public RequestOptions setPrefer(Preference pref, Preference... prefs) {
- setHeader("Prefer", Preference.toString(pref, prefs));
- return this;
- }
-
- public RequestOptions setPreferApplied(Preference pref, Preference... prefs) {
- setHeader("Preference-Applied", Preference.toString(pref, prefs));
- return this;
+ public boolean has(String header) {
+ return headers.containsKey(header);
}
}
Modified: abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/Session.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/Session.java?rev=1201185&r1=1201184&r2=1201185&view=diff
==============================================================================
--- abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/Session.java (original)
+++ abdera/abdera2/client/src/main/java/org/apache/abdera2/protocol/client/Session.java Sat Nov 12 04:25:22 2011
@@ -20,16 +20,17 @@ package org.apache.abdera2.protocol.clie
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
import org.apache.abdera2.common.http.Method;
import org.apache.abdera2.common.http.ResponseType;
+import org.apache.abdera2.common.misc.ExceptionHelper;
import org.apache.abdera2.common.protocol.ProtocolException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.client.AuthCache;
import org.apache.http.client.HttpClient;
@@ -46,6 +47,11 @@ import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
+import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
/**
* A client session. Session's MUST NOT be used by more
* than one Thread of execution as a time as multiple threads would stomp
@@ -82,6 +88,25 @@ public class Session {
public <T extends ClientResponse>T get(String uri, RequestOptions options) {
return (T)wrap(execute("GET", uri, (HttpEntity)null, options));
}
+
+ public <T extends ClientResponse>Callable<T> getTask(
+ final String uri) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return get(uri);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> getTask(
+ final String uri,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return get(uri,options);
+ }
+ };
+ }
/**
* Sends an HTTP POST request to the specified URI.
@@ -90,8 +115,32 @@ public class Session {
* @param entity A RequestEntity object providing the payload of the request
* @param options The request options
*/
- public <T extends ClientResponse>T post(String uri, HttpEntity entity, RequestOptions options) {
- return (T)wrap(execute("POST", uri, entity, options));
+ public <T extends ClientResponse>T post(
+ String uri,
+ HttpEntity entity,
+ RequestOptions options) {
+ return (T)wrap(execute("POST", uri, entity, options));
+ }
+
+ public <T extends ClientResponse>Callable<T> postTask(
+ final String uri,
+ final HttpEntity entity) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return post(uri,entity);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> postTask(
+ final String uri,
+ final HttpEntity entity,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return post(uri,entity,options);
+ }
+ };
}
/**
@@ -104,6 +153,27 @@ public class Session {
public <T extends ClientResponse>T post(String uri, InputStream in, RequestOptions options) {
return (T)wrap(execute("POST", uri, new InputStreamEntity(in,-1), options));
}
+
+ public <T extends ClientResponse>Callable<T> postTask(
+ final String uri,
+ final InputStream in) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return post(uri,in);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> postTask(
+ final String uri,
+ final InputStream in,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return post(uri,in,options);
+ }
+ };
+ }
/**
* Sends an HTTP PUT request to the specified URI.
@@ -116,6 +186,27 @@ public class Session {
return (T)wrap(execute("PUT", uri, entity, options));
}
+ public <T extends ClientResponse>Callable<T> putTask(
+ final String uri,
+ final HttpEntity entity) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return put(uri,entity);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> putTask(
+ final String uri,
+ final HttpEntity entity,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return put(uri,entity,options);
+ }
+ };
+ }
+
/**
* Sends an HTTP PUT request to the specified URI.
*
@@ -126,6 +217,27 @@ public class Session {
public <T extends ClientResponse>T put(String uri, InputStream in, RequestOptions options) {
return (T)wrap(execute("PUT", uri, new InputStreamEntity(in,-1), options));
}
+
+ public <T extends ClientResponse>Callable<T> putTask(
+ final String uri,
+ final InputStream in) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return put(uri,in);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> putTask(
+ final String uri,
+ final InputStream in,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return put(uri,in,options);
+ }
+ };
+ }
/**
* Sends an HTTP DELETE request to the specified URI.
@@ -136,6 +248,25 @@ public class Session {
public <T extends ClientResponse>T delete(String uri, RequestOptions options) {
return (T)wrap(execute("DELETE", uri, (HttpEntity)null, options));
}
+
+ public <T extends ClientResponse>Callable<T> deleteTask(
+ final String uri) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return delete(uri);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> deleteTask(
+ final String uri,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return delete(uri,options);
+ }
+ };
+ }
/**
* Sends an HTTP HEAD request to the specified URI using the default options
@@ -143,7 +274,26 @@ public class Session {
* @param uri The request URI
*/
public <T extends ClientResponse>T head(String uri) {
- return (T)wrap(head(uri, getDefaultRequestOptions()));
+ return (T)wrap(head(uri, getDefaultRequestOptions().get()));
+ }
+
+ public <T extends ClientResponse>Callable<T> headTask(
+ final String uri) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return head(uri);
+ }
+ };
+ }
+
+ public <T extends ClientResponse>Callable<T> headTask(
+ final String uri,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return head(uri,options);
+ }
+ };
}
/**
@@ -161,7 +311,7 @@ public class Session {
* @param uri The request URI
*/
public <T extends ClientResponse>T get(String uri) {
- return (T)wrap(get(uri, getDefaultRequestOptions()));
+ return (T)wrap(get(uri, getDefaultRequestOptions().get()));
}
/**
@@ -171,7 +321,7 @@ public class Session {
* @param entity A RequestEntity object providing the payload of the request
*/
public <T extends ClientResponse>T post(String uri, HttpEntity entity) {
- return (T)wrap(post(uri, entity, getDefaultRequestOptions()));
+ return (T)wrap(post(uri, entity, getDefaultRequestOptions().get()));
}
/**
@@ -181,7 +331,7 @@ public class Session {
* @param in An InputStream providing the payload of the request
*/
public <T extends ClientResponse>T post(String uri, InputStream in) {
- return (T)wrap(post(uri, in, getDefaultRequestOptions()));
+ return (T)wrap(post(uri, in, getDefaultRequestOptions().get()));
}
/**
@@ -191,7 +341,7 @@ public class Session {
* @param entity A RequestEntity object providing the payload of the request
*/
public <T extends ClientResponse>T put(String uri, HttpEntity entity) {
- return (T)wrap(put(uri, entity, getDefaultRequestOptions()));
+ return (T)wrap(put(uri, entity, getDefaultRequestOptions().get()));
}
/**
@@ -201,7 +351,7 @@ public class Session {
* @param in An InputStream providing the payload of the request
*/
public <T extends ClientResponse>T put(String uri, InputStream in) {
- return (T)wrap(put(uri, in, getDefaultRequestOptions()));
+ return (T)wrap(put(uri, in, getDefaultRequestOptions().get()));
}
/**
@@ -210,7 +360,7 @@ public class Session {
* @param uri The request URI
*/
public <T extends ClientResponse>T delete(String uri) {
- return (T)wrap(delete(uri, getDefaultRequestOptions()));
+ return (T)wrap(delete(uri, getDefaultRequestOptions().get()));
}
/**
@@ -228,7 +378,7 @@ public class Session {
InputStream in,
RequestOptions options) {
if (options == null)
- options = getDefaultRequestOptions();
+ options = getDefaultRequestOptions().get();
InputStreamEntity re =
new InputStreamEntity(in, -1);
re.setContentType(
@@ -237,6 +387,18 @@ public class Session {
method, uri, re, options));
}
+ public <T extends ClientResponse>Callable<T> executeTask(
+ final String method,
+ final String uri,
+ final InputStream in,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return execute(method,uri,in,options);
+ }
+ };
+ }
+
/**
* Sends the specified method request to the specified URI. This can be used to send extension HTTP methods to a
* server (e.g. PATCH, LOCK, etc)
@@ -253,6 +415,18 @@ public class Session {
RequestOptions options) {
return (T)wrap(execute(method.name(),uri,in,options));
}
+
+ public <T extends ClientResponse>Callable<T> executeTask(
+ final Method method,
+ final String uri,
+ final InputStream in,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return execute(method,uri,in,options);
+ }
+ };
+ }
public <T extends ClientResponse>T execute(
Method method,
@@ -262,6 +436,18 @@ public class Session {
return (T)wrap(execute(method.name(),uri,entity,options));
}
+ public <T extends ClientResponse>Callable<T> executeTask(
+ final Method method,
+ final String uri,
+ final HttpEntity entity,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return execute(method,uri,entity,options);
+ }
+ };
+ }
+
/**
* Sends the specified method request to the specified URI. This can be used to send extension HTTP methods to a
* server (e.g. PATCH, LOCK, etc)
@@ -279,17 +465,18 @@ public class Session {
options =
options != null ?
options :
- getDefaultRequestOptions();
+ getDefaultRequestOptions()
+ .get();
try {
- HttpUriRequest request =
- RequestHelper.createRequest(
- method, uri, entity, options);
- HttpResponse response =
- getClient().execute(request, localContext);
- ClientResponse resp =
- wrap(new ClientResponseImpl(
- this, response, method, localContext));
- return (T)checkRequestException(resp, options);
+ HttpUriRequest request =
+ RequestHelper.createRequest(
+ method, uri, entity, options);
+ HttpResponse response =
+ getClient().execute(request, localContext);
+ ClientResponse resp =
+ wrap(new ClientResponseImpl(
+ this, response, method, localContext));
+ return (T)checkRequestException(resp, options);
} catch (RuntimeException r) {
throw r;
} catch (Throwable t) {
@@ -297,6 +484,18 @@ public class Session {
}
}
+ public <T extends ClientResponse>Callable<T> executeTask(
+ final String method,
+ final String uri,
+ final HttpEntity entity,
+ final RequestOptions options) {
+ return new Callable<T>() {
+ public T call() throws Exception {
+ return execute(method,uri,entity,options);
+ }
+ };
+ }
+
protected <T extends ClientResponse>T checkRequestException(ClientResponse response, RequestOptions options) {
if (response == null)
return (T)response;
@@ -311,8 +510,8 @@ public class Session {
/**
* Get a copy of the default request options
*/
- public RequestOptions getDefaultRequestOptions() {
- return RequestHelper.createDefaultRequestOptions();
+ public RequestOptions.Builder getDefaultRequestOptions() {
+ return RequestHelper.createAtomDefaultRequestOptions();
}
public void usePreemptiveAuthentication(String target, String realm) throws URISyntaxException {
@@ -321,9 +520,9 @@ public class Session {
String host = AuthScope.ANY_HOST;
int port = AuthScope.ANY_PORT;
if (target != null) {
- URI uri = new URI(target);
- host = uri.getHost();
- port = uri.getPort();
+ URI uri = new URI(target);
+ host = uri.getHost();
+ port = uri.getPort();
}
BasicScheme basicAuth = new BasicScheme();
HttpHost targetHost =
@@ -334,22 +533,357 @@ public class Session {
}
}
- public void doFormLogin(String uri, String userid, String password) {
- doFormLogin(uri, "j_username", userid, "j_password", password);
+ public boolean doFormLogin(String uri, String userid, String password) {
+ return doFormLogin(uri, "j_username", userid, "j_password", password);
}
- public void doFormLogin(String uri, String userfield, String userid, String passfield, String password) {
+ public boolean doFormLogin(String uri, String userfield, String userid, String passfield, String password) {
try {
HttpPost httpost = new HttpPost(uri);
- List <NameValuePair> nvps = new ArrayList <NameValuePair>();
- nvps.add(new BasicNameValuePair(userfield, userid));
- nvps.add(new BasicNameValuePair(passfield, password));
- httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
+ httpost.setEntity(
+ new UrlEncodedFormEntity(
+ ImmutableList.of(
+ new BasicNameValuePair(userfield,userid),
+ new BasicNameValuePair(passfield,password)
+ ),
+ HTTP.UTF_8));
HttpResponse response = getClient().execute(httpost,localContext);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
+ ResponseType type = ResponseType.select(response.getStatusLine().getStatusCode());
+ return type == ResponseType.SUCCESSFUL;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
+
+ public static interface Listener<X extends ClientResponse> {
+ void onResponse(X resp);
+ }
+
+ /**
+ * Processes requests asynchronously.. will return a Future
+ * whose value will be set once the call completes
+ */
+ public <X extends ClientResponse>Future<X> process(
+ ExecutorService executor,
+ Callable<X> resp) {
+ ListeningExecutorService exec =
+ MoreExecutors.listeningDecorator(executor);
+ return exec.submit(resp);
+ }
+
+ /**
+ * Processes requests asynchronously.. the listener will
+ * be invoked once the call completes
+ */
+ public <X extends ClientResponse>void process(
+ ExecutorService executor,
+ Callable<X> resp,
+ final Listener<X> listener) {
+ ListeningExecutorService exec = MoreExecutors.listeningDecorator(executor);
+ final ListenableFuture<X> lf = exec.submit(resp);
+ lf.addListener(
+ new Runnable() {
+ public void run() {
+ try {
+ listener.onResponse(lf.get());
+ } catch (Throwable t) {
+ t.printStackTrace();
+ throw ExceptionHelper.propogate(t);
+ }
+ }
+ },
+ executor);
+ }
+
+ public <T extends ClientResponse>void get(
+ String uri,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>getTask(uri),listener);
+ }
+
+ public <T extends ClientResponse>void get(
+ String uri,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>getTask(uri,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> get(
+ String uri,
+ ExecutorService exec) {
+ return process(exec,this.<T>getTask(uri));
+ }
+
+ public <T extends ClientResponse>Future<T> get(
+ String uri,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>getTask(uri,options));
+ }
+
+ public <T extends ClientResponse>void post(
+ String uri,
+ InputStream in,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>postTask(uri,in),listener);
+ }
+
+ public <T extends ClientResponse>void post(
+ String uri,
+ InputStream in,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>postTask(uri,in,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> post(
+ String uri,
+ InputStream in,
+ ExecutorService exec) {
+ return process(exec,this.<T>postTask(uri,in));
+ }
+
+ public <T extends ClientResponse>Future<T> post(
+ String uri,
+ InputStream in,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>postTask(uri,in,options));
+ }
+
+ public <T extends ClientResponse>void post(
+ String uri,
+ HttpEntity in,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>postTask(uri,in),listener);
+ }
+
+ public <T extends ClientResponse>void post(
+ String uri,
+ HttpEntity in,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>postTask(uri,in,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> post(
+ String uri,
+ HttpEntity in,
+ ExecutorService exec) {
+ return process(exec,this.<T>postTask(uri,in));
+ }
+
+ public <T extends ClientResponse>Future<T> post(
+ String uri,
+ HttpEntity in,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>postTask(uri,in,options));
+ }
+
+
+ public <T extends ClientResponse>void put(
+ String uri,
+ InputStream in,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>putTask(uri,in),listener);
+ }
+
+ public <T extends ClientResponse>void put(
+ String uri,
+ InputStream in,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>putTask(uri,in,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> put(
+ String uri,
+ InputStream in,
+ ExecutorService exec) {
+ return process(exec,this.<T>putTask(uri,in));
+ }
+
+ public <T extends ClientResponse>Future<T> put(
+ String uri,
+ InputStream in,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>putTask(uri,in,options));
+ }
+
+ public <T extends ClientResponse>void put(
+ String uri,
+ HttpEntity in,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>putTask(uri,in),listener);
+ }
+
+ public <T extends ClientResponse>void put(
+ String uri,
+ HttpEntity in,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>putTask(uri,in,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> put(
+ String uri,
+ HttpEntity in,
+ ExecutorService exec) {
+ return process(exec,this.<T>putTask(uri,in));
+ }
+
+ public <T extends ClientResponse>Future<T> put(
+ String uri,
+ HttpEntity in,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>putTask(uri,in,options));
+ }
+
+ public <T extends ClientResponse>void delete(
+ String uri,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>deleteTask(uri),listener);
+ }
+
+ public <T extends ClientResponse>void delete(
+ String uri,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>deleteTask(uri,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> delete(
+ String uri,
+ ExecutorService exec) {
+ return process(exec,this.<T>deleteTask(uri));
+ }
+
+ public <T extends ClientResponse>Future<T> delete(
+ String uri,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>deleteTask(uri,options));
+ }
+
+ public <T extends ClientResponse>void head(
+ String uri,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>headTask(uri),listener);
+ }
+
+ public <T extends ClientResponse>void head(
+ String uri,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>headTask(uri,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> head(
+ String uri,
+ ExecutorService exec) {
+ return process(exec,this.<T>headTask(uri));
+ }
+
+ public <T extends ClientResponse>Future<T> head(
+ String uri,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>headTask(uri,options));
+ }
+
+
+ public <T extends ClientResponse>void execute(
+ String method,
+ String uri,
+ InputStream in,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>executeTask(method,uri,in,options),listener);
+ }
+
+ public <T extends ClientResponse>void execute(
+ String method,
+ String uri,
+ HttpEntity in,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>executeTask(method,uri,in,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> execute(
+ String method,
+ String uri,
+ InputStream in,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>executeTask(method,uri,in,options));
+ }
+
+ public <T extends ClientResponse>Future<T> execute(
+ String method,
+ String uri,
+ HttpEntity in,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>executeTask(method,uri,in,options));
+ }
+
+ public <T extends ClientResponse>void execute(
+ Method method,
+ String uri,
+ InputStream in,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>executeTask(method,uri,in,options),listener);
+ }
+
+ public <T extends ClientResponse>void execute(
+ Method method,
+ String uri,
+ HttpEntity in,
+ RequestOptions options,
+ ExecutorService exec,
+ Listener<T> listener) {
+ process(exec,this.<T>executeTask(method,uri,in,options),listener);
+ }
+
+ public <T extends ClientResponse>Future<T> execute(
+ Method method,
+ String uri,
+ InputStream in,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>executeTask(method,uri,in,options));
+ }
+
+ public <T extends ClientResponse>Future<T> execute(
+ Method method,
+ String uri,
+ HttpEntity in,
+ RequestOptions options,
+ ExecutorService exec) {
+ return process(exec,this.<T>executeTask(method,uri,in,options));
+ }
}
Added: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreExecutors2.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreExecutors2.java?rev=1201185&view=auto
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreExecutors2.java (added)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreExecutors2.java Sat Nov 12 04:25:22 2011
@@ -0,0 +1,54 @@
+package org.apache.abdera2.common.misc;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.util.concurrent.MoreExecutors;
+
+public class MoreExecutors2 {
+
+ public static ExecutorService getExitingExecutor() {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool();
+ return MoreExecutors.getExitingExecutorService(tpe);
+ }
+
+ public static ExecutorService getExitingExecutor(ThreadFactory factory) {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool(factory);
+ return MoreExecutors.getExitingExecutorService(tpe);
+ }
+
+ public static ExecutorService getExitingFixedExecutor(int n) {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(n);
+ return MoreExecutors.getExitingExecutorService(tpe);
+ }
+
+ public static ExecutorService getExitingFixedExecutor(int n, ThreadFactory factory) {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(n,factory);
+ return MoreExecutors.getExitingExecutorService(tpe);
+ }
+
+
+ public static ExecutorService getExitingExecutor(int timeout, TimeUnit unit) {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool();
+ return MoreExecutors.getExitingExecutorService(tpe,timeout,unit);
+ }
+
+ public static ExecutorService getExitingExecutor(ThreadFactory factory,int timeout, TimeUnit unit) {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newCachedThreadPool(factory);
+ return MoreExecutors.getExitingExecutorService(tpe,timeout,unit);
+ }
+
+ public static ExecutorService getExitingFixedExecutor(int n,int timeout, TimeUnit unit) {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(n);
+ return MoreExecutors.getExitingExecutorService(tpe,timeout,unit);
+ }
+
+ public static ExecutorService getExitingFixedExecutor(int n, ThreadFactory factory,int timeout, TimeUnit unit) {
+ ThreadPoolExecutor tpe = (ThreadPoolExecutor) Executors.newFixedThreadPool(n,factory);
+ return MoreExecutors.getExitingExecutorService(tpe,timeout,unit);
+ }
+
+}