Skip to content

Commit e0b097d

Browse files
committed
Clean up NettyBody ContenType override
1 parent f27511c commit e0b097d

10 files changed

+16
-61
lines changed

client/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactory.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,8 @@ private NettyBody body(Request request) {
8484
nettyBody = new NettyInputStreamBody(request.getStreamData());
8585

8686
} else if (isNonEmpty(request.getFormParams())) {
87-
88-
CharSequence contentType = null;
89-
if (!request.getHeaders().contains(CONTENT_TYPE)) {
90-
contentType = HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED;
91-
}
92-
93-
nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentType);
87+
CharSequence contentTypeOverride = request.getHeaders().contains(CONTENT_TYPE) ? null : HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED;
88+
nettyBody = new NettyByteBufferBody(urlEncodeFormParams(request.getFormParams(), bodyCharset), contentTypeOverride);
9489

9590
} else if (isNonEmpty(request.getBodyParts())) {
9691
nettyBody = new NettyMultipartBody(request.getBodyParts(), request.getHeaders(), config);
@@ -188,8 +183,8 @@ public NettyRequest newNettyRequest(Request request, boolean forceConnect, Proxy
188183
headers.set(CONTENT_LENGTH, body.getContentLength());
189184
}
190185

191-
if (body.getContentType() != null) {
192-
headers.set(CONTENT_TYPE, body.getContentType());
186+
if (body.getContentTypeOverride() != null) {
187+
headers.set(CONTENT_TYPE, body.getContentTypeOverride());
193188
}
194189
}
195190

client/src/main/java/org/asynchttpclient/netty/request/body/NettyBody.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public interface NettyBody {
2323

2424
long getContentLength();
2525

26-
CharSequence getContentType();
26+
default CharSequence getContentTypeOverride() {
27+
return null;
28+
}
2729

2830
void write(Channel channel, NettyResponseFuture<?> future) throws IOException;
2931
}

client/src/main/java/org/asynchttpclient/netty/request/body/NettyBodyBody.java

-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ public long getContentLength() {
5151
return body.getContentLength();
5252
}
5353

54-
@Override
55-
public String getContentType() {
56-
return null;
57-
}
58-
5954
@Override
6055
public void write(final Channel channel, NettyResponseFuture<?> future) throws IOException {
6156

client/src/main/java/org/asynchttpclient/netty/request/body/NettyByteArrayBody.java

-11
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,16 @@
2020
public class NettyByteArrayBody extends NettyDirectBody {
2121

2222
private final byte[] bytes;
23-
private final String contentType;
2423

2524
public NettyByteArrayBody(byte[] bytes) {
26-
this(bytes, null);
27-
}
28-
29-
public NettyByteArrayBody(byte[] bytes, String contentType) {
3025
this.bytes = bytes;
31-
this.contentType = contentType;
3226
}
3327

3428
@Override
3529
public long getContentLength() {
3630
return bytes.length;
3731
}
3832

39-
@Override
40-
public String getContentType() {
41-
return contentType;
42-
}
43-
4433
@Override
4534
public ByteBuf byteBuf() {
4635
return Unpooled.wrappedBuffer(bytes);

client/src/main/java/org/asynchttpclient/netty/request/body/NettyByteBufferBody.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@
2121
public class NettyByteBufferBody extends NettyDirectBody {
2222

2323
private final ByteBuffer bb;
24-
private final CharSequence contentType;
24+
private final CharSequence contentTypeOverride;
2525
private final long length;
2626

2727
public NettyByteBufferBody(ByteBuffer bb) {
2828
this(bb, null);
2929
}
3030

31-
public NettyByteBufferBody(ByteBuffer bb, CharSequence contentType) {
31+
public NettyByteBufferBody(ByteBuffer bb, CharSequence contentTypeOverride) {
3232
this.bb = bb;
3333
length = bb.remaining();
3434
bb.mark();
35-
this.contentType = contentType;
35+
this.contentTypeOverride = contentTypeOverride;
3636
}
3737

3838
@Override
@@ -41,8 +41,8 @@ public long getContentLength() {
4141
}
4242

4343
@Override
44-
public CharSequence getContentType() {
45-
return contentType;
44+
public CharSequence getContentTypeOverride() {
45+
return contentTypeOverride;
4646
}
4747

4848
@Override

client/src/main/java/org/asynchttpclient/netty/request/body/NettyCompositeByteArrayBody.java

-11
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@
2121
public class NettyCompositeByteArrayBody extends NettyDirectBody {
2222

2323
private final byte[][] bytes;
24-
private final String contentType;
2524
private final long contentLength;
2625

2726
public NettyCompositeByteArrayBody(List<byte[]> bytes) {
28-
this(bytes, null);
29-
}
30-
31-
public NettyCompositeByteArrayBody(List<byte[]> bytes, String contentType) {
3227
this.bytes = new byte[bytes.size()][];
3328
bytes.toArray(this.bytes);
34-
this.contentType = contentType;
3529
long l = 0;
3630
for (byte[] b : bytes)
3731
l += b.length;
@@ -43,11 +37,6 @@ public long getContentLength() {
4337
return contentLength;
4438
}
4539

46-
@Override
47-
public String getContentType() {
48-
return contentType;
49-
}
50-
5140
@Override
5241
public ByteBuf byteBuf() {
5342
return Unpooled.wrappedBuffer(bytes);

client/src/main/java/org/asynchttpclient/netty/request/body/NettyFileBody.java

-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ public long getContentLength() {
6262
return length;
6363
}
6464

65-
@Override
66-
public String getContentType() {
67-
return null;
68-
}
69-
7065
@Override
7166
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
7267
@SuppressWarnings("resource")

client/src/main/java/org/asynchttpclient/netty/request/body/NettyInputStreamBody.java

-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ public long getContentLength() {
5353
return contentLength;
5454
}
5555

56-
@Override
57-
public String getContentType() {
58-
return null;
59-
}
60-
6156
@Override
6257
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
6358
final InputStream is = inputStream;

client/src/main/java/org/asynchttpclient/netty/request/body/NettyMultipartBody.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424

2525
public class NettyMultipartBody extends NettyBodyBody {
2626

27-
private final String contentType;
27+
private final String contentTypeOverride;
2828

2929
public NettyMultipartBody(List<Part> parts, HttpHeaders headers, AsyncHttpClientConfig config) {
3030
this(newMultipartBody(parts, headers), config);
3131
}
3232

3333
private NettyMultipartBody(MultipartBody body, AsyncHttpClientConfig config) {
3434
super(body, config);
35-
contentType = body.getContentType();
35+
contentTypeOverride = body.getContentType();
3636
}
3737

3838
@Override
39-
public String getContentType() {
40-
return contentType;
39+
public String getContentTypeOverride() {
40+
return contentTypeOverride;
4141
}
4242
}

client/src/main/java/org/asynchttpclient/netty/request/body/NettyReactiveStreamsBody.java

-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ public long getContentLength() {
4949
return contentLength;
5050
}
5151

52-
@Override
53-
public String getContentType() {
54-
return null;
55-
}
56-
5752
@Override
5853
public void write(Channel channel, NettyResponseFuture<?> future) throws IOException {
5954
if (future.isStreamConsumed()) {

0 commit comments

Comments
 (0)