Skip to content

Commit 09d80fd

Browse files
author
Stephane Landelle
committed
Let one pass a List<byte[]> request body, close AsyncHttpClient#763
1 parent 9c28aaa commit 09d80fd

File tree

4 files changed

+97
-17
lines changed

4 files changed

+97
-17
lines changed

src/main/java/com/ning/http/client/providers/netty/request/NettyRequestFactory.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import static com.ning.http.util.AuthenticatorUtils.computeDigestAuthentication;
2727
import static com.ning.http.util.MiscUtils.isNonEmpty;
2828

29-
import org.jboss.netty.buffer.ChannelBuffers;
29+
import org.jboss.netty.buffer.ChannelBuffer;
3030
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
3131
import org.jboss.netty.handler.codec.http.HttpHeaders;
3232
import org.jboss.netty.handler.codec.http.HttpMethod;
@@ -46,6 +46,7 @@
4646
import com.ning.http.client.providers.netty.request.body.NettyBody;
4747
import com.ning.http.client.providers.netty.request.body.NettyBodyBody;
4848
import com.ning.http.client.providers.netty.request.body.NettyByteArrayBody;
49+
import com.ning.http.client.providers.netty.request.body.NettyDirectBody;
4950
import com.ning.http.client.providers.netty.request.body.NettyFileBody;
5051
import com.ning.http.client.providers.netty.request.body.NettyInputStreamBody;
5152
import com.ning.http.client.providers.netty.request.body.NettyMultipartBody;
@@ -263,11 +264,11 @@ public NettyRequest newNettyRequest(Request request, Uri uri, boolean forceConne
263264

264265
HttpRequest httpRequest;
265266
NettyRequest nettyRequest;
266-
if (body instanceof NettyByteArrayBody) {
267-
byte[] bytes = NettyByteArrayBody.class.cast(body).getBytes();
267+
if (body instanceof NettyDirectBody) {
268+
ChannelBuffer buffer = NettyDirectBody.class.cast(body).channelBuffer();
268269
httpRequest = new DefaultHttpRequest(httpVersion, method, requestUri);
269270
// body is passed as null as it's written directly with the request
270-
httpRequest.setContent(ChannelBuffers.wrappedBuffer(bytes));
271+
httpRequest.setContent(buffer);
271272
nettyRequest = new NettyRequest(httpRequest, null);
272273

273274
} else {

src/main/java/com/ning/http/client/providers/netty/request/body/NettyByteArrayBody.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@
1313
*/
1414
package com.ning.http.client.providers.netty.request.body;
1515

16-
import org.jboss.netty.channel.Channel;
16+
import org.jboss.netty.buffer.ChannelBuffer;
17+
import org.jboss.netty.buffer.ChannelBuffers;
1718

18-
import com.ning.http.client.AsyncHttpClientConfig;
19-
import com.ning.http.client.providers.netty.future.NettyResponseFuture;
20-
21-
import java.io.IOException;
22-
23-
public class NettyByteArrayBody implements NettyBody {
19+
public class NettyByteArrayBody extends NettyDirectBody {
2420

2521
private final byte[] bytes;
2622
private final String contentType;
@@ -34,10 +30,6 @@ public NettyByteArrayBody(byte[] bytes, String contentType) {
3430
this.contentType = contentType;
3531
}
3632

37-
public byte[] getBytes() {
38-
return bytes;
39-
}
40-
4133
@Override
4234
public long getContentLength() {
4335
return bytes.length;
@@ -49,7 +41,7 @@ public String getContentType() {
4941
}
5042

5143
@Override
52-
public void write(Channel channel, NettyResponseFuture<?> future, AsyncHttpClientConfig config) throws IOException {
53-
throw new UnsupportedOperationException("This kind of body is supposed to be writen directly");
44+
public ChannelBuffer channelBuffer() {
45+
return ChannelBuffers.wrappedBuffer(bytes);
5446
}
5547
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package com.ning.http.client.providers.netty.request.body;
15+
16+
import org.jboss.netty.buffer.ChannelBuffer;
17+
import org.jboss.netty.buffer.ChannelBuffers;
18+
19+
import java.util.List;
20+
21+
public class NettyCompositeByteArrayBody extends NettyDirectBody {
22+
23+
private final byte[][] bytes;
24+
private final String contentType;
25+
private final long contentLength;
26+
27+
public NettyCompositeByteArrayBody(List<byte[]> bytes) {
28+
this(bytes, null);
29+
}
30+
31+
public NettyCompositeByteArrayBody(List<byte[]> bytes, String contentType) {
32+
this.bytes = new byte[bytes.size()][];
33+
bytes.toArray(this.bytes);
34+
this.contentType = contentType;
35+
long l = 0;
36+
for (byte[] b : bytes)
37+
l += b.length;
38+
contentLength = l;
39+
}
40+
41+
@Override
42+
public long getContentLength() {
43+
return contentLength;
44+
}
45+
46+
@Override
47+
public String getContentType() {
48+
return contentType;
49+
}
50+
51+
@Override
52+
public ChannelBuffer channelBuffer() {
53+
return ChannelBuffers.wrappedBuffer(bytes);
54+
}
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2014 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package com.ning.http.client.providers.netty.request.body;
15+
16+
import org.jboss.netty.buffer.ChannelBuffer;
17+
import org.jboss.netty.channel.Channel;
18+
19+
import com.ning.http.client.AsyncHttpClientConfig;
20+
import com.ning.http.client.providers.netty.future.NettyResponseFuture;
21+
22+
import java.io.IOException;
23+
24+
public abstract class NettyDirectBody implements NettyBody {
25+
26+
public abstract ChannelBuffer channelBuffer();
27+
28+
@Override
29+
public void write(Channel channel, NettyResponseFuture<?> future, AsyncHttpClientConfig config) throws IOException {
30+
throw new UnsupportedOperationException("This kind of body is supposed to be writen directly");
31+
}
32+
}

0 commit comments

Comments
 (0)