Skip to content

Commit 12b7060

Browse files
committed
Clean ParseHttpRequset and ParseHttpResposne
1 parent 0929626 commit 12b7060

File tree

4 files changed

+139
-50
lines changed

4 files changed

+139
-50
lines changed

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

Lines changed: 56 additions & 37 deletions
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 {@link 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 {@link 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,54 @@ 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 Builder newBuilder() {
139+
return new Builder(this);
140+
}
141+
142+
public String getUrl() {
143+
return url;
144+
}
145+
146+
public Method getMethod() {
147+
return method;
148+
}
149+
150+
public Map<String, String> getAllHeaders() {
151+
return headers;
152+
}
153+
154+
public String getHeader(String name) {
155+
return headers.get(name);
156+
}
157+
158+
public ParseHttpBody getBody() {
159+
return body;
160+
}
142161
}

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

Lines changed: 46 additions & 12 deletions
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 {@link 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 {@link 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 {@link ParseHttpResponse} {@code Builder} based on the input
97+
* {@link ParseHttpResponse}.
98+
*
99+
* @param response
100+
* The {@link 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,6 +130,14 @@ public ParseHttpResponse build() {
116130
this.contentType = builder.contentType;
117131
}
118132

133+
/**
134+
* Generates a new {@link com.parse.ParseHttpResponse.Builder} based on an
135+
* {@link ParseHttpResponse}, the {@link com.parse.ParseHttpResponse.Builder}'s values are come
136+
* from the {@link ParseHttpResponse}.
137+
*
138+
* @return A new {@link com.parse.ParseHttpResponse.Builder} whose values are come from the
139+
* {@link ParseHttpResponse}.
140+
*/
119141
public Builder newBuilder() {
120142
return new Builder(this);
121143
}
@@ -124,10 +146,22 @@ public int getStatusCode() {
124146
return statusCode;
125147
}
126148

149+
/**
150+
* Returns the content of the {@link ParseHttpResponse}'s body. The {@link InputStream} can only
151+
* be read once and can't be reset.
152+
*
153+
* @return The {@link InputStream} of the {@link ParseHttpResponse}'s body.
154+
*/
127155
public InputStream getContent() {
128156
return content;
129157
}
130158

159+
/**
160+
* Returns the size of the {@link ParseHttpResponse}'s body. -1 if the size of the
161+
* {@link ParseHttpResponse}'s body is unknown.
162+
*
163+
* @return The size of the {@link ParseHttpResponse}'s body.
164+
*/
131165
public long getTotalSize() {
132166
return totalSize;
133167
}
@@ -141,7 +175,7 @@ public String getContentType() {
141175
}
142176

143177
public String getHeader(String name) {
144-
return headers == null ? null : headers.get(name);
178+
return headers.get(name);
145179
}
146180

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

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

Lines changed: 36 additions & 0 deletions
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 = request.newBuilder()
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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)