This commit is contained in:
Rossen Stoyanchev
2018-07-17 16:40:52 -04:00
parent 0bd7a3646c
commit 6c4289e238
4 changed files with 63 additions and 50 deletions

View File

@@ -40,43 +40,44 @@ import org.springframework.util.StringUtils;
*/
public class DefaultUriBuilderFactory implements UriBuilderFactory {
/**
* Constants that represent different URI encoding strategies.
* Enum to represent multiple URI encoding strategies.
* @see #setEncodingMode
*/
public enum EncodingMode {
/**
* Encode the URI template first, and URI variables later when expanded,
* applying the following to each:
* Pre-encode the URI template first, then strictly encode URI variables
* when expanded, with the following rules:
* <ul>
* <li>URI template is encoded by quoting <em>only</em> illegal
* characters within a given URI component type.
* <li>URI variables are encoded strictly, by quoting both illegal
* characters and characters with reserved meaning.
* <li>For the URI template replace <em>only</em> non-ASCII and illegal
* (within a given URI component type) characters with escaped octets.
* <li>For URI variables do the same and also replace characters with
* reserved meaning.
* </ul>
* <p>For most cases this should be the preferred encoding mode.
* <p>For most cases, this mode is most likely to give the expected
* result because in treats URI variables as opaque data to be fully
* encoded, while {@link #URI_COMPONENT} by comparison is useful only
* if intentionally expanding URI variables with reserved characters.
* @since 5.0.8
* @see UriComponentsBuilder#encode()
*/
TEMPLATE_AND_VALUES,
/**
* Encode only URI variables strictly quoting both illegal characters
* and characters with reserved meaning.
* Does not encode the URI template and instead applies strict encoding
* to URI variables via {@link UriUtils#encodeUriVariables} prior to
* expanding them into the template.
* @see UriUtils#encodeUriVariables(Object...)
* @see UriUtils#encodeUriVariables(Map)
*/
VALUES_ONLY,
/**
* Expand URI variables first, and then encode the expanded URI component
* values, quoting <em>only</em> illegal characters within a given URI
* component type, but not all characters with reserved meaning.
* <p>This is the mode historically used in the {@code RestTemplate} but
* as of 5.0.8 {@link #TEMPLATE_AND_VALUES} is the recommended encoding
* mode to use instead.
* Expand URI variables first, and then encode the resulting URI
* component values, replacing <em>only</em> non-ASCII and illegal
* (within a given URI component type) characters, but not characters
* with reserved meaning.
* @see UriComponents#encode()
*/
URI_COMPONENT,
@@ -129,13 +130,16 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
/**
* Specify the {@link EncodingMode EncodingMode} to use to encode URIs.
*
* <p><strong>Note:</strong> in 5.1 the default value was changed from
* {@link EncodingMode#URI_COMPONENT URI_COMPONENT} to the now recommended
* {@link EncodingMode#TEMPLATE_AND_VALUES TEMPLATE_AND_VALUES} mode of
* encoding.
*
* Set the encoding mode to use.
* <p>By default this is set to {@link EncodingMode#TEMPLATE_AND_VALUES
* EncodingMode.TEMPLATE_AND_VALUES}.
* <p><strong>Note:</strong> In 5.1 the default was changed from
* {@link EncodingMode#URI_COMPONENT EncodingMode.URI_COMPONENT}.
* Consequently the {@code WebClient}, which relies on the built-in default
* has also been switched to the new default. The {@code RestTemplate}
* however sets this explicitly to {@link EncodingMode#URI_COMPONENT
* EncodingMode.URI_COMPONENT} explicitly for historic and backwards
* compatibility reasons.
* @param encodingMode the encoding mode to use
*/
public void setEncodingMode(EncodingMode encodingMode) {

View File

@@ -132,9 +132,10 @@ public abstract class UriComponents implements Serializable {
* Invoke this <em>after</em> expanding URI variables to encode the
* resulting URI component values.
* <p>In comparison to {@link UriComponentsBuilder#encode()}, this method
* quotes <em>only</em> illegal characters within a given URI component type,
* but not all characters with reserved meaning. For most cases, prefer
* using {@link UriComponentsBuilder#encode()} over this method.
* <em>only</em> replaces non-ASCII and illegal (within a given URI
* component type) characters, but not characters with reserved meaning.
* For most cases, {@link UriComponentsBuilder#encode()} is more likely
* to give the expected result.
* @see UriComponentsBuilder#encode()
*/
public final UriComponents encode() {

View File

@@ -328,19 +328,21 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
// Encode methods
/**
* Request to have the URI template encoded first at build time, and
* URI variables encoded later when expanded.
* Request to have the URI template pre-encoded at build time, and
* URI variables encoded separately when expanded.
* <p>In comparison to {@link UriComponents#encode()}, this method has the
* same effect on the URI template, i.e. each URI component is encoded by
* quoting <em>only</em> illegal characters within that URI component type.
* However URI variables are encoded more strictly, by quoting both illegal
* characters and characters with reserved meaning.
* <p>For most cases, prefer this method over {@link UriComponents#encode()}
* which is useful only if intentionally expanding variables with reserved
* characters. For example ';' is legal in a path, but also has reserved
* meaning as a separator. When expanding a variable that contains ';' it
* probably should be encoded, unless the intent is to insert path params
* through the expanded variable.
* replacing non-ASCII and illegal (within the URI component type) characters
* with escaped octets. However URI variables are encoded more strictly, by
* also escaping characters with reserved meaning.
* <p>For most cases, this method is more likely to give the expected result
* because in treats URI variables as opaque data to be fully encoded, while
* {@link UriComponents#encode()} is useful only if intentionally expanding
* URI variables that contain reserved characters.
* <p>For example ';' is legal in a path but has reserved meaning. This
* method replaces ";" with "%3B" in URI variables but not in the URI
* template. By contrast, {@link UriComponents#encode()} never replaces ";"
* since it is a legal character in a path.
* @since 5.0.8
*/
public final UriComponentsBuilder encode() {