Skip to content
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

Deprecate Volley's use of Apache HTTP. #75

Merged
merged 3 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 3 additions & 4 deletions src/main/java/com/android/volley/NetworkResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.android.volley;

import org.apache.http.HttpStatus;

import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.Map;

Expand Down Expand Up @@ -48,11 +47,11 @@ public NetworkResponse(int statusCode, byte[] data, Map<String, String> headers,
}

public NetworkResponse(byte[] data) {
this(HttpStatus.SC_OK, data, Collections.<String, String>emptyMap(), false, 0);
this(HttpURLConnection.HTTP_OK, data, Collections.<String, String>emptyMap(), false, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you want to consider a static import for the codes to cut down on the verbosity (here and elsewhere)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I always thought this was against the style guide, but it doesn't look like it. Well, I guess I personally prefer having the verbosity since I feel like imports are generally not noticed when looking at a file and the source of these constants is kind of relevant, though I wouldn't push back if a static import were used elsewhere. Going to just leave this as is to avoid churn.

}

public NetworkResponse(byte[] data, Map<String, String> headers) {
this(HttpStatus.SC_OK, data, headers, false, 0);
this(HttpURLConnection.HTTP_OK, data, headers, false, 0);
}

/** The HTTP status code. */
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/com/android/volley/toolbox/AdaptedHttpStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.volley.toolbox;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;

import org.apache.http.Header;
import org.apache.http.conn.ConnectTimeoutException;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
* {@link BaseHttpStack} implementation wrapping a {@link HttpStack}.
*
* <p>{@link BasicNetwork} uses this if it is provided a {@link HttpStack} at construction time,
* allowing it to have one implementation based atop {@link BaseHttpStack}.
*/
@SuppressWarnings("deprecation")
class AdaptedHttpStack extends BaseHttpStack {

private final HttpStack mHttpStack;

AdaptedHttpStack(HttpStack httpStack) {
mHttpStack = httpStack;
}

@Override
public HttpResponse executeRequest(
Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
org.apache.http.HttpResponse apacheResp;
try {
apacheResp = mHttpStack.performRequest(request, additionalHeaders);
} catch (ConnectTimeoutException e) {
// BasicNetwork won't know that this exception should be retried like a timeout, since
// it's an Apache-specific error, so wrap it in a standard timeout exception.
throw new SocketTimeoutException(e.getMessage());
}

int statusCode = apacheResp.getStatusLine().getStatusCode();

Header[] headers = apacheResp.getAllHeaders();
Map<String, List<String>> headerMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
for (Header header : headers) {
List<String> valueList = headerMap.get(header.getName());
if (valueList == null) {
valueList = new ArrayList<>();
headerMap.put(header.getName(), valueList);
}
valueList.add(header.getValue());
}

if (apacheResp.getEntity() == null) {
return new HttpResponse(statusCode, headerMap);
}

long contentLength = apacheResp.getEntity().getContentLength();
if ((int) contentLength != contentLength) {
throw new IOException("Response too large: " + contentLength);
}

return new HttpResponse(
statusCode,
headerMap,
(int) apacheResp.getEntity().getContentLength(),
apacheResp.getEntity().getContent());
}
}
95 changes: 95 additions & 0 deletions src/main/java/com/android/volley/toolbox/BaseHttpStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.volley.toolbox;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;

import org.apache.http.Header;
import org.apache.http.ProtocolVersion;
import org.apache.http.StatusLine;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;

import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/** An HTTP stack abstraction. */
@SuppressWarnings("deprecation") // for HttpStack
public abstract class BaseHttpStack implements HttpStack {

/**
* Performs an HTTP request with the given parameters.
*
* <p>A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise,
* and the Content-Type header is set to request.getPostBodyContentType().
*
* @param request the request to perform
* @param additionalHeaders additional headers to be sent together with
* {@link Request#getHeaders()}
* @return the {@link HttpResponse}
* @throws SocketTimeoutException if the request times out
* @throws IOException if another I/O error occurs during the request
* @throws AuthFailureError if an authentication failure occurs during the request
*/
public abstract HttpResponse executeRequest(
Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError;

/**
* @deprecated use {@link #executeRequest} instead to avoid a dependency on the deprecated
* Apache HTTP library. Nothing in Volley's own source calls this method. However, since
* {@link BasicNetwork#mHttpStack} is exposed to subclasses, we provide this implementation in
* case legacy client apps are dependent on that field. This method may be removed in a future
* release of Volley.
*/
@Deprecated
@Override
public final org.apache.http.HttpResponse performRequest(
Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
HttpResponse response = executeRequest(request, additionalHeaders);

ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
StatusLine statusLine = new BasicStatusLine(
protocolVersion, response.getStatusCode(), "" /* reasonPhrase */);
BasicHttpResponse apacheResponse = new BasicHttpResponse(statusLine);

List<Header> headers = new ArrayList<>();
for (Map.Entry<String, List<String>> entry : response.getHeaders().entrySet()) {
for (String value : entry.getValue()) {
headers.add(new BasicHeader(entry.getKey(), value));
}
}
apacheResponse.setHeaders(headers.toArray(new Header[0]));
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you allocate the correct size up front, headers.toArray(new Header[headers.size()]), then you can avoid an unnecessary allocation of an array.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.


InputStream responseStream = response.getContent();
if (responseStream != null) {
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(responseStream);
entity.setContentLength(response.getContentLength());
apacheResponse.setEntity(entity);
}

return apacheResponse;
}
}
Loading