Skip to content

Commit 00ec0ab

Browse files
committed
Clean ParseHttpRequset and ParseHttpResposne
1 parent 0929626 commit 00ec0ab

File tree

3 files changed

+137
-49
lines changed

3 files changed

+137
-49
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: 45 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;
@@ -64,7 +67,7 @@ public T addHeaders(Map<String, String> headers) {
6467
}
6568

6669
public T addHeader(String key, String value) {
67-
this.headers.put(key, value);
70+
headers.put(key, value);
6871
return self();
6972
}
7073

@@ -74,18 +77,28 @@ public T setContentType(String contentType) {
7477
}
7578
}
7679

80+
/**
81+
* Builder of {@link ParseHttpResponse}.
82+
*/
7783
public static class Builder extends Init<Builder> {
7884

7985
@Override
8086
/* package */ Builder self() {
8187
return this;
8288
}
8389

84-
/* package */ Builder() {
90+
public Builder() {
8591
super();
8692
}
8793

88-
/* package */ Builder(ParseHttpResponse response) {
94+
/**
95+
* Makes a new {@link ParseHttpResponse} {@code Builder} based on the input
96+
* {@link ParseHttpResponse}.
97+
*
98+
* @param response
99+
* The {@link ParseHttpResponse} where the {@code Builder}'s values come from.
100+
*/
101+
public Builder(ParseHttpResponse response) {
89102
super();
90103
this.setStatusCode(response.getStatusCode());
91104
this.setContent(response.getContent());
@@ -100,12 +113,12 @@ public ParseHttpResponse build() {
100113
}
101114
}
102115

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;
116+
private final int statusCode;
117+
private final InputStream content;
118+
private final long totalSize;
119+
private final String reasonPhrase;
120+
private final Map<String, String> headers;
121+
private final String contentType;
109122

110123
/* package */ ParseHttpResponse(Init<?> builder) {
111124
this.statusCode = builder.statusCode;
@@ -116,6 +129,14 @@ public ParseHttpResponse build() {
116129
this.contentType = builder.contentType;
117130
}
118131

132+
/**
133+
* Generates a new {@link com.parse.ParseHttpResponse.Builder} based on an
134+
* {@link ParseHttpResponse}, the {@link com.parse.ParseHttpResponse.Builder}'s values are come
135+
* from the {@link ParseHttpResponse}.
136+
*
137+
* @return A new {@link com.parse.ParseHttpResponse.Builder} whose values are come from the
138+
* {@link ParseHttpResponse}.
139+
*/
119140
public Builder newBuilder() {
120141
return new Builder(this);
121142
}
@@ -124,10 +145,22 @@ public int getStatusCode() {
124145
return statusCode;
125146
}
126147

148+
/**
149+
* Returns the content of the {@link ParseHttpResponse}'s body. The {@link InputStream} can only
150+
* be read once and can't be reset.
151+
*
152+
* @return The {@link InputStream} of the {@link ParseHttpResponse}'s body.
153+
*/
127154
public InputStream getContent() {
128155
return content;
129156
}
130157

158+
/**
159+
* Returns the size of the {@link ParseHttpResponse}'s body. -1 if the size of the
160+
* {@link ParseHttpResponse}'s body is unknown.
161+
*
162+
* @return The size of the {@link ParseHttpResponse}'s body.
163+
*/
131164
public long getTotalSize() {
132165
return totalSize;
133166
}
@@ -141,7 +174,7 @@ public String getContentType() {
141174
}
142175

143176
public String getHeader(String name) {
144-
return headers == null ? null : headers.get(name);
177+
return headers.get(name);
145178
}
146179

147180
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
}

0 commit comments

Comments
 (0)