Skip to content

Commit 2c92168

Browse files
committed
LiveBeansView escapes double quotes in resource descriptions
Issue: SPR-12252 (cherry picked from commit 1ffefcb)
1 parent 834ddad commit 2c92168

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

spring-context/src/main/java/org/springframework/context/support/LiveBeansView.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class LiveBeansView implements LiveBeansViewMBean, ApplicationContextAwar
5757
private static final Set<ConfigurableApplicationContext> applicationContexts =
5858
new LinkedHashSet<ConfigurableApplicationContext>();
5959

60+
6061
static void registerApplicationContext(ConfigurableApplicationContext applicationContext) {
6162
String mbeanDomain = applicationContext.getEnvironment().getProperty(MBEAN_DOMAIN_PROPERTY_NAME);
6263
if (mbeanDomain != null) {
@@ -94,6 +95,7 @@ static void unregisterApplicationContext(ConfigurableApplicationContext applicat
9495

9596
private ConfigurableApplicationContext applicationContext;
9697

98+
9799
public void setApplicationContext(ApplicationContext applicationContext) {
98100
Assert.isTrue(applicationContext instanceof ConfigurableApplicationContext,
99101
"ApplicationContext does not implement ConfigurableApplicationContext");
@@ -173,8 +175,7 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
173175
else {
174176
result.append("\"type\": null,\n");
175177
}
176-
String resource = StringUtils.replace(bd.getResourceDescription(), "\\", "/");
177-
result.append("\"resource\": \"").append(resource).append("\",\n");
178+
result.append("\"resource\": \"").append(getEscapedResourceDescription(bd)).append("\",\n");
178179
result.append("\"dependencies\": [");
179180
String[] dependencies = bf.getDependenciesForBean(beanName);
180181
if (dependencies.length > 0) {
@@ -199,7 +200,7 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
199200
}
200201

201202
/**
202-
* Determine whether the specified bean is eligible for inclusion in the
203+
* Determine whether the specified bean is eligible for inclusion in the
203204
* LiveBeansView JSON snapshot.
204205
* @param beanName the name of the bean
205206
* @param bd the corresponding bean definition
@@ -211,4 +212,31 @@ protected boolean isBeanEligible(String beanName, BeanDefinition bd, Configurabl
211212
(!bd.isLazyInit() || bf.containsSingleton(beanName)));
212213
}
213214

215+
/**
216+
* Determine a resource description for the given bean definition and
217+
* apply basic JSON escaping (backslashes, double quotes) to it.
218+
* @param bd the bean definition to build the resource description for
219+
* @return the JSON-escaped resource description
220+
*/
221+
protected String getEscapedResourceDescription(BeanDefinition bd) {
222+
String resourceDescription = bd.getResourceDescription();
223+
if (resourceDescription == null) {
224+
return null;
225+
}
226+
StringBuilder result = new StringBuilder(resourceDescription.length() + 16);
227+
for (int i = 0; i < resourceDescription.length(); i++) {
228+
char character = resourceDescription.charAt(i);
229+
if (character == '\\') {
230+
result.append('/');
231+
}
232+
else if (character == '"') {
233+
result.append("\\").append('"');
234+
}
235+
else {
236+
result.append(character);
237+
}
238+
}
239+
return result.toString();
240+
}
241+
214242
}

0 commit comments

Comments
 (0)