Skip to content

Commit 70f5c20

Browse files
committed
Merge pull request #99 from ParsePlatform/wangmengyan.clean_ParseHttpRequset_and_ParseHttpResponse
Clean ParseHttpRequset and ParseHttpResposne
2 parents 063ce1b + 224909c commit 70f5c20

7 files changed

+131
-58
lines changed

Parse/src/main/java/com/parse/ParseDecompressInterceptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ParseHttpResponse intercept(Chain chain) throws IOException {
3333
// to -1
3434
newHeaders.put(CONTENT_LENGTH_HEADER, "-1");
3535
// TODO(mengyan): Add builder constructor based on an existing ParseHttpResponse
36-
response = response.newBuilder()
36+
response = new ParseHttpResponse.Builder(response)
3737
.setTotalSize(-1)
3838
.setHeaders(newHeaders)
3939
.setContent(new GZIPInputStream(response.getContent()))

Parse/src/main/java/com/parse/ParseHttpRequest.java

+52-37
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@
1212
import java.util.HashMap;
1313
import java.util.Map;
1414

15+
/**
16+
* The http request we send to parse server. Instances of this class are not immutable. The
17+
* request body may be consumed only once. The other fields are immutable.
18+
*/
1519
/** package */ class ParseHttpRequest {
1620

21+
/**
22+
* The {@code ParseHttpRequest} method type.
23+
*/
1724
public enum Method {
1825
GET, POST, PUT, DELETE;
1926

@@ -61,43 +68,14 @@ public String toString() {
6168
}
6269
}
6370

64-
private final String url;
65-
private final ParseHttpRequest.Method method;
66-
private final Map<String, String> headers;
67-
private final ParseHttpBody body;
68-
69-
protected ParseHttpRequest(Builder builder) {
70-
this.url = builder.url;
71-
this.method = builder.method;
72-
this.headers = Collections.unmodifiableMap(new HashMap<>(builder.headers));
73-
this.body = builder.body;
74-
}
75-
76-
public String getUrl() {
77-
return url;
78-
}
79-
80-
public ParseHttpRequest.Method getMethod() {
81-
return method;
82-
}
83-
84-
public Map<String, String> getAllHeaders() {
85-
return headers;
86-
}
87-
88-
public String getHeader(String name) {
89-
return headers.get(name);
90-
}
91-
92-
public ParseHttpBody getBody() {
93-
return body;
94-
}
95-
71+
/**
72+
* Builder of {@code ParseHttpRequest}.
73+
*/
9674
public static class Builder {
97-
protected String url;
98-
protected ParseHttpRequest.Method method;
99-
protected Map<String, String> headers;
100-
protected ParseHttpBody body;
75+
private String url;
76+
private Method method;
77+
private Map<String, String> headers;
78+
private ParseHttpBody body;
10179

10280
public Builder() {
10381
this.headers = new HashMap<>();
@@ -130,13 +108,50 @@ public Builder addHeader(String name, String value) {
130108
return this;
131109
}
132110

111+
public Builder addHeaders(Map<String, String> headers) {
112+
this.headers.putAll(headers);
113+
return this;
114+
}
115+
133116
public Builder setHeaders(Map<String, String> headers) {
134-
this.headers = headers;
117+
this.headers = new HashMap<>(headers);
135118
return this;
136119
}
137120

138121
public ParseHttpRequest build() {
139122
return new ParseHttpRequest(this);
140123
}
141124
}
125+
126+
private final String url;
127+
private final Method method;
128+
private final Map<String, String> headers;
129+
private final ParseHttpBody body;
130+
131+
private ParseHttpRequest(Builder builder) {
132+
this.url = builder.url;
133+
this.method = builder.method;
134+
this.headers = Collections.unmodifiableMap(new HashMap<>(builder.headers));
135+
this.body = builder.body;
136+
}
137+
138+
public String getUrl() {
139+
return url;
140+
}
141+
142+
public Method getMethod() {
143+
return method;
144+
}
145+
146+
public Map<String, String> getAllHeaders() {
147+
return headers;
148+
}
149+
150+
public String getHeader(String name) {
151+
return headers.get(name);
152+
}
153+
154+
public ParseHttpBody getBody() {
155+
return body;
156+
}
142157
}

Parse/src/main/java/com/parse/ParseHttpResponse.java

+38-16
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
import java.util.Map;
1515

1616
/**
17-
* The base class of a http response we receive from parse server. It can be implemented by
18-
* different http library such as Apache http, Android URLConnection, Square OKHttp and so on.
17+
* The http response we receive from parse server. Instances of this class are not immutable. The
18+
* response body may be consumed only once. The other fields are immutable.
1919
*/
2020
/** package */ class ParseHttpResponse {
2121

22+
/**
23+
* Base builder for {@code ParseHttpResponse}.
24+
*/
2225
/* package */ static abstract class Init<T extends Init<T>> {
2326
private int statusCode;
2427
private InputStream content;
@@ -30,6 +33,7 @@
3033
/* package */ abstract T self();
3134

3235
public Init() {
36+
this.totalSize = -1;
3337
this.headers = new HashMap<>();
3438
}
3539

@@ -64,7 +68,7 @@ public T addHeaders(Map<String, String> headers) {
6468
}
6569

6670
public T addHeader(String key, String value) {
67-
this.headers.put(key, value);
71+
headers.put(key, value);
6872
return self();
6973
}
7074

@@ -74,18 +78,28 @@ public T setContentType(String contentType) {
7478
}
7579
}
7680

81+
/**
82+
* Builder of {@code ParseHttpResponse}.
83+
*/
7784
public static class Builder extends Init<Builder> {
7885

7986
@Override
8087
/* package */ Builder self() {
8188
return this;
8289
}
8390

84-
/* package */ Builder() {
91+
public Builder() {
8592
super();
8693
}
8794

88-
/* package */ Builder(ParseHttpResponse response) {
95+
/**
96+
* Makes a new {@code ParseHttpResponse} {@code Builder} based on the input
97+
* {@code ParseHttpResponse}.
98+
*
99+
* @param response
100+
* The {@code ParseHttpResponse} where the {@code Builder}'s values come from.
101+
*/
102+
public Builder(ParseHttpResponse response) {
89103
super();
90104
this.setStatusCode(response.getStatusCode());
91105
this.setContent(response.getContent());
@@ -100,12 +114,12 @@ public ParseHttpResponse build() {
100114
}
101115
}
102116

103-
/* package */ final int statusCode;
104-
/* package */ final InputStream content;
105-
/* package */ final long totalSize;
106-
/* package */ final String reasonPhrase;
107-
/* package */ final Map<String, String> headers;
108-
/* package */ final String contentType;
117+
private final int statusCode;
118+
private final InputStream content;
119+
private final long totalSize;
120+
private final String reasonPhrase;
121+
private final Map<String, String> headers;
122+
private final String contentType;
109123

110124
/* package */ ParseHttpResponse(Init<?> builder) {
111125
this.statusCode = builder.statusCode;
@@ -116,18 +130,26 @@ public ParseHttpResponse build() {
116130
this.contentType = builder.contentType;
117131
}
118132

119-
public Builder newBuilder() {
120-
return new Builder(this);
121-
}
122-
123133
public int getStatusCode() {
124134
return statusCode;
125135
}
126136

137+
/**
138+
* Returns the content of the {@code ParseHttpResponse}'s body. The {@link InputStream} can only
139+
* be read once and can't be reset.
140+
*
141+
* @return The {@link InputStream} of the {@code ParseHttpResponse}'s body.
142+
*/
127143
public InputStream getContent() {
128144
return content;
129145
}
130146

147+
/**
148+
* Returns the size of the {@code ParseHttpResponse}'s body. -1 if the size of the
149+
* {@code ParseHttpResponse}'s body is unknown.
150+
*
151+
* @return The size of the {@code ParseHttpResponse}'s body.
152+
*/
131153
public long getTotalSize() {
132154
return totalSize;
133155
}
@@ -141,7 +163,7 @@ public String getContentType() {
141163
}
142164

143165
public String getHeader(String name) {
144-
return headers == null ? null : headers.get(name);
166+
return headers.get(name);
145167
}
146168

147169
public Map<String, String> getAllHeaders() {

Parse/src/main/java/com/parse/ParseLogInterceptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void done(InputStream proxyInput, IOException e) {
287287
logResponseInfo(getLogger(), requestId, response, IGNORED_BODY_INFO);
288288
}
289289

290-
return response.newBuilder()
290+
return new ParseHttpResponse.Builder(response)
291291
.setContent(newResponseBodyStream)
292292
.build();
293293
}

Parse/src/main/java/com/parse/ParseStethoInterceptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public ParseHttpResponse intercept(Chain chain) throws IOException {
276276
new DefaultResponseHandler(stethoEventReporter, requestId)
277277
);
278278
if (responseStream != null) {
279-
response = response.newBuilder()
279+
response = new ParseHttpResponse.Builder(response)
280280
.setContent(responseStream)
281281
.build();
282282
}

Parse/src/test/java/com/parse/ParseHttpRequestTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
import org.junit.Test;
1212

13+
import java.io.ByteArrayInputStream;
1314
import java.io.IOException;
1415
import java.util.HashMap;
1516
import java.util.Map;
1617

1718
import static org.junit.Assert.assertArrayEquals;
1819
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertSame;
2022
import static org.junit.Assert.assertTrue;
2123

2224
public class ParseHttpRequestTest {
@@ -80,4 +82,38 @@ public void testParseHttpRequestBuilderInitialization() throws IOException {
8082
assertEquals(contentType, bodyAgain.getContentType());
8183
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
8284
}
85+
86+
@Test
87+
public void testParseHttpRequestBuildWithParseHttpRequest() throws IOException {
88+
String url = "www.parse.com";
89+
ParseHttpRequest.Method method = ParseHttpRequest.Method.POST;
90+
Map<String, String> headers = new HashMap<>();
91+
String name = "name";
92+
String value = "value";
93+
headers.put(name, value);
94+
95+
String content = "content";
96+
String contentType = "application/json";
97+
ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType);
98+
99+
ParseHttpRequest request = new ParseHttpRequest.Builder()
100+
.setUrl(url)
101+
.addHeader(name, value)
102+
.setMethod(method)
103+
.setBody(body)
104+
.build();
105+
106+
String newURL = "www.api.parse.com";
107+
ParseHttpRequest newRequest = new ParseHttpRequest.Builder(request)
108+
.setUrl(newURL)
109+
.build();
110+
111+
assertEquals(newURL, newRequest.getUrl());
112+
assertEquals(method.toString(), newRequest.getMethod().toString());
113+
assertEquals(1, newRequest.getAllHeaders().size());
114+
assertEquals(value, newRequest.getHeader(name));
115+
ParseHttpBody bodyAgain = newRequest.getBody();
116+
assertEquals(contentType, bodyAgain.getContentType());
117+
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
118+
}
83119
}

Parse/src/test/java/com/parse/ParseHttpResponseTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void testParseHttpResponseDefaults() throws IOException {
3131
assertNull(response.getContentType());
3232
assertNull(response.getReasonPhrase());
3333
assertEquals(0, response.getStatusCode());
34-
assertEquals(0, response.getTotalSize());
34+
assertEquals(-1, response.getTotalSize());
3535
assertEquals(0, response.getAllHeaders().size());
3636
assertNull(response.getHeader("test"));
3737
}
@@ -88,7 +88,7 @@ public void testParseHttpResponseBuildWithParseHttpResponse() throws IOException
8888
.build();
8989

9090
String newReasonPhrase = "Failed";
91-
ParseHttpResponse newResponse = response.newBuilder()
91+
ParseHttpResponse newResponse = new ParseHttpResponse.Builder(response)
9292
.setReasonPhrase(newReasonPhrase)
9393
.build();
9494

0 commit comments

Comments
 (0)