-
Notifications
You must be signed in to change notification settings - Fork 754
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
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/com/android/volley/toolbox/AdaptedHttpStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
95
src/main/java/com/android/volley/toolbox/BaseHttpStack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.