Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Intercepting all methods of an interface

  1. #11
    Join Date
    Sep 2008
    Posts
    9

    Default

    Hi all,

    I need to intercept all the handleRequest method of a webapp controller, but I'm not able to do so even if I tried to follow a lot of example about Spring AOP.

    Here are my advice class:

    package it.esel.ge.aop;

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;

    @Aspect
    public class ValidUserSessionAdvice {

    @Pointcut("execution(* *..handle+.*(..))")
    public void inControllers() {}

    @Around("inControllers()")
    public Object doValidation(ProceedingJoinPoint pjp) throws Throwable {
    Object[] arguments = pjp.getArgs();
    System.out.println(arguments[0].getClass().getName());
    return pjp.proceed(arguments);
    }

    }

    My application context has already the include <aop:aspectj-autoproxy />, but when a controller execute (no matter what the type is) the advice does nothing.

    I have another aspect tha log every exception and it's work. Here is the code:

    package it.esel.ge.aop;

    import java.util.HashMap;
    import java.util.Map;

    import it.esel.ge.businessobjects.oggetticondivisi.Generi cClass;
    import it.esel.ge.utilita.CastUtils;
    import it.esel.ge.utilita.mail.sender.IAdministrativeMail Sender;

    import org.apache.velocity.app.VelocityEngine;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.ui.velocity.VelocityEngineUtil s;

    @Aspect
    public class ExceptionsLoggingAdvice extends GenericClass {

    private static IAdministrativeMailSender mailService;
    private static VelocityEngine velocityEngine;

    private static final String E_MAIL_VELOCITY_TEMPLATE = "supporto/velocity/eMailAlertEccezione.vm";
    private static final String E_MAIL_SUBJECT_KEY = "mail.admin.eccezione.oggetto";
    private static final String DEFAULT_EMPTY_STRING = getProperty("visualizzazione.dettagli.valore.non.d isponibile");

    public void setMailService(IAdministrativeMailSender mailUtils) {
    ExceptionsLoggingAdvice.mailService = mailUtils;
    }

    public void setVelocityEngine(VelocityEngine velocityEngine) {
    ExceptionsLoggingAdvice.velocityEngine = velocityEngine;
    }

    @AfterThrowing(pointcut="within(it.esel.ge..*)", throwing="ex")
    public void doExceptionLogging(ProceedingJoinPoint pjp, Throwable ex) {
    Map<String, String> exceptionReport = new HashMap<String, String>();
    exceptionReport.put("sourceClassName", CastUtils.nullStringToDefaultTemplate(getTargetCla ssName(pjp)));
    exceptionReport.put("methodSignatureName",
    ....

    Could someone help me?

    Thanks.

    Riccardo.

  2. #12
    Join Date
    Nov 2007
    Posts
    420

    Default

    you pointcut

    Code:
    @Pointcut("execution(* *..handle+.*(..))")
    public void inControllers() {}
    is not correct. your poincut as written will intercept any method of class called handle and any of the class below it in the hierarchy. try

    Code:
    @Pointcut("execution(* *..handleRequest(..))")
    public void inControllers() {}
    if you want advice on any handleRequest method invocation (warn: this may include non-web controllers that just happen to have handleRequest() method.

  3. #13
    Join Date
    Sep 2008
    Posts
    9

    Default

    Hi bdangubic,

    Thank you for the reply.

    I corrected the pointcut, but nothing has changed.

    Maybe it's my controller definition that it's incorrect?

    The controller is as follow:

    package it.esel.ge.controllers;

    import java.util.HashMap;
    ...
    import it.esel.ge.utilita.constants.Constants;

    public class RicercaController extends AWController {

    private final static String QUERY_INFO_MESSAGE = "Query ricerca dati: ";

    private ProfiloRicerca profiloRicerca;
    private String ricercaLike = null;
    private String colonnaOrderBy = null;
    private IJoinsService tableJoinsService;


    /*
    * Riccardo Forafò
    * Mar 7, 2008
    *
    * (non-Javadoc)
    * @see it.esel.ge.controllers.base.AWController#handleReq uest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Map<Object, Object> model = new HashMap<Object, Object>();
    ModelAndView modelAndView = null;
    String ricerca = request.getParameter(getProperty("pagina.utente.cr iteri.ricerca.namePulsanteRicerca"));
    ...

    and it's extend the AWController class:

    public class AWController extends ApplicationObjectSupport implements Controller {

    protected static ReloadableResourceBundleMessageSource propertiesRepository;
    protected static MessageSource staticPropertiesRepository;

    ...

    protected String sortProperty;
    protected boolean sortIgnoreCase;
    protected boolean sortAscending;

    public AWController() {
    super();
    }

    public void setPropertiesRepository(ReloadableResourceBundleMe ssageSource propertiesRepository) {
    AWController.propertiesRepository = propertiesRepository;
    }

    ...
    Best regards,

    Riccardo.

  4. #14
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Please use [ code][/code ] tags when posting code!!!! That way it remains readable!!!

    Your pointcut is still flawed.

    Code:
    @Pointcut(execution(* *..*Controller+.handleRequest(..))
    public void handleRequest(){}
    Also notice that there is onle 1 method you can intercept using Spring AOP, that is handleRequest...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #15
    Join Date
    Mar 2007
    Posts
    561

    Default

    Yes of course.
    But I have to find out which method is called, which ends in somewhat like this:

    Code:
    if (methodname.equals("method1")) {
    ...
    }
    else if (methodname.equals("method2")) {
    ...
    }
    What I was looking for was an "automatic" dispatch from invoke to method1, method2, nothing with if-then-else and strings.

    The problem with the approch above is, that is it not refactorable and not type safe.

  6. #16
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Have you actually read my post?! Even if you have 200 methods it works with just those 5 lines of coding. You still have to do the if stuff because for some reason you want to inspect the arguments of the method. If you want to do that in 1 class you will have to fix something for that.

    Either it is an if else or you create an interceptor per method call. Which seems a bit like an overkill to me.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #17
    Join Date
    Sep 2008
    Posts
    9

    Default

    Hi Marten,

    I put the aspect in the same context of my controllers.

    I corrected my aspect as you suggested:

    Code:
    package it.esel.ge.aop;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class ValidUserSessionAdvice {
        
        @Pointcut("execution(* *..*Controller+.handleRequest(..))")
        public void handleRequest() {}
        
        @Around("ihandleRequest()")
        public Object invoke(ProceedingJoinPoint pjp) throws Throwable {
            Object[] arguments = pjp.getArgs();
            System.out.println(arguments[0].getClass().getName());
            return pjp.proceed(arguments);
        }
    
    }
    My controllers and the aspect are defined in <webapp name>-servlet.xml:

    Code:
    <!--
      - DispatcherServlet application context for the Spring web MVC
      - implementation of AnagrafeWeb's web tier.
      -->
    <beans>
    
        <!-- ========================= VIEW DEFINITIONS ========================= -->
    
        <bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
            <property name="basename" value="views"/>
        </bean>
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    ...
    
        <bean name="/ricerca.do" class="it.esel.ge.controllers.RicercaController">
            <property name="tableJoinsService" ref="tableJoins" />
        </bean>
    
    ...
    
        <bean id="validUserSessionAdvice" class="it.esel.ge.aop.ValidUserSessionAdvice" />
    
    <beans>
    The aspectj-autoproxy it's enabled into the ApplicationContext (<aop:aspectj-autoproxy />).

    The above controller is as follow:

    Code:
    package it.esel.ge.controllers;
    
    ...
    import it.esel.ge.controllers.base.AWController;
    ...
    
    public class RicercaController extends AWController implements IController {
    
        private final static String QUERY_INFO_MESSAGE = "Query ricerca dati: ";
        
    ...
        private IJoinsService tableJoinsService;
            
        /**
         * @author Riccardo Forafò
         * @since Mar 7, 2008
         *
         * @param tableJoinsService
         */
        public void setTableJoinsService(IJoinsService tableJoinsService) {
            this.tableJoinsService = tableJoinsService;
        }
        
        /*
         * Riccardo Forafò
         * Mar 7, 2008
         *
         * (non-Javadoc)
    * @see it.esel.ge.controllers.base.AWController#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
         */
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    
            Map<Object, Object> model = new HashMap<Object, Object>();
            ModelAndView modelAndView = null;
    ...
    and it's extend a base controller that implemets the Controller interface:

    Code:
    package it.esel.ge.controllers.base;
    
    import org.springframework.web.servlet.mvc.Controller;
    ...
    public class AWController extends ApplicationObjectSupport implements Controller {
    
        protected static ReloadableResourceBundleMessageSource propertiesRepository;
        protected static MessageSource staticPropertiesRepository;
    ...    
        public AWController() {
            super();
        }
        
    ...    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            return null;
        }
    
    ...
    I do apologize to boring you, but I'm not able to understand what I'm doing wrong.

    Could you help me please?

    Thank you.

    Riccardo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •