-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add method argument resolver for tenant identifier
Signed-off-by: Thomas Vitale <[email protected]>
- Loading branch information
1 parent
f8f9664
commit 30c9123
Showing
7 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...onfigure/src/main/java/io/arconia/autoconfigure/multitenancy/web/WebMvcConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.arconia.autoconfigure.multitenancy.web; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import io.arconia.web.multitenancy.context.annotations.TenantIdentifierArgumentResolver; | ||
|
||
/** | ||
* Register Arconia-specific Spring Web MVC configuration. | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
public class WebMvcConfiguration implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(new TenantIdentifierArgumentResolver()); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...a-web/src/main/java/io/arconia/web/multitenancy/context/annotations/TenantIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.arconia.web.multitenancy.context.annotations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation that is used to resolve the current tenant identifier as a method argument. | ||
*/ | ||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface TenantIdentifier { | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...ava/io/arconia/web/multitenancy/context/annotations/TenantIdentifierArgumentResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.arconia.web.multitenancy.context.annotations; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
import io.arconia.core.multitenancy.context.TenantContextHolder; | ||
|
||
/** | ||
* Allows resolving the current tenant identifier using the {@link TenantIdentifier} | ||
* annotation. | ||
* <p> | ||
* Example: | ||
* | ||
* <pre> | ||
* @RestController | ||
* class MyRestController { | ||
* @GetMapping("/tenant") | ||
* String getCurrentTenant(@CurrentTenantIdentifier String tenantIdentifier) { | ||
* return tenantIdentifier; | ||
* } | ||
* } | ||
* </pre> | ||
*/ | ||
public final class TenantIdentifierArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.getParameterAnnotation(TenantIdentifier.class) != null | ||
&& parameter.getParameterType().getTypeName().equals(String.class.getTypeName()); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) { | ||
return TenantContextHolder.getTenantIdentifier(); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
arconia-web/src/main/java/io/arconia/web/multitenancy/context/annotations/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package io.arconia.web.multitenancy.context.annotations; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
80 changes: 80 additions & 0 deletions
80
...o/arconia/web/multitenancy/context/annotations/TenantIdentifierArgumentResolverTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.arconia.web.multitenancy.context.annotations; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import io.arconia.core.multitenancy.context.TenantContextHolder; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Unit tests for {@link TenantIdentifierArgumentResolver}. | ||
*/ | ||
class TenantIdentifierArgumentResolverTests { | ||
|
||
private final TenantIdentifierArgumentResolver argumentResolver = new TenantIdentifierArgumentResolver(); | ||
|
||
@AfterEach | ||
void cleanup() { | ||
TenantContextHolder.clear(); | ||
} | ||
|
||
@Test | ||
void doesNotSupportParameterWithoutAnnotation() { | ||
assertThat(argumentResolver.supportsParameter(showTenantIdentifierNoAnnotation())).isFalse(); | ||
} | ||
|
||
@Test | ||
void supportsParameterWithAnnotation() { | ||
assertThat(argumentResolver.supportsParameter(showTenantIdentifierAnnotation())).isTrue(); | ||
} | ||
|
||
@Test | ||
void doesNotSupportParameterWithWrongType() { | ||
assertThat(argumentResolver.supportsParameter(showTenantIdentifierErrorOnInvalidType())).isFalse(); | ||
} | ||
|
||
@Test | ||
void resolveTenantIdentifierArgument() { | ||
String expectedTenantIdentifier = "acme"; | ||
TenantContextHolder.setTenantIdentifier(expectedTenantIdentifier); | ||
String actualTenantIdentifier = (String) argumentResolver.resolveArgument(showTenantIdentifierAnnotation(), | ||
null, null, null); | ||
assertThat(actualTenantIdentifier).isEqualTo(expectedTenantIdentifier); | ||
} | ||
|
||
private MethodParameter showTenantIdentifierNoAnnotation() { | ||
return getMethodParameter("showTenantIdentifierNoAnnotation", String.class); | ||
} | ||
|
||
private MethodParameter showTenantIdentifierAnnotation() { | ||
return getMethodParameter("showTenantIdentifierAnnotation", String.class); | ||
} | ||
|
||
private MethodParameter showTenantIdentifierErrorOnInvalidType() { | ||
return getMethodParameter("showTenantIdentifierErrorOnInvalidType", Long.class); | ||
} | ||
|
||
private MethodParameter getMethodParameter(String methodName, Class<?>... paramTypes) { | ||
Method method = ReflectionUtils.findMethod(TestController.class, methodName, paramTypes); | ||
return new MethodParameter(method, 0); | ||
} | ||
|
||
static class TestController { | ||
|
||
public void showTenantIdentifierNoAnnotation(String tenantIdentifier) { | ||
} | ||
|
||
public void showTenantIdentifierAnnotation(@TenantIdentifier String tenantIdentifier) { | ||
} | ||
|
||
public void showTenantIdentifierErrorOnInvalidType(@TenantIdentifier Long tenantIdentifier) { | ||
} | ||
|
||
} | ||
|
||
} |