|
|
@ -17,6 +17,7 @@ |
|
|
|
package org.springframework.web.util; |
|
|
|
package org.springframework.web.util; |
|
|
|
|
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
import java.net.URI; |
|
|
|
|
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
@ -28,11 +29,12 @@ import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
import org.springframework.util.ObjectUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Default implementation of {@link UriBuilderFactory} providing options to |
|
|
|
* {@code UriBuilderFactory} that relies on {@link UriComponentsBuilder} for |
|
|
|
* pre-configure all {@link UriBuilder} instances with common properties |
|
|
|
* the actual building of the URI. |
|
|
|
* such as a base URI, encoding mode, and default URI variables. |
|
|
|
* |
|
|
|
|
|
|
|
* <p>Provides options to create {@link UriBuilder} instances with a common |
|
|
|
|
|
|
|
* base URI, alternative encoding mode strategies, among others. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Uses {@link UriComponentsBuilder} for URI building. |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @since 5.0 |
|
|
|
* @since 5.0 |
|
|
@ -40,7 +42,43 @@ import org.springframework.util.ObjectUtils; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class DefaultUriBuilderFactory implements UriBuilderFactory { |
|
|
|
public class DefaultUriBuilderFactory implements UriBuilderFactory { |
|
|
|
|
|
|
|
|
|
|
|
public enum EncodingMode {URI_COMPONENT, VALUES_ONLY, NONE} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Constants that represent different URI encoding strategies. |
|
|
|
|
|
|
|
* @see #setEncodingMode |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public enum EncodingMode { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The default way of encoding that {@link UriComponents} supports: |
|
|
|
|
|
|
|
* <ol> |
|
|
|
|
|
|
|
* <li>Expand URI variables. |
|
|
|
|
|
|
|
* <li>Encode individual URI components as described in |
|
|
|
|
|
|
|
* {@link UriComponents#encode(Charset)}. |
|
|
|
|
|
|
|
* </ol> |
|
|
|
|
|
|
|
* <p>This mode <strong>does not</strong> encode all characters with |
|
|
|
|
|
|
|
* reserved meaning but only the ones that are illegal within a given |
|
|
|
|
|
|
|
* URI component as defined in RFC 396. This matches the way the |
|
|
|
|
|
|
|
* multi-argument {@link URI} constructor does encoding. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
URI_COMPONENT, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Comprehensive encoding of URI variable values prior to expanding: |
|
|
|
|
|
|
|
* <ol> |
|
|
|
|
|
|
|
* <li>Apply {@link UriUtils#encode(String, Charset)} to each URI variable value. |
|
|
|
|
|
|
|
* <li>Expand URI variable values. |
|
|
|
|
|
|
|
* </ol> |
|
|
|
|
|
|
|
* <p>This mode encodes all characters with reserved meaning, therefore |
|
|
|
|
|
|
|
* ensuring that expanded URI variable do not have any impact on the |
|
|
|
|
|
|
|
* structure or meaning of the URI. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
VALUES_ONLY, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* No encoding should be applied. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
NONE } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final UriComponentsBuilder baseUri; |
|
|
|
private final UriComponentsBuilder baseUri; |
|
|
@ -103,20 +141,9 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Specify the encoding mode to use when building URIs: |
|
|
|
* Specify the {@link EncodingMode EncodingMode} to use when building URIs. |
|
|
|
* <ul> |
|
|
|
* <p>By default set to |
|
|
|
* <li>URI_COMPONENT -- expand the URI variables first and then encode all URI |
|
|
|
* {@link EncodingMode#URI_COMPONENT EncodingMode.URI_COMPONENT}. |
|
|
|
* component (e.g. host, path, query, etc) according to the encoding rules |
|
|
|
|
|
|
|
* for each individual component. |
|
|
|
|
|
|
|
* <li>VALUES_ONLY -- encode URI variable values only, prior to expanding |
|
|
|
|
|
|
|
* them, using a "strict" encoding mode, i.e. encoding all characters |
|
|
|
|
|
|
|
* outside the unreserved set as defined in |
|
|
|
|
|
|
|
* <a href="https://tools.ietf.org/html/rfc3986#section-2">RFC 3986 Section 2</a>. |
|
|
|
|
|
|
|
* This ensures a URI variable value will not contain any characters with a |
|
|
|
|
|
|
|
* reserved purpose. |
|
|
|
|
|
|
|
* <li>NONE -- in this mode no encoding is performed. |
|
|
|
|
|
|
|
* </ul> |
|
|
|
|
|
|
|
* <p>By default this is set to {@code "URI_COMPONENT"}. |
|
|
|
|
|
|
|
* @param encodingMode the encoding mode to use |
|
|
|
* @param encodingMode the encoding mode to use |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setEncodingMode(EncodingMode encodingMode) { |
|
|
|
public void setEncodingMode(EncodingMode encodingMode) { |
|
|
|