Tuesday, March 30, 2010

Grails: Intercepting Service class methods - part 3

Well - I have a couple of updates to my previous post:

1) Grails 1.2.2 is out and it does look like the Spring DSL for AOP definitions is fixed.
2) using the Spring xml definition in part 2 performed very poorly. I am not sure why but if I changed the pointcut definition to:

pointcut="execution(* com.redpointtech.flex..*Service..*(..))"


it performed MUCH better

Given that the DSL is fixed and I needed it to perform better I have changed the AOP definition again to the following in the resources.groovy file.

 beans = {  
xmlns aop:"http://www.springframework.org/schema/aop"
securityAspect(SecurityAspect)
aop {
config("proxy-target-class":true) {
aspect(id:'theSecurityAspectDef', ref:'securityAspect') {
around method: "invoke", pointcut:"execution(* com.redpointtech.flex..*Service..*(..))"
}
}
}
}


I think this concludes my saga to get Flex services intercepted. I hope this helps you out.

Tuesday, March 16, 2010

Grails: Intercepting Service class methods - part 2

As a continuation from my earlier post about intercepting Grails Service methods - as it turns out that approach will not work when using flex-remoting. If you want to intercept your services that you expose as Flex services you have to use Spring AOP. Because of a bug: http://jira.codehaus.org/browse/GRAILS-5932 you will have to use the XML version of AOP definition.

To do that you have to create a resources.xml file in grails-app/conf/spring directory and it would look like the following:

 <?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--
we are defining the AOP beans in the xml file because of this bug which will
not be fixed until 1.2.2 and we are using 1.2.1
http://jira.codehaus.org/browse/GRAILS-5932
aspect are called twice because of the above bug. See
http://n4.nabble.com/Advice-called-2-or-more-times-for-aspect-with-execution-pointcut-Bug-td1588831.html
toward the bottom Graeme says that 1.2.2 will have the fixes, and 1.3 M1 already does have the
fix. For now we have to live with the interceptor called twice.
-->
<bean id="myAroundAspect" class="com.redpointtech.aop.MyAroundAspect" />
<aop:config proxy-target-class="true">
<aop:aspect id="theAspect" ref="myAroundAspect">
<aop:around method="invoke" pointcut="target(com.redpointtech.aop.AOPMarkerInterface)" />
</aop:aspect>
</aop:config>
</beans>


This assumes that you want to intercept any class that implements the AOPMarkerInterface.

The Aspect looks like the following:

 class MyAroundAspect {  
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnVal = null
println "MyAroundAspect called...."
returnVal = joinPoint.proceed()
return returnVal
}
}


A couple of take aways:
1) do not try to do what I suggested in my previous blog post. That will NOT work for Flex services
2) do not use resources.groovy and the Spring DSL - there is a bug in the 1.2.x version that prevents that from working. This is suppose to be fixed in the 1.2.2 release ( not yet released) and the 1.3 M1 ( available now )
3) do use resources.xml and define your Aspect the old fashion way - the old ways work just great.

I hope this saves you some time.

Saturday, March 13, 2010

Grails: Intercepting Service class methods

It took me a little while to fully understand how to add some kind of interceptor to a Grails Service class and I wanted to add a blog post incase others have a similar question. After doing some research, and piecing together information from various sites I ended up with the information here. I hope it is helpful to you.

What problem am I trying to solve?
Adding interceptors to Controllers is really easy and I wanted to add some interceptors to my Service classes in a similar fashion. But it appears that you cannot do this for Service classes - just Controller classes. If I am wrong, please someone show me how best to do this.

Why am I trying to do this?
I use Flex in many of my application implementations and I use BlazeDS/AMF in many of the cases. I needed to add a security check on every exposed Flex service.

How did I do this?
In my service class I added a custom property that I called 'secureService'. But as you will see you can use anything.

 class TeslaService {  
boolean transactional = true
// secureService is a custom property that is looked for in the BootStrap.groovy file and if
// found in a ServiceClass, then it modifies the invokeMethod to add some code before and
// after the call.
boolean secureService = true
def serviceMethod() {
println 'Tesla Service Method called'
}
}


Next I updated my BootStrap.groovy file and for each ServiceClass, checked for the 'secureService' property being set to true. If it was, I then used the MetaClass InvokeMethod and assigned a new closure that could add a security check.

The updated bootstrap file looked like the following:
 class BootStrap {  
def grailsApplication
def init = { servletContext ->
grailsApplication.serviceClasses.each {
def isSecured = it.getPropertyValue('secureService')
if( isSecured ) {
// example of how to 'intercept' service classes.
it.metaClass.invokeMethod = { name, args ->
println '''SECURE before $name'''
def res = delegate.metaClass.getMetaMethod(name,args).invoke( delegate, args)
println '''SECURE after $name. res=$res'''
res
}
}
}
}
def destroy = {
}
}


To test this I created an Integration test. This test looked like the following:

 class TeslaServiceIntegrationTests extends GrailsUnitTestCase {  
def teslaService
protected void setUp() {
super.setUp()
}
protected void tearDown() {
super.tearDown()
}
void testServiceMethod() {
teslaService.serviceMethod()
}
}


The output of this was the following:
SECURE before serviceMethod
Tesla Service Method called
SECURE after serviceMethod. res=null

Some of the links that I used are below as the information from these posts might also be helpful to you.

http://www.pubbs.net/grails/201001/9868/
http://mrhaki.blogspot.com/2010/01/grails-goodness-access-grails.html
http://www.grails.org/doc/latest/api/org/codehaus/groovy/grails/commons/ClassPropertyFetcher.html