<?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/"
	>

<channel>
	<title>Steademy Blog &#187; groovy</title>
	<atom:link href="http://blog.steademy.com/tag/groovy/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.steademy.com</link>
	<description>Steademy - steademy for every message</description>
	<lastBuildDate>Mon, 25 Jan 2010 22:19:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Login user against Steademy API</title>
		<link>http://blog.steademy.com/2009/03/20/cross-service-communication/</link>
		<comments>http://blog.steademy.com/2009/03/20/cross-service-communication/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 07:00:47 +0000</pubDate>
		<dc:creator>tmaus</dc:creator>
				<category><![CDATA[Groovy & Grails]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://blog.steademy.com/2009/03/20/cross-service-communication/</guid>
		<description><![CDATA[Steademy exposes a public API that allows any third party tool to directly connect to steademy. My first blog outlines the groovy &#38; grails way to login against the steademy platform and to receive a valid sessionKey. The steademy sessionKey uniquely identifies a user at our platform. You have to deliver it with every consequent [...]]]></description>
			<content:encoded><![CDATA[<p>Steademy exposes a public <a href="http://api.steademy.com">API</a> that allows any third party tool to directly connect to steademy.<br />
My first blog outlines the groovy &amp; grails way to login against the steademy platform and to receive a valid sessionKey.</p>
<p>The steademy sessionKey uniquely identifies a user at our platform. You have to deliver it with every consequent request. </p>
<p>We accept simple POST based requests and return JSON strings. </p>
<h2>Setup:</h2>
<p>You basically need to setup a grails project (<em>grails create-app</em> std), and create your service (<em>grails create-service steademy</em>)<br />
Furthermore the <a href="http://www.grails.org/XFire+plugin">xfire plugin</a> needs to be nstalled into your grails project.<br />
I downloaded release 0.8.1 from <a href="http://svn.codehaus.org/grails-plugins/grails-xfire/trunk/grails-xfire-0.8.1.zip">here</a></p>
<h2> Adjust config</h2>
<p>Getting rid of static properties in our class is our first goal<br />
We therefore add a couple of new lines into <i>grails-conf/Config.groovy</i><br />
Grails provides a very elegant way to read properties from the config file. </p>
<pre>
<p style="border: 1px solid black; overflow: auto; width: 100%; height: 80px">
<b>grails-app/conf/Config.groovy</b>

std{
  msapi{
    url = "https://steademy.com
    login.path="/msapi/Login"
  }
}

</pre>
<p></code></p>
<h2>SteademyService</h2>
<p>Our groovy driven service only needs a couple of code-lines for proper execution.</p>
<pre>
<p style="border: 1px solid black; overflow: auto; width: 100%; height: 180px">
<b>grails-service/SteademyService.groovy</b>

...
class SteademyService implements InitializingBean{
    boolean transactional = true
    def grailsApplication

    def msapi
    def crs

    void afterPropertiesSet(){
    	msapi 	= grailsApplication.config.std.msapi
    	crs 	= grailsApplication.config.std.crs
    }
...

String loginUser(String login, String password){
    	log.debug "remote login to steademy using [login:${login}] [password:${password}]"
    	def builder = new HTTPBuilder(msapi.url+msapi.login.path)

    	builder.request(POST, JSON){ req ->
		send URLENC,[login:login, password:password, appId:"12345678", appversion:"1.0"] 

                response.success = {resp,json ->
			if(json.error){
				log.debug "ERROR: ${json.error}"
				return null
			}

		return json.steademy.sessionKey
	       }

    		response.failure = {
		    log.debug "url ${msapi.url}/${msapi.login.path} not valid"
    		    return null
    		}
    	}
    }
}

</pre>
<ul>
<li> The service implements the <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/factory/InitializingBean.html">Initalizing Bean</a>. Thus the method <i>afterPropertiesSet</i> is called once all properties have been initialized by spring.
<li> We directly reference the properties set in the <b>grails-app/conf/Config.groovy</b> file by defining the <b>grailsApplication</b> object. Grails/Spring in the backend takes care for its proper binding. The command <i>grailsApplication.config.std.crs</i> finally reads the corresponding property from with the config file.
</ul>
<p>The actual login method is quite simple. Due to grails perfect support for JSON and thanks to the <a href="http://groovy.codehaus.org/modules/http-builder/">http-builder</a> project, some simple lines of code connect to the steademy api and process the user login. </p>
<h2>SteademyServiceTest</h2>
<p>Lets step forward to our test. If you created the service in the grails way, there should exist a groovy class called. <b>test/integration/SteademyServiceTest</b>. </p>
<pre>
<p style="border: 1px solid black; overflow: auto; width: 100%; height: 150px">
<b>SteademyServicetest</b> 

...
def steademyService
	void testSteademyLogin(){
		steademyService.loginUser("foo", "bar")
	}

</pre>
<p>Thats it basically.<br />
The next blog will outline a couple of more API calls that allow us to send messages through steademy. </p>
<p>Thorsten Maus</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.steademy.com/2009/03/20/cross-service-communication/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
