Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
e98738d8
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e98738d8
编写于
3月 03, 2016
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish UriTemplateHandler related classes
上级
44c32128
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
51 addition
and
44 deletion
+51
-44
spring-web/src/main/java/org/springframework/web/client/AsyncRestTemplate.java
...ava/org/springframework/web/client/AsyncRestTemplate.java
+3
-2
spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
...ain/java/org/springframework/web/client/RestTemplate.java
+6
-2
spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java
...g/springframework/web/util/DefaultUriTemplateHandler.java
+21
-14
spring-web/src/main/java/org/springframework/web/util/UriTemplateHandler.java
...java/org/springframework/web/util/UriTemplateHandler.java
+14
-6
spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java
...ingframework/web/util/DefaultUriTemplateHandlerTests.java
+7
-20
未找到文件。
spring-web/src/main/java/org/springframework/web/client/AsyncRestTemplate.java
浏览文件 @
e98738d8
...
...
@@ -152,8 +152,9 @@ public class AsyncRestTemplate extends InterceptingAsyncHttpAccessor implements
}
/**
* Set a custom {@link UriTemplateHandler} for expanding URI templates.
* <p>By default, RestTemplate uses {@link DefaultUriTemplateHandler}.
* This property has the same purpose as the corresponding property on the
* {@code RestTemplate}. For more details see
* {@link RestTemplate#setUriTemplateHandler}.
* @param handler the URI template handler to use
*/
public
void
setUriTemplateHandler
(
UriTemplateHandler
handler
)
{
...
...
spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
浏览文件 @
e98738d8
...
...
@@ -237,8 +237,12 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
}
/**
* Set a custom {@link UriTemplateHandler} for expanding URI templates.
* <p>By default, RestTemplate uses {@link DefaultUriTemplateHandler}.
* Configure the {@link UriTemplateHandler} to use to expand URI templates.
* By default the {@link DefaultUriTemplateHandler} is used which relies on
* Spring's URI template support and exposes several useful properties that
* customize its behavior for encoding and for prepending a common base URL.
* An alternative implementation may be used to plug an external URI
* template library.
* @param handler the URI template handler to use
*/
public
void
setUriTemplateHandler
(
UriTemplateHandler
handler
)
{
...
...
spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java
浏览文件 @
e98738d8
...
...
@@ -24,8 +24,8 @@ import java.util.Map;
import
org.springframework.util.Assert
;
/**
* Default implementation of {@link UriTemplateHandler} that
relies on
* {@link UriComponentsBuilder}
internally
.
* Default implementation of {@link UriTemplateHandler} that
uses
* {@link UriComponentsBuilder}
to expand and encode variables
.
*
* @author Rossen Stoyanchev
* @since 4.2
...
...
@@ -38,14 +38,10 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
/**
* Configure a base URL to prepend URI templates with. The base URL should
* have a scheme and host but may also contain a port and a partial path.
* Individual URI templates then may provide the remaining part of the URL
* including additional path, query and fragment.
* <p><strong>Note: </strong>Individual URI templates are expanded and
* encoded before being appended to the base URL. Therefore the base URL is
* expected to be fully expanded and encoded, which can be done with the help
* of {@link UriComponentsBuilder}.
* Configure a base URL to prepend URI templates with. The base URL must
* have a scheme and host but may optionally contain a port and a path.
* The base URL must be fully expanded and encoded which can be done via
* {@link UriComponentsBuilder}.
* @param baseUrl the base URL.
*/
public
void
setBaseUrl
(
String
baseUrl
)
{
...
...
@@ -68,10 +64,10 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
/**
* Whether to parse the path of a URI template string into path segments.
* <p>If set to {@code true} the
path of parsed URI templates is
decomposed
* into path segments
so that URI variables expanded into the path are
*
treated according to path segment encoding rules. In effect that means the
* "/" character
i
s percent encoded.
* <p>If set to {@code true} the
URI template path is immediately
decomposed
* into path segments
any URI variables expanded into it are then subject to
*
path segment encoding rules. In effect URI variables in the path have any
* "/" characters percent encoded.
* <p>By default this is set to {@code false} in which case the path is kept
* as a full path and expanded URI variables will preserve "/" characters.
* @param parsePath whether to parse the path into path segments
...
...
@@ -102,6 +98,11 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
return
insertBaseUrl
(
url
);
}
/**
* Create a {@code UriComponentsBuilder} from the UriTemplate string. The
* default implementation also parses the path into path segments if
* {@link #setParsePath parsePath} is enabled.
*/
protected
UriComponentsBuilder
initUriComponentsBuilder
(
String
uriTemplate
)
{
UriComponentsBuilder
builder
=
UriComponentsBuilder
.
fromUriString
(
uriTemplate
);
if
(
shouldParsePath
())
{
...
...
@@ -114,6 +115,12 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
return
builder
;
}
/**
* Invoked after the URI template has been expanded and encoded to prepend
* the configured {@link #setBaseUrl(String) baseUrl} if any.
* @param uriComponents the expanded and encoded URI
* @return the final URI
*/
protected
URI
insertBaseUrl
(
UriComponents
uriComponents
)
{
if
(
getBaseUrl
()
==
null
||
uriComponents
.
getHost
()
!=
null
)
{
return
uriComponents
.
toUri
();
...
...
spring-web/src/main/java/org/springframework/web/util/UriTemplateHandler.java
浏览文件 @
e98738d8
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
6
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -20,15 +20,23 @@ import java.net.URI;
import
java.util.Map
;
/**
* A strategy for expanding a URI template with URI variables into a {@link URI}.
* Strategy for expanding a URI template with full control over the URI template
* syntax and the encoding of variables. Also a convenient central point for
* pre-processing all URI templates for example to insert a common base path.
*
* <p>Supported as a property on the {@code RestTemplate} as well as the
* {@code AsyncRestTemplate}. The {@link DefaultUriTemplateHandler} is built
* on Spring's URI template support via {@link UriComponentsBuilder}. An
* alternative implementation may be used to plug external URI template libraries.
*
* @author Rossen Stoyanchev
* @since 4.2
* @see org.springframework.web.client.RestTemplate#setUriTemplateHandler
*/
public
interface
UriTemplateHandler
{
/**
* Expand the give
URI template with
a map of URI variables.
* Expand the give
n URI template from
a map of URI variables.
* @param uriTemplate the URI template string
* @param uriVariables the URI variables
* @return the resulting URI
...
...
@@ -36,11 +44,11 @@ public interface UriTemplateHandler {
URI
expand
(
String
uriTemplate
,
Map
<
String
,
?>
uriVariables
);
/**
* Expand the give
URI template with an array of URI variable valu
es.
* Expand the give
n URI template from an array of URI variabl
es.
* @param uriTemplate the URI template string
* @param uriVariable
Value
s the URI variable values
* @param uriVariables the URI variable values
* @return the resulting URI
*/
URI
expand
(
String
uriTemplate
,
Object
...
uriVariable
Value
s
);
URI
expand
(
String
uriTemplate
,
Object
...
uriVariables
);
}
spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java
浏览文件 @
e98738d8
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
6
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -15,35 +15,27 @@
*/
package
org.springframework.web.util
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
java.net.URI
;
import
java.util.HashMap
;
import
java.util.Map
;
import
org.junit.Before
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
/**
* Unit tests for {@link DefaultUriTemplateHandler}.
* @author Rossen Stoyanchev
*/
public
class
DefaultUriTemplateHandlerTests
{
private
DefaultUriTemplateHandler
handler
;
@Before
public
void
setUp
()
throws
Exception
{
this
.
handler
=
new
DefaultUriTemplateHandler
();
}
private
final
DefaultUriTemplateHandler
handler
=
new
DefaultUriTemplateHandler
();
@Test
public
void
baseUrl
()
throws
Exception
{
this
.
handler
.
setBaseUrl
(
"http://localhost:8080"
);
URI
actual
=
this
.
handler
.
expand
(
"/myapiresource"
);
URI
expected
=
new
URI
(
"http://localhost:8080/myapiresource"
);
assertEquals
(
expected
,
actual
);
}
...
...
@@ -52,35 +44,30 @@ public class DefaultUriTemplateHandlerTests {
public
void
baseUrlWithPartialPath
()
throws
Exception
{
this
.
handler
.
setBaseUrl
(
"http://localhost:8080/context"
);
URI
actual
=
this
.
handler
.
expand
(
"/myapiresource"
);
URI
expected
=
new
URI
(
"http://localhost:8080/context/myapiresource"
);
assertEquals
(
expected
,
actual
);
}
@Test
public
void
expandWithFullPath
()
throws
Exception
{
Map
<
String
,
String
>
vars
=
new
HashMap
<
String
,
String
>(
2
);
Map
<
String
,
String
>
vars
=
new
HashMap
<>(
2
);
vars
.
put
(
"hotel"
,
"1"
);
vars
.
put
(
"publicpath"
,
"pics/logo.png"
);
String
template
=
"http://example.com/hotels/{hotel}/pic/{publicpath}"
;
URI
actual
=
this
.
handler
.
expand
(
template
,
vars
);
URI
expected
=
new
URI
(
"http://example.com/hotels/1/pic/pics/logo.png"
);
assertEquals
(
expected
,
actual
);
}
@Test
public
void
expandWithFullPathAndParsePathEnabled
()
throws
Exception
{
Map
<
String
,
String
>
vars
=
new
HashMap
<
String
,
String
>(
2
);
this
.
handler
.
setParsePath
(
true
);
Map
<
String
,
String
>
vars
=
new
HashMap
<>(
2
);
vars
.
put
(
"hotel"
,
"1"
);
vars
.
put
(
"publicpath"
,
"pics/logo.png"
);
vars
.
put
(
"scale"
,
"150x150"
);
String
template
=
"http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}"
;
this
.
handler
.
setParsePath
(
true
);
URI
actual
=
this
.
handler
.
expand
(
template
,
vars
);
URI
expected
=
new
URI
(
"http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150"
);
assertEquals
(
expected
,
actual
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录