diff --git a/src/main/java/org/springframework/hateoas/config/EnvironmentConfiguration.java b/src/main/java/org/springframework/hateoas/config/EnvironmentConfiguration.java new file mode 100644 index 000000000..7c3afad94 --- /dev/null +++ b/src/main/java/org/springframework/hateoas/config/EnvironmentConfiguration.java @@ -0,0 +1,29 @@ +/* + * Copyright 2020 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 + * + * https://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.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.hateoas.server.core.EnvironmentContextFilter; + +@Configuration +public class EnvironmentConfiguration { + + @Bean + EnvironmentContextFilter environmentContextFilter() { + return new EnvironmentContextFilter(); + } +} diff --git a/src/main/java/org/springframework/hateoas/server/core/EnvironmentContext.java b/src/main/java/org/springframework/hateoas/server/core/EnvironmentContext.java new file mode 100644 index 000000000..9d1753887 --- /dev/null +++ b/src/main/java/org/springframework/hateoas/server/core/EnvironmentContext.java @@ -0,0 +1,42 @@ +/* + * Copyright 2020 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 + * + * https://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.server.core; + +import org.springframework.core.env.Environment; + +/** + * {@code EnvironmentContext} holds the {@link Environment} of the current servlet request provided by the + * {@link EnvironmentContextFilter}. The {@link Environment} is needed by the {@link PropertyResolvingMappingDiscoverer} + * to be able to resolve properties. + * + * @author Lars Michele + */ +class EnvironmentContext { + + private static final ThreadLocal ENVIRONMENT = new ThreadLocal<>(); + + static Environment get() { + return ENVIRONMENT.get(); + } + + static void set(Environment environment) { + ENVIRONMENT.set(environment); + } + + static void clear() { + ENVIRONMENT.remove(); + } +} diff --git a/src/main/java/org/springframework/hateoas/server/core/EnvironmentContextFilter.java b/src/main/java/org/springframework/hateoas/server/core/EnvironmentContextFilter.java new file mode 100644 index 000000000..4a9703fa5 --- /dev/null +++ b/src/main/java/org/springframework/hateoas/server/core/EnvironmentContextFilter.java @@ -0,0 +1,60 @@ +/* + * Copyright 2020 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 + * + * https://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.server.core; + +import javax.servlet.*; +import java.io.IOException; + +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; + +/** + * The {@code EnvironmentContextFilter} populates the {@link EnvironmentContext} with the {@link Environment} of the + * current servlet request. + * + * @author Lars Michele + */ +public class EnvironmentContextFilter implements Filter, EnvironmentAware { + + private Environment environment; + + @Override + public void init(FilterConfig filterConfig) { + // NOOP + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) + throws IOException, ServletException { + try { + EnvironmentContext.set(environment); + chain.doFilter(servletRequest, servletResponse); + } finally { + EnvironmentContext.clear(); + } + } + + + @Override + public void destroy() { + // NOOP + } + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } +} diff --git a/src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java b/src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java index 9cb886897..342a0ff39 100644 --- a/src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java +++ b/src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java @@ -18,11 +18,10 @@ import java.lang.reflect.Method; import java.util.Collection; +import org.springframework.core.env.Environment; import org.springframework.http.HttpMethod; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import org.springframework.web.context.ContextLoader; -import org.springframework.web.context.WebApplicationContext; /** * Property resolving adapter of {@link MappingDiscoverer}. @@ -84,13 +83,13 @@ public Collection getRequestMethod(Class type, Method method) { private static String resolveProperties(@Nullable String mapping) { if (mapping == null) { - return mapping; + return null; } - WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); + Environment environment = EnvironmentContext.get(); - return context == null // + return environment == null // ? mapping // - : context.getEnvironment().resolvePlaceholders(mapping); + : environment.resolvePlaceholders(mapping); } } diff --git a/src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java b/src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java index dc4668892..a9a5b181e 100755 --- a/src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java +++ b/src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java @@ -16,21 +16,21 @@ package org.springframework.hateoas.server.core; import static org.assertj.core.api.Assertions.*; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; import java.lang.reflect.Method; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.hateoas.IanaLinkRelations; +import org.springframework.hateoas.Link; import org.springframework.hateoas.TestUtils; -import org.springframework.hateoas.server.core.AnnotationMappingDiscoverer; -import org.springframework.hateoas.server.core.PropertyResolvingMappingDiscoverer; -import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; /** @@ -40,14 +40,19 @@ * @author Oliver Drotbohm */ @SpringJUnitWebConfig(classes = PropertyResolvingMappingDiscovererUnitTest.Config.class) -@TestPropertySource(properties = { "test.parent=resolvedparent", "test.child=resolvedchild" }) +@TestPropertySource(properties = {"test.parent=resolvedparent", "test.child=resolvedchild"}) class PropertyResolvingMappingDiscovererUnitTest extends TestUtils { @Autowired WebApplicationContext context; @BeforeEach - void contextLoading() { - new ContextLoader(context).initWebApplicationContext(new MockServletContext()); + void fakeRequestFiltering() { + EnvironmentContext.set(context.getEnvironment()); + } + + @AfterEach + void clearContext() { + EnvironmentContext.clear(); } /** @@ -74,6 +79,14 @@ void resolvesVariablesInMappings() throws NoSuchMethodException { .isEqualTo("/resolvedparent/resolvedchild"); } + @Test + void resolvesVariablesInLinkToMethodOnController() { + + Link link = linkTo(methodOn(ResolveMethodEndpointController.class).method()).withSelfRel(); + assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF); + assertThat(link.getHref()).endsWith("/resolvedparent/resolvedchild"); + } + @RequestMapping("/${test.parent}") interface ResolveEndpointController {} @@ -81,7 +94,7 @@ interface ResolveEndpointController {} interface ResolveMethodEndpointController { @RequestMapping("/${test.child}") - void method(); + Object method(); } @Configuration