Accessing the raw body of a PUT or POST request

It is possible by overriding the HttpServletRequest in a Servlet Filter. You need to implement a HttpServletRequestWrapper that stores the request body: src/java/grails/util/http/MultiReadHttpServletRequest.java package grails.util.http; import org.apache.commons.io.IOUtils; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletRequest; import javax.servlet.ServletInputStream; import java.io.*; import java.util.concurrent.atomic.AtomicBoolean; public class MultiReadHttpServletRequest extends HttpServletRequestWrapper { private byte[] body; public MultiReadHttpServletRequest(HttpServletRequest httpServletRequest) { super(httpServletRequest); // Read the request … Read more

How to pass parameters to a modal?

To pass the parameter you need to use resolve and inject the items in controller $scope.Edit = function (Id) { var modalInstance = $modal.open({ templateUrl: ‘/app/views/admin/addeditphone.html’, controller: ‘EditCtrl’, resolve: { editId: function () { return Id; } } }); } Now if you will use like this: app.controller(‘EditCtrl’, [‘$scope’, ‘$location’ , function ($scope, $location, editId) … Read more

Found shared references to a collection org.hibernate.HibernateException

Hibernate shows this error when you attempt to persist more than one entity instance sharing the same collection reference (i.e. the collection identity in contrast with collection equality). Note that it means the same collection, not collection element – in other words relatedPersons on both person and anotherPerson must be the same. Perhaps you’re resetting … Read more

Externalizing Grails Datasource configuration

You can use a properties file specified in the grails.config.locations as a way to externalize the datasource configuration. Below is how I typically set up a Grails project: In my DataSource.groovy I specify this for the production environment: …. …. production { dataSource { dbCreate = “update” driverClassName = “com.myorg.jdbcDriverNotExists” url = “” username = … Read more

What in the world are Spring beans?

The Spring core technologies reference documentation describes what beans are. Per the Introduction to the Spring IoC Container and Beans section (where “IoC” means “inversion of control“): In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object … Read more

Binding a Grails date from params in a controller

Grails Version >= 2.3 A setting in Config.groovy defines the date formats which will be used application-wide when binding params to a Date grails.databinding.dateFormats = [ ‘MMddyyyy’, ‘yyyy-MM-dd HH:mm:ss.S’, “yyyy-MM-dd’T’hh:mm:ss’Z'” ] The formats specified in grails.databinding.dateFormats will be attempted in the order in which they are included in the List. You can override these application-wide … Read more

Convert base64 string to image

In the server, do something like this: Suppose String data=”data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAYAAAA+VemSAAAgAEl…==” Then: String base64Image = data.split(“,”)[1]; byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image); Then you can do whatever you like with the bytes like: BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));

How to log SQL statements in Grails

Setting datasource { … logSql = true } in DataSource.groovy (as per these instructions) was enough to get it working in my environment. It seems that parts of the FAQ are out of date (e.g. the many-to-many columns backwards question) so this might also be something that changed in the meantime.

Grails unable to install plugin

The repository changed and it’s causing problem for older Grails versions. See my answer at: Grails Url shortener plugin not getting installed : Please use mavenRepo “https://repo.grails.org/grails/plugins” As a repository definition. http://grails.1312388.n4.nabble.com/Grails-central-repo-seemingly-missing-plugin-versions-td4658720.html

How to calculate elapsed time from now with Joda-Time?

To calculate the elapsed time with JodaTime, use Period. To format the elapsed time in the desired human representation, use PeriodFormatter which you can build by PeriodFormatterBuilder. Here’s a kickoff example: DateTime myBirthDate = new DateTime(1978, 3, 26, 12, 35, 0, 0); DateTime now = new DateTime(); Period period = new Period(myBirthDate, now); PeriodFormatter formatter … Read more