0

I'm trying to intercept the request and change the controller on the basis of a domain.

My Application Structure:

  • Base
    • controllers
      • IndexController
  • domain
    • domain-one
      • controllers
        • IndexController <- Extends the base.controller.IndexController

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.

Touqeer Shafi
  • 5,084
  • 3
  • 28
  • 45
  • 1
    Can't you simply narrow the mappings by specifying header value in the `@RequestMapping` definitions is the individual controllers https://www.baeldung.com/spring-requestmapping. Or use a custom property on the request mappings: https://stackoverflow.com/questions/10312177/how-to-implement-requestmapping-custom-properties – Alan Hay Nov 29 '19 at 12:27
  • np, because that will create complexity, I'm making this behavior because I have multiple domains, if for that domain controller is not exist then it should go to the default one. – Touqeer Shafi Dec 03 '19 at 07:00
  • *because that will create complexity*. As opposed to your current proposal!! – Alan Hay Dec 03 '19 at 08:44
  • @AlanHay thanks for the comment the below URL really helps me to achieve what I was looking for https://stackoverflow.com/a/10336769/1635676 – Touqeer Shafi Dec 03 '19 at 10:10

0 Answers0