#749 - Polishing.

Significant rewrite of the code contributed in the previous commit. Made all caches static and extracted all UriComponentsBuilder lookup and caching code into a package protected UriComponentsBuilderFactory.
This commit is contained in:
Oliver Drotbohm
2019-02-20 18:41:32 +01:00
parent a5f2b602fb
commit 77392de587
5 changed files with 286 additions and 178 deletions

View File

@@ -1,21 +1,56 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.core;
import lombok.RequiredArgsConstructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.StringUtils;
/**
* Caching adapter of {@link MappingDiscoverer}.
*
* @author Michal Stochmialek
* @author Oliver Drotbohm
*/
@RequiredArgsConstructor(staticName = "of")
public class CachingMappingDiscoverer implements MappingDiscoverer {
private Map<String, String> mappingCache = new ConcurrentHashMap<String, String>();
private MappingDiscoverer discoverer;
public CachingMappingDiscoverer(MappingDiscoverer discoverer) {
this.discoverer = discoverer;
}
private static final Map<String, String> CACHE = new ConcurrentReferenceHashMap<String, String>();
private final MappingDiscoverer discoverer;
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class)
*/
@Override
public String getMapping(final Class<?> type) {
String key = key(type, null);
return getMapping(key, new CachedCall() {
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.CachingMappingDiscoverer.CachedCall#getMapping()
*/
@Override
public String getMapping() {
return discoverer.getMapping(type);
@@ -23,10 +58,21 @@ public class CachingMappingDiscoverer implements MappingDiscoverer {
});
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.reflect.Method)
*/
@Override
public String getMapping(final Method method) {
String key = key(method.getDeclaringClass(), method);
return getMapping(key, new CachedCall() {
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.CachingMappingDiscoverer.CachedCall#getMapping()
*/
@Override
public String getMapping() {
return discoverer.getMapping(method);
@@ -34,10 +80,21 @@ public class CachingMappingDiscoverer implements MappingDiscoverer {
});
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class, java.lang.reflect.Method)
*/
@Override
public String getMapping(final Class<?> type, final Method method) {
String key = key(type, method);
return getMapping(key, new CachedCall() {
/*
* (non-Javadoc)
* @see org.springframework.hateoas.core.CachingMappingDiscoverer.CachedCall#getMapping()
*/
@Override
public String getMapping() {
return discoverer.getMapping(type, method);
@@ -45,12 +102,17 @@ public class CachingMappingDiscoverer implements MappingDiscoverer {
});
}
public String getMapping(String key, CachedCall cachedCall) {
if (mappingCache.containsKey(key)) {
return mappingCache.get(key);
private String getMapping(String key, CachedCall cachedCall) {
if (CACHE.containsKey(key)) {
return CACHE.get(key);
} else {
String mapping = cachedCall.getMapping();
mappingCache.put(key, mapping);
CACHE.put(key, mapping);
return mapping;
}
}
@@ -59,15 +121,17 @@ public class CachingMappingDiscoverer implements MappingDiscoverer {
String getMapping();
}
private String key(Class<?> type, Method method) {
StringBuilder buf = new StringBuilder();
buf.append(type.getName());
if (method != null) {
buf.append(method.getName());
for (Class<?> par: method.getParameterTypes()) {
buf.append(par.getName());
}
private static String key(Class<?> type, Method method) {
StringBuilder builder = new StringBuilder(type.getName());
if (method == null) {
return builder.toString();
}
return buf.toString();
builder.append(method.getName());
builder.append(StringUtils.arrayToCommaDelimitedString(method.getParameterTypes()));
return builder.toString();
}
}

View File

@@ -15,31 +15,18 @@
*/
package org.springframework.hateoas.mvc;
import static org.springframework.hateoas.mvc.ForwardedHeader.*;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.TemplateVariables;
import org.springframework.hateoas.core.AnnotationMappingDiscoverer;
import org.springframework.hateoas.core.CachingMappingDiscoverer;
import org.springframework.hateoas.core.DummyInvocationUtils;
import org.springframework.hateoas.core.LinkBuilderSupport;
import org.springframework.hateoas.core.MappingDiscoverer;
import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.DefaultUriTemplateHandler;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -47,7 +34,7 @@ import org.springframework.web.util.UriTemplate;
/**
* Builder to ease building {@link Link} instances pointing to Spring MVC controllers.
*
*
* @author Oliver Gierke
* @author Kamill Sokol
* @author Greg Turnquist
@@ -58,18 +45,16 @@ import org.springframework.web.util.UriTemplate;
*/
public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
private static final String REQUEST_ATTRIBUTES_MISSING = "Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?";
private static final CachingAnnotationMappingDiscoverer DISCOVERER = new CachingAnnotationMappingDiscoverer(
new AnnotationMappingDiscoverer(RequestMapping.class));
private static final CachingMappingDiscoverer DISCOVERER = CachingMappingDiscoverer
.of(new AnnotationMappingDiscoverer(RequestMapping.class));
private static final ControllerLinkBuilderFactory FACTORY = new ControllerLinkBuilderFactory();
private static final String CACHE_KEY = ControllerLinkBuilder.class.getName() + "#BUILDER_CACHE";
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler();
private final TemplateVariables variables;
/**
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponentsBuilder}.
*
*
* @param builder must not be {@literal null}.
*/
ControllerLinkBuilder(UriComponentsBuilder builder) {
@@ -97,7 +82,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
/**
* Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class.
*
*
* @param controller the class to discover the annotation on, must not be {@literal null}.
* @return
*/
@@ -108,7 +93,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
/**
* Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class. The
* additional parameters are used to fill up potentially available path variables in the class scop request mapping.
*
*
* @param controller the class to discover the annotation on, must not be {@literal null}.
* @param parameters additional parameters to bind to the URI template declared in the annotation, must not be
* {@literal null}.
@@ -124,7 +109,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
return new ControllerLinkBuilder(getBuilder()).slash(uriComponents, true);
return new ControllerLinkBuilder(UriComponentsBuilderFactory.getBuilder()).slash(uriComponents, true);
}
/**
@@ -146,7 +131,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
return new ControllerLinkBuilder(getBuilder()).slash(uriComponents, true);
return new ControllerLinkBuilder(UriComponentsBuilderFactory.getBuilder()).slash(uriComponents, true);
}
/*
@@ -164,31 +149,32 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
Assert.notNull(controller, "Controller type must not be null!");
Assert.notNull(method, "Method must not be null!");
UriTemplate template = DISCOVERER.getMappingAsUriTemplate(controller, method);
String mapping = DISCOVERER.getMapping(controller, method);
UriTemplate template = UriTemplateFactory.templateFor(mapping);
URI uri = template.expand(parameters);
return new ControllerLinkBuilder(getBuilder()).slash(uri);
return new ControllerLinkBuilder(UriComponentsBuilderFactory.getBuilder()).slash(uri);
}
/**
* Creates a {@link ControllerLinkBuilder} pointing to a controller method. Hand in a dummy method invocation result
* you can create via {@link #methodOn(Class, Object...)} or {@link DummyInvocationUtils#methodOn(Class, Object...)}.
*
*
* <pre>
* &#64;RequestMapping("/customers")
* class CustomerController {
*
*
* &#64;RequestMapping("/{id}/addresses")
* HttpEntity&lt;Addresses&gt; showAddresses(@PathVariable Long id) { … }
* HttpEntity&lt;Addresses&gt; showAddresses(@PathVariable Long id) { … }
* }
*
*
* Link link = linkTo(methodOn(CustomerController.class).showAddresses(2L)).withRel("addresses");
* </pre>
*
*
* The resulting {@link Link} instance will point to {@code /customers/2/addresses} and have a rel of
* {@code addresses}. For more details on the method invocation constraints, see
* {@link DummyInvocationUtils#methodOn(Class, Object...)}.
*
*
* @param invocationValue
* @return
*/
@@ -199,7 +185,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
/**
* Wrapper for {@link DummyInvocationUtils#methodOn(Class, Object...)} to be available in case you work with static
* imports of {@link ControllerLinkBuilder}.
*
*
* @param controller must not be {@literal null}.
* @param parameters parameters to extend template variables in the type level mapping.
* @return
@@ -208,7 +194,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
return DummyInvocationUtils.methodOn(controller, parameters);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.UriComponentsLinkBuilder#getThis()
*/
@@ -217,7 +203,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
return this;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.UriComponentsLinkBuilder#createNewInstance(org.springframework.web.util.UriComponentsBuilder)
*/
@@ -228,7 +214,7 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
/**
* Returns a {@link UriComponentsBuilder} to continue to build the already built URI in a more fine grained way.
*
*
* @return
*/
public UriComponentsBuilder toUriComponentsBuilder() {
@@ -256,107 +242,6 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
return parts[0].concat(variables.toString()).concat("#").concat(parts[0]);
}
/**
* Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with scheme tweaked in case the
* request contains an {@code X-Forwarded-Ssl} header, which is not (yet) supported by the underlying
* {@link UriComponentsBuilder}. If no {@link RequestContextHolder} exists (you're outside a Spring Web call), fall
* back to relative URIs.
*
* @return
*/
static UriComponentsBuilder getBuilder() {
if (RequestContextHolder.getRequestAttributes() == null) {
return UriComponentsBuilder.fromPath("/");
}
URI baseUri = getCachedBaseUri();
if (baseUri == null) {
UriComponentsBuilder builderFromRequest = createBuilderFromRequest();
cacheBaseUri(builderFromRequest.build().toUri());
return builderFromRequest;
} else {
return UriComponentsBuilder.fromUri(baseUri);
}
}
private static UriComponentsBuilder createBuilderFromRequest() {
HttpServletRequest request = getCurrentRequest();
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);
// Spring 5.1 can handle X-Forwarded-Ssl headers...
if (isSpringAtLeast5_1()) {
return builder;
} else {
return handleXForwardedSslHeader(request, builder);
}
}
/**
* Check if the current version of Spring Framework is 5.1 or higher.
*
* @return
*/
private static boolean isSpringAtLeast5_1() {
String versionOfSpringFramework = ApplicationContext.class.getPackage().getImplementationVersion();
String[] parts = versionOfSpringFramework.split("\\.");
int majorVersion = Integer.parseInt(parts[0]);
int minorVersion = Integer.parseInt(parts[1]);
return (majorVersion >= 5 && minorVersion >= 1) || (majorVersion > 5);
}
/**
* Copy of {@link ServletUriComponentsBuilder#getCurrentRequest()} until SPR-10110 gets fixed.
*
* @return
*/
@SuppressWarnings("null")
private static HttpServletRequest getCurrentRequest() {
RequestAttributes requestAttributes = getRequestAttributes();
HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
Assert.state(servletRequest != null, "Could not find current HttpServletRequest");
return servletRequest;
}
private static RequestAttributes getRequestAttributes() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
Assert.state(requestAttributes != null, REQUEST_ATTRIBUTES_MISSING);
Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
return requestAttributes;
}
private static void cacheBaseUri(URI uri) {
getRequestAttributes().setAttribute(CACHE_KEY, uri, RequestAttributes.SCOPE_REQUEST);
}
private static URI getCachedBaseUri() {
return (URI) getRequestAttributes().getAttribute(CACHE_KEY, RequestAttributes.SCOPE_REQUEST);
}
@RequiredArgsConstructor
private static class CachingAnnotationMappingDiscoverer implements MappingDiscoverer {
private final @Delegate AnnotationMappingDiscoverer delegate;
private final Map<String, UriTemplate> templates = new ConcurrentReferenceHashMap<String, UriTemplate>();
public UriTemplate getMappingAsUriTemplate(Class<?> type, Method method) {
String mapping = delegate.getMapping(type, method);
UriTemplate template = templates.get(mapping);
if (template == null) {
template = new UriTemplate(mapping);
templates.put(mapping, template);
}
return template;
}
}
private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {
public CustomUriTemplateHandler() {

View File

@@ -56,7 +56,7 @@ import org.springframework.web.util.UriTemplate;
/**
* Factory for {@link LinkBuilderSupport} instances based on the request mapping annotated on the given controller.
*
*
* @author Ricardo Gladwell
* @author Oliver Gierke
* @author Dietrich Schulten
@@ -68,19 +68,18 @@ import org.springframework.web.util.UriTemplate;
*/
public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<ControllerLinkBuilder> {
private static final MappingDiscoverer DISCOVERER = new CachingMappingDiscoverer(
new AnnotationMappingDiscoverer(RequestMapping.class));
private static final MappingDiscoverer DISCOVERER = CachingMappingDiscoverer
.of(new AnnotationMappingDiscoverer(RequestMapping.class));
private static final AnnotatedParametersParameterAccessor PATH_VARIABLE_ACCESSOR = new AnnotatedParametersParameterAccessor(
new AnnotationAttribute(PathVariable.class));
private static final AnnotatedParametersParameterAccessor REQUEST_PARAM_ACCESSOR = new RequestParamParameterAccessor();
private List<UriComponentsContributor> uriComponentsContributors = new ArrayList<UriComponentsContributor>();
private UriTemplateFactory uriTemplateFactory = new UriTemplateFactory();
/**
* Configures the {@link UriComponentsContributor} to be used when building {@link Link} instances from method
* invocations.
*
*
* @see #linkTo(Object)
* @param uriComponentsContributors the uriComponentsContributors to set
*/
@@ -115,7 +114,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
return ControllerLinkBuilder.linkTo(controller, parameters);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.Class, java.lang.reflect.Method, java.lang.Object[])
*/
@@ -124,7 +123,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
return ControllerLinkBuilder.linkTo(controller, method, parameters);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.Object)
*/
@@ -139,9 +138,9 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
Method method = invocation.getMethod();
String mapping = DISCOVERER.getMapping(invocation.getTargetType(), method);
UriComponentsBuilder builder = ControllerLinkBuilder.getBuilder().path(mapping);
UriComponentsBuilder builder = UriComponentsBuilderFactory.getBuilder().path(mapping);
UriTemplate template = uriTemplateFactory.templateFor(mapping);
UriTemplate template = UriTemplateFactory.templateFor(mapping);
Map<String, Object> values = new HashMap<String, Object>();
Iterator<String> names = template.getVariableNames().iterator();
@@ -189,7 +188,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
return new ControllerLinkBuilder(components, variables);
}
/*
/*
* (non-Javadoc)
* @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.reflect.Method, java.lang.Object[])
*/
@@ -200,7 +199,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
/**
* Applies the configured {@link UriComponentsContributor}s to the given {@link UriComponentsBuilder}.
*
*
* @param builder will never be {@literal null}.
* @param invocation will never be {@literal null}.
* @return
@@ -226,7 +225,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
/**
* Populates the given {@link UriComponentsBuilder} with request parameters found in the given
* {@link BoundMethodParameter}.
*
*
* @param builder must not be {@literal null}.
* @param parameter must not be {@literal null}.
*/
@@ -274,7 +273,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
/**
* Custom extension of {@link AnnotatedParametersParameterAccessor} for {@link RequestParam} to allow {@literal null}
* values handed in for optional request parameters.
*
*
* @author Oliver Gierke
*/
private static class RequestParamParameterAccessor extends AnnotatedParametersParameterAccessor {

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.mvc;
import static org.springframework.hateoas.mvc.ForwardedHeader.*;
import java.net.URI;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
/**
* Factory class for {@link UriComponentsBuilder} instances caching the lookups to avoid unnecessary subsequent lookups.
*
* @author Michal Stochmialek
* @author Oliver Gierke
*/
class UriComponentsBuilderFactory {
private static final String REQUEST_ATTRIBUTES_MISSING = "Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?";
private static final String CACHE_KEY = ControllerLinkBuilder.class.getName() + "#BUILDER_CACHE";
/**
* Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with scheme tweaked in case the
* request contains an {@code X-Forwarded-Ssl} header, which is not (yet) supported by the underlying
* {@link UriComponentsBuilder}. If no {@link RequestContextHolder} exists (you're outside a Spring Web call), fall
* back to relative URIs.
*
* @return
*/
public static UriComponentsBuilder getBuilder() {
if (RequestContextHolder.getRequestAttributes() == null) {
return UriComponentsBuilder.fromPath("/");
}
URI baseUri = getCachedBaseUri();
return baseUri != null //
? UriComponentsBuilder.fromUri(baseUri) //
: cacheBaseUri(createBuilderFromRequest());
}
private static UriComponentsBuilder createBuilderFromRequest() {
HttpServletRequest request = getCurrentRequest();
ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromServletMapping(request);
// Spring 5.1 can handle X-Forwarded-Ssl headers...
return isSpringAtLeast5_1() ? builder : handleXForwardedSslHeader(request, builder);
}
/**
* Check if the current version of Spring Framework is 5.1 or higher.
*
* @return
*/
private static boolean isSpringAtLeast5_1() {
String versionOfSpringFramework = ApplicationContext.class.getPackage().getImplementationVersion();
String[] parts = versionOfSpringFramework.split("\\.");
int majorVersion = Integer.parseInt(parts[0]);
int minorVersion = Integer.parseInt(parts[1]);
return majorVersion >= 5 && minorVersion >= 1 || majorVersion > 5;
}
/**
* Copy of {@link ServletUriComponentsBuilder#getCurrentRequest()} until SPR-10110 gets fixed.
*
* @return
*/
private static HttpServletRequest getCurrentRequest() {
RequestAttributes requestAttributes = getRequestAttributes();
HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
Assert.state(servletRequest != null, "Could not find current HttpServletRequest");
return servletRequest;
}
private static RequestAttributes getRequestAttributes() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
Assert.state(requestAttributes != null, REQUEST_ATTRIBUTES_MISSING);
Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
return requestAttributes;
}
private static UriComponentsBuilder cacheBaseUri(UriComponentsBuilder builder) {
URI uri = builder.build().toUri();
getRequestAttributes().setAttribute(CACHE_KEY, uri, RequestAttributes.SCOPE_REQUEST);
return builder;
}
private static URI getCachedBaseUri() {
return (URI) getRequestAttributes().getAttribute(CACHE_KEY, RequestAttributes.SCOPE_REQUEST);
}
}

View File

@@ -1,21 +1,55 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.mvc;
import java.util.Map;
import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.web.util.UriTemplate;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Builds and caches UriTemplates.
* Builds and caches {@link UriTemplate} instances.
*
* @author Michal Stochmialek
* @author Oliver Drotbohm
*/
class UriTemplateFactory {
private Map<String, UriTemplate> templateCache = new ConcurrentHashMap<String, UriTemplate>();
UriTemplate templateFor(String mapping) {
if (templateCache.containsKey(mapping)) {
return templateCache.get(mapping);
private static final Map<String, UriTemplate> CACHE = new ConcurrentReferenceHashMap<String, UriTemplate>();
/**
* Returns the the {@link UriTemplate} for the given mapping.
*
* @param mapping must not be {@literal null} or empty.
* @return
*/
public static UriTemplate templateFor(String mapping) {
Assert.hasText(mapping, "Mapping must not be null or empty!");
if (CACHE.containsKey(mapping)) {
return CACHE.get(mapping);
} else {
UriTemplate template = new UriTemplate(mapping);
templateCache.put(mapping, template);
CACHE.put(mapping, template);
return template;
}
}