Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
adc595b5
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 搜索 >>
提交
adc595b5
编写于
7月 19, 2016
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Avoid dependency on WebUtils for extracting file extension
Issue: SPR-14479
上级
86c659f5
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
53 addition
and
119 deletion
+53
-119
spring-web-reactive/src/main/java/org/springframework/web/reactive/accept/PathExtensionContentTypeResolver.java
...web/reactive/accept/PathExtensionContentTypeResolver.java
+2
-3
spring-web/src/main/java/org/springframework/web/accept/PathExtensionContentNegotiationStrategy.java
...k/web/accept/PathExtensionContentNegotiationStrategy.java
+3
-4
spring-web/src/main/java/org/springframework/web/util/UriUtils.java
.../src/main/java/org/springframework/web/util/UriUtils.java
+26
-1
spring-web/src/main/java/org/springframework/web/util/WebUtils.java
.../src/main/java/org/springframework/web/util/WebUtils.java
+0
-78
spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java
...test/java/org/springframework/web/util/UriUtilsTests.java
+20
-1
spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java
...test/java/org/springframework/web/util/WebUtilsTests.java
+0
-28
spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
...work/web/servlet/support/ServletUriComponentsBuilder.java
+2
-4
未找到文件。
spring-web-reactive/src/main/java/org/springframework/web/reactive/accept/PathExtensionContentTypeResolver.java
浏览文件 @
adc595b5
...
...
@@ -26,7 +26,7 @@ import org.springframework.util.Assert;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.server.NotAcceptableStatusException
;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.util.
Web
Utils
;
import
org.springframework.web.util.
Uri
Utils
;
/**
* A {@link RequestedContentTypeResolver} that extracts the file extension from
...
...
@@ -84,8 +84,7 @@ public class PathExtensionContentTypeResolver extends AbstractMappingContentType
@Override
protected
String
extractKey
(
ServerWebExchange
exchange
)
{
String
path
=
exchange
.
getRequest
().
getURI
().
getRawPath
();
String
filename
=
WebUtils
.
extractFullFilenameFromUrlPath
(
path
);
String
extension
=
StringUtils
.
getFilenameExtension
(
filename
);
String
extension
=
UriUtils
.
extractFileExtension
(
path
);
return
(
StringUtils
.
hasText
(
extension
))
?
extension
.
toLowerCase
(
Locale
.
ENGLISH
)
:
null
;
}
...
...
spring-web/src/main/java/org/springframework/web/accept/PathExtensionContentNegotiationStrategy.java
浏览文件 @
adc595b5
...
...
@@ -31,8 +31,8 @@ import org.springframework.util.ClassUtils;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.HttpMediaTypeNotAcceptableException
;
import
org.springframework.web.context.request.NativeWebRequest
;
import
org.springframework.web.util.UriUtils
;
import
org.springframework.web.util.UrlPathHelper
;
import
org.springframework.web.util.WebUtils
;
/**
* A {@code ContentNegotiationStrategy} that resolves the file extension in the
...
...
@@ -114,9 +114,8 @@ public class PathExtensionContentNegotiationStrategy extends AbstractMappingCont
return
null
;
}
String
path
=
this
.
urlPathHelper
.
getLookupPathForRequest
(
request
);
String
filename
=
WebUtils
.
extractFullFilenameFromUrlPath
(
path
);
String
extension
=
StringUtils
.
getFilenameExtension
(
filename
);
return
(
StringUtils
.
hasText
(
extension
))
?
extension
.
toLowerCase
(
Locale
.
ENGLISH
)
:
null
;
String
extension
=
UriUtils
.
extractFileExtension
(
path
);
return
(
StringUtils
.
hasText
(
extension
)
?
extension
.
toLowerCase
(
Locale
.
ENGLISH
)
:
null
);
}
@Override
...
...
spring-web/src/main/java/org/springframework/web/util/UriUtils.java
浏览文件 @
adc595b5
/*
* 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.
...
...
@@ -34,6 +34,7 @@ import org.springframework.util.Assert;
* </ul>
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
*/
...
...
@@ -213,4 +214,28 @@ public abstract class UriUtils {
return
(
changed
?
new
String
(
bos
.
toByteArray
(),
encoding
)
:
source
);
}
/**
* Extract the file extension from the given URI path.
* @param path the URI path (e.g. "/products/index.html")
* @return the extracted file extension (e.g. "html")
* @since 4.3.2
*/
public
static
String
extractFileExtension
(
String
path
)
{
int
end
=
path
.
indexOf
(
'?'
);
if
(
end
==
-
1
)
{
end
=
path
.
indexOf
(
'#'
);
if
(
end
==
-
1
)
{
end
=
path
.
length
();
}
}
int
begin
=
path
.
lastIndexOf
(
'/'
,
end
)
+
1
;
int
paramIndex
=
path
.
indexOf
(
';'
,
begin
);
end
=
(
paramIndex
!=
-
1
&&
paramIndex
<
end
?
paramIndex
:
end
);
int
extIndex
=
path
.
lastIndexOf
(
'.'
,
end
);
if
(
extIndex
!=
-
1
&&
extIndex
>
begin
)
{
return
path
.
substring
(
extIndex
+
1
,
end
);
}
return
null
;
}
}
spring-web/src/main/java/org/springframework/web/util/WebUtils.java
浏览文件 @
adc595b5
...
...
@@ -260,7 +260,6 @@ public abstract class WebUtils {
return
realPath
;
}
/**
* Determine the session id of the given request, if any.
* @param request current HTTP request
...
...
@@ -470,20 +469,6 @@ public abstract class WebUtils {
request
.
removeAttribute
(
ERROR_SERVLET_NAME_ATTRIBUTE
);
}
/**
* Expose the given Map as request attributes, using the keys as attribute names
* and the values as corresponding attribute values. Keys need to be Strings.
* @param request current HTTP request
* @param attributes the attributes Map
*/
public
static
void
exposeRequestAttributes
(
ServletRequest
request
,
Map
<
String
,
?>
attributes
)
{
Assert
.
notNull
(
request
,
"Request must not be null"
);
Assert
.
notNull
(
attributes
,
"Attributes Map must not be null"
);
for
(
Map
.
Entry
<
String
,
?>
entry
:
attributes
.
entrySet
())
{
request
.
setAttribute
(
entry
.
getKey
(),
entry
.
getValue
());
}
}
/**
* Retrieve the first cookie with the given name. Note that multiple
* cookies can have the same name but different paths or domains.
...
...
@@ -629,69 +614,6 @@ public abstract class WebUtils {
return
params
;
}
/**
* Return the target page specified in the request.
* @param request current servlet request
* @param paramPrefix the parameter prefix to check for
* (e.g. "_target" for parameters like "_target1" or "_target2")
* @param currentPage the current page, to be returned as fallback
* if no target page specified
* @return the page specified in the request, or current page if not found
*/
public
static
int
getTargetPage
(
ServletRequest
request
,
String
paramPrefix
,
int
currentPage
)
{
Enumeration
<
String
>
paramNames
=
request
.
getParameterNames
();
while
(
paramNames
.
hasMoreElements
())
{
String
paramName
=
paramNames
.
nextElement
();
if
(
paramName
.
startsWith
(
paramPrefix
))
{
for
(
int
i
=
0
;
i
<
WebUtils
.
SUBMIT_IMAGE_SUFFIXES
.
length
;
i
++)
{
String
suffix
=
WebUtils
.
SUBMIT_IMAGE_SUFFIXES
[
i
];
if
(
paramName
.
endsWith
(
suffix
))
{
paramName
=
paramName
.
substring
(
0
,
paramName
.
length
()
-
suffix
.
length
());
}
}
return
Integer
.
parseInt
(
paramName
.
substring
(
paramPrefix
.
length
()));
}
}
return
currentPage
;
}
/**
* Extract the URL filename from the given request URL path.
* Correctly resolves nested paths such as "/products/view.html" as well.
* @param urlPath the request URL path (e.g. "/index.html")
* @return the extracted URI filename (e.g. "index")
*/
public
static
String
extractFilenameFromUrlPath
(
String
urlPath
)
{
String
filename
=
extractFullFilenameFromUrlPath
(
urlPath
);
int
dotIndex
=
filename
.
lastIndexOf
(
'.'
);
if
(
dotIndex
!=
-
1
)
{
filename
=
filename
.
substring
(
0
,
dotIndex
);
}
return
filename
;
}
/**
* Extract the full URL filename (including file extension) from the given
* request URL path. Correctly resolve nested paths such as
* "/products/view.html" and remove any path and or query parameters.
* @param urlPath the request URL path (e.g. "/products/index.html")
* @return the extracted URI filename (e.g. "index.html")
*/
public
static
String
extractFullFilenameFromUrlPath
(
String
urlPath
)
{
int
end
=
urlPath
.
indexOf
(
'?'
);
if
(
end
==
-
1
)
{
end
=
urlPath
.
indexOf
(
'#'
);
if
(
end
==
-
1
)
{
end
=
urlPath
.
length
();
}
}
int
begin
=
urlPath
.
lastIndexOf
(
'/'
,
end
)
+
1
;
int
paramIndex
=
urlPath
.
indexOf
(
';'
,
begin
);
end
=
(
paramIndex
!=
-
1
&&
paramIndex
<
end
?
paramIndex
:
end
);
return
urlPath
.
substring
(
begin
,
end
);
}
/**
* Parse the given string with matrix variables. An example string would look
* like this {@code "q1=a;q1=b;q2=a,b,c"}. The resulting map would contain
...
...
spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java
浏览文件 @
adc595b5
/*
* Copyright 2002-201
4
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.
...
...
@@ -24,6 +24,7 @@ import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Juergen Hoeller
*/
public
class
UriUtilsTests
{
...
...
@@ -104,4 +105,22 @@ public class UriUtilsTests {
UriUtils
.
decode
(
"foo%2"
,
ENC
);
}
@Test
public
void
extractFileExtension
()
{
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"index.html"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/index.html"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html#/a"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html#/path/a"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html#/path/a.do"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html?param=a"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html?param=/path/a"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html?param=/path/a.do"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html?param=/path/a#/path/a"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products/view.html?param=/path/a.do#/path/a.do"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products;q=11/view.html?param=/path/a.do"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products;q=11/view.html;r=22?param=/path/a.do"
));
assertEquals
(
"html"
,
UriUtils
.
extractFileExtension
(
"/products;q=11/view.html;r=22;s=33?param=/path/a.do"
));
}
}
spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java
浏览文件 @
adc595b5
...
...
@@ -55,34 +55,6 @@ public class WebUtilsTests {
assertEquals
(
"myValue4"
,
WebUtils
.
findParameterValue
(
params
,
"myKey4"
));
}
@Test
public
void
extractFilenameFromUrlPath
()
{
assertEquals
(
"index"
,
WebUtils
.
extractFilenameFromUrlPath
(
"index.html"
));
assertEquals
(
"index"
,
WebUtils
.
extractFilenameFromUrlPath
(
"/index.html"
));
assertEquals
(
"view"
,
WebUtils
.
extractFilenameFromUrlPath
(
"/products/view.html"
));
assertEquals
(
"view"
,
WebUtils
.
extractFilenameFromUrlPath
(
"/products/view.html?param=a"
));
assertEquals
(
"view"
,
WebUtils
.
extractFilenameFromUrlPath
(
"/products/view.html?param=/path/a"
));
assertEquals
(
"view"
,
WebUtils
.
extractFilenameFromUrlPath
(
"/products/view.html?param=/path/a.do"
));
}
@Test
public
void
extractFullFilenameFromUrlPath
()
{
assertEquals
(
"index.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"index.html"
));
assertEquals
(
"index.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/index.html"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html#/a"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html#/path/a"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html#/path/a.do"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html?param=a"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html?param=/path/a"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html?param=/path/a.do"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html?param=/path/a#/path/a"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products/view.html?param=/path/a.do#/path/a.do"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products;q=11/view.html?param=/path/a.do"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products;q=11/view.html;r=22?param=/path/a.do"
));
assertEquals
(
"view.html"
,
WebUtils
.
extractFullFilenameFromUrlPath
(
"/products;q=11/view.html;r=22;s=33?param=/path/a.do"
));
}
@Test
public
void
parseMatrixVariablesString
()
{
MultiValueMap
<
String
,
String
>
variables
;
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
浏览文件 @
adc595b5
...
...
@@ -28,8 +28,8 @@ import org.springframework.web.context.request.RequestContextHolder;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.springframework.web.util.UriComponents
;
import
org.springframework.web.util.UriComponentsBuilder
;
import
org.springframework.web.util.UriUtils
;
import
org.springframework.web.util.UrlPathHelper
;
import
org.springframework.web.util.WebUtils
;
/**
* A UriComponentsBuilder that extracts information from the HttpServletRequest.
...
...
@@ -44,7 +44,6 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
/**
* Default constructor. Protected to prevent direct instantiation.
*
* @see #fromContextPath(HttpServletRequest)
* @see #fromServletMapping(HttpServletRequest)
* @see #fromRequest(HttpServletRequest)
...
...
@@ -219,8 +218,7 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
public
String
removePathExtension
()
{
String
extension
=
null
;
if
(
this
.
originalPath
!=
null
)
{
String
filename
=
WebUtils
.
extractFullFilenameFromUrlPath
(
this
.
originalPath
);
extension
=
StringUtils
.
getFilenameExtension
(
filename
);
extension
=
UriUtils
.
extractFileExtension
(
this
.
originalPath
);
if
(!
StringUtils
.
isEmpty
(
extension
))
{
int
end
=
this
.
originalPath
.
length
()
-
(
extension
.
length
()
+
1
);
replacePath
(
this
.
originalPath
.
substring
(
0
,
end
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录