Grails catch all url mapping with exceptions - grails

I am trying to create a mock rest service. There are basically two components to this. I need a controller with actions and views that allows me to create ResourceMappings where I create a mapping between a uri and a mock response that I will pass back. The second component is a catch-all Grails url mapping for every other url so that when they hit the uri of this application they will be sent to my catch all controller that will return the mapped response that they created earlier.
For example... I go to the url http://someserver.com:1234/restMapping/list. This url is the exception to the catch all rule. It takes me to some views that allow me to create a rest uri mapping. Here I create the mapping /mockservice/test and give it the response "This is a test" with a content type of text/plain. Now, if I go to the url http://someserver.com:1234/mockservice/test I should hit the catch all that sends me to a controller that returns a page with the content type text/plain and the response "This is a test".
I have tried the following and it does not seem to work. Does anyone have any ideas?
static mappings = {
"/$control/**" {
controller = "catchAllHandler"
action = "index"
constraints {
control(validator: {!['restMapping','css','js','images'].contains(it)})
}
}
"/$controller/$action?/$id?"{
}
"/"(controller:"restMapping", action="index")
"500"(view:'/error')
}
An interesting thing to note is that when I get rid of ** and add in tons of extra variables like $s1?/$s2?/$s3? etc then it does seem to work. The problem is that I don't know how long the uri is that I am trying to map so I would rather use the ** to catch everything exception the few exceptions that I have.

I finally figured it out. I needed to include WEB-INF in my list to exclude. I now use the static excludes field as well as the validator to exclude specific controller urls.
class UrlMappings {
static excludes = ["/images/*","/css/*","/js/*","/WEB-INF/*"]
static mappings = {
"/restResourceMapping/$action?/$id?"{
controller = "restMapping"
}
"/$control/?**" {
controller = "catchAllHandler"
action = "index"
constraints {
control(validator: {!['restMapping'].contains(it)})
}
}
"/"(controller:"restMapping", action="index")
"500"(view:'/error')
}
}
I decided to just exclude all url mappings that should never be any of my rest urls. These included /images, /css, /js, and /WEB-INF. I can now create urls of any length and have them go to my catch all controller. If the person goes to the base url or the restMapping url set then they will be taken to the crud pages where they can create new rest resource mappings. If I want to create any other controllers and views that I want to go around the catch all controller I can simply add it to my validator and make them process normally.
Also you might notice that I am using a ? in the catch all right after the /. This seems to make it so my catch all works with urls that only have one word after the server name as in http://server.com:1234/something.

I am not sure about this, but I think the order in which the URL mappings are defined is important. So try listing the URL mapping for your special cases at the beginning of the mappings closure and then list the general ones (the one that uses **). Do let me know if this works :)

Related

Grails <g:link> rewriting links to match UrlMappings instead of controller/action

Currently using grails 4.0.3.
I'm trying to generate a link to a controller action using a simple <g:link> tag:
<g:link controller="myController" action="myAction" params="[someParam:'myValue']">Link text</g:link>
In my UrlMappings file, I have this controller mapped to a common URL for external calls. This mapping forces some parameters that I want forced when coming to this mapping:
class UrlMappings {
static mappings = {
"/service/someExposedUrl" {
controller = "myController"
action = "myAction"
someParam = "defaultValue"
}
}
}
However, the link that gets written to the page in my application gets written using the UrlMapping definition.
http://serverUrl/service/someExposedUrl?someParam=<ignored>
instead of
http://serverUrl/myController/myAction?someParam=myValue
In this case, the parameters are ignored on the /service URL because they're hard-coded in the UrlMapping.
Is there a way to force grails to link to the specified controller/action instead of the mapping?
Up front caveat: I have not tested this for this specific situation, though I have used these pieces individually. Second caveat: I would not be remotely surprised to learn there's an even better way to do this...grails has a lot of options sometimes!
You should be able to create a named mapping in your UrlMappings, then reference that in your createLink.
UrlMappings:
name arbitraryName: "/myController/myAction" {
controller = "myController"
action = "myAction"
}
Link:
<g:link mapping="arbitraryName" params="[someParam:'myValue']">Link text</g:link>
You may be able to get what you need with a combination of Custom URL mapping and the exclude keyword in the original URLMappings.
Create MyControllerUrlMappings. This guide gives several examples of different ways of writing them out. I found it helpful.
class MyControllerUrlMappings {
static mappings = {
// Url Mappings specific only to this controller
}
}
https://guides.grails.org/grails_url_mappings/guide/index.html#_multiple_urlmappings_files
Your app will still be running the original UrlMappings file though. So you'll have the new custom Url mappings and the default ones. To fix that you can exclude your controller and it's methods in the original UrlMappings file.
class UrlMappings {
static excludes = [
"/myController",
"/myController/*"
]
static mappings = {...}
...
Not as elegant as I'd like, but in a similar situation this is how I got it working.

Grails: URL mapping with .gsp extension/format

I have a url request like www.xyz.com/customer/list.gsp
When I try to map the url to remove .gsp:
"/customer/list.gsp"(controller: "customer") {
action = "list"
}
grails application won't recognize the url and is throwing 404 error. Am I missing something here?
If you want to remove .gsp from the url then you can use a mapping like this...
"/customer/list"(controller: "customer") {
action = "list"
}
You could also do this...
"/customer/list"(controller: "customer", action: "list")
If you want 1 mapping for all the actions in the controller, you could do this:
"/customer/$action"(controller: "customer")
The default generated mapping includes "/$controller/$action" which allows you map to any action in any controller.
With any of that, sending a request to /customer/list would work.
Update: apparently it is fine to map to GSPs. I still think that the info below may be helpful so I'm leaving the answer up, but perhaps I have misunderstood your question.
Original response:
You shouldn't be mapping to or requesting gsps at all. They're used to generate views, but are not viewable without rendering.
Instead, go to url like www.xyz.com/customer/list and map that like
"/customer/list" (controller: "customer") {
action = "list"
}
Or even better, you don't need a custom mapping for each endpoint. A default like this will work:
"/$controller/$action?/$id?" { }
Your CustomerController will render the list.gsp in the list action.

Grails 2.3.8 404 when controller/method called

I have a weird error going on where when I start my Grails application and see the default home page, no matter what controller I click on I get a 404 not found. But if I change my URL mappings file to point a particular controller and method, then the associated view is rendered fine. I tried typing the url as 'controller/method' but that also gave the 404 and I know that the methods aren't being executed because my test log statements don't print. The only way my views are coming up is if I force them using the URL mappings file and then the test log statements in the methods will also print.
Make sure you have a URL mapping defined which supports the controller/action pattern.
class UrlMappings {
static mappings = {
// you probably want a mapping like this...
"/$controller/$action?/$id?(.$format)?"{
}
// other mappings here
}
}

Grails Reverse Url Mapping: Is there a way to build links based on the currently matched route?

I'm trying to build some dynamic URL mappings that are based on the domain classes of the grails project. The goal is to use a single generic controller, but the URL decides which domain is being used for processing. The problem is now, that I don't get the desired URLs when using the <g:link /> tag.
I tried the following variants to create the URL mappings:
static mappings = {
Holders.grailsApplication.domainClasses.each {
"/admin/${it.propertyName}/$action?/$id?"(controller: "admin")
}
}
static mappings = {
"/admin/$domainClass/$action?/$id?"(controller: "admin")
}
Both variants work for the actual URL matching. But I personally don't like the behavior of the reverse URL mapping by grails. In case of variant 1, the reverse mapping always resolves to the last added URL mapping for the AdminController. For case 2 I have the problem that I would have to pass the domainClass-Parameter to every link-creating call, even though it is theoretically not necessary, since the information is already present in the current request.
I know there is the possibility of using named URL mappings and then using something like <g:link mapping="mapping_name" />. The problem is that I am using some generic application-wide partial-views, where I try to only provide the necessary information for creating a link, like <g:link action="something" />.
This leads to my two questions:
Is there a possibility to get g:link to build the URL based on the matched mapping in the current request?
Is there a way to get a reference to the mapping that matched the current request, so I can implement to desired behavior myself?
You could define named mappings like
Holders.grailsApplication.domainClasses.each { dc ->
name((dc.propertyName):"/admin/${dc.propertyName}/$action?/$id?" {
controller = "admin"
domainClass = dc.propertyName
})
}
With the mapping name saved in the params you can now do
<g:link mapping="${params.domainClass}">link text</g:link>

RESTful grails application: DRYing up UrlMapping

Let's say we have a grails web application exposing several resources.
tags
urls
users
The application has a classical web-interface which the users interact with and some administration.
We want to expose the resources from the application to clients via a RESTful API and we don't want that part of the app to clutter up the controllers and code we already have.
So we came up with the following:
If the web interface offers host/app_path/url/[list|show|create] we want the REST API to be at /host/app_path/rest/url.
So we ended up with the following UrlMappings file:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
}
/* adding new urls and listing them */
"/rest/url"{
controller = "urlRest"
action = [POST: "save", PUT: "save", GET: "list", DELETE:"error"]
}
/* accessing a single url */
"/rest/url/$id"{
controller = "urlRest"
action = [POST: "update", PUT: "update", GET: "show", DELETE: "delete"]
}
/* non-crud stuff on urls */
"/rest/url/$action?/$id?"{
controller = "urlRest"
}
"/"(view:"/index")
"500"(view:'/error')
}
}
The problem is, that this isn't exactly the most DRY thing here. It gets worse as we add more resources such as tags. They would translate to yet another three blocks of very similar code...
The non-crud functions will be things like searching with specific criterions and such...
We tried generating the mapping closures with a loop, but without success. Are we completely on the wrong track here?
I would recommend the following mapping:
"/rest/url/$id?"(resource:"urlRest")
Below is the HTTP method to action mapping that this would create for the urlRestController:
GET show
PUT update
POST save
DELETE delete
I see why you might want to map /rest/url POST to save and /rest/url/id PUT to update, but that goes against the meaning of those verbs. A PUT should be the only way to add a new url and POST the only way to update a url. Doing it the way you have laid out would work and might be the best way if your constraint is to keep your current controller code untouched. However, my guess is that you controller might already be coded to handle the default mappings just fine (update/delete give error if no id, show redirects to list if no id, etc.).

Resources