Trimming String request parameters in controllers actions - grails

I am working in a grails 2.4.4 application which has some legacy code that doesn't use binding or command objects in controllers, in this application the request parameters are passed from controller actions directly to java classes using their constructors.
We want to trim white spaces from strings parameters (just like Grails does by default when binding) but without using binding or command objects.
Maybe injecting a cloned version of the params varible but with trimmed values?
Have any of you done something like this?

Here's an implementation I used in a 2.x app in a filter. It supports nested objects by calling the method recursively:
def filters = {
blankToNullAndTrim(controller: '*', action: '*') {
before = {
convertBlanksToNullsAndTrim params
true
}
}
}
private static void convertBlanksToNullsAndTrim(Map<String, Object> map) {
def keys = map.keySet() as List // copy to avoid ConcurrentModificationException
for (name in keys) {
def value = map[name]
if (value instanceof String) {
value = value.trim()
if (value.length() == 0) {
map[name] = null // have to explicity set to null, not remove
}
else {
map[name] = value // update if trimmed
}
}
else if (value instanceof Map) {
// empty nested param, e.g. "location":["id":""]
convertBlanksToNullsAndTrim value
}
}
}
and here's the equivalent implementation (with type information included so you can use #GrailsCompileStatic) for use in an interceptor when you upgrade to Grails 3.x:
boolean before() {
convertBlanksToNullsAndTrim params
true
}
private void convertBlanksToNullsAndTrim(Map<String, Object> map) {
List<String> keys = map.keySet() as List // copy to avoid ConcurrentModificationException
for (String name in keys) {
def value = map[name]
if (value instanceof String) {
value = value.trim()
if (value.length() == 0) {
map[name] = null // have to explicity set to null, not remove
}
else {
map[name] = value // update if trimmed
}
}
else if (value instanceof Map) {
// empty nested param, e.g. "location":["id":""]
convertBlanksToNullsAndTrim value
}
}
}

One option will be to use Dynamically Resolved Variables in UrlMappings.groovy. For example if there is a mapping as follows:
"/airport/status(controller: 'airport', action: 'checkStatus')
and expecting a request parameter as airportName then it can be rewritten as:
"/airport/status(controller: 'airport', action: 'checkStatus') {
airportName = { params.airportName?.trim() }
}
This would make sure that the arbitrary variable is set to params after trimming the actual request parameter airportName. Care has to be taken to use the same variable name as the request parameter to support the legacy system underneath.
Another option can be to use a filter across the controller to trim the request parameters. Something like:
class UtilityFilters {
def filters = {
trim(controller: "airport", action: "*") {
before = {
// If immutable then make a copy and edit
params.airportName = params.airportName?.trim()
return true
}
}
}
}

Related

change job parameter value in Groovy System Script

My parameterized Freestyle job got one string parameter. MAIL_PARAM with the default value FREESTYLE_ERROR.
I am able to print the value with:
println "MAIL_PARAM=$Mail_Param"
Inside an Groovy execute Script. Now I want to change the value of this parameter based on some conditions. But I am not able to change it. I tried:
MAIL_PARAM = 'String'
$MAIL_PARAM ='String'
${MAIL_PARAM} ='String'
def params = new StringParameterValue('MAIL_PARAM', 'String')
and some more, but none of them are working. I have to change it because based on some results my groovy script has, I need different Strings inside of my parameter.
After the groovy script I need to pass this parameter to the next job. This works fine. But I only get the default value.
If I understand correctly, replaceAction should do the trick (there is also addOrReplaceAction):
import hudson.model.ParametersAction
import hudson.model.ParameterValue
import hudson.model.StringParameterValue
def newMailParameter = new StringParameterValue('MAIL_PARAM', '...')
build.replaceAction(new ParametersAction(newMailParameter))
Edit : if you get error "current build does not have any parameter" then please try "build.addOrReplaceAction" in place of "build.replaceAction".
modify from setBuildParameters: http://jenkins-ci.361315.n4.nabble.com/Modifying-a-builds-parameters-in-a-system-Groovy-script-td4636966.html
def addOrReplaceParamValue = { String name,String value ->
def build = currentBuild.getRawBuild();
def npl = new ArrayList<StringParameterValue>()
def pv = new hudson.model.StringParameterValue(name,value);
npl.add(pv);
def newPa = null
def oldPa = build.getAction(hudson.model.ParametersAction.class)
if (oldPa != null) {
build.actions.remove(oldPa)
newPa = oldPa.createUpdated(npl)
} else {
newPa = new hudson.model.ParametersAction(npl)
}
build.actions.add(newPa);
};
addOrReplaceParamValue("P1","p1");
Here's a more complete example if you need to modify based in the original value:
import hudson.model.ParametersAction
import hudson.model.ParameterValue
import hudson.model.StringParameterValue
def transformJobParameter(Closure transform) {
build.getActions(ParametersAction).each { paramAction ->
List<ParameterValue> overrides = []
paramAction.each { param ->
// Transformation takes a ParameterValue object but returns only its new value object, if any.
def newValue = transform(param)
if (newValue != null && newValue != param.value) {
// Create whatever the original object type was, but with a new value.
def newParam = param.class.newInstance([param.name, newValue, param.description] as Object[])
overrides << newParam
println("INFO - Transformed ${param.name} parameter from '${param.value}' to '$newValue'.")
}
}
if (!overrides.empty) {
def mergedParamAction = paramAction.merge(new ParametersAction(overrides))
build.replaceAction(mergedParamAction)
}
}
}
transformJobParameter { param ->
if (param instanceof StringParameterValue) {
def value = param.value.trim()
if (param.name == 'MAIL_PARAM') {
'String'
} else {
value
}
}
}

How to map Grails domain class properties to non-matching json string?

Is there a built-in / easy way to set mappings between domain class properties and JSON strings that don't have exact matches for the property names?
For example, when I have a domain class:
class Person {
String jobTitle
String favoriteColor
static constraints = {
jobTitle(blank: false)
favoriteColor(blank: false)
}
}
And someone's giving me the following JSON:
{ "currentJob" : "secret agent", "the-color" : "red" }
I'd like to be able to still do this:
new Person(request.JSON).save()
Is there a way in groovy/grails for me to map currentJob -> jobTitle and the-color -> favorite color?
EDIT:
I've done a little experimenting, but I still haven't gotten it working. But I have found out a couple interesting things...
At first I tried overwriting the setProperty method:
#Override
setProperty(String name, Object value) {
if(this.hasProperty(name)) this[name] = value
else {
switch(name) {
'currentJob': this.jobTitle = value; break;
'the-color': this.favoriteColor = value; break;
}
}
}
But this doesn't work for two reasons: 1) setProperty is only called if there is a property that matches name and 2) "this[name] = value" calls setProperty, leading to an infinite recursive loop.
So then I thought, well screw it, I know what the incoming json string looks like (If only I could control it), I'll just get rid of the line that handles the scenario where the names match and I'll override hasProperty, maybe that will work:
#Override
void setProperty(String name, Object value) {
switch(name) {
'currentJob': this.jobTitle = value; break;
'the-color': this.favoriteColor = value; break;
}
}
#Override
boolean hasProperty(String name) {
if(name == "currentJob" || name == "the-color") return true
return false
}
But no, that didn't work either. By a random stroke of luck I discovered, that not only did I have to overwrite hasProperty(), but I also had to have an empty setter for the property.
void setCurrentJob(){ }
That hack worked for currentJob - I guess setProperty only gets called if hasProperty returns true and there is a setter for the property (Even if that setter is auto generated under the covers in grails). Unfortunately I can't make a function "setThe-Color" because of the dash, so this solution doesn't work for me.
Still stuck on this, any help would definitely be appreciated.
EDIT:
Overriding the void propertyMissing(String name, Object value){} method is called by this:
Person person = new Person()
person["currentJob"] = "programmer"
person["the-color"] = "red"
But not by this:
Person person = new Person(["currentJob":"programmer", "the-color":"red"])

Type of Iqueryable at runtime

I have a method which returns Iqueryable result, but the result is based on an if else condition, where if condition satisfies then I will use "AssetDetails" class object ,otherwise "UserandClientdetails" object.
Here is the code:
private IQueryable<?> GetAssetDetails(ShareViewModel item)
{
...
if (type == "Video")
{
if (type == "Video")
{
return from meta in my.Assets().OfType<Model.Video>()
join content in my.Contents() on meta.ContentId equals content.ID
join channel in my.Channels() on content.ChannelId equals channel.ID
where meta.ID == item.ID
select new AssetDetails
{
ContentTitle = content.Title,
ChannelName = channel.ChannelName,
...
};
}
else
{ return from meta in my.Assets().OfType<Model.Client>()
join country in db.Countries on meta.ResellerCountry equals country.ID
where meta.ID == item.ID
select new UserAndClientDetails
{
Name = meta.ResellerName,
UserName = meta.ResellerEmail,
..
};}
So how to decide type of Iqueyable here at runtime??
So, I was able to verify that this works, so I'll go ahead and post it as an answer.
You can return IQueryable instead of the generic IQueryable<>. That will accept any IQueryable<T>. However, IQueryable, since it has no direct inner type, is very limited. So, you'll still likely need to cast to IQueryable<> at some other point in your code to get anything done:
// Piece of code where you know you are working with `IQueryable<AssetDetails>`
IQueryable<AssetDetails> assetDetails = GetAssetDetails(someItem);
That's a little dangerous, though, as you're assuming that your code is working perfectly and the right type of thing is being returned. Better would be:
try
{
var assetDetails = (IQueryable<AssetDetails>)GetAssetDetails(someItem);
// do something with `assetDetails`
}
catch (InvalidCastException)
{
// recover gracefully
}
What about using a base class ?
public abstract class BaseDetails
{
// ...
}
public class AssetDetails : BaseDetails
{
// ...
}
public class UserAndClientDetails: BaseDetails
{
// ...
}
Then you method would be like :
private IQueryable<BaseDetails> GetAssetDetails(ShareViewModel item)
{
// return either IQueryable<AssetDetails> or IQueryable<UserAndClientDetails>
}

combining criteria from params array

This example here is from the grails docs:
def emeaCriteria = {
eq "region", "EMEA"
}
def results = Airport.withCriteria {
emeaCriteria.delegate = delegate
emeaCriteria()
flights {
like "number", "BA%"
}
}
My webpage is passing back a checkbox group of ethnicities, returning the row ids. So what the server gets is:
ethnicity:[1, 4]
or if the user only picks one ethnicity:
ethnicity:4
def criteria = { params ->
//handle case where only one ethnicity is returned as just a string, not a list of strings
def list = params.ethnicty instanceof String ? [params.ethnicty] : params.ethnicity
if (list) {
inList('ethnicity', list)
}
}
I'm getting an error: java.lang.String cannot be cast to java.lang.Enum.
If I didn't have a list I think I could figure it out. The params are sending back string values, and they need to be converted to the enum class. But within the closure, how do you convert each entry into a list to the enum?
I figured it out through a combination of multiple website posts and with help from dmahapatro above.
def genderCriteria = {
if (params.gender) {
inList('gender', params.list('gender').collect { Gender.valueOf(it)} )
}
}
If a webpage passes back one or more enums (a single string or a list of strings) and you want criteria to check values from the list passed back, you have to provide a list of enum types (not strings or ints).
Here is my enum class for reference:
public enum Gender {
M('Male'),
F('Female'),
U('Unknown')
final String value
Gender(String value) {
this.value = value
}
public String toString() {
value
}
public String getKey() {
name()
}
public String getValue() {
value
}
}
And my criteria builder:
def c = MyDomain.createCriteria()
results = c.list {
genderCriteria.delegate = delegate
genderCriteria(params)
}
Even if no values are passed back for the gender field, it still works (because of the if statement in genderCriteria.
It may not be the best or cleanest solution, but it does work.

How to override standard behavior of ApplicationTagLib#createLink and g:link?

Background:
I have grails 1.3.7 application which uses g:createLink and g:link on many pages.
Recently I decided to make big change in url mappings - introduce preceding path element.
Currently I have: /$controller/$action?/$id?
But want to have: /$regionId/$controller/$action?/$id?
It was easy to change urlMappings, but I can't figure out how to easily change the behavior how links are built throught the application.
Basically, I don't want to go through each page and change links. But want to do this in one place.
Question
How to override ApplicationTagLib#createLink functionality so grails will use this implementation without the need of changes pages which use this tag (or function)?
Any help greatly appriciated!
I had a smilar problem. Actually you can decorate g:link tag like this.
1) TagLib
import org.codehaus.groovy.grails.plugins.web.taglib.*
class OverrideDefaultTagLib {
static namespace = "g"
def link = { attrs, body ->
def c = "1" // Get it from session or somewhere else
if (attrs.params) {
attrs.params.put("region", c)
} else {
attrs.params = [region: c]
}
def applicationTagLib = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
applicationTagLib.link.call(attrs, body)
}
}
}
2) add to UrlMappings.groovy
"/$region/$controller/$action?/$id?"{}
I was unable to solve this problem in terms of OOP. I mean I can't find way how to override closure. I tried several approaches, but with no success. And documentation says that you can't override closure, you can only replace it with new implementation (please correct me if I wrong).
But (!) I was able to solve task by copy-pasting source code of ApplicationTagLib#createLink method.
I think this is brutal solution, but after 8 hours of fighting with this simple task - it's acceptable.
So finally all I need to do - is define this class, grails will immediately use it for link generation (for all views, no need to change their code):
import java.text.SimpleDateFormat;
import groovy.time.*;
import java.text.*;
import org.codehaus.groovy.grails.commons.GrailsControllerClass
import org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib;
import org.codehaus.groovy.grails.web.mapping.UrlCreator
import org.codehaus.groovy.grails.commons.ControllerArtefactHandler
import org.springframework.web.context.request.RequestContextHolder
class OverrideTagLib extends ApplicationTagLib {
def createLink = { attrs ->
// get value for regionId parameter
def regionId = regionIdFinderService.currentRegionId
// add cutsom regionId parameter
if (attrs) {
if (attrs.params)
attrs.params.put("regionId", regionId);
else {
attrs.params = ["regionId":regionId];
}
}
// process
def writer = getOut()
// prefer URI attribute
if (attrs.uri) {
writer << handleAbsolute(attrs)
writer << attrs.uri.toString()
}
else {
// prefer a URL attribute
def urlAttrs = attrs
if (attrs.url instanceof Map) {
urlAttrs = attrs.remove('url').clone()
}
else if (attrs.url) {
urlAttrs = attrs.remove('url').toString()
}
if (urlAttrs instanceof String) {
if (useJsessionId) {
writer << response.encodeURL(urlAttrs)
}
else {
writer << urlAttrs
}
}
else {
def controller = urlAttrs.containsKey("controller") ? urlAttrs.remove("controller")?.toString() : controllerName
def action = urlAttrs.remove("action")?.toString()
if (controller && !action) {
GrailsControllerClass controllerClass = grailsApplication.getArtefactByLogicalPropertyName(ControllerArtefactHandler.TYPE, controller)
String defaultAction = controllerClass?.getDefaultAction()
if (controllerClass?.hasProperty(defaultAction)) {
action = defaultAction
}
}
def id = urlAttrs.remove("id")
def frag = urlAttrs.remove('fragment')?.toString()
def params = urlAttrs.params && urlAttrs.params instanceof Map ? urlAttrs.remove('params') : [:]
def mappingName = urlAttrs.remove('mapping')
if (mappingName != null) {
params.mappingName = mappingName
}
if (request['flowExecutionKey']) {
params."execution" = request['flowExecutionKey']
}
if (urlAttrs.event) {
params."_eventId" = urlAttrs.remove('event')
}
def url
if (id != null) params.id = id
def urlMappings = applicationContext.getBean("grailsUrlMappingsHolder")
UrlCreator mapping = urlMappings.getReverseMapping(controller,action,params)
// cannot use jsessionid with absolute links
if (useJsessionId && !attrs.absolute) {
url = mapping.createURL(controller, action, params, request.characterEncoding, frag)
def base = attrs.remove('base')
if (base) writer << base
writer << response.encodeURL(url)
}
else {
url = mapping.createRelativeURL(controller, action, params, request.characterEncoding, frag)
writer << handleAbsolute(attrs)
writer << url
}
}
}
}
}
add regionId to params in createLink and g:link and grails is smart enough to match your urlmappings. i.e
${createLink(controller:'c',action:'a',id:1,params:[regionId:2])}

Resources