redirect user from php url to grails page - grails

I am using grails 2.X.
when user hits on http://www.abcdef.com/redir/abc.php?id=123, i want to redirect user to some other url mappings like http://www.abcdef.com/abcd.
I already used grails filter but can't get appropriate solution.
Can anyone tell me how to do so using grails?
Do we need Filters or some scripts to achieve this.
Thanks in advance.

This can be achieved in UrlMappings.groovy. This configuration should suit your needs:
"/$controller.$suffix" {
constraints {
suffix(matches: 'php')
}
}

Related

Generic URLMapping in Grails for all 4xx or 5xx Errors

I am using Grails 2.4.4 and would like to define a generic UrlMapping for a range of HTTP-error-codes (like 450-499, 510-540).
I've found some related questions - even on SO - but the answers are either outdated or not working.
The container does not start once I use regular expressions on error-mappings.
For instance, this simple example will fail:
"$errorCode" {
controller = "error"
action = "general"
constraints {
errorCode(matches:/\d{3}/)
}
}
Does anyone know how I may approach this?
I tried doing same using Filters but we cannot redirect again after checking status code in httpResponse hence that also doesn't help.
As per grails-doc "Mapping to Response Codes"
http://grails.github.io/grails-doc/3.1.x/guide/single.html#urlmappings
we can only hard code them and redirect it to mentioned controller and action.
So you need to mention all http codes and handle all of them separately.
Thanks.

Retrieving HTTP parameters in Grails

the request was sent from an HTML form, the request body contains the form data,how can i retrieve that data in my Grails application.Below is the URL from where i need to retrieve data.I'm a Rookie in Grails so please help me with this.
hxxp//localhost:8080/copypolicyNumber/loginaction.jsp?fname=Roger&lname=wallace&city=Des+Moines&pnum=123456&submit=Submit
As you just started with Grails, I suggest you look at the screencasts available in the Grails website, and also check the free e-book Getting Started with Grails (need registration).
Grails works with the params map for both GET and POST requests. Also, it uses an special url mapping that you need to be aware of.
So, assuming that you have a login controller with the action login and considering that you called the url: myapp/login/login?fname=Roger&lname=wallace&city=Des+Moines&pnum=123456
class LoginController {
def login() {
println params.fname //Roger
println params.lname //wallace
println params.city //Des Moines
println params.pnum //123456
}
}

Grails: How to protect gsp views with shiro?

I am using shiro to protect my grails application, using the default setup as with
grails shiro-quick-start
and a custom user. So far, all controller actions are protected, but if I put in the URL to a GSP-File (say, index.gsp), I do not get a login prompt.
The default filter I use is
def filters = {
all(uri: "/**") {
before = {
accessControl()
}
}
}
but seemingly GSP-Files do not match this URI-Filter. What URI-Filter do I have to use? I am using grails 1.3.7 and shiro plugin 1.1.3
very interesting, but seems to be a grails problem, not a shiro one.
I didn't manage to secure /object/view.gsp pages through the filters mechanism - it seems to be based on controller/action and since there is no controller involved...
But I did find another solution. Add the following line to your UrlMappings.groovy file
"/$folder/${view}.gsp"(controller:'auth', action:'login')
this will send your .gsp files through a controller.

Best practice for redirecting from one web domain to another in Grails?

I am currently working on a filter in Grails that will allow me to redirect all incoming requests on foo.org to the same subpage on foo.com.
So far I have been doing the following:
if(!(""+request.requestURL).toLowerCase().startsWith(
grailsApplication.config.grails.serverURL ))
{redirect(url:"${grailsApplication.config.grails.serverURL}${request.requestURI}",params:params) }
Unfortunately, I am experiencing several issues in this approach:
The request.requestURI value seems to behave differently than expected: instead of giving me the normal "/[controller]/[action]" pattern as I would expect, it returns something like: "/grails/[controller]/[action].dispatch". - Is there an alternative way to obtain the "normal" URI? (excuse me if this is trivial, but have not been able to find it in the documentation, nor by trying out the various methods available on the request object)
Params are not being passed in the above redirect. This is probably due to the fact that I am using the "url" parameter in the redirect which according to the docs is supposed to be used for redirects to absolute paths (which again causes it to ignore the params section?). However, since I will not be able to use the normal redirect(controller:...,action:...) approach when redirecting to another domain what approach could I use in order to pass the params correctly along to the subpage on foo.com ? Am considering a solution where I will add the params manually via a params.each{} closure, but isn't there a more elegant solution to this?
301 redirects. Since my redirects are of a permanent nature, I would like to use the 301 status code. I have tried to set "response.status = 301" but it seems to be ignored when using the Grails redirect(...) method. Further I can see from grails.org that this seems to be introduced with grails 2.0, but is there a way to obtain this already now in Grails 1.3.7?
Use request.forwardURI.
If you have meant GET params, then it should be resolved using the above URI?
I think 301 redirects are not possible using classic redirect. You can do this in a filter like this, which is obviously not the cleanest way:
def filters = {
all(controller:'*', action:'*') {
before = {
if (request.serverName == "foo.org") {
response.setStatus(301);
response.setHeader("Location", "http://foo.com" + request.forwardURI)
response.flushBuffer()
return false; // return false, otherwise request is handled from controller
}
}
}
}

Grails set language (internationalization) via URL mappin

I'm interested in supporting English and French in my Grails app with user-friendly URL.
format: /appname/language/controller/action
example: /store/en/product/list
What is the best way without passing as a parameter (?lang=fr) then rewriting the URL.
Thanks
Adding the following mapping in your appname\grails-app\conf\UrlMappings.groovy should do it.
"/$lang/$controller/$action?/$id?"{
}
See the URL mapping article on the grails website for details.

Resources