Skip to content

Commit a1977f0

Browse files
author
mcxiaoke
committed
http: tweak request, fix progress body and listener
1 parent 4baaa56 commit a1977f0

File tree

12 files changed

+149
-128
lines changed

12 files changed

+149
-128
lines changed

http/src/main/java/com/mcxiaoke/next/http/HttpMethod.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
package com.mcxiaoke.next.http;
1717

1818
import java.util.Arrays;
19+
import java.util.HashSet;
1920
import java.util.LinkedHashSet;
2021
import java.util.Set;
2122

2223
public enum HttpMethod {
2324

2425
GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, TRACE;
2526

26-
public static final Set<String> METHODS = new LinkedHashSet<String>(Arrays.asList(
27+
public static final Set<String> METHODS = new HashSet<String>(Arrays.asList(
2728
"OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "PATCH"));
2829

2930
public static boolean isValid(final String method) {
@@ -37,7 +38,6 @@ public static boolean supportBody(final String method) {
3738
public static boolean supportBody(final HttpMethod method) {
3839
return HttpMethod.POST.equals(method)
3940
|| HttpMethod.PUT.equals(method)
40-
|| HttpMethod.PATCH.equals(method)
41-
|| HttpMethod.DELETE.equals(method);
41+
|| HttpMethod.PATCH.equals(method);
4242
}
4343
}

http/src/main/java/com/mcxiaoke/next/http/LoggingInterceptor.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* Time: 16:01
1616
*/
1717
public class LoggingInterceptor implements Interceptor {
18-
private final boolean mHeaders;
19-
private final boolean mBody;
18+
private final boolean dumpHeaders;
19+
private final boolean dumpBody;
2020

2121
public LoggingInterceptor() {
2222
this(false, false);
@@ -27,8 +27,8 @@ public LoggingInterceptor(final boolean headers) {
2727
}
2828

2929
public LoggingInterceptor(final boolean headers, final boolean body) {
30-
mHeaders = headers;
31-
mBody = body;
30+
dumpHeaders = headers;
31+
dumpBody = body;
3232
}
3333

3434
@Override
@@ -37,7 +37,7 @@ public Response intercept(final Chain chain) throws IOException {
3737
final Request request = chain.request();
3838
Log.v(NextClient.TAG, String.format("[OkHttp Request] %s %s on %s%n",
3939
request.method(), request.url(), chain.connection()));
40-
if (mHeaders) {
40+
if (dumpHeaders) {
4141
Log.v(NextClient.TAG, "[Request Headers] " + request.headers());
4242
}
4343
final Response response = chain.proceed(request);
@@ -46,10 +46,10 @@ public Response intercept(final Chain chain) throws IOException {
4646
, request.method(), request.url()
4747
, response.code(), response.message()
4848
, (t2 - t1) / 1e6d));
49-
if (mHeaders) {
49+
if (dumpHeaders) {
5050
Log.v(NextClient.TAG, "[Response Headers] " + response.headers());
5151
}
52-
if (mBody) {
52+
if (dumpBody) {
5353
Log.v(NextClient.TAG, "[Response Body] " + responseToText(response));
5454
}
5555
return response;

http/src/main/java/com/mcxiaoke/next/http/NextClient.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import okhttp3.Headers;
88
import okhttp3.OkHttpClient;
99
import okhttp3.Request;
10+
import okhttp3.RequestBody;
1011
import okhttp3.Response;
1112

1213
import java.io.IOException;
@@ -335,8 +336,7 @@ protected <T> T executeInternal(final NextRequest request,
335336
final HttpTransformer<T> transformer)
336337
throws IOException {
337338
final Response response = sendRequest(request);
338-
final NextResponse nextResponse = new NextResponse(response);
339-
return transformer.transform(nextResponse);
339+
return transformer.transform(new NextResponse(response));
340340
}
341341

342342
public Response sendRequest(final NextRequest request)
@@ -347,14 +347,6 @@ public Response sendRequest(final NextRequest request)
347347
logHttpCurl(request);
348348
builder.addNetworkInterceptor(new LoggingInterceptor());
349349
}
350-
final ProgressListener li = request.getListener();
351-
if (li != null) {
352-
builder.addInterceptor(new ProgressInterceptor(li));
353-
}
354-
final OkClientInterceptor it = request.getInterceptor();
355-
if (it != null) {
356-
it.intercept(builder);
357-
}
358350
final OkHttpClient client = builder.build();
359351
return sendOkRequest(createOkRequest(request), client, request.isDebug());
360352
}

http/src/main/java/com/mcxiaoke/next/http/NextParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Date: 14-2-8
1515
* Time: 11:22
1616
*/
17-
public final class NextParams {
17+
public class NextParams {
1818
final Map<String, String> headers;
1919
final Map<String, String> queries;
2020
final Map<String, String> forms;

http/src/main/java/com/mcxiaoke/next/http/NextRequest.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import okhttp3.HttpUrl;
2323
import okhttp3.MultipartBody;
2424
import okhttp3.RequestBody;
25+
import okio.ByteString;
2526

2627
import java.io.File;
2728
import java.io.IOException;
@@ -38,7 +39,6 @@ public class NextRequest {
3839
protected NextParams params;
3940
protected byte[] body;
4041
protected ProgressListener listener;
41-
protected OkClientInterceptor interceptor;
4242
protected boolean debug;
4343

4444
public static NextRequest head(final String url) {
@@ -98,16 +98,11 @@ public NextRequest debug(final boolean debug) {
9898
return this;
9999
}
100100

101-
public NextRequest listener(final ProgressListener listener) {
101+
public NextRequest progressListener(final ProgressListener listener) {
102102
this.listener = listener;
103103
return this;
104104
}
105105

106-
public NextRequest interceptor(final OkClientInterceptor interceptor) {
107-
this.interceptor = interceptor;
108-
return this;
109-
}
110-
111106
public NextRequest userAgent(final String userAgent) {
112107
return header(HttpConsts.USER_AGENT, userAgent);
113108
}
@@ -282,14 +277,10 @@ public String originalUrl() {
282277
return httpUrl.toString();
283278
}
284279

285-
public ProgressListener getListener() {
280+
public ProgressListener getProgressListener() {
286281
return listener;
287282
}
288283

289-
public OkClientInterceptor getInterceptor() {
290-
return interceptor;
291-
}
292-
293284
protected boolean supportBody() {
294285
return HttpMethod.supportBody(method);
295286
}
@@ -405,19 +396,17 @@ protected void copy(final NextRequest source) {
405396
this.params = source.params;
406397
this.body = source.body;
407398
this.listener = source.listener;
408-
this.interceptor = source.interceptor;
409399
this.debug = source.debug;
410400
}
411401

412402
protected RequestBody getRequestBody() throws IOException {
413403
if (!supportBody()) {
414404
return null;
415405
}
416-
if (body != null) {
417-
return RequestBody.create(HttpConsts.MEDIA_TYPE_OCTET_STREAM, body);
418-
}
419406
RequestBody requestBody;
420-
if (hasParts()) {
407+
if (body != null) {
408+
requestBody = RequestBody.create(HttpConsts.MEDIA_TYPE_OCTET_STREAM, body);
409+
} else if (hasParts()) {
421410
final MultipartBody.Builder multipart = new MultipartBody.Builder();
422411
for (final BodyPart part : parts()) {
423412
if (part.getBody() != null) {
@@ -441,12 +430,26 @@ protected RequestBody getRequestBody() throws IOException {
441430
} else {
442431
requestBody = null;
443432
}
433+
if (requestBody == null) {
434+
return getEmptyBody();
435+
}
436+
if (listener != null) {
437+
requestBody = new ProgressRequestBody(requestBody, listener);
438+
}
444439
return requestBody;
445440
}
446441

447442
@Override
448443
public String toString() {
449-
return "Request{HTTP " + method + " " + httpUrl + ' ' + params + '}';
444+
return "Request{HTTP " + method() + " " + url() + ' ' + params + '}';
445+
}
446+
447+
public RequestBody getEmptyBody() {
448+
if (supportBody()) {
449+
return RequestBody.create(null, ByteString.EMPTY);
450+
} else {
451+
return null;
452+
}
450453
}
451454

452455
}

http/src/main/java/com/mcxiaoke/next/http/NextResponse.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import okhttp3.Headers;
88
import okhttp3.Response;
99

10-
import java.io.Closeable;
1110
import java.io.File;
1211
import java.io.IOException;
1312
import java.io.InputStream;
@@ -21,7 +20,7 @@
2120
* Date: 14-2-8
2221
* Time: 11:22
2322
*/
24-
public class NextResponse implements Closeable {
23+
public class NextResponse {
2524
public static final String TAG = NextResponse.class.getSimpleName();
2625

2726
private Response mResponse;
@@ -64,7 +63,7 @@ public String description() {
6463
return mStatusCode + ":" + mMessage;
6564
}
6665

67-
public long contentLength() throws IOException {
66+
public long contentLength() {
6867
return mResponse.body().contentLength();
6968
}
7069

@@ -88,39 +87,31 @@ public String location() {
8887
return header(HttpConsts.LOCATION);
8988
}
9089

91-
private InputStream getInputStream() throws IOException {
90+
public InputStream stream() {
9291
return mResponse.body().byteStream();
9392
}
9493

95-
private byte[] getByteArray() throws IOException {
96-
return IOUtils.readBytes(mResponse.body().byteStream());
97-
}
98-
99-
public InputStream stream() throws IOException {
100-
return getInputStream();
101-
}
102-
10394
public byte[] bytes() throws IOException {
104-
return getByteArray();
95+
return mResponse.body().bytes();
10596
}
10697

107-
public Reader reader() throws IOException {
98+
public Reader reader() {
10899
return mResponse.body().charStream();
109100
}
110101

111102
public String string() throws IOException {
112-
return IOUtils.readString(reader());
103+
return mResponse.body().string();
113104
}
114105

115106
public int writeTo(OutputStream os) throws IOException {
116-
return IOUtils.copy(getInputStream(), os);
107+
return IOUtils.copy(stream(), os);
117108
}
118109

119110
public boolean writeTo(File file) throws IOException {
120-
return IOUtils.writeStream(file, getInputStream());
111+
return IOUtils.writeStream(file, stream());
121112
}
122113

123-
public void close() throws IOException {
114+
public void close() {
124115
mResponse.body().close();
125116
}
126117

http/src/main/java/com/mcxiaoke/next/http/OkClientInterceptor.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
package com.mcxiaoke.next.http;
2-
31
/**
42
* User: mcxiaoke
5-
* Date: 14-5-30
6-
* Time: 14:21
3+
* Date: 2016/10/25
4+
* Time: 12:28
75
*/
86

9-
/**
10-
* http write data progress listener
11-
*/
12-
public interface ProgressListener {
7+
package com.mcxiaoke.next.http;
138

14-
void update(long bytesRead, long contentLength, boolean done);
9+
public interface ProgressListener {
10+
void onProgress(long bytesWritten, long contentLength, boolean done);
1511
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* User: mcxiaoke
3+
* Date: 2016/10/25
4+
* Time: 12:28
5+
*/
6+
7+
package com.mcxiaoke.next.http;
8+
9+
import okhttp3.MediaType;
10+
import okhttp3.RequestBody;
11+
import okio.Buffer;
12+
import okio.BufferedSink;
13+
import okio.ForwardingSink;
14+
import okio.Okio;
15+
import okio.Sink;
16+
17+
import java.io.IOException;
18+
19+
public class ProgressRequestBody extends RequestBody {
20+
21+
private final RequestBody body;
22+
private final ProgressListener listener;
23+
private BufferedSink buffer;
24+
25+
public ProgressRequestBody(RequestBody body, ProgressListener listener) {
26+
this.body = body;
27+
this.listener = listener;
28+
}
29+
30+
@Override
31+
public MediaType contentType() {
32+
return body.contentType();
33+
}
34+
35+
@Override
36+
public long contentLength() throws IOException {
37+
return body.contentLength();
38+
}
39+
40+
@Override
41+
public void writeTo(BufferedSink sink) throws IOException {
42+
if (buffer == null) {
43+
buffer = Okio.buffer(sink(sink));
44+
}
45+
body.writeTo(buffer);
46+
buffer.flush();
47+
}
48+
49+
private Sink sink(Sink sink) {
50+
return new ForwardingSink(sink) {
51+
long bytesWritten = 0L;
52+
long contentLength = 0L;
53+
54+
@Override
55+
public void write(Buffer source, long byteCount) throws IOException {
56+
super.write(source, byteCount);
57+
if (contentLength == 0) {
58+
contentLength = contentLength();
59+
}
60+
bytesWritten += byteCount;
61+
if (listener != null) {
62+
listener.onProgress(
63+
bytesWritten, contentLength, bytesWritten == contentLength);
64+
}
65+
}
66+
};
67+
}
68+
}

0 commit comments

Comments
 (0)