Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
1ba0625c
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,体验更适合开发者的 AI 搜索 >>
提交
1ba0625c
编写于
5月 20, 2015
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add baseUrl to DefaultUriTemplateHandler
Issue: SPR-13035
上级
01d1e71d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
77 addition
and
8 deletion
+77
-8
spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java
...g/springframework/web/util/DefaultUriTemplateHandler.java
+56
-5
spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java
...ingframework/web/util/DefaultUriTemplateHandlerTests.java
+21
-3
未找到文件。
spring-web/src/main/java/org/springframework/web/util/DefaultUriTemplateHandler.java
浏览文件 @
1ba0625c
...
...
@@ -16,9 +16,12 @@
package
org.springframework.web.util
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.util.Assert
;
/**
* Default implementation of {@link UriTemplateHandler} that relies on
* {@link UriComponentsBuilder} internally.
...
...
@@ -28,9 +31,42 @@ import java.util.Map;
*/
public
class
DefaultUriTemplateHandler
implements
UriTemplateHandler
{
private
String
baseUrl
;
private
boolean
parsePath
;
/**
* 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}.
*
* @param baseUrl the base URL.
*/
public
void
setBaseUrl
(
String
baseUrl
)
{
if
(
baseUrl
!=
null
)
{
UriComponents
uriComponents
=
UriComponentsBuilder
.
fromUriString
(
baseUrl
).
build
();
Assert
.
hasText
(
uriComponents
.
getScheme
(),
"'baseUrl' must have a scheme"
);
Assert
.
hasText
(
uriComponents
.
getHost
(),
"'baseUrl' must have a host"
);
Assert
.
isNull
(
uriComponents
.
getQuery
(),
"'baseUrl' cannot have a query"
);
Assert
.
isNull
(
uriComponents
.
getFragment
(),
"'baseUrl' cannot have a fragment"
);
}
this
.
baseUrl
=
baseUrl
;
}
/**
* Return the configured base URL.
*/
public
String
getBaseUrl
()
{
return
this
.
baseUrl
;
}
/**
* 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
...
...
@@ -55,17 +91,19 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
@Override
public
URI
expand
(
String
uriTemplate
,
Map
<
String
,
?>
uriVariables
)
{
UriComponentsBuilder
builder
=
initBuilder
(
uriTemplate
);
return
builder
.
build
().
expand
(
uriVariables
).
encode
().
toUri
();
UriComponentsBuilder
uriComponentsBuilder
=
initUriComponentsBuilder
(
uriTemplate
);
UriComponents
uriComponents
=
uriComponentsBuilder
.
build
().
expand
(
uriVariables
).
encode
();
return
insertBaseUrl
(
uriComponents
);
}
@Override
public
URI
expand
(
String
uriTemplate
,
Object
...
uriVariableValues
)
{
UriComponentsBuilder
builder
=
initBuilder
(
uriTemplate
);
return
builder
.
build
().
expand
(
uriVariableValues
).
encode
().
toUri
();
UriComponentsBuilder
uriComponentsBuilder
=
initUriComponentsBuilder
(
uriTemplate
);
UriComponents
uriComponents
=
uriComponentsBuilder
.
build
().
expand
(
uriVariableValues
).
encode
();
return
insertBaseUrl
(
uriComponents
);
}
protected
UriComponentsBuilder
initBuilder
(
String
uriTemplate
)
{
protected
UriComponentsBuilder
init
UriComponents
Builder
(
String
uriTemplate
)
{
UriComponentsBuilder
builder
=
UriComponentsBuilder
.
fromUriString
(
uriTemplate
);
if
(
shouldParsePath
())
{
List
<
String
>
pathSegments
=
builder
.
build
().
getPathSegments
();
...
...
@@ -77,4 +115,17 @@ public class DefaultUriTemplateHandler implements UriTemplateHandler {
return
builder
;
}
protected
URI
insertBaseUrl
(
UriComponents
uriComponents
)
{
if
(
getBaseUrl
()
==
null
||
uriComponents
.
getHost
()
!=
null
)
{
return
uriComponents
.
toUri
();
}
String
url
=
getBaseUrl
()
+
uriComponents
.
toUriString
();
try
{
return
new
URI
(
url
);
}
catch
(
URISyntaxException
ex
)
{
throw
new
IllegalArgumentException
(
"Invalid URL after inserting base URL: "
+
url
,
ex
);
}
}
}
spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java
浏览文件 @
1ba0625c
...
...
@@ -39,6 +39,24 @@ public class DefaultUriTemplateHandlerTests {
}
@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
);
}
@Test
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
);
...
...
@@ -49,11 +67,11 @@ public class DefaultUriTemplateHandlerTests {
URI
actual
=
this
.
handler
.
expand
(
template
,
vars
);
URI
expected
=
new
URI
(
"http://example.com/hotels/1/pic/pics/logo.png"
);
assertEquals
(
"Invalid expanded template"
,
expected
,
actual
);
assertEquals
(
expected
,
actual
);
}
@Test
public
void
expandWithFullPath
ParsedIntoPathSegments
()
throws
Exception
{
public
void
expandWithFullPath
AndParsePathEnabled
()
throws
Exception
{
Map
<
String
,
String
>
vars
=
new
HashMap
<
String
,
String
>(
2
);
vars
.
put
(
"hotel"
,
"1"
);
vars
.
put
(
"publicpath"
,
"pics/logo.png"
);
...
...
@@ -64,7 +82,7 @@ public class DefaultUriTemplateHandlerTests {
URI
actual
=
this
.
handler
.
expand
(
template
,
vars
);
URI
expected
=
new
URI
(
"http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150"
);
assertEquals
(
"Invalid expanded template"
,
expected
,
actual
);
assertEquals
(
expected
,
actual
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录