Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
5719fab1
Commit
5719fab1
authored
Dec 14, 2015
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.2.x'
parents
004fa11b
edb16a13
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
187 additions
and
26 deletions
+187
-26
ErrorMvcAutoConfiguration.java
...ork/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java
+52
-25
NonRecursivePropertyPlaceholderHelper.java
...oconfigure/web/NonRecursivePropertyPlaceholderHelper.java
+61
-0
DefaultErrorViewIntegrationTests.java
...t/autoconfigure/web/DefaultErrorViewIntegrationTests.java
+21
-1
NonRecursivePropertyPlaceholderHelperTests.java
...igure/web/NonRecursivePropertyPlaceholderHelperTests.java
+53
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration.java
View file @
5719fab1
...
...
@@ -16,6 +16,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
web
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -50,10 +51,10 @@ import org.springframework.context.expression.MapAccessor;
import
org.springframework.core.Ordered
;
import
org.springframework.core.io.support.SpringFactoriesLoader
;
import
org.springframework.core.type.AnnotatedTypeMetadata
;
import
org.springframework.expression.EvaluationContext
;
import
org.springframework.expression.Expression
;
import
org.springframework.expression.spel.standard.SpelExpressionParser
;
import
org.springframework.expression.spel.support.StandardEvaluationContext
;
import
org.springframework.util.PropertyPlaceholderHelper
;
import
org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver
;
import
org.springframework.web.servlet.DispatcherServlet
;
import
org.springframework.web.servlet.View
;
...
...
@@ -162,19 +163,18 @@ public class ErrorMvcAutoConfiguration {
*/
private
static
class
SpelView
implements
View
{
private
final
String
template
;
private
final
StandardEvaluationContext
context
=
new
StandardEvaluationContext
();
private
final
NonRecursivePropertyPlaceholderHelper
helper
;
private
PropertyPlaceholderHelper
helper
;
private
final
String
template
;
private
PlaceholderResolver
resolver
;
private
final
Map
<
String
,
Expression
>
expressions
;
SpelView
(
String
template
)
{
this
.
helper
=
new
NonRecursivePropertyPlaceholderHelper
(
"${"
,
"}"
);
this
.
template
=
template
;
this
.
context
.
addPropertyAccessor
(
new
MapAccessor
()
);
this
.
helper
=
new
PropertyPlaceholderHelper
(
"${"
,
"}"
);
this
.
resolver
=
new
SpelPlaceholderResolver
(
this
.
context
);
ExpressionCollector
expressionCollector
=
new
ExpressionCollector
(
);
this
.
helper
.
replacePlaceholders
(
this
.
template
,
expressionCollector
);
this
.
expressions
=
expressionCollector
.
getExpressions
(
);
}
@Override
...
...
@@ -190,36 +190,63 @@ public class ErrorMvcAutoConfiguration {
}
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>(
model
);
map
.
put
(
"path"
,
request
.
getContextPath
());
this
.
context
.
setRootObject
(
map
);
String
result
=
this
.
helper
.
replacePlaceholders
(
this
.
template
,
this
.
resolver
);
PlaceholderResolver
resolver
=
new
ExpressionResolver
(
this
.
expressions
,
map
);
String
result
=
this
.
helper
.
replacePlaceholders
(
this
.
template
,
resolver
);
response
.
getWriter
().
append
(
result
);
}
}
/**
*
SpEL based {@link PlaceholderResolver}
.
*
{@link PlaceholderResolver} to collect placeholder expressions
.
*/
private
static
class
SpelPlaceholderResolve
r
implements
PlaceholderResolver
{
private
static
class
ExpressionCollecto
r
implements
PlaceholderResolver
{
private
final
SpelExpressionParser
parser
=
new
SpelExpressionParser
();
private
final
StandardEvaluationContext
context
;
private
final
Map
<
String
,
Expression
>
expressions
=
new
HashMap
<
String
,
Expression
>()
;
SpelPlaceholderResolver
(
StandardEvaluationContext
context
)
{
this
.
context
=
context
;
@Override
public
String
resolvePlaceholder
(
String
name
)
{
this
.
expressions
.
put
(
name
,
this
.
parser
.
parseExpression
(
name
));
return
null
;
}
public
Map
<
String
,
Expression
>
getExpressions
()
{
return
Collections
.
unmodifiableMap
(
this
.
expressions
);
}
}
/**
* SpEL based {@link PlaceholderResolver}.
*/
private
static
class
ExpressionResolver
implements
PlaceholderResolver
{
private
final
Map
<
String
,
Expression
>
expressions
;
private
final
EvaluationContext
context
;
ExpressionResolver
(
Map
<
String
,
Expression
>
expressions
,
Map
<
String
,
?>
map
)
{
this
.
expressions
=
expressions
;
this
.
context
=
getContext
(
map
);
}
private
EvaluationContext
getContext
(
Map
<
String
,
?>
map
)
{
StandardEvaluationContext
context
=
new
StandardEvaluationContext
();
context
.
addPropertyAccessor
(
new
MapAccessor
());
context
.
setRootObject
(
map
);
return
context
;
}
@Override
public
String
resolvePlaceholder
(
String
name
)
{
Expression
expression
=
this
.
parser
.
parseExpression
(
name
);
try
{
Object
value
=
expression
.
getValue
(
this
.
context
);
return
HtmlUtils
.
htmlEscape
(
value
==
null
?
null
:
value
.
toString
());
}
catch
(
Exception
ex
)
{
return
null
;
}
public
String
resolvePlaceholder
(
String
placeholderName
)
{
Expression
expression
=
this
.
expressions
.
get
(
placeholderName
);
return
escape
(
expression
==
null
?
null
:
expression
.
getValue
(
this
.
context
));
}
private
String
escape
(
Object
value
)
{
return
HtmlUtils
.
htmlEscape
(
value
==
null
?
null
:
value
.
toString
());
}
}
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/NonRecursivePropertyPlaceholderHelper.java
0 → 100644
View file @
5719fab1
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
web
;
import
java.util.Set
;
import
org.springframework.util.PropertyPlaceholderHelper
;
/**
* {@link PropertyPlaceholderHelper} that doesn't allow recursive resolution.
*
* @author Phillip Webb
*/
class
NonRecursivePropertyPlaceholderHelper
extends
PropertyPlaceholderHelper
{
NonRecursivePropertyPlaceholderHelper
(
String
placeholderPrefix
,
String
placeholderSuffix
)
{
super
(
placeholderPrefix
,
placeholderSuffix
);
}
@Override
protected
String
parseStringValue
(
String
strVal
,
PlaceholderResolver
placeholderResolver
,
Set
<
String
>
visitedPlaceholders
)
{
return
super
.
parseStringValue
(
strVal
,
new
NonRecursivePlaceholderResolver
(
placeholderResolver
),
visitedPlaceholders
);
}
private
static
class
NonRecursivePlaceholderResolver
implements
PlaceholderResolver
{
private
final
PlaceholderResolver
resolver
;
public
NonRecursivePlaceholderResolver
(
PlaceholderResolver
resolver
)
{
this
.
resolver
=
resolver
;
}
@Override
public
String
resolvePlaceholder
(
String
placeholderName
)
{
if
(
this
.
resolver
instanceof
NonRecursivePlaceholderResolver
)
{
return
null
;
}
return
this
.
resolver
.
resolvePlaceholder
(
placeholderName
);
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DefaultErrorViewIntegrationTests.java
View file @
5719fab1
...
...
@@ -42,6 +42,7 @@ import org.springframework.test.web.servlet.MvcResult;
import
org.springframework.test.web.servlet.setup.MockMvcBuilders
;
import
org.springframework.web.context.WebApplicationContext
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
...
...
@@ -76,7 +77,7 @@ public class DefaultErrorViewIntegrationTests {
}
@Test
public
void
testErrorWithEscape
()
throws
Exception
{
public
void
testErrorWith
Html
Escape
()
throws
Exception
{
MvcResult
response
=
this
.
mockMvc
.
perform
(
get
(
"/error"
)
.
requestAttr
(
"javax.servlet.error.exception"
,
...
...
@@ -90,6 +91,21 @@ public class DefaultErrorViewIntegrationTests {
assertTrue
(
"Wrong content: "
+
content
,
content
.
contains
(
"999"
));
}
@Test
public
void
testErrorWithSpelEscape
()
throws
Exception
{
String
spel
=
"${T("
+
getClass
().
getName
()
+
").injectCall()}"
;
MvcResult
response
=
this
.
mockMvc
.
perform
(
get
(
"/error"
)
.
requestAttr
(
"javax.servlet.error.exception"
,
new
RuntimeException
(
spel
))
.
accept
(
MediaType
.
TEXT_HTML
))
.
andExpect
(
status
().
is5xxServerError
()).
andReturn
();
String
content
=
response
.
getResponse
().
getContentAsString
();
System
.
out
.
println
(
content
);
assertFalse
(
"Wrong content: "
+
content
,
content
.
contains
(
"injection"
));
}
@Target
(
ElementType
.
TYPE
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
...
...
@@ -112,4 +128,8 @@ public class DefaultErrorViewIntegrationTests {
}
public
static
String
injectCall
()
{
return
"injection"
;
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/NonRecursivePropertyPlaceholderHelperTests.java
0 → 100644
View file @
5719fab1
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
web
;
import
java.util.Properties
;
import
org.junit.Test
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
* Tests for {@link NonRecursivePropertyPlaceholderHelper}.
*
* @author Phillip Webb
*/
public
class
NonRecursivePropertyPlaceholderHelperTests
{
private
final
NonRecursivePropertyPlaceholderHelper
helper
=
new
NonRecursivePropertyPlaceholderHelper
(
"${"
,
"}"
);
@Test
public
void
canResolve
()
{
Properties
properties
=
new
Properties
();
properties
.
put
(
"a"
,
"b"
);
String
result
=
this
.
helper
.
replacePlaceholders
(
"${a}"
,
properties
);
assertThat
(
result
,
equalTo
(
"b"
));
}
@Test
public
void
cannotResolveRecursive
()
{
Properties
properties
=
new
Properties
();
properties
.
put
(
"a"
,
"${b}"
);
properties
.
put
(
"b"
,
"c"
);
String
result
=
this
.
helper
.
replacePlaceholders
(
"${a}"
,
properties
);
assertThat
(
result
,
equalTo
(
"${b}"
));
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment