Ok, warnings:
this is just a proof of concept , which I understood should be done like this:
+ @MobileViewEnable and @MobileView annotated (and related) methods should remain in the same controller
+ no validation of used httpAction
+ both methods must have the same signature
+ MobileView annotation value and requestMapping annotation value must be equal and uniques
+ the logic inside the call YourLogic (..) determines which method will be called, at the moment there is a very simple logic that checks if the parameter ("mobile") exists in the request, just to check
+ this code is not intended to be used as is (in general)
+ I donβt know if it works at all outside my computer (joke: D, ehm ..)
SO:
Annotations:
@Retention(RetentionPolicy.RUNTIME) public @interface MobileView { String value() default ""; } @Retention(RetentionPolicy.RUNTIME) public @interface MobileViewEnable { }
ExampleController:
@Controller public class MainController extends BaseController { private final static Logger logger = LoggerFactory.getLogger(MainController.class); private final static String PROVA_ROUTE = "prova"; @MobileViewEnable @RequestMapping(PROVA_ROUTE) public String prova() { logger.debug("inside prova!!!"); return "provaview"; } @MobileView(PROVA_ROUTE) public String prova2() { logger.debug("inside prova2!!!"); return "prova2view"; } }
Definition of aspect:
<bean id="viewAspect" class="xxx.yyy.ViewAspect" /> <aop:config> <aop:pointcut expression="@annotation(xxx.yyy.MobileViewEnable)" id="viewAspectPointcut" /> <aop:aspect ref="viewAspect" order="1"> <aop:around method="around" pointcut-ref="viewAspectPointcut" arg-names="viewAspectPointcut"/> </aop:aspect> </aop:config>
Aspect of implementation:
public class ViewAspect implements BeforeAdvice, ApplicationContextAware { private final static Logger logger = LoggerFactory.getLogger(ViewAspect.class); private ApplicationContext applicationContext; public Object around(ProceedingJoinPoint joinPoint) { Method mobileViewAnnotatedMethod = null; HttpServletRequest request = getCurrentHttpRequest(); String controllerName = getSimpleClassNameWithFirstLetterLowercase(joinPoint); Object[] interceptedMethodArgs = getInterceptedMethodArgs(joinPoint); String methodName = getCurrentMethodName(joinPoint); Method[] methods = getAllControllerMethods(joinPoint); Method interceptedMethod = getInterceptedMethod(methods, methodName); String interceptedMethodRoute = getRouteFromInterceptedMethod(interceptedMethod); if (callYourLogic(request)) { mobileViewAnnotatedMethod = getMobileViewAnnotatedMethodWithRouteName(methods, interceptedMethodRoute); if (mobileViewAnnotatedMethod != null) return invokeMethod(mobileViewAnnotatedMethod, interceptedMethodArgs, controllerName); } return continueInterceptedMethodExecution(joinPoint, interceptedMethodArgs); } private Object continueInterceptedMethodExecution(ProceedingJoinPoint joinPoint, Object[] interceptedMethodArgs) { try { return joinPoint.proceed(interceptedMethodArgs); } catch (Throwable e) { logger.error("unable to proceed with intercepted method call: " + e); } return null; } private Object[] getInterceptedMethodArgs(JoinPoint joinPoint) { return joinPoint.getArgs(); } private boolean callYourLogic(HttpServletRequest request) {