Showing posts with label grails. Show all posts
Showing posts with label grails. Show all posts

Monday, 25 June 2012

oauth twitter plugin grails problem with production

My production app is deployed via cloudfoundry, so I get a url like myapp.cloudfoundry.com. However, I wanted my main domain, myapp.com to forward to that domain. Everything worked initially until the point where it forwards the user to sign in with twitter. All I got was a blank page. Turned out that I need to set the production environment variables in Datasource.groovy like so:

production {
  ...
grails.serverURL = "http://www.myapp.com/" //needed b/c of spring security and url createlinks
grails.app.context = "/"
  ...
}

get app context in grails view

I have a custom link that I need to render so the search engines can index the search terms more effectively. Specifically, I wanted to generate something like this:

http://mytestdomain.local:8080/tweetcommendation/review/business-name/1_Informotion%20Software_Vancouver,%20BC

However, I found that I couldn't do this using createLink, because it only had the concept of controller / action mapping, and my /review/business-name/${businessName} is not mapped to a controller (it's just defined in DataSource.groovy).

Originally, I had something like this:
${request.contextPath}/review/business-name/${it.id}_${it.name}_${it.location}

That way works. However, I actually found a cleaner solution that I thought I'd share:

${resource(file: "review/business-name/${it.id}_${it.name}_${it.location}")}

Sunday, 24 June 2012

Grails app on cloudfondry: Caused by: java.lang.OutOfMemoryError: PermGen space

So I was deploying my app to production (yay) on cloud foundry today. The deployment was successful, but then the system would crap out whenever I goto any of the pages:

504 Gateway Time-out nginx

I checked the logs:

vmc log

...and it appeared to be a permgen issue:

Caused by: java.lang.OutOfMemoryError: PermGen space



To solve it, type this in:

vmc env-add tweetcommendation "JAVA_OPTS=-XX:MaxPermSize=512m"

 To check that you have correctly set the value:

me@tommy:~/repo/tweetcommendation/target$ vmc env tweetcommendation

+-----------+----------------------+
| Variable  | Value                |
+-----------+----------------------+
| JAVA_OPTS | -XX:MaxPermSize=512m |
+-----------+----------------------+

Friday, 22 June 2012

Grails - Twitter Bootstrap with the resources plugin stops working

I was mucking around last night with the intention of making my resources load more efficiently, and all of a sudden my Twitter Bootstrap enabled  drop downs are no longer working. This was what I had in my declaration initially (after I was mucking around):



 modules = { 

      core { 

           dependsOn 'jquery, jquery-ui, jquery-theme, bootstrap, utils'  

           resource url: '/js/spin.js', disposition: 'head' 

           resource url: '/js/jquery.spin.js', disposition: 'head' 

           resource url: '/js/bootstrap.min.js', disposition: 'head' 

      } 

}


It turned out that was a really bad mistake, as the bootstrap module already declares a bootstrap.js. By re declaring the dependency elsewhere, you get into weirdness with some of the bootstrap plugins not working the way it should, in my case the dropdowns.




Tuesday, 19 June 2012

Shorten urls using the bit.ly api without using oauth

There is a poorly documented feature of bit.ly, and that is using their shortening url without using oauth, as long as you're fine with stats being posted to your account (which is what you want in most cases when shortening urls anyways).

In their api for shorten (located @ http://dev.bitly.com/links.html#v3_shorten), it appears that it only accepts an oauth access_token.

However, there is a small blurb on another page allow the user to use the parameters apiKey and login. Just goto http://bitly.com/a/your_api_key and you will see your login and apiKey information. Use these two parameters instead of the access_token, and then you'll be in business.

So an example would look like this:


/v3/shorten?apiKey=blah&login=blah&longUrl=http%3A%2F%2Fgoogle.com%2F

Hope that saves someone the ten minutes I took to figure it out!


Saturday, 16 June 2012

Fixing grails problem: java.lang.IllegalStateException: Method on class [] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.


So I was getting this exception when I am writing a unit test for a controller for an application that uses the grails mongo gorm plugin:

 java.lang.IllegalStateException: Method on class [com.tweetcommendation.Tweetcommendation] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.  

I'm also writing the unit using the annotation style (ala 2.0+ style). It turns out that this problem comes up when you don't define the @Mock annotation with the domain class (which happens to be mapped with mongo).
So add in the domain class with the @Mock annotation:


 @Mock([DomainClassMappedWithMongo])  


Grails exception when rendering a view: java.util.EmptyStackException

So I was getting this exception after a change in my view page:

 Stacktrace follows: 

 java.lang.RuntimeException: Error initializing GroovyPageView 

      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 

      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 

      at java.lang.Thread.run(Thread.java:662) 

 Caused by: java.util.EmptyStackException 

      at java.util.Stack.peek(Stack.java:85) 

      ... 3 more  

This is what I added:

                               <div id="positive-review"> 

                                    <img 

                                         src="${resource(dir: 'images', file: 'Thumbs_up_icon_sm.jpg')}" 

                                         width="38px" height="38px"></img> <span>${reviewCountsMap['1']}</span> 

                               </div> 

                               <div id="neutral-review"> 

                                    <img src="${resource(dir: 'images', file: 'Icon_neutral.png')}" 

                                         width="38px" height="38px"></img> <span>${reviewCountsMap['0']]}</span> 

                               </div> 

                               <div id="negative-review"> 

                                    <img 

                                         src="${resource(dir: 'images', file: 'thumbs-down-icon.jpg')}" 

                                         width="38px" height="38px"></img> <span>${reviewCountsMap['-1']}</span> 

                               </div>  

Notice the error? It's an extra square bracket after reviewCountsMap['0]! Remove it and you'll be fine.

Thursday, 14 June 2012

No signature of method: org.codehaus.groovy.grails.web.servlet.GrailsFlashScope.success() is applicable for argument types: (java.lang.String) values: [Comment created!]

So I'm trying to add informational flash messages to display to my users. In my controller I set something up like this:



                if (createAndSaveComment(tweet, comment, currentUser) != null) { 

                     rabbitSend('comment.created', '', tweet.id) 

                     flash.success('Comment created!') 

                }  


Seemed correct, right? WRONG! This is what the error I got:



 No signature of method: org.codehaus.groovy.grails.web.servlet.GrailsFlashScope.success() is applicable for argument types: (java.lang.String) values: [Comment created!]  


Turns out I'm doing it wrong. It should look like this:



                if (createAndSaveComment(tweet, comment, currentUser) != null) { 

                     rabbitSend('comment.created', '', tweet.id) 

                     flash.success = 'Comment created!'

                }  


You might be snickering at this point because I made such a stupid mistake...well it's late in the day and I've been coding since 8am okay? Give me a break!