<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"
>

<channel>
	<title>acidum.de &#187; Grails</title>
	<atom:link href="http://www.acidum.de/tag/grails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.acidum.de</link>
	<description></description>
	<lastBuildDate>Sun, 08 Nov 2009 20:12:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Deploy a Grails Application into Glassfish</title>
		<link>http://www.acidum.de/2009/04/16/deploy-a-grails-application-into-glassfish/</link>
		<comments>http://www.acidum.de/2009/04/16/deploy-a-grails-application-into-glassfish/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 15:34:04 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Code Examples]]></category>
		<category><![CDATA[Glassfish]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=220</guid>
		<description><![CDATA[In most cases a Grails application uses a database to store its content. Fortunately Grails is tightly coupled with GORM to ease the use. Since Grails distinguish between different deployment environments, it uses a special configuration for development and production. Therefore you have to think of a database in production environment while you don not [...]]]></description>
			<content:encoded><![CDATA[<p>In most cases a <a title="Grails Site" href="http://www.grails.org/" target="_blank">Grails</a> application uses a database to store its content. Fortunately Grails is tightly coupled with GORM to ease the use. Since Grails distinguish between different deployment environments, it uses a special configuration for development and production. Therefore you have to think of a database in production environment while you don not in development stage.</p>
<p><a title="Glassfish Site" href="http://www.glassfish.org/" target="_blank">Glassfish</a> v2 and v3 Prelude comes with an internal database JavaDB (based on the Apache Derby) that could be used for first tests. It would be great to use the default database by default for deployments for Glassfish via JNDI. This article shows how to use the bundled database with an Grails application.</p>
<p>In preparation for the next steps you have to install <a title="Grails Installation" href="http://www.grails.org/Installation" target="_blank">Grails 1.1</a> and <a title="Glassfish v2 Installation Instructions" href="https://glassfish.dev.java.net/downloads/v2.1-b60e.html">Glassfish v2</a> or <a title="Glassfish v3 Prelude Installation Instructions" href="http://docs.sun.com/app/docs/doc/820-5968/ggssq?a=view">Glassfish v3 Prelude.</a></p>
<p>To create the demo app just run the following at command line:<br />
<code><br />
grails create-app glassfishapp<br />
cd glassfishapp<br />
grails create-domain-class Bookmark<br />
grails create-controller Bookmark<br />
</code></p>
<p>As the next step edit the domain class and the controller. E.g. open Eclipse with installed <a title="Eclipse Groovy Plugin" href="http://groovy.codehaus.org/Eclipse+Plugin" target="_blank">Groovy plugin.</a></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> BookmarkController <span style="color: #009900;">&#123;</span>
    def scaffold <span style="color: #339933;">=</span> Bookmark
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Bookmark <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">String</span> url
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now you are ready to test your app with</p>
<p><code><br />
grails run-app<br />
</code></p>
<p>To prepare the Grails application for Glassfish the data source for the production environment has to be changed. Open the <em>DataSource.groovy</em> and change</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">production <span style="color: #009900;">&#123;</span>
	dataSource <span style="color: #009900;">&#123;</span>
		dbCreate <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;update&quot;</span>
		url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc:hsqldb:file:prodDb;shutdown=true&quot;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>to</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">production <span style="color: #009900;">&#123;</span>
	dataSource <span style="color: #009900;">&#123;</span>
		dbCreate <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;update&quot;</span>
		<span style="color: #666666; font-style: italic;">// do not use java:/jdbc as documented</span>
		jndiName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;jdbc/__default&quot;</span>
		username <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;APP&quot;</span>
		password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;APP&quot;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Do not use <code>jndiName = "java:jdbc/__default"</code> as <a href="http://www.grails.org/doc/1.1/guide/3.%20Configuration.html">documented</a>. Be aware that it may be dangerous to use <code>dbCreate= "update"</code> in production environments.</p>
<p>Now compile the web archive with </p>
<p><code>grails war</code></p>
<p>Start your database and the application server<br />
<code>./asadmin start-database<br />
./asadmin start-domain</code></p>
<p>Finally open the sun web interface at http://localhost:4848/ to deploy the war file</p>
<p><code>http://localhost:8080/glassfishapp-0.1/</code></p>
<p>I experienced some memory problems by using Grails with Glassfish. To fix that problem I added <code>-XX:MaxPermSize=256m</code> and <code>-Xmx1024m</code> as JVM option. JVM options can be changed via the web interface (Common Tasks -> Application Server -> JVM Settings -> JVM Options)</p>
<div id="attachment_225" class="wp-caption aligncenter" style="width: 160px"><a href="http://www.acidum.de/wp-content/uploads/2009/04/grails_glassfish.png" rel="lightbox"><img class="size-thumbnail wp-image-225" title="Grails running in Glassfish" src="http://www.acidum.de/wp-content/uploads/2009/04/grails_glassfish-150x150.png" alt="Grails running in Glassfish" width="150" height="150" /></a><p class="wp-caption-text">Grails running in Glassfish</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2009/04/16/deploy-a-grails-application-into-glassfish/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>J2ME, REST and Grails</title>
		<link>http://www.acidum.de/2008/11/15/j2me-rest-and-grails/</link>
		<comments>http://www.acidum.de/2008/11/15/j2me-rest-and-grails/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 12:56:00 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[J2ME]]></category>
		<category><![CDATA[Web Service]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=169</guid>
		<description><![CDATA[I set up an mobile application that sends data via a REST based interface to a Grails-based server. Unfortunately J2ME does not support all HTTP methods. To overcome this weakness we have to emulate the methods PUT and DELETE. Like Rails just did this by adding a &#8220;_method=PUT&#8221; parameter to the query URL, we will [...]]]></description>
			<content:encoded><![CDATA[<p>I set up an mobile application that sends data via a REST based interface to a Grails-based server. Unfortunately J2ME <a title="J2ME and &quot;missing&quot; PUT/DELETE HTTP verbs" href="http://www.burningdoor.com/steve/archives/000751.html" target="_blank">does not support all HTTP methods</a>. To overcome this weakness we have to emulate the methods PUT and DELETE. Like Rails just did this by adding a &#8220;_method=PUT&#8221; parameter to the query URL, we will do the same. Rails does the HTTP method emulation handling automatically, whilst in Grails we have do this by ourselves. By using the UrlMapping.groovy it will become quite easy to overcome this weakness. A standard REST mapping should look like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UrlMappings <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">static</span> mappings <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">// identifies a rest object like /rest/airport/1</span>
	  <span style="color: #0000ff;">&quot;/rest/$domain/$id&quot;</span><span style="color: #009900;">&#123;</span>
		  controller <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;rest&quot;</span>
		  action <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>GET<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;show&quot;</span>, PUT<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;create&quot;</span>, POST<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;update&quot;</span>,DELETE<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;delete&quot;</span><span style="color: #009900;">&#93;</span>
         <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The action attribute may be a simple string, a hash map or a closure. Grails calls the following method evaluateNameForValue(Object value, GrailsWebRequest webRequest) to retrieve the value. The method is located within the org.codehaus.groovy.grails.web.mapping.DefaultUrlMappingInfo.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> evaluateNameForValue<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value, GrailsWebRequest webRequest<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #000000; font-weight: bold;">instanceof</span> Closure<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Closure callable <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Closure<span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
            <span style="color: #003399;">Object</span> result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Closure<span style="color: #009900;">&#41;</span> callable.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">call</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            name <span style="color: #339933;">=</span> result <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> result.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Map</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Map</span> httpMethods <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
            name <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> httpMethods.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>webRequest.<span style="color: #006633;">getCurrentRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            name <span style="color: #339933;">=</span> value.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Due to our knowledge of the action retrieval process, we are able to implement a &#8220;_method&#8221; emulation. This could look like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UrlMappings <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">static</span> mappings <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">// identifies a rest object like /rest/airport/1</span>
	  <span style="color: #0000ff;">&quot;/rest/$domain/$id&quot;</span><span style="color: #009900;">&#123;</span>
		  controller <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;rest&quot;</span>
		  <span style="color: #666666; font-style: italic;">// action = [GET:&quot;show&quot;, PUT:&quot;create&quot;, POST:&quot;update&quot;,DELETE:&quot;delete&quot;]</span>
&nbsp;
	         action <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
		   <span style="color: #666666; font-style: italic;">// closure will not be invoked with request, log ... therefore we have to do this manually</span>
		  GrailsWebRequest webRequest <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>GrailsWebRequest<span style="color: #009900;">&#41;</span> RequestContextHolder.<span style="color: #006633;">getRequestAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  <span style="color: #003399;">String</span> method <span style="color: #339933;">=</span> webRequest.<span style="color: #006633;">getCurrentRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		  <span style="color: #003399;">String</span> methodParam <span style="color: #339933;">=</span> webRequest.<span style="color: #006633;">getCurrentRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_method&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		  <span style="color: #666666; font-style: italic;">// check if we can use _method</span>
		  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>methodParam <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		    methodParam <span style="color: #339933;">=</span> methodParam.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
		    <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>methodParam<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;PUT&quot;</span> <span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;DELETE&quot;</span> <span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;POST&quot;</span> <span style="color: #339933;">:</span>
			<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;GET&quot;</span> <span style="color: #339933;">:</span>
			  method <span style="color: #339933;">=</span> methodParam<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span>
		  <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> 
&nbsp;
		<span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>method.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;PUT&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;create&quot;</span>
			<span style="color: #000000; font-weight: bold;">break</span>
		  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;DELETE&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;delete&quot;</span>
			<span style="color: #000000; font-weight: bold;">break</span>
		  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;POST&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;update&quot;</span>
			<span style="color: #000000; font-weight: bold;">break</span>
		  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;GET&quot;</span> <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;show&quot;</span>
			<span style="color: #000000; font-weight: bold;">break</span>
		  <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;show&quot;</span>
		        <span style="color: #000000; font-weight: bold;">break</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> method<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I hope that helps all Grails REST developers to enable their services for J2ME. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2008/11/15/j2me-rest-and-grails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Grails Spring Security</title>
		<link>http://www.acidum.de/2008/10/08/grails-spring-security/</link>
		<comments>http://www.acidum.de/2008/10/08/grails-spring-security/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 13:13:35 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Acegi Security]]></category>
		<category><![CDATA[Spring Security]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=160</guid>
		<description><![CDATA[For my current grails project I use the Spring Security Plugin (formerly Acegi Security) to secure my views and services. A tutorial at the Grails site gives a great overview how to install the grails plugin.
Right after installation I faced some issues:
Secure your Rest Services
This sounds quite easy due to a simple switch within the [...]]]></description>
			<content:encoded><![CDATA[<p>For my current grails project I use the <a title="Grails Acegi Plugin" href="http://grails.org/AcegiSecurity+Plugin" target="_blank">Spring Security Plugin (formerly Acegi Security)</a> to secure my views and services. A <a title="Acegi Installation Tutorial" href="http://www.grails.org/AcegiSecurity+Plugin+-+Basic+Tutorial" target="_blank">tutorial</a> at the Grails site gives a great overview how to install the grails plugin.</p>
<p>Right after installation I faced some issues:</p>
<p><strong>Secure your Rest Services</strong></p>
<p>This sounds quite easy due to a simple switch within the SecurityConfig.groovy. Just activate the BasicProcessingFilter. Now grails uses the Http Basic Authentification.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/** use basicProcessingFilter */</span>
basicProcessingFilter <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span></pre></div></div>

<p>At the client side the code would like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">sun.misc.BASE64Encoder</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">groovy.util.XmlSlurper</span>
&nbsp;
def userid <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;john.doe&quot;</span>
def password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;pass&quot;</span>
&nbsp;
def url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://localhost:8080/CandyStreamServer/rest/gps/&quot;</span>
&nbsp;
def conn <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userid <span style="color: #339933;">&amp;&amp;</span> password<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	println <span style="color: #0000ff;">&quot;set authorization&quot;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// add HTTP authentication</span>
       <span style="color: #003399;">String</span> encodedAuth <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BASE64Encoder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>userid <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;:&quot;</span> <span style="color: #339933;">+</span> password<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
&nbsp;
	conn.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Authorization&quot;</span>, <span style="color: #0000ff;">&quot;Basic &quot;</span> <span style="color: #339933;">+</span> encodedAuth<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
def slurper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XmlSlurper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
conn.<span style="color: #006633;">requestMethod</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GET&quot;</span>
conn.<span style="color: #006633;">doOutput</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span>
&nbsp;
println <span style="color: #0000ff;">&quot;check connection&quot;</span>
def response
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>conn.<span style="color: #006633;">responseCode</span> <span style="color: #339933;">==</span> conn.<span style="color: #006633;">HTTP_OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	conn.<span style="color: #006633;">inputStream</span>.<span style="color: #006633;">withStream</span> <span style="color: #009900;">&#123;</span>
		response <span style="color: #339933;">=</span> slurper.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>it<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	response <span style="color: #339933;">=</span> conn.<span style="color: #006633;">responseCod</span>
<span style="color: #009900;">&#125;</span>
println response
conn.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>Retrieve the Current User</strong></p>
<p>On the server side it may necessary to retrieve the current user. Spring Security offers a SecurityContextHolder. Unfortunately the  securityContext.getAuthentication().getPrincipal(); does not return the Groovy user object. Instead the Spring Security Plugin uses its own user implementation that holds the Groovy user object. This is required due to Springs dependency on a specific interface.</p>
<p>The plugin provides a org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser interface that extends the org.springframework.security.userdetails.UserDetails interface. To retrieve your Groovy user object just call getDomainClass()</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">SecurityContext securityContext <span style="color: #339933;">=</span> SecurityContextHolder.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
def springUser <span style="color: #339933;">=</span> securityContext.<span style="color: #006633;">getAuthentication</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPrincipal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">return</span> springUser.<span style="color: #006633;">getDomainClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2008/10/08/grails-spring-security/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>10 Steps to Integrate OpenID into Grails</title>
		<link>http://www.acidum.de/2008/08/11/10-steps-to-integrate-openid-into-grails/</link>
		<comments>http://www.acidum.de/2008/08/11/10-steps-to-integrate-openid-into-grails/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 12:32:24 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[OpenID]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=123</guid>
		<description><![CDATA[Check out how easy the integration of OpenID into Grails is:
1. Create a new grails application: grails create-app openID
2. Change into directory: cd openID
3. Install Plugin: grails install-plugin openid
4. Create a new controller: grails create-controller User
5. Create logon.gsp at grails-app/views/user
6. Add the following lines of code
 &#60;openid:css /&#62;
&#60;html&#62;
&#60;head&#62;
&#60;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&#62;
&#60;meta name="layout" content="main" /&#62;
&#60;title&#62;Login&#60;/title&#62;
&#60;openid:css /&#62;
&#60;/head&#62;
&#60;body&#62;
&#60;h1&#62;Login&#60;/h1&#62;
&#60;openid:hasLoginError&#62;
&#60;div [...]]]></description>
			<content:encoded><![CDATA[<p>Check out how easy the integration of <a title="OpenID" href="http://openid.net/" target="_blank">OpenID</a> into Grails is:</p>
<p>1. Create a new grails application: grails create-app openID</p>
<p>2. Change into directory: cd openID</p>
<p>3. Install Plugin: grails install-plugin openid</p>
<p>4. Create a new controller: grails create-controller User</p>
<p>5. Create logon.gsp at grails-app/views/user</p>
<p>6. Add the following lines of code</p>
<p><code> &lt;openid:css /&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;<br />
&lt;meta name="layout" content="main" /&gt;<br />
&lt;title&gt;Login&lt;/title&gt;<br />
&lt;openid:css /&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;Login&lt;/h1&gt;<br />
&lt;openid:hasLoginError&gt;<br />
&lt;div class="errors"&gt;<br />
&lt;ul&gt;<br />
&lt;li&gt;&lt;openid:renderLoginError /&gt;&lt;/li&gt;<br />
&lt;/ul&gt;<br />
&lt;/div&gt;<br />
&lt;/openid:hasLoginError&gt;<br />
&lt;openid:form success="[action:'loggedin']"&gt;<br />
&lt;openid:input size="30" value="http://" /&gt; (e.g. http://username.myopenid.com)<br />
&lt;br/&gt;<br />
&lt;g:submitButton name="login" value="Login" /&gt;<br />
&lt;/openid:form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</code></p>
<p>7. Edit UserController and add</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UserController <span style="color: #009900;">&#123;</span>
def login <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
def loggedin <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> redirect<span style="color: #009900;">&#40;</span>uri<span style="color: #339933;">:</span><span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>8. Call grails run-app</p>
<p>9. Open your web browser at http://localhost:8080/openID/User/login.gsp</p>
<p>10. Use your OpenID account to log on like <a title="Yahoo OpenID" href="http://openid.yahoo.com/" target="_blank">Yahoo OpenID</a>.</p>
<p>Check out <a title="Grails OpenID Integration" href="http://docs.codehaus.org/display/GRAILS/OpenID+Plugin" target="_blank">http://docs.codehaus.org/display/GRAILS/OpenID+Plugin</a> for further information about the Grails openID plugin</p>
]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2008/08/11/10-steps-to-integrate-openid-into-grails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.427 seconds -->
