Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
60d2fdfc
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,发现更多精彩内容 >>
提交
60d2fdfc
编写于
11月 13, 2009
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6188 - UriTemplate: Insufficient handling of characters that need to be escaped.
上级
8be36faf
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
454 addition
and
152 deletion
+454
-152
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/UrlTag.java
...ain/java/org/springframework/web/servlet/tags/UrlTag.java
+22
-31
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java
...ava/org/springframework/web/servlet/tags/UrlTagTests.java
+0
-20
org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
...c/main/java/org/springframework/web/util/UriTemplate.java
+7
-42
org.springframework.web/src/main/java/org/springframework/web/util/UriUtils.java
.../src/main/java/org/springframework/web/util/UriUtils.java
+308
-31
org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java
...ingframework/web/client/RestTemplateIntegrationTests.java
+28
-9
org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java
...t/java/org/springframework/web/util/UriTemplateTests.java
+3
-6
org.springframework.web/src/test/java/org/springframework/web/util/UriUtilsTest.java
.../test/java/org/springframework/web/util/UriUtilsTest.java
+86
-13
未找到文件。
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/UrlTag.java
浏览文件 @
60d2fdfc
...
...
@@ -34,6 +34,7 @@ import org.springframework.web.util.HtmlUtils;
import
org.springframework.web.util.JavaScriptUtils
;
import
org.springframework.web.util.TagUtils
;
import
org.springframework.web.util.UriUtils
;
import
org.springframework.util.StringUtils
;
/**
* JSP tag for creating URLs. Modeled after the JSTL c:url tag with backwards
...
...
@@ -236,23 +237,29 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
* @return the query string
* @throws JspException
*/
protected
String
createQueryString
(
List
<
Param
>
params
,
Set
<
String
>
usedParams
,
boolean
includeQueryStringDelimiter
)
protected
String
createQueryString
(
List
<
Param
>
params
,
Set
<
String
>
usedParams
,
boolean
includeQueryStringDelimiter
)
throws
JspException
{
String
encoding
=
pageContext
.
getResponse
().
getCharacterEncoding
();
StringBuilder
qs
=
new
StringBuilder
();
for
(
Param
param
:
params
)
{
if
(!
usedParams
.
contains
(
param
.
getName
())
&&
param
.
getName
()
!=
null
&&
!
""
.
equals
(
param
.
getName
()))
{
if
(!
usedParams
.
contains
(
param
.
getName
())
&&
StringUtils
.
hasLength
(
param
.
getName
()))
{
if
(
includeQueryStringDelimiter
&&
qs
.
length
()
==
0
)
{
qs
.
append
(
"?"
);
}
else
{
qs
.
append
(
"&"
);
}
qs
.
append
(
urlEncode
(
param
.
getName
()));
if
(
param
.
getValue
()
!=
null
)
{
qs
.
append
(
"="
);
qs
.
append
(
urlEncode
(
param
.
getValue
()));
try
{
qs
.
append
(
UriUtils
.
encodeQueryParam
(
param
.
getName
(),
encoding
));
if
(
param
.
getValue
()
!=
null
)
{
qs
.
append
(
"="
);
qs
.
append
(
UriUtils
.
encodeQueryParam
(
param
.
getValue
(),
encoding
));
}
}
catch
(
UnsupportedEncodingException
ex
)
{
throw
new
JspException
(
ex
);
}
}
}
...
...
@@ -271,39 +278,23 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
*/
protected
String
replaceUriTemplateParams
(
String
uri
,
List
<
Param
>
params
,
Set
<
String
>
usedParams
)
throws
JspException
{
String
encoding
=
pageContext
.
getResponse
().
getCharacterEncoding
();
for
(
Param
param
:
params
)
{
String
template
=
URL_TEMPLATE_DELIMITER_PREFIX
+
param
.
getName
()
+
URL_TEMPLATE_DELIMITER_SUFFIX
;
if
(
uri
.
contains
(
template
))
{
usedParams
.
add
(
param
.
getName
());
uri
=
uri
.
replace
(
template
,
urlEncode
(
param
.
getValue
()));
try
{
uri
=
uri
.
replace
(
template
,
UriUtils
.
encodePath
(
param
.
getValue
(),
encoding
));
}
catch
(
UnsupportedEncodingException
ex
)
{
throw
new
JspException
(
ex
);
}
}
}
return
uri
;
}
/**
* URL-encode the provided String using the character encoding for the response.
* <p>This method will <strong>not</strong> URL-encode according to the
* <code>application/x-www-form-urlencoded</code> MIME format. Spaces will
* encoded as regular character instead of <code>+</code>. In <code>UTF-8</code>
* a space encodes to <code>%20</code>.
* @param value the value to encode
* @return the URL encoded value
* @throws JspException if the character encoding is invalid
*/
protected
String
urlEncode
(
String
value
)
throws
JspException
{
if
(
value
==
null
)
{
return
null
;
}
try
{
String
encoding
=
pageContext
.
getResponse
().
getCharacterEncoding
();
return
UriUtils
.
encode
(
value
,
encoding
);
}
catch
(
UnsupportedEncodingException
ex
)
{
throw
new
JspException
(
ex
);
}
}
/**
* Internal enum that classifies URLs by type.
*/
...
...
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/tags/UrlTagTests.java
浏览文件 @
60d2fdfc
...
...
@@ -550,26 +550,6 @@ public class UrlTagTests extends AbstractTagTests {
assertEquals
(
"url/path?foo=bar&name=value"
,
uri
);
}
public
void
testUrlEncode
()
throws
JspException
{
assertEquals
(
"my%20name%2Bis"
,
tag
.
urlEncode
(
"my name+is"
));
}
public
void
testUrlEncodeNull
()
throws
JspException
{
assertNull
(
tag
.
urlEncode
(
null
));
}
public
void
testUrlEncodeBadEncoding
()
{
context
.
getResponse
().
setCharacterEncoding
(
"bad encoding"
);
try
{
tag
.
urlEncode
(
"my name"
);
fail
(
"expected JspException"
);
}
catch
(
JspException
e
)
{
// we want this
}
}
public
void
testJspWriterOutput
()
{
// TODO assert that the output to the JspWriter is the expected output
}
...
...
org.springframework.web/src/main/java/org/springframework/web/util/UriTemplate.java
浏览文件 @
60d2fdfc
...
...
@@ -25,6 +25,7 @@ import java.util.List;
import
java.util.Map
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.io.UnsupportedEncodingException
;
import
org.springframework.util.Assert
;
...
...
@@ -175,48 +176,12 @@ public class UriTemplate {
private
static
URI
encodeUri
(
String
uri
)
{
try
{
int
schemeIdx
=
uri
.
indexOf
(
':'
);
if
(
schemeIdx
==
-
1
)
{
int
queryIdx
=
uri
.
indexOf
(
'?'
);
if
(
queryIdx
==
-
1
)
{
int
fragmentIdx
=
uri
.
indexOf
(
'#'
);
if
(
fragmentIdx
==
-
1
)
{
return
new
URI
(
null
,
null
,
uri
,
null
);
}
else
{
String
path
=
uri
.
substring
(
0
,
fragmentIdx
);
String
fragment
=
uri
.
substring
(
fragmentIdx
+
1
);
return
new
URI
(
null
,
null
,
path
,
fragment
);
}
}
else
{
int
fragmentIdx
=
uri
.
indexOf
(
'#'
,
queryIdx
+
1
);
if
(
fragmentIdx
==
-
1
)
{
String
path
=
uri
.
substring
(
0
,
queryIdx
);
String
query
=
uri
.
substring
(
queryIdx
+
1
);
return
new
URI
(
null
,
null
,
path
,
query
,
null
);
}
else
{
String
path
=
uri
.
substring
(
0
,
queryIdx
);
String
query
=
uri
.
substring
(
queryIdx
+
1
,
fragmentIdx
);
String
fragment
=
uri
.
substring
(
fragmentIdx
+
1
);
return
new
URI
(
null
,
null
,
path
,
query
,
fragment
);
}
}
}
else
{
int
fragmentIdx
=
uri
.
indexOf
(
'#'
,
schemeIdx
+
1
);
String
scheme
=
uri
.
substring
(
0
,
schemeIdx
);
if
(
fragmentIdx
==
-
1
)
{
String
ssp
=
uri
.
substring
(
schemeIdx
+
1
);
return
new
URI
(
scheme
,
ssp
,
null
);
}
else
{
String
ssp
=
uri
.
substring
(
schemeIdx
+
1
,
fragmentIdx
);
String
fragment
=
uri
.
substring
(
fragmentIdx
+
1
);
return
new
URI
(
scheme
,
ssp
,
fragment
);
}
}
String
encoded
=
UriUtils
.
encodeUri
(
uri
,
"UTF-8"
);
return
new
URI
(
encoded
);
}
catch
(
UnsupportedEncodingException
ex
)
{
// should not happen, UTF-8 is always supported
throw
new
IllegalStateException
(
ex
);
}
catch
(
URISyntaxException
ex
)
{
throw
new
IllegalArgumentException
(
"Could not create URI from ["
+
uri
+
"]: "
+
ex
);
...
...
org.springframework.web/src/main/java/org/springframework/web/util/UriUtils.java
浏览文件 @
60d2fdfc
...
...
@@ -19,63 +19,340 @@ package org.springframework.web.util;
import
java.io.ByteArrayOutputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.util.BitSet
;
import
java.util.regex.Pattern
;
import
java.util.regex.Matcher
;
import
org.springframework.util.Assert
;
/**
* Utility class for URI encoding. Based on RFC 2396.
* Utility class for URI encoding and decoding based on RFC 3986. Offers encoding methods for
* the various URI components.
*
* <p>Effectively, the encoding and decoding methods in this class
* are similar to those found in {@link java.net.URLEncoder} and
* {@link java.net.URLDecoder}, except that the space character
* is encoded as {@code %20}, not {@code +}.
* <p>All {@code encode*(String, String} methods in this class operate in a similar way:
* <ul>
* <li>Valid characters for the specific URI component as defined in RFC 3986 stay the same.
* <li>All other characters are converted into one or more bytes in the given encoding scheme.
* Each of the resulting bytes is written as a hexadecimal string in the "<code>%<i>xy</i></code>" format.
* </ul>
*
* @author Arjen Poutsma
* @since 3.0
* @see <a href="http://www.ietf.org/rfc/rfc
2396.txt">RFC 239
6</a>
* @see <a href="http://www.ietf.org/rfc/rfc
3986.txt">RFC 398
6</a>
*/
public
abstract
class
UriUtils
{
private
static
final
BitSet
notEncoded
=
new
BitSet
(
256
);
private
static
final
BitSet
SCHEME
;
private
static
final
BitSet
USER_INFO
;
private
static
final
BitSet
HOST
;
private
static
final
BitSet
PORT
;
private
static
final
BitSet
PATH
;
private
static
final
BitSet
SEGMENT
;
private
static
final
BitSet
QUERY
;
private
static
final
BitSet
QUERY_PARAM
;
private
static
final
BitSet
FRAGMENT
;
private
static
final
String
SCHEME_PATTERN
=
"([^:/?#]+):"
;
private
static
final
String
USERINFO_PATTERN
=
"([^@]*)"
;
private
static
final
String
HOST_PATTERN
=
"([^/?#:]*)"
;
private
static
final
String
PORT_PATTERN
=
"(\\d*)"
;
private
static
final
String
PATH_PATTERN
=
"([^?#]*)"
;
private
static
final
String
QUERY_PATTERN
=
"([^#]*)"
;
private
static
final
String
FRAGMENT_PATTERN
=
"(.*)"
;
// Regex patterns that matches URIs. See RFC 3986, appendix B
private
static
final
Pattern
URI_PATTERN
=
Pattern
.
compile
(
"^("
+
SCHEME_PATTERN
+
")?"
+
"(//("
+
USERINFO_PATTERN
+
"@)?"
+
HOST_PATTERN
+
"(:"
+
PORT_PATTERN
+
")?"
+
")?"
+
PATH_PATTERN
+
"(\\?"
+
QUERY_PATTERN
+
")?"
+
"(#"
+
FRAGMENT_PATTERN
+
")?"
);
static
{
// variable names refer to RFC 3986, appendix A
BitSet
alpha
=
new
BitSet
(
256
);
for
(
int
i
=
'a'
;
i
<=
'z'
;
i
++)
{
notEncoded
.
set
(
i
);
alpha
.
set
(
i
);
}
for
(
int
i
=
'A'
;
i
<=
'Z'
;
i
++)
{
notEncoded
.
set
(
i
);
alpha
.
set
(
i
);
}
BitSet
digit
=
new
BitSet
(
256
);
for
(
int
i
=
'0'
;
i
<=
'9'
;
i
++)
{
notEncoded
.
set
(
i
);
digit
.
set
(
i
);
}
notEncoded
.
set
(
'-'
);
notEncoded
.
set
(
'_'
);
notEncoded
.
set
(
'.'
);
notEncoded
.
set
(
'*'
);
BitSet
gendelims
=
new
BitSet
(
256
);
gendelims
.
set
(
':'
);
gendelims
.
set
(
'/'
);
gendelims
.
set
(
'?'
);
gendelims
.
set
(
'#'
);
gendelims
.
set
(
'['
);
gendelims
.
set
(
']'
);
gendelims
.
set
(
'@'
);
BitSet
subdelims
=
new
BitSet
(
256
);
subdelims
.
set
(
'!'
);
subdelims
.
set
(
'$'
);
subdelims
.
set
(
'&'
);
subdelims
.
set
(
'\''
);
subdelims
.
set
(
'('
);
subdelims
.
set
(
')'
);
subdelims
.
set
(
'*'
);
subdelims
.
set
(
'+'
);
subdelims
.
set
(
','
);
subdelims
.
set
(
';'
);
subdelims
.
set
(
'='
);
BitSet
reserved
=
new
BitSet
(
256
);
reserved
.
or
(
gendelims
);
reserved
.
or
(
subdelims
);
BitSet
unreserved
=
new
BitSet
(
256
);
unreserved
.
or
(
alpha
);
unreserved
.
or
(
digit
);
unreserved
.
set
(
'-'
);
unreserved
.
set
(
'.'
);
unreserved
.
set
(
'_'
);
unreserved
.
set
(
'~'
);
SCHEME
=
new
BitSet
(
256
);
SCHEME
.
or
(
alpha
);
SCHEME
.
or
(
digit
);
SCHEME
.
set
(
'+'
);
SCHEME
.
set
(
'-'
);
SCHEME
.
set
(
'.'
);
USER_INFO
=
new
BitSet
(
256
);
USER_INFO
.
or
(
unreserved
);
USER_INFO
.
or
(
subdelims
);
USER_INFO
.
set
(
':'
);
HOST
=
new
BitSet
(
256
);
HOST
.
or
(
unreserved
);
HOST
.
or
(
subdelims
);
PORT
=
new
BitSet
(
256
);
PORT
.
or
(
digit
);
BitSet
pchar
=
new
BitSet
(
256
);
pchar
.
or
(
unreserved
);
pchar
.
or
(
subdelims
);
pchar
.
set
(
':'
);
pchar
.
set
(
'@'
);
SEGMENT
=
new
BitSet
(
256
);
SEGMENT
.
or
(
pchar
);
PATH
=
new
BitSet
(
256
);
PATH
.
or
(
SEGMENT
);
PATH
.
set
(
'/'
);
QUERY
=
new
BitSet
(
256
);
QUERY
.
or
(
pchar
);
QUERY
.
set
(
'/'
);
QUERY
.
set
(
'?'
);
QUERY_PARAM
=
new
BitSet
(
256
);
QUERY_PARAM
.
or
(
pchar
);
QUERY_PARAM
.
set
(
'/'
);
QUERY_PARAM
.
set
(
'?'
);
QUERY_PARAM
.
clear
(
'='
);
QUERY_PARAM
.
clear
(
'+'
);
QUERY_PARAM
.
clear
(
'&'
);
FRAGMENT
=
new
BitSet
(
256
);
FRAGMENT
.
or
(
pchar
);
FRAGMENT
.
set
(
'/'
);
FRAGMENT
.
set
(
'?'
);
}
/**
* Encodes the given source URI into an encoded String. Based on the following
* rules:
* <ul>
* <li>Alphanumeric characters {@code "a"} through {@code "z"},
* {@code "A"} through {@code "Z"}, and {@code "0"} through {@code "9"}
* stay the same.
* <li>Special characters {@code "-"}, {@code "_"}, {@code "."}, and
* {@code "*"} stay the same.
* <li>All other characters are converted into one or more bytes using the
* given encoding scheme. Each of the resulting bytes is written as a
* hexadecimal string in the "<code>%<i>xy</i></code>" format.
* </ul>
* Encodes the given source URI into an encoded String. All various URI components are encoded according to
* their respective valid character sets.
*
* @param uri the URI to be encoded
* @param encoding the character encoding to encode to
* @return the encoded URI
* @throws IllegalArgumentException when the given uri parameter is not a valid URI
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodeUri
(
String
uri
,
String
encoding
)
throws
UnsupportedEncodingException
{
Assert
.
notNull
(
uri
,
"'uri' must not be null"
);
Assert
.
hasLength
(
encoding
,
"'encoding' must not be empty"
);
Matcher
m
=
URI_PATTERN
.
matcher
(
uri
);
if
(
m
.
matches
())
{
String
scheme
=
m
.
group
(
2
);
String
authority
=
m
.
group
(
3
);
String
userinfo
=
m
.
group
(
5
);
String
host
=
m
.
group
(
6
);
String
port
=
m
.
group
(
8
);
String
path
=
m
.
group
(
9
);
String
query
=
m
.
group
(
11
);
String
fragment
=
m
.
group
(
13
);
StringBuilder
sb
=
new
StringBuilder
();
if
(
scheme
!=
null
)
{
sb
.
append
(
encodeScheme
(
scheme
,
encoding
));
sb
.
append
(
':'
);
}
if
(
authority
!=
null
)
{
sb
.
append
(
"//"
);
if
(
userinfo
!=
null
)
{
sb
.
append
(
encodeUserInfo
(
userinfo
,
encoding
));
sb
.
append
(
'@'
);
}
if
(
host
!=
null
)
{
sb
.
append
(
encodeHost
(
host
,
encoding
));
}
if
(
port
!=
null
)
{
sb
.
append
(
':'
);
sb
.
append
(
encodePort
(
port
,
encoding
));
}
}
sb
.
append
(
encodePath
(
path
,
encoding
));
if
(
query
!=
null
)
{
sb
.
append
(
'?'
);
sb
.
append
(
encodeQuery
(
query
,
encoding
));
}
if
(
fragment
!=
null
)
{
sb
.
append
(
'#'
);
sb
.
append
(
encodeFragment
(
fragment
,
encoding
));
}
return
sb
.
toString
();
}
else
{
throw
new
IllegalArgumentException
(
"["
+
uri
+
"] is not a valid URI"
);
}
}
/**
* Encodes the given URI scheme.
*
* @param scheme the scheme to be encoded
* @param encoding the character encoding to encode to
* @return the encoded scheme
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodeScheme
(
String
scheme
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
scheme
,
encoding
,
SCHEME
);
}
/**
* Encodes the given URI user info.
*
* @param userInfo the user info to be encoded
* @param encoding the character encoding to encode to
* @return the encoded user info
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodeUserInfo
(
String
userInfo
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
userInfo
,
encoding
,
USER_INFO
);
}
/**
* Encodes the given URI host.
*
* @param host the host to be encoded
* @param encoding the character encoding to encode to
* @return the encoded host
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodeHost
(
String
host
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
host
,
encoding
,
HOST
);
}
/**
* Encodes the given URI port.
*
* @param
source the String
to be encoded
* @param encoding the
encoding to use
* @return the encoded
string
* @param
port the port
to be encoded
* @param encoding the
character encoding to encode to
* @return the encoded
port
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
* @see java.net.URLEncoder#encode(String, String)
*/
public
static
String
encode
(
String
source
,
String
encoding
)
throws
UnsupportedEncodingException
{
public
static
String
encodePort
(
String
port
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
port
,
encoding
,
PORT
);
}
/**
* Encodes the given URI path.
*
* @param path the path to be encoded
* @param encoding the character encoding to encode to
* @return the encoded path
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodePath
(
String
path
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
path
,
encoding
,
PATH
);
}
/**
* Encodes the given URI path segment.
*
* @param segment the segment to be encoded
* @param encoding the character encoding to encode to
* @return the encoded segment
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodePathSegment
(
String
segment
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
segment
,
encoding
,
SEGMENT
);
}
/**
* Encodes the given URI query.
*
* @param query the query to be encoded
* @param encoding the character encoding to encode to
* @return the encoded query
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodeQuery
(
String
query
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
query
,
encoding
,
QUERY
);
}
/**
* Encodes the given URI query parameter.
*
* @param query the query parameter to be encoded
* @param encoding the character encoding to encode to
* @return the encoded query parameter
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodeQueryParam
(
String
queryParam
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
queryParam
,
encoding
,
QUERY_PARAM
);
}
/**
* Encodes the given URI fragment.
*
* @param fragment the fragment to be encoded
* @param encoding the character encoding to encode to
* @return the encoded fragment
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
*/
public
static
String
encodeFragment
(
String
fragment
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
encode
(
fragment
,
encoding
,
FRAGMENT
);
}
private
static
String
encode
(
String
source
,
String
encoding
,
BitSet
notEncoded
)
throws
UnsupportedEncodingException
{
Assert
.
notNull
(
source
,
"'source' must not be null"
);
Assert
.
hasLength
(
encoding
,
"'encoding' must not be empty"
);
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
(
source
.
length
()
*
2
);
...
...
org.springframework.web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java
浏览文件 @
60d2fdfc
...
...
@@ -51,6 +51,8 @@ public class RestTemplateIntegrationTests {
private
static
String
helloWorld
=
"H\u00e9llo W\u00f6rld"
;
private
static
final
String
URI
=
"http://localhost:8889"
;
@BeforeClass
public
static
void
startJettyServer
()
throws
Exception
{
jettyServer
=
new
Server
(
8889
);
...
...
@@ -60,10 +62,11 @@ public class RestTemplateIntegrationTests {
jettyContext
.
addServlet
(
new
ServletHolder
(
new
GetServlet
(
bytes
,
contentType
)),
"/get"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
GetServlet
(
new
byte
[
0
],
contentType
)),
"/get/nothing"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
PostServlet
(
helloWorld
,
"http://localhost:8889
/post/1"
,
bytes
,
contentType
)),
new
ServletHolder
(
new
PostServlet
(
helloWorld
,
URI
+
"
/post/1"
,
bytes
,
contentType
)),
"/post"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
ErrorServlet
(
404
)),
"/errors/notfound"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
ErrorServlet
(
500
)),
"/errors/server"
);
jettyContext
.
addServlet
(
new
ServletHolder
(
new
UriServlet
()),
"/uri/*"
);
jettyServer
.
start
();
}
...
...
@@ -81,45 +84,51 @@ public class RestTemplateIntegrationTests {
@Test
public
void
getString
()
{
String
s
=
template
.
getForObject
(
"http://localhost:8889
/{method}"
,
String
.
class
,
"get"
);
String
s
=
template
.
getForObject
(
URI
+
"
/{method}"
,
String
.
class
,
"get"
);
assertEquals
(
"Invalid content"
,
helloWorld
,
s
);
}
@Test
public
void
getNoResponse
()
{
String
s
=
template
.
getForObject
(
"http://localhost:8889
/get/nothing"
,
String
.
class
);
String
s
=
template
.
getForObject
(
URI
+
"
/get/nothing"
,
String
.
class
);
assertEquals
(
"Invalid content"
,
""
,
s
);
}
@Test
public
void
postForLocation
()
throws
URISyntaxException
{
URI
location
=
template
.
postForLocation
(
"http://localhost:8889
/{method}"
,
helloWorld
,
"post"
);
assertEquals
(
"Invalid location"
,
new
URI
(
"http://localhost:8889
/post/1"
),
location
);
URI
location
=
template
.
postForLocation
(
URI
+
"
/{method}"
,
helloWorld
,
"post"
);
assertEquals
(
"Invalid location"
,
new
URI
(
URI
+
"
/post/1"
),
location
);
}
@Test
public
void
postForObject
()
throws
URISyntaxException
{
String
s
=
template
.
postForObject
(
"http://localhost:8889
/{method}"
,
helloWorld
,
String
.
class
,
"post"
);
String
s
=
template
.
postForObject
(
URI
+
"
/{method}"
,
helloWorld
,
String
.
class
,
"post"
);
assertEquals
(
"Invalid content"
,
helloWorld
,
s
);
}
@Test
(
expected
=
HttpClientErrorException
.
class
)
public
void
notFound
()
{
template
.
execute
(
"http://localhost:8889
/errors/notfound"
,
HttpMethod
.
GET
,
null
,
null
);
template
.
execute
(
URI
+
"
/errors/notfound"
,
HttpMethod
.
GET
,
null
,
null
);
}
@Test
(
expected
=
HttpServerErrorException
.
class
)
public
void
serverError
()
{
template
.
execute
(
"http://localhost:8889
/errors/server"
,
HttpMethod
.
GET
,
null
,
null
);
template
.
execute
(
URI
+
"
/errors/server"
,
HttpMethod
.
GET
,
null
,
null
);
}
@Test
public
void
optionsForAllow
()
throws
URISyntaxException
{
Set
<
HttpMethod
>
allowed
=
template
.
optionsForAllow
(
new
URI
(
"http://localhost:8889
/get"
));
Set
<
HttpMethod
>
allowed
=
template
.
optionsForAllow
(
new
URI
(
URI
+
"
/get"
));
assertEquals
(
"Invalid response"
,
EnumSet
.
of
(
HttpMethod
.
GET
,
HttpMethod
.
OPTIONS
,
HttpMethod
.
HEAD
,
HttpMethod
.
TRACE
),
allowed
);
}
@Test
public
void
uri
()
throws
InterruptedException
,
URISyntaxException
{
String
result
=
template
.
getForObject
(
URI
+
"/uri/{query}"
,
String
.
class
,
"Zrich"
);
assertEquals
(
"Invalid request URI"
,
"/uri/Z%FCrich"
,
result
);
}
/** Servlet that returns and error message for a given status code. */
private
static
class
ErrorServlet
extends
GenericServlet
{
...
...
@@ -187,4 +196,14 @@ public class RestTemplateIntegrationTests {
}
}
private
static
class
UriServlet
extends
HttpServlet
{
@Override
protected
void
doGet
(
HttpServletRequest
req
,
HttpServletResponse
resp
)
throws
ServletException
,
IOException
{
resp
.
setContentType
(
"text/plain"
);
resp
.
setCharacterEncoding
(
"UTF-8"
);
resp
.
getWriter
().
write
(
req
.
getRequestURI
());
}
}
}
org.springframework.web/src/test/java/org/springframework/web/util/UriTemplateTests.java
浏览文件 @
60d2fdfc
...
...
@@ -84,12 +84,9 @@ public class UriTemplateTests {
@Test
public
void
expandEncoded
()
throws
Exception
{
UriTemplate
template
=
new
UriTemplate
(
"http://example.com/hotel list/{hotel}"
);
URI
result
=
template
.
expand
(
Collections
.
singletonMap
(
"hotel"
,
"foo bar \u20AC"
));
assertEquals
(
"Invalid expanded template"
,
new
URI
(
"http"
,
"//example.com/hotel list/foo bar \u20AC"
,
null
),
result
);
assertEquals
(
"Invalid expanded template"
,
"http://example.com/hotel%20list/foo%20bar%20%E2%82%AC"
,
result
.
toASCIIString
());
UriTemplate
template
=
new
UriTemplate
(
"http://example.com//hotel list/{hotel}"
);
URI
result
=
template
.
expand
(
"Zrich"
);
assertEquals
(
"Invalid expanded template"
,
new
URI
(
"http://example.com//hotel%20list/Z%FCrich"
),
result
);
}
@Test
...
...
org.springframework.web/src/test/java/org/springframework/web/util/UriUtilsTest.java
浏览文件 @
60d2fdfc
...
...
@@ -16,35 +16,108 @@
package
org.springframework.web.util
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
import
java.util.regex.Pattern
;
import
java.util.regex.Matcher
;
import
java.io.UnsupportedEncodingException
;
import
static
org
.
junit
.
Assert
.*;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
/** @author Arjen Poutsma */
public
class
UriUtilsTest
{
private
static
final
String
ENC
=
"UTF-8"
;
@Test
public
void
encodeScheme
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"foobar+-."
,
UriUtils
.
encodeScheme
(
"foobar+-."
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foo%20bar"
,
UriUtils
.
encodeScheme
(
"foo bar"
,
ENC
));
}
@Test
public
void
encode
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded URI"
,
"foobar"
,
UriUtils
.
encode
(
"foobar"
,
"UTF-8"
));
assertEquals
(
"Invalid encoded URI"
,
"foo%20bar"
,
UriUtils
.
encode
(
"foo bar"
,
"UTF-8"
));
assertEquals
(
"Invalid encoded URI"
,
"foo%2Bbar"
,
UriUtils
.
encode
(
"foo+bar"
,
"UTF-8"
));
public
void
encodeUserInfo
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"foobar:"
,
UriUtils
.
encodeUserInfo
(
"foobar:"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foo%20bar"
,
UriUtils
.
encodeUserInfo
(
"foo bar"
,
ENC
));
}
@Test
public
void
encodeHost
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"foobar"
,
UriUtils
.
encodeHost
(
"foobar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foo%20bar"
,
UriUtils
.
encodeHost
(
"foo bar"
,
ENC
));
}
@Test
public
void
encodePort
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"80"
,
UriUtils
.
encodePort
(
"80"
,
ENC
));
}
@Test
public
void
encodePath
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"/foo/bar"
,
UriUtils
.
encodePath
(
"/foo/bar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"/foo%20bar"
,
UriUtils
.
encodePath
(
"/foo bar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"/Z%FCrich"
,
UriUtils
.
encodePath
(
"/Z\u00fcrich"
,
ENC
));
}
@Test
public
void
encodePathSegment
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"foobar"
,
UriUtils
.
encodePathSegment
(
"foobar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"%2Ffoo%2Fbar"
,
UriUtils
.
encodePathSegment
(
"/foo/bar"
,
ENC
));
}
@Test
public
void
encodeQuery
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"foobar"
,
UriUtils
.
encodeQuery
(
"foobar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foo%20bar"
,
UriUtils
.
encodeQuery
(
"foo bar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foobar/+"
,
UriUtils
.
encodeQuery
(
"foobar/+"
,
ENC
));
}
@Test
public
void
encodeQueryParam
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"foobar"
,
UriUtils
.
encodeQueryParam
(
"foobar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foo%20bar"
,
UriUtils
.
encodeQueryParam
(
"foo bar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foo%26bar"
,
UriUtils
.
encodeQueryParam
(
"foo&bar"
,
ENC
));
}
@Test
public
void
encodeFragment
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded result"
,
"foobar"
,
UriUtils
.
encodeFragment
(
"foobar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foo%20bar"
,
UriUtils
.
encodeFragment
(
"foo bar"
,
ENC
));
assertEquals
(
"Invalid encoded result"
,
"foobar/"
,
UriUtils
.
encodeFragment
(
"foobar/"
,
ENC
));
}
@Test
public
void
decode
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded URI"
,
"foobar"
,
UriUtils
.
decode
(
"foobar"
,
"UTF-8"
));
assertEquals
(
"Invalid encoded URI"
,
"foo bar"
,
UriUtils
.
decode
(
"foo%20bar"
,
"UTF-8"
));
assertEquals
(
"Invalid encoded URI"
,
"foo+bar"
,
UriUtils
.
decode
(
"foo%2bbar"
,
"UTF-8"
));
assertEquals
(
"Invalid encoded URI"
,
"foobar"
,
UriUtils
.
decode
(
"foobar"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"foo bar"
,
UriUtils
.
decode
(
"foo%20bar"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"foo+bar"
,
UriUtils
.
decode
(
"foo%2bbar"
,
ENC
));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
decodeInvalidSequence
()
throws
UnsupportedEncodingException
{
UriUtils
.
decode
(
"foo%2"
,
"UTF-8"
);
UriUtils
.
decode
(
"foo%2"
,
ENC
);
}
@Test
public
void
encodeUri
()
throws
UnsupportedEncodingException
{
assertEquals
(
"Invalid encoded URI"
,
"http://www.ietf.org/rfc/rfc3986.txt"
,
UriUtils
.
encodeUri
(
"http://www.ietf.org/rfc/rfc3986.txt"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"http://www.google.com/?q=z%FCrich"
,
UriUtils
.
encodeUri
(
"http://www.google.com/?q=zrich"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)"
,
UriUtils
.
encodeUri
(
"http://arjen:foobar@java.sun.com:80/javase/6/docs/api/java/util/BitSet.html?foo=bar#and(java.util.BitSet)"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"mailto:java-net@java.sun.com"
,
UriUtils
.
encodeUri
(
"mailto:java-net@java.sun.com"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"news:comp.lang.java"
,
UriUtils
.
encodeUri
(
"news:comp.lang.java"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"urn:isbn:096139210x"
,
UriUtils
.
encodeUri
(
"urn:isbn:096139210x"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"http://java.sun.com/j2se/1.3/"
,
UriUtils
.
encodeUri
(
"http://java.sun.com/j2se/1.3/"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"docs/guide/collections/designfaq.html#28"
,
UriUtils
.
encodeUri
(
"docs/guide/collections/designfaq.html#28"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"../../../demo/jfc/SwingSet2/src/SwingSet2.java"
,
UriUtils
.
encodeUri
(
"../../../demo/jfc/SwingSet2/src/SwingSet2.java"
,
ENC
));
assertEquals
(
"Invalid encoded URI"
,
"file:///~/calendar"
,
UriUtils
.
encodeUri
(
"file:///~/calendar"
,
ENC
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录