Posted: Fri, Mar 25 11:54 AM (PDT)
I don't know why, but function decodeBase64 in Grails doesn't work correctly. And I found other solution. See below
Now we include the base64 library
import org.apache.commons.codec.binary.Base64
Our encode function
def encodeBase64(String text) {
return new String(Base64.encodeBase64(text.getBytes()))
}
Our decode function
def decodeBase64(String text) {
return new String(Base64.decodeBase64(text))
}
That's all
Tags:
Posted: Thu, Mar 24 01:49 PM (PDT)
1. include JSON library
import grails.converters.JSON
2. use JSON library
def jsonString = '{"syntax":true,"email":"test@test.local","status":true,"domain":true}'
def json = JSON.parse(jsonString)
println json
All records are serializabled
happy end :-)
Tags:
Posted: Thu, Mar 24 04:39 AM (PDT)
Simple function how you can make HTTP request in Grails
def httpPostRequest(String address, String data, String referer = "http://www.google.com", method = "POST", userAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13") {
method = method?.toString()?.toUpperCase()
if (method != "GET") { method = "POST" }
try {
def url = new URL(address)
def conn = url.openConnection()
conn.setRequestMethod(method)
conn.setRequestProperty("User-Agent", userAgent)
conn.setRequestProperty("Referer", referer)
conn.doOutput = true
Writer wr = new OutputStreamWriter(conn.outputStream)
wr.write(data)
wr.flush()
wr.close()
conn.connect()
return conn?.content?.text
} catch (e) {
println e
return false
}
}
Now, try to use our function
def content = httpPostRequest("http://www.atlas365.com", "test=my%20first%20test")
if (content) { println content } else { println "Error!" }
This example show us "How we can get the web page http://www.atlas365.com/ by POST request with test parameter". The test parameter is equal "my first test"
Tags:
Posted: Wed, Mar 23 06:43 AM (PDT)
Perfect Grails plugin "grails email validator"
This plugin
- Check if domain exist in DNS
- Check domain MX records
- Check if email address is exist and valid
The plugin is available on GitHub: https://github.com/tomaslin/grails-email-validator
Tags:
Posted: Fri, Mar 11 01:40 PM (PST)
In your GSP view page you can use below code, but I don't recommend you use this solution
<%@ page import="my.project.MyService" %>
<% def myService = grailsApplication.classLoader.loadClass('my.project.MyService').newInstance() %>
And then you just call ${myService.method()} in your gsp view
While this works, it would be far more elegant to either inject the service into the view via model or to create a taglib for the service and use this from the gsp view.
Tags:
Posted: Thu, Mar 10 04:12 AM (PST)
Simple exec method as shown below to run sub processes from groovy / grails. If you run the standard exec like:
def cmd = "/bin/ls -la"
def proc = cmd.execute()
proc.waitFor()
But I have other advanced solution for you. Where you can read response from your executed program. See below:
def command = "/bin/ls -la"
StringWriter stringWriterOutput = new StringWriter()
StringWriter stringWriterError = new StringWriter()
Process proc = command.execute()
stringWriterOutput << proc.in
stringWriterError << proc.err
proc.waitFor()
Tags:
Posted: Thu, Mar 10 03:41 AM (PST)
First get Image Magic and install it. You'll find the binary release for any platform accompanied with installation instructions on the Image Magic website.
The Image Magic tool we use is convert. It lets us apply various filters and operations to the images in just one step. Open terminal and go to the directory where you placed the images. What we are going to do is to resize all pictures to 100×100 pixels keeping proportions (no stretch or squash) and then extend the canvas size to exactly 100×100 pixels so that all thumbnails have the same size.
convert -resize 100x100 -gravity center -extent 100x100 -background white big_cover.jpg small_cover.jpg
-resize 100x100 it's self explanatory. Note that the image is not stretched to fit the given dimensions.
-gravity center centers the thumbnail in the canvas.
-extent 100x100 extends the proportionally resized image to fit exactly 100×100 pixels.
-background white create a white canvas to the thumbnail. If the image does not fit completely the 100×100 space, convert will fill the empty space with the specified color.
PS: For better quality, we recommended use PNG format
Tags:
Posted: Wed, Mar 9 10:50 AM (PST)
An example how to use findAndModify with MongoDB Java driver in Grails
def getAutoIncrementId(String key = "mycounter") {
def Id = mongoService.getCollection("mycollection").findAndModify([ "key": key ] as BasicDBObject, [ "\$inc": [ "value" : 1 ]] as BasicDBObject)
return Id?.value?.toInteger()
}
Good luck :-)
Tags: