Skip to content

Commit 4c7de7a

Browse files
committed
Merge pull request AsyncHttpClient#270 from saturnism/master
Add support for JDeferred library
2 parents 9908f3e + f6b341a commit 4c7de7a

File tree

7 files changed

+316
-0
lines changed

7 files changed

+316
-0
lines changed

extras/jdeferred/pom.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!--
2+
Copyright 2013 Ray Tsang
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<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">
17+
<modelVersion>4.0.0</modelVersion>
18+
<parent>
19+
<artifactId>async-http-client-extras-parent</artifactId>
20+
<groupId>com.ning</groupId>
21+
<version>1.8.0-SNAPSHOT</version>
22+
<relativePath>..</relativePath>
23+
</parent>
24+
<artifactId>async-http-client-extras-jdeferred</artifactId>
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.jdeferred</groupId>
28+
<artifactId>jdeferred-core</artifactId>
29+
<version>1.0.0</version>
30+
</dependency>
31+
</dependencies>
32+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2013 Ray Tsang
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ning.http.client.extra;
17+
18+
import java.io.IOException;
19+
20+
import org.jdeferred.Promise;
21+
import org.jdeferred.impl.DeferredObject;
22+
23+
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
24+
import com.ning.http.client.AsyncCompletionHandler;
25+
import com.ning.http.client.HttpResponseBodyPart;
26+
import com.ning.http.client.Response;
27+
28+
public class AsyncHttpDeferredObject extends DeferredObject<Response, Throwable, HttpProgress> {
29+
public AsyncHttpDeferredObject(BoundRequestBuilder builder) throws IOException {
30+
builder.execute(new AsyncCompletionHandler<Void>() {
31+
@Override
32+
public Void onCompleted(Response response) throws Exception {
33+
AsyncHttpDeferredObject.this.resolve(response);
34+
return null;
35+
}
36+
37+
@Override
38+
public void onThrowable(Throwable t) {
39+
AsyncHttpDeferredObject.this.reject(t);
40+
}
41+
42+
@Override
43+
public com.ning.http.client.AsyncHandler.STATE onContentWriteProgress(
44+
long amount, long current, long total) {
45+
AsyncHttpDeferredObject.this.notify(new ContentWriteProgress(amount, current, total));
46+
return super.onContentWriteProgress(amount, current, total);
47+
}
48+
49+
@Override
50+
public com.ning.http.client.AsyncHandler.STATE onBodyPartReceived(
51+
HttpResponseBodyPart content) throws Exception {
52+
AsyncHttpDeferredObject.this.notify(new HttpResponseBodyPartProgress(content));
53+
return super.onBodyPartReceived(content);
54+
}
55+
});
56+
}
57+
58+
public static Promise<Response, Throwable, HttpProgress> promise(final BoundRequestBuilder builder) throws IOException {
59+
return new AsyncHttpDeferredObject(builder).promise();
60+
}
61+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2013 Ray Tsang
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ning.http.client.extra;
17+
18+
public class ContentWriteProgress implements HttpProgress {
19+
private final long amount;
20+
private final long current;
21+
private final long total;
22+
23+
public ContentWriteProgress(long amount, long current, long total) {
24+
this.amount = amount;
25+
this.current = current;
26+
this.total = total;
27+
}
28+
29+
public long getAmount() {
30+
return amount;
31+
}
32+
33+
public long getCurrent() {
34+
return current;
35+
}
36+
37+
public long getTotal() {
38+
return total;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "ContentWriteProgress [amount=" + amount + ", current="
44+
+ current + ", total=" + total + "]";
45+
}
46+
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2013 Ray Tsang
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ning.http.client.extra;
17+
18+
public interface HttpProgress {
19+
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2013 Ray Tsang
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ning.http.client.extra;
17+
18+
import com.ning.http.client.HttpResponseBodyPart;
19+
20+
public class HttpResponseBodyPartProgress implements HttpProgress {
21+
private final HttpResponseBodyPart part;
22+
23+
public HttpResponseBodyPartProgress(HttpResponseBodyPart part) {
24+
this.part = part;
25+
}
26+
27+
public HttpResponseBodyPart getPart() {
28+
return part;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "HttpResponseBodyPartProgress [part=" + part + "]";
34+
}
35+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2013 Ray Tsang
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ning.http.client.extra;
17+
18+
import java.io.IOException;
19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.atomic.AtomicInteger;
21+
22+
import junit.framework.Assert;
23+
import junit.framework.TestCase;
24+
25+
import org.jdeferred.DoneCallback;
26+
import org.jdeferred.ProgressCallback;
27+
import org.jdeferred.Promise;
28+
import org.jdeferred.impl.DefaultDeferredManager;
29+
import org.jdeferred.multiple.MultipleResults;
30+
31+
import com.ning.http.client.AsyncHttpClient;
32+
import com.ning.http.client.Response;
33+
34+
public class AsyncHttpTest extends TestCase {
35+
protected DefaultDeferredManager deferredManager;
36+
37+
protected void setUp() throws Exception {
38+
super.setUp();
39+
deferredManager = new DefaultDeferredManager();
40+
}
41+
42+
protected void tearDown() throws Exception {
43+
super.tearDown();
44+
}
45+
46+
public void testPromiseAdapter() throws IOException {
47+
final CountDownLatch latch = new CountDownLatch(1);
48+
final AtomicInteger successCount = new AtomicInteger();
49+
final AtomicInteger progressCount = new AtomicInteger();
50+
51+
AsyncHttpClient client = new AsyncHttpClient();
52+
53+
Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject
54+
.promise(client.prepareGet("http://www.ning.com"));
55+
p1.done(new DoneCallback<Response>() {
56+
@Override
57+
public void onDone(Response response) {
58+
try {
59+
Assert.assertEquals(200, response.getStatusCode());
60+
successCount.incrementAndGet();
61+
} finally {
62+
latch.countDown();
63+
}
64+
}
65+
}).progress(new ProgressCallback<HttpProgress>() {
66+
67+
@Override
68+
public void onProgress(HttpProgress progress) {
69+
progressCount.incrementAndGet();
70+
}
71+
});
72+
73+
try {
74+
latch.await();
75+
Assert.assertTrue(progressCount.get() > 0);
76+
} catch (InterruptedException e) {
77+
Thread.currentThread().interrupt();
78+
}
79+
}
80+
81+
public void testMultiplePromiseAdapter() throws IOException {
82+
final CountDownLatch latch = new CountDownLatch(1);
83+
final AtomicInteger successCount = new AtomicInteger();
84+
85+
AsyncHttpClient client = new AsyncHttpClient();
86+
87+
Promise<Response, Throwable, HttpProgress> p1 = AsyncHttpDeferredObject
88+
.promise(client.prepareGet("http://www.ning.com"));
89+
Promise<Response, Throwable, HttpProgress> p2 = AsyncHttpDeferredObject
90+
.promise(client.prepareGet("http://www.google.com"));
91+
AsyncHttpDeferredObject deferredRequest = new AsyncHttpDeferredObject(
92+
client.prepareGet("http://jdeferred.org"));
93+
94+
deferredManager.when(p1, p2, deferredRequest).then(
95+
new DoneCallback<MultipleResults>() {
96+
@Override
97+
public void onDone(MultipleResults result) {
98+
try {
99+
Assert.assertEquals(3, result.size());
100+
Assert.assertEquals(200, ((Response) result.get(0)
101+
.getResult()).getStatusCode());
102+
Assert.assertEquals(200, ((Response) result.get(1)
103+
.getResult()).getStatusCode());
104+
Assert.assertEquals(200, ((Response) result.get(2)
105+
.getResult()).getStatusCode());
106+
successCount.incrementAndGet();
107+
} finally {
108+
latch.countDown();
109+
}
110+
}
111+
});
112+
113+
try {
114+
latch.await();
115+
} catch (InterruptedException e) {
116+
Thread.currentThread().interrupt();
117+
}
118+
}
119+
120+
}

extras/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
<modules>
4747
<module>guava</module>
48+
<module>jdeferred</module>
4849
</modules>
4950

5051
<dependencies>

0 commit comments

Comments
 (0)