Skip to content

Commit

Permalink
#1719 - Move off DefaultUriTemplateHandler.
Browse files Browse the repository at this point in the history
DefaultUriTemplateHandler was deprecated and removed in Spring 6.0. We now use DefaultUriBuilderFactory as recommended in the deprecation part of DUTH's Javadoc.
  • Loading branch information
odrotbohm committed Jan 14, 2022
1 parent a370fb9 commit 2431358
Showing 1 changed file with 10 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.hateoas.server.core.SpringAffordanceBuilder;
import org.springframework.hateoas.server.core.TemplateVariableAwareLinkBuilderSupport;
import org.springframework.util.Assert;
import org.springframework.web.util.DefaultUriTemplateHandler;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

Expand All @@ -43,11 +43,10 @@
* @author Greg Turnquist
* @author Lars Michele
*/
@SuppressWarnings("deprecation")
public class WebMvcLinkBuilder extends TemplateVariableAwareLinkBuilderSupport<WebMvcLinkBuilder> {

private static final WebMvcLinkBuilderFactory FACTORY = new WebMvcLinkBuilderFactory();
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler();
private static final DefaultUriBuilderFactory URI_FACTORY = new DefaultUriBuilderFactory();

/**
* Creates a new {@link WebMvcLinkBuilder} using the given {@link UriComponentsBuilder}.
Expand Down Expand Up @@ -86,10 +85,11 @@ public static WebMvcLinkBuilder linkTo(Class<?> controller, Object... parameters
Assert.notNull(controller, "Controller must not be null!");
Assert.notNull(parameters, "Parameters must not be null!");

String mapping = SpringAffordanceBuilder.DISCOVERER.getMapping(controller);
var mapping = SpringAffordanceBuilder.DISCOVERER.getMapping(controller);
var defaulted = mapping == null ? "/" : mapping;

UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
var uri = URI_FACTORY.expand(defaulted, parameters);
var uriComponents = UriComponentsBuilder.fromUri(uri).build();

return new WebMvcLinkBuilder(UriComponentsBuilderFactory.getComponents()).slash(uriComponents, true);
}
Expand All @@ -108,10 +108,11 @@ public static WebMvcLinkBuilder linkTo(Class<?> controller, Map<String, ?> param
Assert.notNull(controller, "Controller must not be null!");
Assert.notNull(parameters, "Parameters must not be null!");

String mapping = SpringAffordanceBuilder.DISCOVERER.getMapping(controller);
var mapping = SpringAffordanceBuilder.DISCOVERER.getMapping(controller);
var defaulted = mapping == null ? "/" : mapping;

UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
var uri = URI_FACTORY.expand(defaulted, parameters);
var uriComponents = UriComponentsBuilder.fromUri(uri).build();

return new WebMvcLinkBuilder(UriComponentsBuilderFactory.getComponents()).slash(uriComponents, true);
}
Expand Down Expand Up @@ -254,29 +255,4 @@ protected WebMvcLinkBuilder createNewInstance(UriComponents components, List<Aff
public UriComponentsBuilder toUriComponentsBuilder() {
return UriComponentsBuilder.fromUri(toUri());
}

private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {

public CustomUriTemplateHandler() {
setStrictEncoding(true);
}

/*
* (non-Javadoc)
* @see org.springframework.web.util.DefaultUriTemplateHandler#expandAndEncode(org.springframework.web.util.UriComponentsBuilder, java.util.Map)
*/
@Override
public UriComponents expandAndEncode(UriComponentsBuilder builder, Map<String, ?> uriVariables) {
return super.expandAndEncode(builder, uriVariables);
}

/*
* (non-Javadoc)
* @see org.springframework.web.util.DefaultUriTemplateHandler#expandAndEncode(org.springframework.web.util.UriComponentsBuilder, java.lang.Object[])
*/
@Override
public UriComponents expandAndEncode(UriComponentsBuilder builder, Object[] uriVariables) {
return super.expandAndEncode(builder, uriVariables);
}
}
}

0 comments on commit 2431358

Please sign in to comment.