<?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; J2ME</title>
	<atom:link href="http://www.acidum.de/tag/j2me/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>J2ME REST Client</title>
		<link>http://www.acidum.de/2008/12/29/j2me-rest-client/</link>
		<comments>http://www.acidum.de/2008/12/29/j2me-rest-client/#comments</comments>
		<pubDate>Mon, 29 Dec 2008 16:41:00 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[J2ME]]></category>
		<category><![CDATA[Web Service]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=203</guid>
		<description><![CDATA[Unfortunately J2ME does not provide the best basis for writing a REST Web Service. As I already mentioned in previous posts the J2ME reference implementation does not support all HTTP methods. To overcome this restriction I wrote a J2ME Rest Client that uses HTTP method emulation like Rails does. Even though I heard that some [...]]]></description>
			<content:encoded><![CDATA[<p>Unfortunately J2ME does not provide the best basis for writing a REST Web Service. As I already mentioned in previous posts the J2ME reference implementation does not support all HTTP methods. To overcome this restriction I wrote a J2ME Rest Client that uses HTTP method emulation like Rails does. Even though I heard that some J2ME platforms support PUT and DELETE I decided to use the standard. Method emulation may become a challenge if you&#8217;re not talking to your own services since it require a suitable server implementation like I posted for Grails earlier.</p>
<p>The following code illustrates how the Rest Client works. It uses the <a href="http://java.sun.com/docs/books/j2mewireless/examples/src/examples/netclient/BasicAuth.java">BasicAuth</a> class from Sun. I removed the imports, because it should be obvious. In future versions of the class I plan to use one method to read the data from stream an check the status to reduce the file size. But for my first prototype it works that way.
</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;">/**
 * This class provided a REST implementation for J2ME.
 * @author Christoph Hartmann
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RestClient <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> userid<span style="color: #339933;">;</span>
	<span style="color: #003399;">String</span> password<span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// User Agent</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> ua<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> RestClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> RestClient<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userid, <span style="color: #003399;">String</span> password<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">userid</span> <span style="color: #339933;">=</span> userid<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">password</span> <span style="color: #339933;">=</span> password<span style="color: #339933;">;</span>
		ua <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Profile/&quot;</span> <span style="color: #339933;">+</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;microedition.profiles&quot;</span><span style="color: #009900;">&#41;</span>
				<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; Configuration/&quot;</span>
				<span style="color: #339933;">+</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;microedition.configuration&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * prepare the HTTP connection
	 * @param conn
	 * @throws IOException
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> configureConncetion<span style="color: #009900;">&#40;</span>HttpConnection conn<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		conn.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;User-Agent&quot;</span>, ua<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">String</span> locale <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;microedition.locale&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>locale <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> locale <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;en-US&quot;</span><span style="color: #339933;">;</span>	<span style="color: #009900;">&#125;</span>
		conn.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Accept-Language&quot;</span>, locale<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		conn.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span>, <span style="color: #0000ff;">&quot;text/plain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		conn.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Accept&quot;</span>, <span style="color: #0000ff;">&quot;text/plain&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// set HTTP basic authentification</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userid <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;</span>#038<span style="color: #339933;">;&amp;</span>#038<span style="color: #339933;">;</span> password <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>
			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> BasicAuth.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>userid, password<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: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> HttpConnection getConnection<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		HttpConnection conn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpConnection<span style="color: #009900;">&#41;</span> Connector.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		configureConncetion<span style="color: #009900;">&#40;</span>conn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> conn<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> HttpConnection getConnection<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #000066; font-weight: bold;">int</span> access<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		HttpConnection conn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpConnection<span style="color: #009900;">&#41;</span> Connector.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span>url, access<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		configureConncetion<span style="color: #009900;">&#40;</span>conn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> conn<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * READ
	 * @param url
	 * @return
	 * @throws IOException
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> get<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		HttpConnection hcon <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">DataInputStream</span> dis <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">StringBuffer</span> responseMessage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">int</span> redirectTimes <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">boolean</span> redirect<span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span>
				redirect <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// a standard HttpConnection with READ access</span>
				hcon <span style="color: #339933;">=</span> getConnection<span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// obtain a DataInputStream from the HttpConnection</span>
				dis <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">DataInputStream</span><span style="color: #009900;">&#40;</span>hcon.<span style="color: #006633;">openInputStream</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: #666666; font-style: italic;">// retrieve the response from the server</span>
				<span style="color: #000066; font-weight: bold;">int</span> ch<span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ch <span style="color: #339933;">=</span> dis.<span style="color: #006633;">read</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: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					responseMessage.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> ch<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// end while ( ( ch = dis.read() ) != -1 )</span>
				<span style="color: #666666; font-style: italic;">// check status code</span>
				<span style="color: #000066; font-weight: bold;">int</span> status <span style="color: #339933;">=</span> hcon.<span style="color: #006633;">getResponseCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>status<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">case</span> HttpConnection.<span style="color: #006633;">HTTP_OK</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// Success!</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">case</span> HttpConnection.<span style="color: #006633;">HTTP_TEMP_REDIRECT</span><span style="color: #339933;">:</span>
				<span style="color: #000000; font-weight: bold;">case</span> HttpConnection.<span style="color: #006633;">HTTP_MOVED_TEMP</span><span style="color: #339933;">:</span>
				<span style="color: #000000; font-weight: bold;">case</span> HttpConnection.<span style="color: #006633;">HTTP_MOVED_PERM</span><span style="color: #339933;">:</span>
					<span style="color: #666666; font-style: italic;">// Redirect: get the new location</span>
					url <span style="color: #339933;">=</span> hcon.<span style="color: #006633;">getHeaderField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Redirect: &quot;</span> <span style="color: #339933;">+</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dis <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> dis.<span style="color: #006633;">close</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;">if</span> <span style="color: #009900;">&#40;</span>hcon <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> hcon.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					hcon <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
					redirectTimes<span style="color: #339933;">++;</span>
					redirect <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
					<span style="color: #666666; font-style: italic;">// Error: throw exception</span>
					hcon.<span style="color: #006633;">close</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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Response status not OK:&quot;</span> <span style="color: #339933;">+</span> status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// max 5 redirects</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>redirect <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #339933;">&amp;</span>#038<span style="color: #339933;">;&amp;</span>#038<span style="color: #339933;">;</span> redirectTimes <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>redirectTimes <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Too much redirects&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// TODO bad style</span>
			responseMessage.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ERROR: &quot;</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;">finally</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hcon <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
					hcon.<span style="color: #006633;">close</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;">if</span> <span style="color: #009900;">&#40;</span>dis <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
					dis.<span style="color: #006633;">close</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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> ioe<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				ioe.<span style="color: #006633;">printStackTrace</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: #666666; font-style: italic;">// end try/catch</span>
		<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// end try/catch/finally</span>
		<span style="color: #000000; font-weight: bold;">return</span> responseMessage.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// end sendGetRequest( String )</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * UPDATE
	 *
	 * @param url
	 * @param data
	 *            the request body
	 * @throws IOException
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> post<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #003399;">String</span> data<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		HttpConnection hcon <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">DataInputStream</span> dis <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">DataOutputStream</span> dos <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">StringBuffer</span> responseMessage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">int</span> redirectTimes <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">boolean</span> redirect<span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span>
				redirect <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// an HttpConnection with both read and write access</span>
				hcon <span style="color: #339933;">=</span> getConnection<span style="color: #009900;">&#40;</span>url, Connector.<span style="color: #006633;">READ_WRITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// set the request method to POST</span>
				hcon.<span style="color: #006633;">setRequestMethod</span><span style="color: #009900;">&#40;</span>HttpConnection.<span style="color: #006633;">POST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// overwrite content type to be form based</span>
				hcon.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span>, <span style="color: #0000ff;">&quot;application/x-www-form-urlencoded&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// set message length</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data <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>
					hcon.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Length&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span>
							<span style="color: #339933;">+</span> data.<span style="color: #006633;">length</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>
&nbsp;
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data <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: #666666; font-style: italic;">// obtain DataOutputStream for sending the request string</span>
					dos <span style="color: #339933;">=</span> hcon.<span style="color: #006633;">openDataOutputStream</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;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> request_body <span style="color: #339933;">=</span> data.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// send request string to server</span>
					<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> request_body.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						dos.<span style="color: #006633;">writeByte</span><span style="color: #009900;">&#40;</span>request_body<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// end for( int i = 0; i &lt; request_body.length; i++ )</span>
					dos.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Including this line may produce</span>
					<span style="color: #666666; font-style: italic;">// undesiredresults on certain devices</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// obtain DataInputStream for receiving server response</span>
				dis <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">DataInputStream</span><span style="color: #009900;">&#40;</span>hcon.<span style="color: #006633;">openInputStream</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: #666666; font-style: italic;">// retrieve the response from server</span>
				<span style="color: #000066; font-weight: bold;">int</span> ch<span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>ch <span style="color: #339933;">=</span> dis.<span style="color: #006633;">read</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: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					responseMessage.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> ch<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// end while( ( ch = dis.read() ) != -1 ) {</span>
				<span style="color: #666666; font-style: italic;">// check status code</span>
				<span style="color: #000066; font-weight: bold;">int</span> status <span style="color: #339933;">=</span> hcon.<span style="color: #006633;">getResponseCode</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;">switch</span> <span style="color: #009900;">&#40;</span>status<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">case</span> HttpConnection.<span style="color: #006633;">HTTP_OK</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// Success!</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">case</span> HttpConnection.<span style="color: #006633;">HTTP_TEMP_REDIRECT</span><span style="color: #339933;">:</span>
				<span style="color: #000000; font-weight: bold;">case</span> HttpConnection.<span style="color: #006633;">HTTP_MOVED_TEMP</span><span style="color: #339933;">:</span>
				<span style="color: #000000; font-weight: bold;">case</span> HttpConnection.<span style="color: #006633;">HTTP_MOVED_PERM</span><span style="color: #339933;">:</span>
					<span style="color: #666666; font-style: italic;">// Redirect: get the new location</span>
					url <span style="color: #339933;">=</span> hcon.<span style="color: #006633;">getHeaderField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;location&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Redirect: &quot;</span> <span style="color: #339933;">+</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dis <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> dis.<span style="color: #006633;">close</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;">if</span> <span style="color: #009900;">&#40;</span>hcon <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> hcon.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					hcon <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
					redirectTimes<span style="color: #339933;">++;</span>
					redirect <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
					<span style="color: #666666; font-style: italic;">// Error: throw exception</span>
					hcon.<span style="color: #006633;">close</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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Response status not OK:&quot;</span> <span style="color: #339933;">+</span> status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// max 5 redirects</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>redirect <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #339933;">&amp;</span>#038<span style="color: #339933;">;&amp;</span>#038<span style="color: #339933;">;</span> redirectTimes <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>redirectTimes <span style="color: #339933;">==</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IOException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Too much redirects&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			responseMessage.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ERROR&quot;</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;">finally</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// free up i/o streams and http connection</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hcon <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> hcon.<span style="color: #006633;">close</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;">if</span> <span style="color: #009900;">&#40;</span>dis <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> dis.<span style="color: #006633;">close</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;">if</span> <span style="color: #009900;">&#40;</span>dos <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> dos.<span style="color: #006633;">close</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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> ioe<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				ioe.<span style="color: #006633;">printStackTrace</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: #666666; font-style: italic;">// end try/catch</span>
		<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">// end try/catch/finally</span>
		<span style="color: #000000; font-weight: bold;">return</span> responseMessage.<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>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * DELETE
	 * not possible on J2ME therefore we use Rails emulation on PUT and
	 * DELETE like
	 * http://localhost:8080/CandyStreamServer/airport/1?_method=delete
	 * @param url
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> delete<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		url <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;?_method=DELETE&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> post<span style="color: #009900;">&#40;</span>url, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * CREATE not possible on J2ME therefore we use Rails emulation on PUT and
	 * DELETE
	 * @param url
	 * @param data
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> put<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #003399;">String</span> data<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		data <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;&amp;#038;_method=PUT&quot;</span><span style="color: #339933;">;</span>
		url <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;?&quot;</span> <span style="color: #339933;">+</span> data<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> post<span style="color: #009900;">&#40;</span>url, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2008/12/29/j2me-rest-client/feed/</wfw:commentRss>
		<slash:comments>2</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>
	</channel>
</rss>

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