How to selectivelly pretty print Grails responses? - grails

Grails converters can be configured to pretty print (or not) by default.
I like to use the respond method instead any MarkupBuilder, JsonBuilder or whatever other library that I should handle response format.
respond myObject
The user can query xml, json or hal.
How can I allow the user to query for a pretty print (assuming default is not)
If the user pass a parameter in the url like this:
/foos?pretty=true
How can I force the respond to pretty print?
Obs. I saw this question but they all solve the problem statically. I want to change it dinamically on request scope.

Since you want to be able to select the format based on a controller parameter, I think a Grails converter long with render() will be better than using respond(). Here's an example:
import grails.converters.*
class SomeController {
def foos() {
def pretty = params.pretty
def prettyType
def myObject = /* whatever */
if(pretty == 'xml') prettyType = XML
if(pretty == 'json') prettyType = JSON
if(prettyType) render myObject.asType(prettyType)
else render myObject
}
}
The URL would be something like: /foos?pretty=xml or /foos?pretty=json.

Related

Combina pyramid with jinja2 install_gettext_translations function?

I have a pyramid application that uses a translation factory defined in this way:
from pyramid.i18n import get_localizer, TranslationStringFactory
from pyramid.threadlocal import get_current_request
def add_renderer_globals(event):
request = event.get('request')
if request is None:
request = get_current_request()
event['_'] = request.translate
event['localizer'] = request.localizer
tsf = TranslationStringFactory('climmob3')
def add_localizer(event):
request = event.request
localizer = get_localizer(request)
def auto_translate(string):
return localizer.translate(tsf(string))
request.localizer = localizer
request.translate = auto_translate
It works fine, however somewhere else I use jinja2 render() function to render small pieces of reusable code (snippets) as a jinja2 extension:
from jinja2 import Environment
jinjaEnv = Environment(extensions=['jinja2.ext.i18n'])
output = template.render(snippetVars=kw,renderer='snippet')
The problem here is that when I use the '_' translation function in the template code I get:
UndefinedError: 'gettext' is undefined
I saw some posts that maybe I need to use jinjaEnv.install_gettext_translations() but I cannot make it work. I tried:
jinjaEnv.install_gettext_translations(pyramid.il8n)
jinjaEnv.install_gettext_translations(tsf)
How can I integrate jinjaEnv.install_gettext_translations() with my pyramid translation factory?
Depending on your exact case, you could use pyramid_jinja2 or get inspiration from it. It creates a GetTextWrapper https://github.com/Pylons/pyramid_jinja2/blob/28944ce627745691ccd1603c56251e038aadd892/pyramid_jinja2/i18n.py that makes its way in the options passed when creating the Environment https://github.com/Pylons/pyramid_jinja2/blob/28944ce627745691ccd1603c56251e038aadd892/pyramid_jinja2/settings.py#L133
https://github.com/Pylons/pyramid_jinja2/blob/28944ce627745691ccd1603c56251e038aadd892/pyramid_jinja2/__init__.py#L394
https://github.com/Pylons/pyramid_jinja2/blob/28944ce627745691ccd1603c56251e038aadd892/pyramid_jinja2/__init__.py#L404-L405
The wrapper is needed because the localizer will change every request, depending on the user locale.
Or you can pass the gettext and ngettext arguments directly when you render. In you case, it would look something like:
localizer = request.localizer
def gt(message):
return localizer.translate(message, domain='your-domain')
def ngt(singular, plural, n):
return localizer.pluralize(singular, plural, n, domain='your-domain')
output = template.render(
snippetVars=kw,
renderer='snippet',
gettext=gt,
ngettext=ngt,
)

Grails - Command object, service method

I'm not a programming savvy person, so please bear with me.
I've read blog entries and docs about command object. I've never used it and was wondering if I should. (I probably should...)
My project requires parsing, sorting, calculating, and saving results into database when users upload files.
So according to one of the blog entries I read and its corresponding github code,
1) SERVICE should receive file uploads, parse uploaded files (mainly docs and pdfs), sort parsed data using RegEx, and calculate data,
2) COMMAND OBJECT should call SERVICE, collect results and send results back to controller, and save results into the database,
3) CONTROLLER should receive request from VIEW, get results from COMMAND OBJECT, and send results back to VIEW.
Did I understand correctly?
Thanks.
I found this to be the best setup. Here is an example that I use on production:
Command Object (to carry data and ensure their validity):
#grails.validation.Validateable
class SearchCommand implements Serializable {
// search query
String s
// page
Integer page
static constraints = {
s nullable: true
page nullable: true
}
}
Controller (directs a request to a Service and then gets a response back from the Service and directs this response to a view):
class SomeController {
//inject service
def someService
def search(SearchCommand cmd) {
def result = someService.search(cmd)
// can access result in .gsp as ${result} or in other forms
render(view: "someView", model: [result: result])
}
}
Service (handles business logic and grabs data from Domain(s)):
class SomeService {
def search(SearchCommand cmd) {
if(cmd.hasErrors()) {
// errors found in cmd.errors
return
}
// do some logic for example calc offset from cmd.page
def result = Stuff.searchAll(cmd.s, offset, max)
return result
}
}
Domain (all database queries are handled here):
class Stuff {
String name
static constraints = {
name nullable: false, blank: false, size: 1..30
}
static searchAll(String searchQuery, int offset, int max) {
return Stuff.executeQuery("select s.name from Stuff s where s.name = :searchQuery ", [searchQuery: searchQuery, offset: offset, max:max])
}
}
Yes, you understood it correctly except the one thing: command object shouldn't save the data to DB - let service to do that. The other advantage of command object is data binding and validation of data from the client. Read more about command objects here grails command object docs
You can also find helpful information regarding your question in this article
grails best practices
I guess not. Its not really related to whether the save is done in a service it should always attempt to carry out complex stuff and specifically db stuff in a service. so that is regardless. I tend to not use command object but have got hooked on helper classes aka beans that sit in src/main/groovy and do all of the validation and formatting. I just did a form and in it has feedback and reason.
Initially I thought I would get away with
def someAction(String feedback, String reason) {
someService.doSomething(feedback,reason)
}
But then I looked closed and my form was firstly a textarea then the selection objects were bytes so above was not working and to simply fix it without adding the complexity to my controller/service I did this:
packe some.package
import grails.validation.Validateable
class SomeBean implements Validateable {
User user
byte reason
String feedback
static constraints = {
user(nullable: true)
reason(nullable:true, inList:UsersRemoved.REASONS)
feedback(nullable:true)
}
void setReason(String t) {
reason=t as byte
}
void setFeedback(String t) {
feedback=t?.trim()
}
}
Now my controller
class SomeController {
def userService
def someService
def doSomething(SomeBean bean){
bean.user = userService.currentUser
if (!bean.validate()) {
flash.message=bean.errors.allErrors.collect{g.message([error : it])}
render view: '/someTemplate', model: [instance: bean,template:'/some/template']
return
}
someService.doSomeThing(bean)
}
}
Now my service
Class SomeService {
def doSomeThing(SomeBean bean) {
if (bean.user=='A') {
.....
}
}
All of that validation would have still had to have been done somewhere, you say no validation but in a good model you should do validation and set things to be stored in proper structures to reduce overloading your db over time. difficult to explain but in short i am talking about your domain class objects and ensuring you are not setting up String something string somethingelse and then not even defining their lenghts etc. be strict and validate
if you have a text area this will be stored in the back end - so you will need to trim it like above - you will need to ensure the input does not exceed the max character of the actual db structure which if not defined will probably be 255
and by doing
static constraints = {
user(nullable: true)
reason(min:1, max:255, nullable:true, inList:UsersRemoved.REASONS)
Has already invalidated it through the bean.validate() in the controller if the user exceeded somehow my front end checks and put in more than 255.
This stuff takes time be patient
Edited to finally add in that example byte - is one to be careful of -
When adding any String or what ever I have started to define the specific like this and in the case of byte if it is a boolean true false - fine if not then define it as a tinyint
static mapping = {
//since there is more than 1 type in this case
reason(sqlType:'tinyint(1)')
feedback(sqlType:'varchar(1000)')
// name(sqlType:'varchar(70)')
}
If you then look at your tables created in the db you should find they have been created as per definition rather than standard 255 varchar which I think is the default for a declared String.

grails reflection - Calling a dynamic tag

I know this is odd, but I'm trying to provide a pass through interface in a taglib that allows the caller to pass in any other tag to be displayed in a container with additional processing.
To this effect, I'm trying to dynamically call an arbitrary tag in an arbitrary namespace. This may be clearer by example.
GSP:
<myLib:myTag someProp="blah" anotherProp="blah2" size="80" namespace="g" tag="textField">
In my taglib, I'm trying to display the tag they pass.
Taglib:
def myTag = {
String id = //some processing, not specified by caller
attrs.put("id", id)
def namespace = attrs.remove("namespace")
def tag = attrs.remove("tag")
out << ?????
}
The problem comes after the out... I'm having trouble calling the tag. I've tried the following with the following errors
namespace.tag(attrs) //No signature of method: java.lang.String.tag()
namespace."${tag}"(attrs) //No signature of method: java.lang.String.textField()
"${namespace}"."${tag}"(attrs) //No signature of method: java.lang.String.textField()
This seems to work, but the method needs to support tags in other namespaces
g."${tag}"(attrs)
So the question is How can I use reflection to use a dynamically defined taglib?
I don't have them pass the fully formed tag in the body() because I need to interact with it in the taglib.
I believe the following works, I'm not completely certain this is the best answer, so will leave the question open for suggestions from others.
String namespace = attrs.remove("namespace")
String tag = attrs.remove("tag")
GrailsTagLibClass tagLibClass = grailsApplication.getTagLibClasses().find { it.namespace == namespace && it.hasTag(tag)}
def tagLibBean = grailsApplication.mainContext.getBean(tagLibClass.getFullName())
out << tagLibBean."${tag}"(attrs)

Response Header Size in Grails Controller class

Is it possible to find out the size of response header in Grails Controller groovy class ?
Can't test it out right now but something like this should work:
def headers = response.getHeaderNames()
def size
headers.each { header ->
def headerValue = response.getHeader(header)
// get the size of headerValue in whatever unit you're looking for and add it to
// the size variable
}
Remember this is free hand since I don't have the setup to test it right now but grails response objects are just instances of HttpServletResponse so it should be pretty simple.

Grails - how to pass Domain Objects and use services

I'm writing a service that shouldn't have to save anything. It gets some values. Looks up a few things in the database, and then coughs back a response. Is there anything I should do to make the service faster/less overhead? Also whats the best way to pass it something. I usually pass the id and get it again; is that good/bad/dumb?
example
class DoStuffController {
def ExampleProcessingService
def yesDoIt = {
def lookup = "findme"
def theObject = ExampleThing.findByLookie(lookup)
def lolMap = ExampleProcessingService.doYourThing(theObject.id)
if(lolMap["successBool"]){
theObject.imaString = "Stuff"
theObject.save()
}
[]
}
}
service
class ExampleProcessingService{
static transactional = true //???????? false? not-a?
def doYourThing = {theID ->
def returnMap = [:]
def myInstance = ExampleThing.get(theID)
if(myInstance.something)returnMap.put "successBool", true
else returnMap.put "successBool", false
return returnMap
}
}
domain object
class ExampleThing {
String imaString
String lookie
static constraints = {
imaString(nullable:true)
}
def getSomething() {
return true
}
}
bootstrap
import learngrails.*
class BootStrap {
def init = { servletContext ->
def newThing = new ExampleThing(lookie:"findme")
newThing.save()
}
def destroy = {
}
}
Is the an advantage, disadvantage or standard to passing ID and doing get vs. passing the object? Does this change given my case of not going to save anything in the service? Is there something I'm doing glaringly wrong? Do you have a better suggestion for the title?
You've asked a lot of questions, and should split this into several individual questions. But I'll address the overall issue - this approach is fine in general.
There's not a lot of overhead in starting and committing a transaction that doesn't do any database persistence, but it is wasteful, so you should add
static transactional = false
In this case you're using the class as an easily injected singleton helper class. It's convenient to do transactional work in services because they're automatically transactional, but it's far from a requirement.
One thing though - do not use closures in services. They're required in controllers and taglibs (until 2.0 anyway) but should always be avoided in services and other classes. If you're not using the fact that it's a closure - i.e. passing it as an object to a method as a parameter, or setting its delegate, etc. - then you're just being way too groovy. If you're calling it like a method, make it a method. The real downside to closures in services is that when you want them to be transactional, they cannot be. This is because Spring interceptors intercept method calls, not closure calls that Groovy pretends are method calls. So there won't be any interception for transactions, security, etc.

Resources