Announcement Announcement Module
Collapse
No announcement yet.
Intercept getter method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Intercept getter method

    Newbie at AOP

    Task: A string stored in a MySQL DB contains newlines, which however isn't diplayed in the webpage by <c:out value="">-tag.

    I was thinking on intercepting the getXXX-methods of a POJO in order to inject <br>-tags instead of newlines. For different reasons, I don' want to store HTML in datastore.

    How do I do this?
    First problem is to get the return value of the getXXX-method, second is where I'm supposed to return it. 'invocation.proceed()' isn't optional, is it?

    My code so far:

    Code:
    public class AddHtmlBreakInterceptor implements MethodInterceptor &#123;
    	
    	public Object invoke&#40;MethodInvocation invocation&#41; throws Throwable&#123;
    		String out = &#40;String&#41; invocation.getMethod&#40;&#41;.invoke&#40;invocation.getThis&#40;&#41;, new Object&#91;0&#93;&#41;;
    		//Do injection of <br> tags
    		return invocation.proceed&#40;&#41;;
    		// How to return the <br>-injected string???
    	&#125; 		
    &#125;
    Thanks!

  • #2
    You can use code like this:
    String fromDb = (String) invocation.proceed();
    String massagedString = doMySubstitution(fromDb);

    return massagedString;
    You are responsible for calling proceed() in interception around advice; it's up to you if you want to massage the return value.

    You can use a Pointcut to target this interceptor to only the appropriate getters.

    HTH,
    Rod

    Comment


    • #3
      Thanks for the quick and clarifying response!!

      About Pointcut: is it possible to target multiple getXXX-methods with only one pointcut, or do one need one Pointcut per method?

      Comment


      • #4
        Can't get advice to 'fire'

        I don't seem to be able to 'fire' the advice

        Here's a part of my setup

        Code:
        <bean id="breakInjector" class="ks.rah.avik2.web.form.support.AddHtmlBreakInterceptor"/>
        	
        	<bean id="breakInjectorTarget" class="ks.rah.avik2.domain.Event"/>
        
        	<!-- Interceptor to inject <br>-tags into output -->
        	<bean id="breakInjectorPointcut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        		<property name="advice"><ref local="breakInjector"/></property>
        		<property name="patterns">
        			<list>
        				<value>^ks.rah.avik2.domain.Event.getEventDescr.*</value>
        				<value>^ks.rah.avik2.domain.Event.getWhyComment.*</value>
        				<value>^ks.rah.avik2.domain.Event.getConseqComment.*</value>
        				<value>^ks.rah.avik2.domain.Event.getPreventComment.*</value>
        				<value>^ks.rah.avik2.domain.Event.getAuxComment.*</value>
        				<value>^ks.rah.avik2.domain.support.TextEntry.getText.*</value>
        			</list>
        		</property>
        	</bean>
        	
        	
        	<bean id="breakInjectorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        		<property name="target"><ref local="breakInjectorTarget"/></property>
        		<property name="interceptorNames">
        			<list>
        				<value>breakInjector</value>
        			</list>		
        		</property>
        	</bean>
        The interceptor:

        Code:
        public class AddHtmlBreakInterceptor implements MethodInterceptor &#123;
        	
        	public static Logger log = Logger.getLogger&#40;AddHtmlBreakInterceptor.class.getName&#40;&#41;&#41;;
        	
        	public Object invoke&#40;MethodInvocation invocation&#41; throws Throwable&#123;
        		log.debug&#40;"AddHtmlBreakInterceptor invoked"&#41;;
        		String out =  &#40;String&#41; invocation.proceed&#40;&#41;;
        		log.debug&#40;"Instring = " + out&#41;;
        		String returnthis = replaceWithBrakes&#40; out &#41;;
        		log.debug&#40;"Instring massaged = " + returnthis&#41;;
        		return  returnthis;
        	&#125;
        	
        	public String replaceWithBrakes&#40;String instr&#41;&#123;
        		if&#40; instr == null || instr.length&#40;&#41; < 1 &#41;&#123;
        			return "";	
        		&#125;
        		StringBuffer sb = new StringBuffer&#40;instr&#41;;
        		int cursor = 0;
        		for&#40;int i = 0; i < sb.length&#40;&#41; && cursor != -1;i = cursor&#41;&#123;
        			cursor = sb.indexOf&#40;"\n", cursor&#41;;
        			sb.insert&#40;cursor, "<br/>"&#41;;
        		&#125;
        		return sb.toString&#40;&#41;;
        	&#125; 			
        &#125;
        I don't know what I'm doing wrong here...

        Thanks!

        Comment


        • #5
          I don't know if it's relevant, but the Event object is a POJO persisted to a datastore. I want the advice to fire when I display certain get-methods in the web layer, but perhaps this is not an applicable use of Spring AOP?

          Any suggestions?

          Comment


          • #6
            About Pointcut: is it possible to target multiple getXXX-methods with only one pointcut, or do one need one Pointcut per method?
            A pointcut will typically define a set of methods.

            Comment


            • #7
              OK.

              Does this look alright?
              I cannot get it to fire... no log trace... nothing

              Code:
              <bean id="breakInjector" class="ks.rah.avik2.web.form.support.AddHtmlBreakInterceptor"/>
                 
                 <bean id="breakInjectorTarget" class="ks.rah.avik2.domain.Event"/>
              
                 <!-- Interceptor to inject <br>-tags into output -->
                 <bean id="breakInjectorPointcut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
                    <property name="advice"><ref local="breakInjector"/></property>
                    <property name="patterns">
                       <list>
                          <value>^ks.rah.avik2.domain.Event.getEventDescr.*</value>
                          <value>^ks.rah.avik2.domain.Event.getWhyComment.*</value>
                          <value>^ks.rah.avik2.domain.Event.getConseqComment.*</value>
                          <value>^ks.rah.avik2.domain.Event.getPreventComment.*</value>
                          <value>^ks.rah.avik2.domain.Event.getAuxComment.*</value>
                          <value>^ks.rah.avik2.domain.support.TextEntry.getText.*</value>
                       </list>
                    </property>
                 </bean>
                 
                 
                 <bean id="breakInjectorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
                    <property name="target"><ref local="breakInjectorTarget"/></property>
                    <property name="interceptorNames">
                       <list>
                          <value>breakInjector</value>
                       </list>      
                    </property>
                 </bean>

              Comment


              • #8
                Resolved

                This particular problem was successfully solved with the HTML

                Code:
                <PRE></PRE>
                tag.

                Comment

                Working...
                X