Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property Resolving in @RequestMapping for Spring Boot #1375

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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> ENVIRONMENT = new ThreadLocal<>();

static Environment get() {
return ENVIRONMENT.get();
}

static void set(Environment environment) {
ENVIRONMENT.set(environment);
}

static void clear() {
ENVIRONMENT.remove();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -84,13 +83,13 @@ public Collection<HttpMethod> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

/**
Expand All @@ -74,14 +79,22 @@ 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 {}

@RequestMapping("/${test.parent}")
interface ResolveMethodEndpointController {

@RequestMapping("/${test.child}")
void method();
Object method();
}

@Configuration
Expand Down