Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #270 from saturnism/master

Add support for JDeferred library
  • Loading branch information...
commit 4c7de7acdf2fbad23a16d310ef5bc4cb2d71428b 2 parents 9908f3e + f6b341a
@jfarcand jfarcand authored
View
32 extras/jdeferred/pom.xml
@@ -0,0 +1,32 @@
+<!--
+ Copyright 2013 Ray Tsang
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>async-http-client-extras-parent</artifactId>
+ <groupId>com.ning</groupId>
+ <version>1.8.0-SNAPSHOT</version>
+ <relativePath>..</relativePath>
+ </parent>
+ <artifactId>async-http-client-extras-jdeferred</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>org.jdeferred</groupId>
+ <artifactId>jdeferred-core</artifactId>
+ <version>1.0.0</version>
+ </dependency>
+ </dependencies>
+</project>
View
61 extras/jdeferred/src/main/java/com/ning/http/client/extra/AsyncHttpDeferredObject.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2013 Ray Tsang
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.ning.http.client.extra;
+
+import java.io.IOException;
+
+import org.jdeferred.Promise;
+import org.jdeferred.impl.DeferredObject;
+
+import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
+import com.ning.http.client.AsyncCompletionHandler;
+import com.ning.http.client.HttpResponseBodyPart;
+import com.ning.http.client.Response;
+
+public class AsyncHttpDeferredObject extends DeferredObject<Response, Throwable, HttpProgress> {
+ public AsyncHttpDeferredObject(BoundRequestBuilder builder) throws IOException {
+ builder.execute(new AsyncCompletionHandler<Void>() {
+ @Override
+ public Void onCompleted(Response response) throws Exception {
+ AsyncHttpDeferredObject.this.resolve(response);
+ return null;
+ }
+
+ @Override
+ public void onThrowable(Throwable t) {
+ AsyncHttpDeferredObject.this.reject(t);
+ }
+
+ @Override
+ public com.ning.http.client.AsyncHandler.STATE onContentWriteProgress(
+ long amount, long current, long total) {
+ AsyncHttpDeferredObject.this.notify(new ContentWriteProgress(amount, current, total));
+ return super.onContentWriteProgress(amount, current, total);
+ }
+
+ @Override
+ public com.ning.http.client.AsyncHandler.STATE onBodyPartReceived(
+ HttpResponseBodyPart content) throws Exception {
+ AsyncHttpDeferredObject.this.notify(new HttpResponseBodyPartProgress(content));
+ return super.onBodyPartReceived(content);
+ }
+ });
+ }
+
+ public static Promise<Response, Throwable, HttpProgress> promise(final BoundRequestBuilder builder) throws IOException {
+ return new AsyncHttpDeferredObject(builder).promise();
+ }
+}
View
47 extras/jdeferred/src/main/java/com/ning/http/client/extra/ContentWriteProgress.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2013 Ray Tsang
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.ning.http.client.extra;
+
+public class ContentWriteProgress implements HttpProgress {
+ private final long amount;
+ private final long current;
+ private final long total;
+
+ public ContentWriteProgress(long amount, long current, long total) {
+ this.amount = amount;
+ this.current = current;
+ this.total = total;
+ }
+
+ public long getAmount() {
+ return amount;
+ }
+
+ public long getCurrent() {
+ return current;
+ }
+
+ public long getTotal() {
+ return total;
+ }
+
+ @Override
+ public String toString() {
+ return "ContentWriteProgress [amount=" + amount + ", current="
+ + current + ", total=" + total + "]";
+ }
+
+}
View
20 extras/jdeferred/src/main/java/com/ning/http/client/extra/HttpProgress.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2013 Ray Tsang
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.ning.http.client.extra;
+
+public interface HttpProgress {
+
+}
View
35 extras/jdeferred/src/main/java/com/ning/http/client/extra/HttpResponseBodyPartProgress.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2013 Ray Tsang
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.ning.http.client.extra;
+
+import com.ning.http.client.HttpResponseBodyPart;
+
+public class HttpResponseBodyPartProgress implements HttpProgress {
+ private final HttpResponseBodyPart part;
+
+ public HttpResponseBodyPartProgress(HttpResponseBodyPart part) {
+ this.part = part;
+ }
+
+ public HttpResponseBodyPart getPart() {
+ return part;
+ }
+
+ @Override
+ public String toString() {
+ return "HttpResponseBodyPartProgress [part=" + part + "]";
+ }
+}
View
120 extras/jdeferred/src/test/java/com/ning/http/client/extra/AsyncHttpTest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2013 Ray Tsang
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.ning.http.client.extra;
+
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.jdeferred.DoneCallback;
+import org.jdeferred.ProgressCallback;
+import org.jdeferred.Promise;
+import org.jdeferred.impl.DefaultDeferredManager;
+import org.jdeferred.multiple.MultipleResults;
+
+import com.ning.http.client.AsyncHttpClient;
+import com.ning.http.client.Response;
+
+public class AsyncHttpTest extends TestCase {
+ protected DefaultDeferredManager deferredManager;
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ deferredManager = new DefaultDeferredManager();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testPromiseAdapter() throws IOException {
+ final CountDownLatch latch = new CountDownLatch(1);
+ final AtomicInteger successCount = new AtomicInteger();
+ final AtomicInteger progressCount = new AtomicInteger();
+
+ AsyncHttpClient client = new AsyncHttpClient();
+
+ Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject
+ .promise(client.prepareGet("http://www.ning.com"));
+ p1.done(new DoneCallback<Response>() {
+ @Override
+ public void onDone(Response response) {
+ try {
+ Assert.assertEquals(200, response.getStatusCode());
+ successCount.incrementAndGet();
+ } finally {
+ latch.countDown();
+ }
+ }
+ }).progress(new ProgressCallback<HttpProgress>() {
+
+ @Override
+ public void onProgress(HttpProgress progress) {
+ progressCount.incrementAndGet();
+ }
+ });
+
+ try {
+ latch.await();
+ Assert.assertTrue(progressCount.get() > 0);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ public void testMultiplePromiseAdapter() throws IOException {
+ final CountDownLatch latch = new CountDownLatch(1);
+ final AtomicInteger successCount = new AtomicInteger();
+
+ AsyncHttpClient client = new AsyncHttpClient();
+
+ Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject
+ .promise(client.prepareGet("http://www.ning.com"));
+ Promise<Response, Throwable, HttpProgress> p2 = AsyncHttpDeferredObject
+ .promise(client.prepareGet("http://www.google.com"));
+ AsyncHttpDeferredObject deferredRequest = new AsyncHttpDeferredObject(
+ client.prepareGet("http://jdeferred.org"));
+
+ deferredManager.when(p1, p2, deferredRequest).then(
+ new DoneCallback<MultipleResults>() {
+ @Override
+ public void onDone(MultipleResults result) {
+ try {
+ Assert.assertEquals(3, result.size());
+ Assert.assertEquals(200, ((Response) result.get(0)
+ .getResult()).getStatusCode());
+ Assert.assertEquals(200, ((Response) result.get(1)
+ .getResult()).getStatusCode());
+ Assert.assertEquals(200, ((Response) result.get(2)
+ .getResult()).getStatusCode());
+ successCount.incrementAndGet();
+ } finally {
+ latch.countDown();
+ }
+ }
+ });
+
+ try {
+ latch.await();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ }
+
+}
View
1  extras/pom.xml
@@ -45,6 +45,7 @@
<modules>
<module>guava</module>
+ <module>jdeferred</module>
</modules>
<dependencies>
Please sign in to comment.
Something went wrong with that request. Please try again.