|
17 | 17 | package org.springframework.web.util;
|
18 | 18 |
|
19 | 19 | import java.net.URI;
|
20 |
| -import java.nio.charset.Charset; |
21 | 20 | import java.util.Collections;
|
22 | 21 | import java.util.HashMap;
|
23 | 22 | import java.util.Map;
|
@@ -49,16 +48,33 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
|
49 | 48 | public enum EncodingMode {
|
50 | 49 |
|
51 | 50 | /**
|
52 |
| - * Apply strict encoding to URI variables at the time of expanding, |
53 |
| - * quoting both illegal characters and characters with reserved meaning |
54 |
| - * via {@link UriUtils#encode(String, Charset)}. |
| 51 | + * Encode the URI template first, and URI variables later when expanded, |
| 52 | + * applying the following to each: |
| 53 | + * <ul> |
| 54 | + * <li>URI template is encoded by quoting <em>only</em> illegal |
| 55 | + * characters within a given URI component type. |
| 56 | + * <li>URI variables are encoded strictly, by quoting both illegal |
| 57 | + * characters and characters with reserved meaning. |
| 58 | + * </ul> |
| 59 | + * <p>For most cases this should be the preferred encoding mode. |
| 60 | + * @since 5.0.8 |
| 61 | + * @see UriComponentsBuilder#encode() |
| 62 | + */ |
| 63 | + TEMPLATE_AND_VALUES, |
| 64 | + |
| 65 | + /** |
| 66 | + * Encode only URI variables strictly quoting both illegal characters |
| 67 | + * and characters with reserved meaning. |
| 68 | + * @see UriUtils#encodeUriVariables(Object...) |
| 69 | + * @see UriUtils#encodeUriVariables(Map) |
55 | 70 | */
|
56 | 71 | VALUES_ONLY,
|
57 | 72 |
|
58 | 73 | /**
|
59 |
| - * Expand URI variables first, then encode the resulting URI component |
| 74 | + * Expand URI variables first, and then encode the expanded URI component |
60 | 75 | * values, quoting <em>only</em> illegal characters within a given URI
|
61 | 76 | * component type, but not all characters with reserved meaning.
|
| 77 | + * @see UriComponents#encode() |
62 | 78 | */
|
63 | 79 | URI_COMPONENT,
|
64 | 80 |
|
@@ -110,7 +126,7 @@ public DefaultUriBuilderFactory(UriComponentsBuilder baseUri) {
|
110 | 126 |
|
111 | 127 |
|
112 | 128 | /**
|
113 |
| - * Specify the {@link EncodingMode EncodingMode} to use when building URIs. |
| 129 | + * Specify the {@link EncodingMode EncodingMode} to use to encode URIs. |
114 | 130 | * <p>By default set to
|
115 | 131 | * {@link EncodingMode#URI_COMPONENT EncodingMode.URI_COMPONENT}.
|
116 | 132 | * @param encodingMode the encoding mode to use
|
@@ -216,13 +232,17 @@ private UriComponentsBuilder initUriComponentsBuilder(String uriTemplate) {
|
216 | 232 | result = UriComponentsBuilder.fromUriString(uriTemplate);
|
217 | 233 | }
|
218 | 234 |
|
| 235 | + if (encodingMode.equals(EncodingMode.TEMPLATE_AND_VALUES)) { |
| 236 | + result.encode(); |
| 237 | + } |
| 238 | + |
219 | 239 | parsePathIfNecessary(result);
|
220 | 240 |
|
221 | 241 | return result;
|
222 | 242 | }
|
223 | 243 |
|
224 | 244 | private void parsePathIfNecessary(UriComponentsBuilder result) {
|
225 |
| - if (shouldParsePath() && encodingMode.equals(EncodingMode.URI_COMPONENT)) { |
| 245 | + if (parsePath && encodingMode.equals(EncodingMode.URI_COMPONENT)) { |
226 | 246 | UriComponents uric = result.build();
|
227 | 247 | String path = uric.getPath();
|
228 | 248 | result.replacePath(null);
|
|
0 commit comments