I'm trying to intercept the request and change the controller on the basis of a domain.
My Application Structure:
- Base
- controllers
- IndexController
- controllers
- domain
- domain-one
- controllers
- IndexController
<- Extends the base.controller.IndexController
- IndexController
- controllers
- domain-one
Now if the request comes from a domain which is domain.one then HandlerInterceptorAdapter
should load the subclass of the IndexController which is located in this namespace domain-one.controllers.IndexController
I'm able to get the subclass by using the ClassPathScanningCandidateComponentProvider
for example in preHandle
method:
HashMap<String, String> domains = config.getDomain(); // <-- This gives me the domain name list with mapped directory in domain directory
String domain = request.getHeader("host").split(":")[0];
if(! domains.containsKey(domain) ) { // If domain mapping not exist then use the default one.
return super.preHandle(request, response, handler);
}
String contextDir = domains.get(domain); // <-- this will return domain-one String
String controllerNamespace = "domain.";
Object handlerMethod = ((HandlerMethod) handler).getBean();
classPathFinder.addIncludeFilter(new AssignableTypeFilter(handlerMethod.getClass()));
Set<BeanDefinition> components = classPathFinder.findCandidateComponents(controllerNamespace + contextDir);
BeanDefinition controllerBean = components.iterator().next(); // Found 1 BeanDifinition in the domain-one controller directory
String newControllerClassPath = ((ScannedGenericBeanDefinition)controllerBean).getMetadata().getClassName(); // This will return the complete class namespace like: domain.domain-one.controller.IndexController
Now how can I tell the preHandle method to use the newControllerClassPath
as a controller for this request? I know I need to create an object of it but after that what should I do
if I do return super.preHandle(request, response, handler);
it will load the existing controller but I need to change the handler
object before returning the super.prehandle
from the method.
Basically what I'm trying to do is to implement kind of a multi-domain architecture
I hope I have cleared my question.