33
33
* (<code>{</code>, <code>}</code>), which can be expanded to produce a URI.
34
34
* <p/>
35
35
* See {@link #expand(Map)}, {@link #expand(String[])}, and {@link #match(String)} for example usages.
36
- *
37
36
* @author Arjen Poutsma
38
37
* @see <a href="http://bitworking.org/projects/URI-Templates/">URI Templates</a>
38
+ * @since 3.0
39
39
*/
40
40
public final class UriTemplate {
41
41
@@ -57,7 +57,6 @@ public final class UriTemplate {
57
57
58
58
/**
59
59
* Constructs a new {@link UriTemplate} with the given string.
60
- *
61
60
* @param uriTemplate the uri template string
62
61
*/
63
62
public UriTemplate (String uriTemplate ) {
@@ -69,7 +68,6 @@ public UriTemplate(String uriTemplate) {
69
68
70
69
/**
71
70
* Returns the names of the variables in the template, in order.
72
- *
73
71
* @return the template variable names
74
72
*/
75
73
public List <String > getVariableNames () {
@@ -89,7 +87,6 @@ public List<String> getVariableNames() {
89
87
* System.out.println(template.expand(uriVariables));
90
88
* </pre>
91
89
* will print: <blockquote><code>http://example.com/hotels/1/bookings/42</code></blockquote>
92
- *
93
90
* @param uriVariables the map of uri variables
94
91
* @return the expanded uri
95
92
* @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>; or if it does not contain
@@ -116,7 +113,6 @@ public URI expand(Map<String, String> uriVariables) {
116
113
* System.out.println(template.expand("1", "42));
117
114
* </pre>
118
115
* will print: <blockquote><code>http://example.com/hotels/1/bookings/42</code></blockquote>
119
- *
120
116
* @param uriVariableValues the array of uri variables
121
117
* @return the expanded uri
122
118
* @throws IllegalArgumentException if <code>uriVariables</code> is <code>null</code>; or if it does not contain
@@ -137,12 +133,11 @@ public URI expand(String... uriVariableValues) {
137
133
m .appendReplacement (buffer , uriVariable );
138
134
}
139
135
m .appendTail (buffer );
140
- return URI . create (buffer .toString ());
136
+ return encodeUri (buffer .toString ());
141
137
}
142
138
143
139
/**
144
140
* Indicates whether the given URI matches this template.
145
- *
146
141
* @param uri the URI to match to
147
142
* @return <code>true</code> if it matches; <code>false</code> otherwise
148
143
*/
@@ -164,7 +159,6 @@ public boolean matches(String uri) {
164
159
* System.out.println(template.match("http://example.com/hotels/1/bookings/42"));
165
160
* </pre>
166
161
* will print: <blockquote><code>{hotel=1, booking=42}</code></blockquote>
167
- *
168
162
* @param uri the URI to match to
169
163
* @return a map of variable values
170
164
*/
@@ -182,6 +176,26 @@ public Map<String, String> match(String uri) {
182
176
return result ;
183
177
}
184
178
179
+ private static URI encodeUri (String uri ) {
180
+ try {
181
+ int idx = uri .indexOf (':' );
182
+ URI result ;
183
+ if (idx != -1 ) {
184
+ String scheme = uri .substring (0 , idx );
185
+ String ssp = uri .substring (idx + 1 );
186
+ result = new URI (scheme , ssp , null );
187
+ }
188
+ else {
189
+ result = new URI (null , null , uri , null );
190
+ }
191
+ return result ;
192
+ }
193
+ catch (URISyntaxException e ) {
194
+ throw new IllegalArgumentException ("Could not create URI from [" + uri + "]" );
195
+ }
196
+ }
197
+
198
+
185
199
@ Override
186
200
public String toString () {
187
201
return uriTemplate ;
@@ -218,17 +232,11 @@ private String encodeAndQuote(String fullPath, int start, int end) {
218
232
if (start == end ) {
219
233
return "" ;
220
234
}
221
- String result = fullPath .substring (start , end );
222
- try {
223
- URI uri = new URI (null , null , result , null );
224
- result = uri .toASCIIString ();
225
- }
226
- catch (URISyntaxException e ) {
227
- throw new IllegalArgumentException ("Could not create URI from [" + fullPath + "]" );
228
- }
235
+ String result = encodeUri (fullPath .substring (start , end )).toASCIIString ();
229
236
return Pattern .quote (result );
230
237
}
231
238
239
+
232
240
private List <String > getVariableNames () {
233
241
return Collections .unmodifiableList (variableNames );
234
242
}
0 commit comments