Browse Source

Polish UriBuilderFactory and implementation

Issue: SPR-16422
pull/1690/head
Rossen Stoyanchev 7 years ago
parent
commit
4db0d999af
  1. 20
      spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
  2. 65
      spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java
  3. 14
      spring-web/src/main/java/org/springframework/web/util/UriBuilderFactory.java
  4. 25
      spring-web/src/main/java/org/springframework/web/util/UriTemplateHandler.java

20
spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

@ -298,18 +298,16 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
} }
/** /**
* Configure the {@link UriTemplateHandler} to use to expand URI templates. * Customize how URI templates are expanded into URI instances.
* By default the {@link DefaultUriBuilderFactory} is used which relies on * <p>By default {@link DefaultUriBuilderFactory} with default settings is
* Spring's URI template support and exposes several useful properties that * used. You can supply a {@code DefaultUriBuilderFactory} configured
* customize its behavior for encoding and for pre-pending a common base URL. * differently, or an entirely different implementation, for example that
* An alternative implementation may be used to plug an external URI * plugs in a 3rd party URI template library.
* template library. * <p><strong>Note:</strong> in 5.0 the switch from
* <p><strong>Note:</strong> if switching from
* {@link org.springframework.web.util.DefaultUriTemplateHandler * {@link org.springframework.web.util.DefaultUriTemplateHandler
* DefaultUriTemplateHandler} (deprecated in 4.3) to * DefaultUriTemplateHandler} (deprecated in 4.3), as the default to use, to
* {@link DefaultUriBuilderFactory} keep in mind that the * {@link DefaultUriBuilderFactory} brings in a different default for the
* {@link DefaultUriBuilderFactory} has a different default for the * {@code parsePath} property (switching from false to true).
* {@code parsePath} property (from false to true).
* @param handler the URI template handler to use * @param handler the URI template handler to use
*/ */
public void setUriTemplateHandler(UriTemplateHandler handler) { public void setUriTemplateHandler(UriTemplateHandler handler) {

65
spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java

@ -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) {

14
spring-web/src/main/java/org/springframework/web/util/UriBuilderFactory.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,9 @@
package org.springframework.web.util; package org.springframework.web.util;
/** /**
* Factory to create {@link UriBuilder} instances pre-configured in a specific * Factory to create {@link UriBuilder} instances with shared configuration
* way such as sharing a common base URI across all builders. * such as a base URI, an encoding mode strategy, and others across all URI
* builder instances created through a factory.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 5.0 * @since 5.0
@ -25,15 +26,14 @@ package org.springframework.web.util;
public interface UriBuilderFactory extends UriTemplateHandler { public interface UriBuilderFactory extends UriTemplateHandler {
/** /**
* Create a builder from the given URI template string. * Initialize a builder with the given URI template.
* Implementations may further combine the URI template with a base URI.
* @param uriTemplate the URI template to use * @param uriTemplate the URI template to use
* @return the builder instance * @return the URI builder instance
*/ */
UriBuilder uriString(String uriTemplate); UriBuilder uriString(String uriTemplate);
/** /**
* Create a builder with default settings. * Create a URI builder with default settings.
* @return the builder instance * @return the builder instance
*/ */
UriBuilder builder(); UriBuilder builder();

25
spring-web/src/main/java/org/springframework/web/util/UriTemplateHandler.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,30 +20,27 @@ import java.net.URI;
import java.util.Map; import java.util.Map;
/** /**
* Strategy for expanding a URI template. * Defines methods for expanding a URI template with variables.
*
* <p>Supported as a property on the {@code RestTemplate} as well as the
* {@code AsyncRestTemplate}.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.2 * @since 4.2
* @see DefaultUriBuilderFactory * @see org.springframework.web.client.RestTemplate#setUriTemplateHandler(UriTemplateHandler)
*/ */
public interface UriTemplateHandler { public interface UriTemplateHandler {
/** /**
* Expand the given URI template from a map of URI variables. * Expand the given URI template with a map of URI variables.
* @param uriTemplate the URI template string * @param uriTemplate the URI template
* @param uriVariables the URI variables * @param uriVariables variable values
* @return the resulting URI * @return the created URI instance
*/ */
URI expand(String uriTemplate, Map<String, ?> uriVariables); URI expand(String uriTemplate, Map<String, ?> uriVariables);
/** /**
* Expand the given URI template from an array of URI variables. * Expand the given URI template with an array of URI variables.
* @param uriTemplate the URI template string * @param uriTemplate the URI template
* @param uriVariables the URI variable values * @param uriVariables variable values
* @return the resulting URI * @return the created URI instance
*/ */
URI expand(String uriTemplate, Object... uriVariables); URI expand(String uriTemplate, Object... uriVariables);

Loading…
Cancel
Save