Grails 3 Interceptor and render custom JSON view - grails

I am trying to write a Grails 3 interceptor that should check if certain variables are present in the HTTP Headers. If they are not present i would like to render a specific json view but it seems that the render method is not availble in the before() method.
boolean before() {
String header = request.getHeader("Authorization")
if(!header) {
BaseException exception = new BadRequestException("test")
render view: "/genericErrorReponse", model: [e: exception]
return false
}
Is there a better way to achieve the desired result?
I am getting the following error when trying to render the JSON view.
No qualifying bean of type [org.springframework.web.servlet.ViewResolver] is defined.
No qualifying bean of type [org.springframework.web.servlet.ViewResolver] is defined: expected single matching bean but found 4: groovyMarkupViewResolver,jsonViewResolver,beanNameViewResolver,mvcViewResolver. Stacktrace follows:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.ViewResolver] is defined: expected single matching bean but found 4: groovyMarkupViewResolver,jsonViewResolver,beanNameViewResolver,mvcViewResolver
at grails.artefact.Interceptor$Trait$Helper.render(Interceptor.groovy:254) ~[grails-plugin-interceptors-3.1.1.jar:3.1.1]
at device.registration.RegistrationInterceptor.before(RegistrationInterceptor.groovy:13) ~[main/:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_66]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_66]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66]
Interceptor Code
class RegistrationInterceptor {
boolean before() {
String header = request.getHeader("Authorization")
if(!header) {
render view: "/genericErrorResponse", model: [e: new BadRequestException()]
}
false
}
boolean after() { true }
void afterView() {
// no-op
}
}
JSON View [/genericErrorResponse]
model {
BaseException e
}
response.status e.status
json {
message e.message
error e.error
status e.status
timestamp e.timestamp
}

Stacktrace shows that you are trying to get a bean of type org.springframework.web.servlet.ViewResolver at RegistrationInterceptor.groovy:13. Grails has by default 4 different implementations for ViewResolver and you have to be specific which one do you want to use.

It seemed that it actually was a bug inside Grails 3. Please see https://github.com/grails/grails-core/issues/9688

Related

grails4 migration Traits lazy load issue - HHH000142: Bytecode enhancement failed

I have migrated my application from Grails-3.x to Grails-4.1.1
Most of my Domain classes implemented the following Traits class (DynamicProperties), which has an implementation of GormEntity for some reason - to override the propertyMissing method.
trait DynamicProperties<D> implements GormEntity<D> {
def dynamic = [:]
def propertyMissing(String name, value) {
if (!propertyIsDatasource(name)) {
dynamic[name] = value
}
}
def propertyMissing(String name) {
if (propertyIsDatasource(name)) {
super.propertyMissing(name)
} else {
dynamic[name]
}
}
boolean propertyIsDatasource(String name) {
false
}
}
The above trait has been implemented by many domain classes like this
class Customer implements DynamicProperties<Customer> {
String customerCode
String customerName
Address address
....
}
Now, when I run my application, It is throwing the following exception
HHH000142: Bytecode enhancement failed: com.apps.billing.Customer.
Caused by: java.lang.NullPointerException
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:37)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:75)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
at **com.apps.billing.common.DynamicProperties$Trait$Helper.$init$**(DynamicProperties.groovy:7)
It used to work fine with Grails3.x
I had a similar problem when migrating a project to Grails 4.0.13.
I tracked the trigger of this problem down to having default values for member variables in a trait class. I see you have the same with the property dynamic being initialized with the empty map.
I recommend lazy initializing it in one of your propertyMissing methods.

Grails 3 - assign controller's variable in interceptor

I'm upgrading by Grails 2.5.1 web-app to grails 3, but I'm stuck with this problem: in my controllers I was using beforeInterceptors to pre-calculate a set of variables to be used in their action methods.
class MyController {
def myVar
def beforeInterceptor = {
myVar = calculateMyVarFromParams(params)
}
def index() {
/* myVar is already initialized */
}
}
Now that with Grails 3 interceptors are more powerful and on separate files, how can I achieve the same result? To avoid using request-scope variables, I tried with the following code
class MyInterceptor {
boolean before() {
MyController.myVar = calculateMyVarFromParams(params)
MyController.myVar != null // also block execution if myVar is still null
}
boolean after() { true }
void afterView() { /* nothing */ }
}
class MyController {
def myVar
def index() {
println('myVar: '+myVar)
}
}
but I get
ERROR org.grails.web.errors.GrailsExceptionResolver - MissingPropertyException occurred when processing request: [GET] /my/index
No such property: myVar for class: com.usablenet.utest.MyController
Possible solutions: myVar. Stacktrace follows:
groovy.lang.MissingPropertyException: No such property: myVar for class: com.usablenet.utest.MyController
Possible solutions: myVar
at com.usablenet.utest.MyInterceptor.before(MyInterceptor.groovy:15) ~[main/:na]
I assumed (wrongly, apparently) that this would be feasible. Is there a solution? Thanks in advance!
Note: in my case MyController is an abstract class extended by all other controllers
What I was missing is to declare myVar as static, as simple as that!
Update
If for any reason you cannot define the variable as static, you can set it as attribute on request object in the interceptor, and read it from there in the controller
// Interceptor
request.setAttribute('myVar', calculateMyVarFromParams(params))
// Controller
request.getAttribute('myVar')

GroovyCastException metaclass i18n

This question is connected with another.
I'd like to add properties to constructor and overwrite getLocalisedMessage() function to get proper translated message with error. First I want to overload constructor to set properties, but when I add:
GroovyCastException.metaClass.constructor = { Object objectToCast, Class classToCastTo ->
def constructor = GroovyCastException.class.getConstructor(Object, Class)
def instance = constructor.newInstance(objectToCast, classToCastTo)
// ... do some further stuff with the instance ...
println "Created ${instance} and executed!"
instance
}
and then get thrown GroovyCastException I don't get println in console.
Why?
How to overload constructor, set properties (objectToCast, classToCastTo) and then overload getLocalizedMessage?
I tried also:
def originalMapConstructor = GroovyCastException.metaClass.retrieveConstructor(Map)
GroovyCastException.metaClass.constructor = { Map m ->
// do work before creation
print "boot do work before creation "
m.each{
print it
}
print "boot do work before creation 2"
def instance = originalMapConstructor.newInstance(m)
// do work after creation
print "boot do work after creation"
instance
}
I 've put it in controller (right before catching exception) and in Bootstrap.groovy. Unfortunatelly there is no printlns in console output.
You're better off not using meta-programming to do internationalization. In grails, you should do it in the view layer with the <g:message> tag if possible. If not, the next best choice is the controller layer.
If you just want to display localized messages on an error page when an exception occurs, the best practice is to have a "500" URL mapping, and render the exception with a <g:renderException> in the view.
If you want to intercept the exception, you can change the "500" URL mapping to a controller and wrap it there before passing it to the view. Example:
// UrlMappings.groovy
class UrlMappings {
static mappings = {
...
"500"(controller:"error", method: "serverError")
}
}
// ErrorController.groovy
class ErrorController {
def serverError() {
def exception = request.exception.cause
if (exception instanceof GroovyCastException) {
exception = new LocalizedGroovyCastException(exception)
}
[exception: exception]
}
}
And then do your localization in a new class LocalizedGroovyCastException.

How do you instantiate your own Validateable Object

I have a validateable object. When I instantiate it with new from an input Map, the ApplicationContext that should be embedded in the command object is not populated, and the invocaction fails when I call validate(), with the following Exception:
java.lang.IllegalStateException: Could not find ApplicationContext, configure Grails correctly first
What do I have to do to get a proper spring bean when I want to create a Validateable object and bind it to a Map of values?
Please note This code does not run in a grails controller: it runs in a rabbitMq consumer. So simple solutions applying to controllers will not work here.
class MyConsumer {
static rabbitConfig = [
queue : 'myQueue'
]
def handleMessage(Map message, MessageContext context) {
def request = new MyValidateableRequest(message) //instantiates just a POGO
if (request.validate()) { //Exception here
//...
} else {
//...
}
}
}
It depends on Grails version and if you use plugin for RabitMQ. Normally consumer should be a spring bean and you can use code like below. grailsApplication is bean which should be injected into consumer
def object = new MyValidateableRequest()
//do request binding here
object.properties = [message: message]
AutowireCapableBeanFactory acbf = grailsApplication.mainContext.autowireCapableBeanFactory
acbf.autowireBeanProperties object, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false
object.validate()
If you are on Grails 3, example which will not require plugin
https://spring.io/guides/gs/messaging-rabbitmq/ may be more interesting

Grails resources.groovy beans

Im having some problems trying to actually determine if my beans have been properly loaded.
Is there some log4j property which can show me in the log what beans that are properly loaded?.
After some trying i went off and tried another example from here
redefining my resources.groovy like follows:
import grails.spring.BeanBuilder
BeanBuilder bb = new BeanBuilder()
bb.beans = {
ldapUserDetailsMapper(example.CustomDetailsContextMapper) {
}
}
CustomDetailsContextMapper in its turn is defined as:
class CustomDetailsContextMapper implements UserDetailsContextMapper {
def springSecuritySource
#Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection authorities) {
This is the error im getting when using this kind of resource.groovy setup:
2012-10-12 12:52:31,663 [main] ERROR spring.GrailsRuntimeConfigurator - [RuntimeConfiguration] Unable to load beans from resources.groovy
org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack: No such property: beans for class: resources
Possible solutions: class
When not using beanbuilder i get no errors but i cant really verify that my beans are loaded, since they seem never to be called.
In the other case i get the below error.
Any suggestions?
in resource.groovy simply write
beans = {
ldapUserDetailsMapper(example.CustomDetailsContextMapper) {
}
The BeanBuilder is already defined for you
A bean is called when used... inside a controller try thi
VeryBeanController{
def ldapUserDetailsMapper
def index = {
render( ldapUserDetailsMapper )
}
}
And call the controller

Resources