提交 93a7613b 编写于 作者: S Sam Judd

Pass through headers into http fetchers.

Fixes #198.
上级 a60bc512
......@@ -8,6 +8,7 @@ import com.squareup.okhttp.Request;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
/**
* Fetches an {@link InputStream} using the okhttp library.
......@@ -15,7 +16,6 @@ import java.io.InputStream;
public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private final OkHttpClient client;
private final GlideUrl url;
private volatile Request request;
private InputStream stream;
public OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) {
......@@ -25,9 +25,12 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
@Override
public InputStream loadData(Priority priority) throws Exception {
request = new Request.Builder()
.url(url.toStringUrl())
.build();
Request.Builder requestBuilder = new Request.Builder()
.url(url.toStringUrl());
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue());
}
Request request = requestBuilder.build();
stream = client.newCall(request)
.execute()
......
......@@ -3,6 +3,8 @@ package com.bumptech.glide.integration.volley;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.os.SystemClock;
......@@ -14,6 +16,7 @@ import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.Headers;
import com.bumptech.glide.testutil.TestUtil;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
......@@ -33,6 +36,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
/**
......@@ -182,7 +187,26 @@ public class VolleyStreamFetcherServerTest {
}
}
@Test
public void testAppliesHeadersInGlideUrl() throws Exception {
mockWebServer.enqueue(new MockResponse().setResponseCode(200));
String headerField = "field";
String headerValue = "value";
Map<String, String> headersMap = new HashMap<String, String>();
headersMap.put(headerField, headerValue);
Headers headers = mock(Headers.class);
when(headers.getHeaders()).thenReturn(headersMap);
getFetcher(headers).loadData(Priority.HIGH);
assertThat(mockWebServer.takeRequest().getHeader(headerField)).isEqualTo(headerValue);
}
private DataFetcher<InputStream> getFetcher() {
return getFetcher(Headers.NONE);
}
private DataFetcher<InputStream> getFetcher(Headers headers) {
URL url = mockWebServer.getUrl(DEFAULT_PATH);
VolleyRequestFuture<InputStream> requestFuture = new VolleyRequestFuture<InputStream>() {
@Override
......@@ -197,7 +221,7 @@ public class VolleyStreamFetcherServerTest {
return super.get();
}
};
return new VolleyStreamFetcher(requestQueue, new GlideUrl(url.toString()), requestFuture);
return new VolleyStreamFetcher(requestQueue, new GlideUrl(url.toString(), headers), requestFuture);
}
/** A shadow clock that doesn't rely on running on an Android thread with a Looper. */
......
......@@ -4,6 +4,7 @@ import com.android.volley.Request;
import com.android.volley.Request.Priority;
import java.io.InputStream;
import java.util.Map;
/**
* Used to construct a custom Volley request, such as for authentication header decoration.
......@@ -14,6 +15,7 @@ public interface VolleyRequestFactory {
* Returns a Volley request for the given image url. The given future should be set as a
* listener or called when the request completes.
*/
Request<byte[]> create(String url, VolleyRequestFuture<InputStream> future, Priority priority);
Request<byte[]> create(String url, VolleyRequestFuture<InputStream> future, Priority priority,
Map<String, String> headers);
}
package com.bumptech.glide.integration.volley;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
......@@ -11,6 +12,8 @@ import com.bumptech.glide.load.model.GlideUrl;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
/**
* A DataFetcher backed by volley for fetching images via http.
......@@ -21,8 +24,8 @@ public class VolleyStreamFetcher implements DataFetcher<InputStream> {
@Override
public Request<byte[]> create(
String url, VolleyRequestFuture<InputStream> future,
Request.Priority priority) {
return new GlideRequest(url, future, priority);
Request.Priority priority, Map<String, String> headers) {
return new GlideRequest(url, future, priority, headers);
}
};
......@@ -58,7 +61,7 @@ public class VolleyStreamFetcher implements DataFetcher<InputStream> {
// Make sure the string url safely encodes non ascii characters.
String stringUrl = url.toStringUrl();
Request<byte[]> request = requestFactory.create(
stringUrl, requestFuture, glideToVolleyPriority(priority));
stringUrl, requestFuture, glideToVolleyPriority(priority), url.getHeaders());
requestFuture.setRequest(requestQueue.add(request));
......@@ -100,11 +103,23 @@ public class VolleyStreamFetcher implements DataFetcher<InputStream> {
private static class GlideRequest extends Request<byte[]> {
private final VolleyRequestFuture<InputStream> future;
private final Priority priority;
private final Map<String, String> headers;
public GlideRequest(String url, VolleyRequestFuture<InputStream> future, Priority priority) {
super(Method.GET, url, future);
this.future = future;
this.priority = priority;
this(url, future, priority, Collections.<String, String>emptyMap());
}
public GlideRequest(String url, VolleyRequestFuture<InputStream> future, Priority priority,
Map<String, String> headers) {
super(Method.GET, url, future);
this.future = future;
this.priority = priority;
this.headers = headers;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
@Override
......
......@@ -3,9 +3,12 @@ package com.bumptech.glide.load.data;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.Headers;
import com.bumptech.glide.testutil.TestUtil;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
......@@ -23,6 +26,8 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
......@@ -210,8 +215,27 @@ public class HttpUrlFetcherServerTest {
}
}
@Test
public void testAppliesHeadersInGlideUrl() throws Exception {
mockWebServer.enqueue(new MockResponse().setResponseCode(200));
String headerField = "field";
String headerValue = "value";
Map<String, String> headersMap = new HashMap<String, String>();
headersMap.put(headerField, headerValue);
Headers headers = mock(Headers.class);
when(headers.getHeaders()).thenReturn(headersMap);
getFetcher(headers).loadData(Priority.HIGH);
assertThat(mockWebServer.takeRequest().getHeader(headerField)).isEqualTo(headerValue);
}
private HttpUrlFetcher getFetcher() {
return getFetcher(Headers.NONE);
}
private HttpUrlFetcher getFetcher(Headers headers) {
URL url = mockWebServer.getUrl(DEFAULT_PATH);
return new HttpUrlFetcher(new GlideUrl(url));
return new HttpUrlFetcher(new GlideUrl(url, headers));
}
}
......@@ -10,6 +10,7 @@ import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
/**
* A DataFetcher that retrieves an {@link java.io.InputStream} for a Url.
......@@ -37,10 +38,11 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
@Override
public InputStream loadData(Priority priority) throws Exception {
return loadDataWithRedirects(glideUrl.toURL(), 0 /*redirects*/, null /*lastUrl*/);
return loadDataWithRedirects(glideUrl.toURL(), 0 /*redirects*/, null /*lastUrl*/, glideUrl.getHeaders());
}
private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl) throws IOException {
private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl, Map<String, String> headers)
throws IOException {
if (redirects >= MAXIMUM_REDIRECTS) {
throw new IOException("Too many (> " + MAXIMUM_REDIRECTS + ") redirects!");
} else {
......@@ -55,6 +57,9 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
}
}
urlConnection = connectionFactory.build(url);
for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue());
}
urlConnection.setConnectTimeout(2500);
urlConnection.setReadTimeout(2500);
urlConnection.setUseCaches(false);
......@@ -75,7 +80,7 @@ public class HttpUrlFetcher implements DataFetcher<InputStream> {
throw new IOException("Received empty or null redirect url");
}
URL redirectUrl = new URL(url, redirectUrlString);
return loadDataWithRedirects(redirectUrl, redirects + 1, url);
return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers);
} else {
if (statusCode == -1) {
throw new IOException("Unable to retrieve response code from HttpUrlConnection.");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册