Skip to content

Commit 2fde8ef

Browse files
committed
Fixed URI encoding in URIEditor to be RFC 2396 compliant
1 parent eb47a4b commit 2fde8ef

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/propertyeditors/URIEditor.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,10 +105,19 @@ public void setAsText(String text) throws IllegalArgumentException {
105105
* constructor, replacing spaces with "%20" quotes first.
106106
* @param value the value to convert into a URI instance
107107
* @return the URI instance
108-
* @throws URISyntaxException if URI conversion failed
108+
* @throws java.net.URISyntaxException if URI conversion failed
109109
*/
110110
protected URI createURI(String value) throws URISyntaxException {
111-
return new URI(StringUtils.replace(value, " ", "%20"));
111+
int idx = value.indexOf(':');
112+
if (idx != -1) {
113+
String scheme = value.substring(0, idx);
114+
String ssp = value.substring(idx + 1);
115+
return new URI(scheme, ssp, null);
116+
}
117+
else {
118+
// value contains no scheme, fallback to default
119+
return new URI(value);
120+
}
112121
}
113122

114123

org.springframework.beans/src/test/java/org/springframework/beans/propertyeditors/URIEditorTests.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,16 +19,19 @@
1919
import java.beans.PropertyEditor;
2020
import java.net.URI;
2121

22-
import junit.framework.TestCase;
22+
import static org.junit.Assert.*;
23+
import org.junit.Test;
2324

2425
import org.springframework.util.ClassUtils;
2526

2627
/**
2728
* @author Juergen Hoeller
29+
* @author Arjen Poutsma
2830
*/
29-
public class URIEditorTests extends TestCase {
31+
public class URIEditorTests {
3032

31-
public void testStandardURI() throws Exception {
33+
@Test
34+
public void standardURI() throws Exception {
3235
PropertyEditor uriEditor = new URIEditor();
3336
uriEditor.setAsText("mailto:[email protected]");
3437
Object value = uriEditor.getValue();
@@ -37,7 +40,8 @@ public void testStandardURI() throws Exception {
3740
assertEquals(uri.toString(), uriEditor.getAsText());
3841
}
3942

40-
public void testStandardURL() throws Exception {
43+
@Test
44+
public void standardURL() throws Exception {
4145
PropertyEditor uriEditor = new URIEditor();
4246
uriEditor.setAsText("http://www.springframework.org");
4347
Object value = uriEditor.getValue();
@@ -46,7 +50,8 @@ public void testStandardURL() throws Exception {
4650
assertEquals(uri.toString(), uriEditor.getAsText());
4751
}
4852

49-
public void testStandardURLWithWhitespace() throws Exception {
53+
@Test
54+
public void standardURLWithWhitespace() throws Exception {
5055
PropertyEditor uriEditor = new URIEditor();
5156
uriEditor.setAsText(" http://www.springframework.org ");
5257
Object value = uriEditor.getValue();
@@ -55,7 +60,8 @@ public void testStandardURLWithWhitespace() throws Exception {
5560
assertEquals(uri.toString(), uriEditor.getAsText());
5661
}
5762

58-
public void testClasspathURL() throws Exception {
63+
@Test
64+
public void classpathURL() throws Exception {
5965
PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader());
6066
uriEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
6167
"/" + ClassUtils.getShortName(getClass()) + ".class");
@@ -66,7 +72,8 @@ public void testClasspathURL() throws Exception {
6672
assertTrue(!uri.getScheme().startsWith("classpath"));
6773
}
6874

69-
public void testClasspathURLWithWhitespace() throws Exception {
75+
@Test
76+
public void classpathURLWithWhitespace() throws Exception {
7077
PropertyEditor uriEditor = new URIEditor(getClass().getClassLoader());
7178
uriEditor.setAsText(" classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) +
7279
"/" + ClassUtils.getShortName(getClass()) + ".class ");
@@ -77,7 +84,8 @@ public void testClasspathURLWithWhitespace() throws Exception {
7784
assertTrue(!uri.getScheme().startsWith("classpath"));
7885
}
7986

80-
public void testClasspathURLAsIs() throws Exception {
87+
@Test
88+
public void classpathURLAsIs() throws Exception {
8189
PropertyEditor uriEditor = new URIEditor();
8290
uriEditor.setAsText("classpath:test.txt");
8391
Object value = uriEditor.getValue();
@@ -87,7 +95,8 @@ public void testClasspathURLAsIs() throws Exception {
8795
assertTrue(uri.getScheme().startsWith("classpath"));
8896
}
8997

90-
public void testWithNonExistentResource() throws Exception {
98+
@Test
99+
public void withNonExistentResource() throws Exception {
91100
PropertyEditor uriEditor = new URIEditor();
92101
uriEditor.setAsText("gonna:/freak/in/the/morning/freak/in/the.evening");
93102
Object value = uriEditor.getValue();
@@ -96,16 +105,29 @@ public void testWithNonExistentResource() throws Exception {
96105
assertEquals(uri.toString(), uriEditor.getAsText());
97106
}
98107

99-
public void testSetAsTextWithNull() throws Exception {
108+
@Test
109+
public void setAsTextWithNull() throws Exception {
100110
PropertyEditor uriEditor = new URIEditor();
101111
uriEditor.setAsText(null);
102112
assertNull(uriEditor.getValue());
103113
assertEquals("", uriEditor.getAsText());
104114
}
105115

106-
public void testGetAsTextReturnsEmptyStringIfValueNotSet() throws Exception {
116+
@Test
117+
public void getAsTextReturnsEmptyStringIfValueNotSet() throws Exception {
107118
PropertyEditor uriEditor = new URIEditor();
108119
assertEquals("", uriEditor.getAsText());
109120
}
110121

122+
@Test
123+
public void encodeURI() throws Exception {
124+
PropertyEditor uriEditor = new URIEditor();
125+
uriEditor.setAsText("http://example.com/spaces and \u20AC");
126+
Object value = uriEditor.getValue();
127+
assertTrue(value instanceof URI);
128+
URI uri = (URI) value;
129+
assertEquals(uri.toString(), uriEditor.getAsText());
130+
assertEquals("http://example.com/spaces%20and%20%E2%82%AC", uri.toASCIIString());
131+
}
132+
111133
}

0 commit comments

Comments
 (0)