Skip to content

Clean ParseHttpRequset and ParseHttpResposne #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ParseHttpResponse intercept(Chain chain) throws IOException {
// to -1
newHeaders.put(CONTENT_LENGTH_HEADER, "-1");
// TODO(mengyan): Add builder constructor based on an existing ParseHttpResponse
response = response.newBuilder()
response = new ParseHttpResponse.Builder(response)
.setTotalSize(-1)
.setHeaders(newHeaders)
.setContent(new GZIPInputStream(response.getContent()))
Expand Down
89 changes: 52 additions & 37 deletions Parse/src/main/java/com/parse/ParseHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
import java.util.HashMap;
import java.util.Map;

/**
* The http request we send to parse server. Instances of this class are not immutable. The
* request body may be consumed only once. The other fields are immutable.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this class mutable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body of the ParseHttpRequest is mutable, they can be change after we build the ParseHttpRequest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/** package */ class ParseHttpRequest {

/**
* The {@code ParseHttpRequest} method type.
*/
public enum Method {
GET, POST, PUT, DELETE;

Expand Down Expand Up @@ -61,43 +68,14 @@ public String toString() {
}
}

private final String url;
private final ParseHttpRequest.Method method;
private final Map<String, String> headers;
private final ParseHttpBody body;

protected ParseHttpRequest(Builder builder) {
this.url = builder.url;
this.method = builder.method;
this.headers = Collections.unmodifiableMap(new HashMap<>(builder.headers));
this.body = builder.body;
}

public String getUrl() {
return url;
}

public ParseHttpRequest.Method getMethod() {
return method;
}

public Map<String, String> getAllHeaders() {
return headers;
}

public String getHeader(String name) {
return headers.get(name);
}

public ParseHttpBody getBody() {
return body;
}

/**
* Builder of {@code ParseHttpRequest}.
*/
public static class Builder {
protected String url;
protected ParseHttpRequest.Method method;
protected Map<String, String> headers;
protected ParseHttpBody body;
private String url;
private Method method;
private Map<String, String> headers;
private ParseHttpBody body;

public Builder() {
this.headers = new HashMap<>();
Expand Down Expand Up @@ -130,13 +108,50 @@ public Builder addHeader(String name, String value) {
return this;
}

public Builder addHeaders(Map<String, String> headers) {
this.headers.putAll(headers);
return this;
}

public Builder setHeaders(Map<String, String> headers) {
this.headers = headers;
this.headers = new HashMap<>(headers);
return this;
}

public ParseHttpRequest build() {
return new ParseHttpRequest(this);
}
}

private final String url;
private final Method method;
private final Map<String, String> headers;
private final ParseHttpBody body;

private ParseHttpRequest(Builder builder) {
this.url = builder.url;
this.method = builder.method;
this.headers = Collections.unmodifiableMap(new HashMap<>(builder.headers));
this.body = builder.body;
}

public String getUrl() {
return url;
}

public Method getMethod() {
return method;
}

public Map<String, String> getAllHeaders() {
return headers;
}

public String getHeader(String name) {
return headers.get(name);
}

public ParseHttpBody getBody() {
return body;
}
}
54 changes: 38 additions & 16 deletions Parse/src/main/java/com/parse/ParseHttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
import java.util.Map;

/**
* The base class of a http response we receive from parse server. It can be implemented by
* different http library such as Apache http, Android URLConnection, Square OKHttp and so on.
* The http response we receive from parse server. Instances of this class are not immutable. The
* response body may be consumed only once. The other fields are immutable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this class mutable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with ParseHttpRequest, body is mutable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

*/
/** package */ class ParseHttpResponse {

/**
* Base builder for {@code ParseHttpResponse}.
*/
/* package */ static abstract class Init<T extends Init<T>> {
private int statusCode;
private InputStream content;
Expand All @@ -30,6 +33,7 @@
/* package */ abstract T self();

public Init() {
this.totalSize = -1;
this.headers = new HashMap<>();
}

Expand Down Expand Up @@ -64,7 +68,7 @@ public T addHeaders(Map<String, String> headers) {
}

public T addHeader(String key, String value) {
this.headers.put(key, value);
headers.put(key, value);
return self();
}

Expand All @@ -74,18 +78,28 @@ public T setContentType(String contentType) {
}
}

/**
* Builder of {@code ParseHttpResponse}.
*/
public static class Builder extends Init<Builder> {

@Override
/* package */ Builder self() {
return this;
}

/* package */ Builder() {
public Builder() {
super();
}

/* package */ Builder(ParseHttpResponse response) {
/**
* Makes a new {@code ParseHttpResponse} {@code Builder} based on the input
* {@code ParseHttpResponse}.
*
* @param response
* The {@code ParseHttpResponse} where the {@code Builder}'s values come from.
*/
public Builder(ParseHttpResponse response) {
super();
this.setStatusCode(response.getStatusCode());
this.setContent(response.getContent());
Expand All @@ -100,12 +114,12 @@ public ParseHttpResponse build() {
}
}

/* package */ final int statusCode;
/* package */ final InputStream content;
/* package */ final long totalSize;
/* package */ final String reasonPhrase;
/* package */ final Map<String, String> headers;
/* package */ final String contentType;
private final int statusCode;
private final InputStream content;
private final long totalSize;
private final String reasonPhrase;
private final Map<String, String> headers;
private final String contentType;

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

public Builder newBuilder() {
return new Builder(this);
}

public int getStatusCode() {
return statusCode;
}

/**
* Returns the content of the {@code ParseHttpResponse}'s body. The {@link InputStream} can only
* be read once and can't be reset.
*
* @return The {@link InputStream} of the {@code ParseHttpResponse}'s body.
*/
public InputStream getContent() {
return content;
}

/**
* Returns the size of the {@code ParseHttpResponse}'s body. -1 if the size of the
* {@code ParseHttpResponse}'s body is unknown.
*
* @return The size of the {@code ParseHttpResponse}'s body.
*/
public long getTotalSize() {
return totalSize;
}
Expand All @@ -141,7 +163,7 @@ public String getContentType() {
}

public String getHeader(String name) {
return headers == null ? null : headers.get(name);
return headers.get(name);
}

public Map<String, String> getAllHeaders() {
Expand Down
2 changes: 1 addition & 1 deletion Parse/src/main/java/com/parse/ParseLogInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public void done(InputStream proxyInput, IOException e) {
logResponseInfo(getLogger(), requestId, response, IGNORED_BODY_INFO);
}

return response.newBuilder()
return new ParseHttpResponse.Builder(response)
.setContent(newResponseBodyStream)
.build();
}
Expand Down
2 changes: 1 addition & 1 deletion Parse/src/main/java/com/parse/ParseStethoInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public ParseHttpResponse intercept(Chain chain) throws IOException {
new DefaultResponseHandler(stethoEventReporter, requestId)
);
if (responseStream != null) {
response = response.newBuilder()
response = new ParseHttpResponse.Builder(response)
.setContent(responseStream)
.build();
}
Expand Down
36 changes: 36 additions & 0 deletions Parse/src/test/java/com/parse/ParseHttpRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

public class ParseHttpRequestTest {
Expand Down Expand Up @@ -80,4 +82,38 @@ public void testParseHttpRequestBuilderInitialization() throws IOException {
assertEquals(contentType, bodyAgain.getContentType());
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
}

@Test
public void testParseHttpRequestBuildWithParseHttpRequest() throws IOException {
String url = "www.parse.com";
ParseHttpRequest.Method method = ParseHttpRequest.Method.POST;
Map<String, String> headers = new HashMap<>();
String name = "name";
String value = "value";
headers.put(name, value);

String content = "content";
String contentType = "application/json";
ParseByteArrayHttpBody body = new ParseByteArrayHttpBody(content, contentType);

ParseHttpRequest request = new ParseHttpRequest.Builder()
.setUrl(url)
.addHeader(name, value)
.setMethod(method)
.setBody(body)
.build();

String newURL = "www.api.parse.com";
ParseHttpRequest newRequest = new ParseHttpRequest.Builder(request)
.setUrl(newURL)
.build();

assertEquals(newURL, newRequest.getUrl());
assertEquals(method.toString(), newRequest.getMethod().toString());
assertEquals(1, newRequest.getAllHeaders().size());
assertEquals(value, newRequest.getHeader(name));
ParseHttpBody bodyAgain = newRequest.getBody();
assertEquals(contentType, bodyAgain.getContentType());
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(body.getContent()));
}
}
4 changes: 2 additions & 2 deletions Parse/src/test/java/com/parse/ParseHttpResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testParseHttpResponseDefaults() throws IOException {
assertNull(response.getContentType());
assertNull(response.getReasonPhrase());
assertEquals(0, response.getStatusCode());
assertEquals(0, response.getTotalSize());
assertEquals(-1, response.getTotalSize());
assertEquals(0, response.getAllHeaders().size());
assertNull(response.getHeader("test"));
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public void testParseHttpResponseBuildWithParseHttpResponse() throws IOException
.build();

String newReasonPhrase = "Failed";
ParseHttpResponse newResponse = response.newBuilder()
ParseHttpResponse newResponse = new ParseHttpResponse.Builder(response)
.setReasonPhrase(newReasonPhrase)
.build();

Expand Down