Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
93a7613b
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
93a7613b
编写于
3月 03, 2015
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Pass through headers into http fetchers.
Fixes #198.
上级
a60bc512
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
89 addition
and
16 deletion
+89
-16
integration/okhttp/src/main/java/com/bumptech/glide/integration/okhttp/OkHttpStreamFetcher.java
...umptech/glide/integration/okhttp/OkHttpStreamFetcher.java
+7
-4
integration/volley/src/androidTest/java/com/bumptech/glide/integration/volley/VolleyStreamFetcherServerTest.java
...ide/integration/volley/VolleyStreamFetcherServerTest.java
+25
-1
integration/volley/src/main/java/com/bumptech/glide/integration/volley/VolleyRequestFactory.java
...mptech/glide/integration/volley/VolleyRequestFactory.java
+3
-1
integration/volley/src/main/java/com/bumptech/glide/integration/volley/VolleyStreamFetcher.java
...umptech/glide/integration/volley/VolleyStreamFetcher.java
+21
-6
library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherServerTest.java
...om/bumptech/glide/load/data/HttpUrlFetcherServerTest.java
+25
-1
library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
...ain/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
+8
-3
未找到文件。
integration/okhttp/src/main/java/com/bumptech/glide/integration/okhttp/OkHttpStreamFetcher.java
浏览文件 @
93a7613b
...
...
@@ -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
()
...
...
integration/volley/src/androidTest/java/com/bumptech/glide/integration/volley/VolleyStreamFetcherServerTest.java
浏览文件 @
93a7613b
...
...
@@ -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. */
...
...
integration/volley/src/main/java/com/bumptech/glide/integration/volley/VolleyRequestFactory.java
浏览文件 @
93a7613b
...
...
@@ -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
);
}
integration/volley/src/main/java/com/bumptech/glide/integration/volley/VolleyStreamFetcher.java
浏览文件 @
93a7613b
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
...
...
library/src/androidTest/java/com/bumptech/glide/load/data/HttpUrlFetcherServerTest.java
浏览文件 @
93a7613b
...
...
@@ -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
));
}
}
library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java
浏览文件 @
93a7613b
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录