grails url mapping for google ajax search - grails

This is probably a very simple problem but for the life of me I can't get it to work.
I need to redirect google requests for ajax generated code to return a html template for indexing
I have the following in my urlmappings.conf
"/?_escaped_fragment_=$id"(controller:"google",action:"getOfferDetails")
However if I enter mysite?_escaped_fragment_=200 in the browser the controller is not called
If however I enter mysite_escaped_fragment=200 the controller is called and the action executed.
Any suggestions would be greatly appreciated.
Thanks
Ash

You can not use '?' char in the route matching i.e. it will be ignored.
Use this filter instead (put this class in the config folder w/ fileName CrawlerFilters.groovy):
class CrawlerFilters {
def filters = {
google(controller: '*', action: '*') {
before = {
boolean isCrawler = webRequest.params._escaped_fragment_ != null
if (isCrawler && !request._alreadyForwarded) {
request._alreadyForwarded = true
forward controller: 'google', action: 'getOfferDetails'
}
}
}
}`

Related

How do I call a Stimulus controller that's more than one word?

So I've tried
{ controller: "reset_form", action: "turbo:submit-end->reset_form#reset" }
{ controller: "reset-form", action: "turbo:submit-end->reset-form#reset" }
{ controller: "reset_form", action: "turbo:submit-end->reset-form#reset" }
{ controller: "reset-form", action: "turbo:submit-end->reset_form#reset" }
With reset_form_controller.js, reset-form-controller.js, reset-form_controller.js...
Which all failed. Every single one-- I thought Stimulus just didn't work. Then I tried the base hello_controller.js example in my erb, and it did work. And yes, I did follow the hello_controller.js's guidelines(didn't work) and copied code LETTER FOR LETTER from hotwired.dev's showcase video(didn't work).
I ended up just changing it to reset_controller.js and doing
{ controller: "reset", action: "turbo:submit-end->reset#reset" }
to get it to work. But I'm still curious-- why did everything else fail? I feel my sanity slipping away. Out of all the things to be absolutely defeated by, it's trying to get a stimulus controller that's more than one word to run.
As #Maxence comment, it works fine when we manually register stimulus controller, then there's something wrong with the automatically stimulus controllers registration>
It turns out the stimulus-rails gem replace '_' to '-' when it registers stimulus controllers automatically code
function registerControllerFromPath(path, under, application) {
const name = path
.replace(new RegExp(`^${under}/`), "")
.replace("_controller", "")
.replace(/\//g, "--")
.replace(/_/g, "-") // <---- THIS
...
So i guess you could name your stimulus controller with _ like reset_form_controller.js and use it with the - like reset-form
{ controller: "reset-form", action: "turbo:submit-end->reset-form#reset" }

How do I get controller method params when using #Secured closure?

I have successfully set up my Grails application to authenticate the user.
I map controller method arguments using URL params, in my UrlMappings.groovy:
"/$source/owner/$ownerId/show"(controller:"myController", action: "show", method: "GET")
How do I get the values of $source and $ownerId in my #Secured closure?
My controller method looks like this:
#Secured(closure = {
//null
def testSource = params.source
//also null
def testOwnerId = params.ownerId
//null
def owner = request.ownerId
//null
def owner2 = request.getParameter('ownerId')
//contains only query string params
def grailsParams = request['org.codehaus.groovy.grails.WEB_REQUEST']?.params
return true
})
def show(String source, String ownerId) {
...
}
How do I get these values? What am I doing wrong, here?
I thought that this post would provide a solution, but the answer given there didn't work for me:
Is it possible to hack the spring-security-core plugin #Secured annotation to be able to reference request params given to a controller?
I am using the following grails and plugin versions:
grails 2.5.1
compile ":spring-security-core:2.0-RC5"
compile ":spring-security-rest:1.5.3", {
excludes 'com.fasterxml.jackson.core:jackson-databind:'
}
Brief :
Use request.getParameter
Details :
In 2.0 you can use a closure as the annotation's check; there's a brief writeup and example in the docs: https://grails-plugins.github.io/grails-spring-security-core/v2/guide/newInV2.html
You'd express your example as this:
#Secured(closure={
request.getParameter('ownerId') >=123 //this is example
})
Return true to allow access, false to deny access.

Does Grails Filter `actionExclude` work with method-based routing?

I have a method-based route like:
name base: "/" {
controller="api"
action=[GET: "welcome", POST: "post"]
}
And I'd like to apply a filter to handle authorization, e.g.
class RequestFilters {
def filters = {
authorizeRequest(controller: 'api', actionExclude: 'welcome') {
before = {
log.debug("Applying authorization filter.")
}
}
}
}
But when I apply this in practice, the filter runs on all requests (even GET requests, which should use the welcome method and thus should not trigger this filter.)
When I inspect the code running the in the filter, I see that params.action is set to the Map from my routing file, rather than to "welcome". Not sure if this is related to the issue.
My current workaround (which feels very wrong) is to add the following to my filter's body:
if(params.action[request.method] == 'welcome'){
return true
}
The short question is: does Grails support this combination of method-based routing + action-name-based filtering? If so, how? If not, what are some reasonable alternatives for restructuring this logic?
Thanks!
You need to use the filter as below:
class RequestFilters {
def filters = {
authorizeRequest(controller:'api', action:'*', actionExclude:'welcome'){
before = {
log.debug("Applying authorization filter.")
return true
}
}
}
}
Apply the filter to all actions of the controller but "welcome". :)
If there is no other welcome action in the other controllers then, you would not need the controller specified in the filter as well.
authorizeRequest(action:'*', actionExclude:'welcome'){...}

Grails parameter passing

Basically I am using the code below for debugging. It renders null and I cannot figure out the
reason. And by the way the else statement will always be executed at this point. At first I
didnt have the save method but I then thought it might fix my issue. May is have to do with
the scope of my domains? As of now I have them set to session scope:
class Info {
static scope = "session"
String name
String smokingStatus
String[] symptom
static constraints = {
}
}
else{//query did not find patient with that id
def patInfo = new Info()
patInfo.name = "Dennis"
patInfo.smokingStatus = "Former Smoker"
patInfo.symptom = ["Cough"]
patInfo.save()
redirect(action:"display", params: [patInfo:patInfo])
//redirect(action:"login")
//return to login action and
}
}
}
def display(){
render params.name
}
Thanks for any help its much appreciated.
You are assign the value of patInfo to variable name patInfo so in the display action you must use:
render params.patInfo
By example if you will use the following:
redirect(action:"display", params: [duck:patInfo])
You must use:
render params.duck

Grails: Redirecting actions

I have a relatively small app. I have 2 actions in my controller, action1 and action2. What I want is that, if my app is accessed in US, the controller will call action1, and if it is accessed in UK, action2 is called. How can I do this? Can someone show me how to do it in URLMappings or in the controller? Or is their another way to do this?
Thank you.
A simple example for a newbie like me would be greatly appreciated :)
What you can do is to create a filter that will redirect to the appropriate controller depending on the current locale.
More info on Filters.
Edit:
Do something like this:
class LocaleFilters {
def filters = {
checkLocale(controller: '*', action: '*') {
before = {
if (org.springframework.web.servlet.support.RequestContextUtils.getLocale(request) == Locale.US ) {
redirect(action: 'action1')
return false
} else {
redirect(action: 'action2')
return false
}
}
}
}
}

Resources