<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>random ripples - grails tag</title>
  <link>http://www.rippleinteractive.com:80/blog/tags/grails/</link>
  <description>java, groovy and other random thoughts</description>
  <language>en</language>
  <copyright>Edward Povazan</copyright>
  <lastBuildDate>Fri, 29 Aug 2008 05:24:00 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  
  <item>
    <title>Groovily Verifying a PayPal Event</title>
    <link>http://www.rippleinteractive.com:80/blog/2008/08/28/1219987440000.html</link>
    
      
        <description>
          &lt;p&gt;
I&#039;ve been dealing with PayPal subscriptions for a little while now, using it to manage quite a complex subscription scheme. Though I encrypt my PayPal buttons, the notifications from PayPal are sent to an unencrypted URL, so I need check with PayPal to make sure the notifications are in fact from them. To do this, one sends back the notification&#039;s request parameters, to which PayPal respond with a &#039;VERIFIED&#039; or &#039;INVALID&#039; string.
&lt;p&gt;
&lt;p&gt;
After digging about for some Java code to do this, I couldn&#039;t resist converting it to Groovy. I like to do this, both to understand what the code does, and also to practice some of the Groovy APIs I don&#039;t get to use every day. And of course, any excuse to call one of my favourites, &lt;code&gt;inject&lt;/code&gt;.&lt;br/&gt;
The code is quite terse:
&lt;pre class=&#034;codeSample&#034;&gt;
def verifyIPN(request) {
    def names = request.getParameterNames().toList()
    def paramString = names.inject(&#039;cmd=_notify-validate&#039;) { result, name -&gt;
        def value = request.getParameter(name)
        result &lt;&lt; &#034;&amp;${name}=${URLEncoder.encode(value)}&#034;
    }

    def url = new URL(grailsApplication.config.grails.paypal.url)
    def conn = url.openConnection()
    conn.setDoOutput(true)
    conn.setRequestProperty(&#034;Content-Type&#034;, &#034;application/x-www-form-urlencoded&#034;)

    conn.getOutputStream().withWriter { writer -&gt; writer &lt;&lt; paramString }
    return conn.getInputStream().withReader { reader -&gt; reader.readLine() }
}
&lt;/pre&gt;
In the end, all I want is clear code. &lt;br /&gt;
I sometimes think my code will start appealing to Perl hackers. What do you think?
&lt;/p&gt;
        </description>
      
      
    
    
    
    <category>Programming</category>
    
    <category>Groovy</category>
    
    <category>Snippets</category>
    
    <category>Grails</category>
    
    <comments>http://www.rippleinteractive.com:80/blog/2008/08/28/1219987440000.html#comments</comments>
    <guid isPermaLink="true">http://www.rippleinteractive.com:80/blog/2008/08/28/1219987440000.html</guid>
    <pubDate>Fri, 29 Aug 2008 05:24:00 GMT</pubDate>
  </item>
  
  <item>
    <title>Grails and Error Handling</title>
    <link>http://www.rippleinteractive.com:80/blog/2008/08/20/1219288620000.html</link>
    
      
        <description>
          &lt;p&gt;
I finished writing my first plugin today. As with most things Grails, it was surprisingly easy. The plugin injects some methods into my domain and controller classes which I can use to streamline my code.
&lt;/p&gt;
&lt;p&gt;
I am not too fond of using &lt;code&gt;if&lt;/code&gt; statements to direct flow around error handling code. For me, it clouds the core logic of a piece of code. If the normal flow of the code is assumed to execute error free, why shouldn&#039;t the code look like it does?
&lt;/p&gt;
&lt;p&gt;
The idea is very simple. I inject some new methods that will throw exceptions on failure. I&#039;ve called them &lt;code&gt;trySave&lt;/code&gt;, &lt;code&gt;tryDelete&lt;/code&gt; and &lt;code&gt;tryValidate&lt;/code&gt;. Consider this code, generated kindly by Grails:
&lt;pre class=&#034;codeSample&#034;&gt;
 def save = {
    def jsecUser = new JsecUser(params)
    if(!jsecUser.hasErrors() &amp;&amp; jsecUser.save()) {
        flash.message = &#034;JsecUser ${jsecUser.id} created&#034;
        redirect(action:show,id:jsecUser.id)
    }
    else {
        render(view:&#039;create&#039;,model:[jsecUser:jsecUser])
    }
}
&lt;/pre&gt;
After installing my plugin, I can do this:
&lt;pre class=&#034;codeSample&#034;&gt;
 def save = {
    withErrorHandling {
        try {
            def jsecUser = new JsecUser(params)
            jsecUser.trySave()
            render(view:&#039;create&#039;,model:[jsecUser:jsecUser])
        } catch (ValidationException e) {
            flash.message = &#034;JsecUser ${jsecUser.id} created&#034;
            redirect(action:show,id:jsecUser.id)
        }
    }
}
&lt;/pre&gt;
And if I define a closure named &lt;code&gt;errorHandler&lt;/code&gt; in my controller, I can add custom error handling for all actions in this controller:
&lt;pre class=&#034;codeSample&#034;&gt;
def errorHandler = { ex -&gt;
    if (ex.class == StoreException) {
        // log this and redirect to somewhere useful to the user, or perhaps return to the &#039;edit&#039; page if there is an &#039;id&#039; parameter.
    } else {
        defaultErrorHandler()
    }
}
&lt;/pre&gt;
Ideally, I would like to wrap the controller actions in a &lt;code&gt;try..catch&lt;/code&gt; block and not have the extra &lt;code&gt;withErrorHandling&lt;/code&gt; syntax, though I am not sure how to go about this yet. Any pointers anyone?
&lt;/p&gt;
&lt;p&gt;
All in all, I rather like not dealing with the &lt;code&gt;save&lt;/code&gt; method returning null on failure, and also catching those annoying constraint exceptions, as the try* methods are called with flush:true by default. Now it&#039;s time to use this a little and see how I like it in practice - I can already see I&#039;ve over-designed this simple API as I can&#039;t think of a use case for &lt;code&gt;tryValidate&lt;/code&gt; :)
&lt;/p&gt;
        </description>
      
      
    
    
    
    <category>Grails</category>
    
    <comments>http://www.rippleinteractive.com:80/blog/2008/08/20/1219288620000.html#comments</comments>
    <guid isPermaLink="true">http://www.rippleinteractive.com:80/blog/2008/08/20/1219288620000.html</guid>
    <pubDate>Thu, 21 Aug 2008 03:17:00 GMT</pubDate>
  </item>
  
  </channel>
</rss>
