<?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>Spice World! &#187; Java</title>
	<atom:link href="http://www.cyberspice.org.uk/blog/category/geek/programming/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cyberspice.org.uk/blog</link>
	<description>The life and times of a jet setting software engineer!</description>
	<lastBuildDate>Fri, 02 Dec 2011 14:52:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Upgrading your UK T-Mobile Google G1 (HTC Dream) to Android 2.x</title>
		<link>http://www.cyberspice.org.uk/blog/2010/10/22/upgrading-your-uk-t-mobile-google-g1-htc-dream-to-android-2-x/</link>
		<comments>http://www.cyberspice.org.uk/blog/2010/10/22/upgrading-your-uk-t-mobile-google-g1-htc-dream-to-android-2-x/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 15:33:22 +0000</pubDate>
		<dc:creator>cyberspice</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Geek]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Google G1]]></category>
		<category><![CDATA[HTC Dream]]></category>
		<category><![CDATA[T-Mobile]]></category>

		<guid isPermaLink="false">http://www.cyberspice.org.uk/blog/?p=480</guid>
		<description><![CDATA[I&#8217;m currently developing a new project. Its a bluetooth enabled bracelet. I wanted to write an iPhone app to control it. However, much as I like my iPhone 4, Apple&#8217;s policy about locking the thing down sucks. So I then looked at writing an Android application for my G1. The problem with that is that [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently developing a new project.  Its a bluetooth enabled bracelet.  I wanted to write an iPhone app to control it.  However, much as I like my iPhone 4, Apple&#8217;s policy about locking the thing down sucks.  So I then looked at writing an Android application for my G1.  The problem with that is that bluetooth support only arrived in Android 2.0 and T-Mobile/HTC ceased upgrades at 1.6 on the G1.  Since my G1 is two years old and out of contract I decided to be brave and upgrade it to Android 2.2.  Note the G1 is called the HTC Dream in many markets.</p>
<p><span id="more-480"></span><br />
<h2>Overview</h2>
<p>In order to install kind of new operating system I needed to have root access on the device.  This meant I had to <em>root</em> it.  Since Android 1.6 (Release R10) on the G1 doesn&#8217;t have any useful exploits I had to downgrade the OS to Release R7 first.  Once it is downgraded it you can use the rooting exploit in R7 to get root access and install a recovery image that will allow the installation of an updated radio software and the new operating system.</p>
<h2>Preliminaries</h2>
<p>Firstly read all of this blog post and all of the pages linked to from this blog post.  There is at least one point in this process where you can &#8220;brick your phone&#8221;.  I.e. you can put it in a state where by a regular user cannot make it usable.</p>
<p>Secondly you will be wiping your phone and replacing the software on it.  Back anything up you want to keep.  This includes purchased applications, data, contacts, whatever.</p>
<p>Make sure the phone is fully charged.  You don&#8217;t want it running out of power mid-flash!</p>
<p><span style="text-color: #ff0000;"><strong>Finally I provide no warranty express or implied.  You are doing this to your phone.  If it goes wrong its your fault!  You have been warned!</strong></span></p>
<h2>Downgrading and rooting</h2>
<p>Downgrading, rooting the phone, and installing a recovery image is described in the link below.  However since there are specific requirements for upgrading to Android 2.X I&#8217;ll explain each stage here.</p>
<p><a href="http://forum.xda-developers.com/showthread.php?t=442480">http://forum.xda-developers.com/showthread.php?t=442480</a></p>
<h3>Downgrading</h3>
<p>Before you do anything take note of the radio version string in the boot-loader.  Its the fourth line down.  You get the boot-loader screen up by holding down the camera button when powering on the phone.  With the original loader you should have multi-coloured bars with yellow text in the top left hand corner.  On my phone the radio version string read <em>&#8220;RADIO-2.22.19.26I&#8221;</em>.  Write down the string and store it somewhere safe.  You&#8217;ll need it later.</p>
<p>Now download the appropriate older image for your phone.  The links to the image downloads are on the page above however here&#8217;s the link for <a href="http://koushikdutta.blurryfox.com/G1/DREAMIMG-RC7.zip">RC7</a> for those with UK phones.  Download the image, unzip it, and save the <em>DREAMIMG.nbh</em> file in the root of your SD card.  While you&#8217;re at it also download <a href="http://www.androidspin.com/downloads.php?dir=amon_ra/RECOVERY/&#038;file=recovery-RA-dream-v1.5.2.img">recovery-RA-dream-v1.5.2.img</a> which is a recovery image and will be used later.  Save this in the root of your SD card also.</p>
<p>Turn off your phone, insert the SD card, and turn it back on again holding down the camera key as before.  It should now re-flash your phone.  There should be a progress bar.  When its all complete you can restart your phone normally (without the camera key held down).  This will result in some old school Android graphics and you wondering how anyone ever coped with that software.  Once the phone has booted find the phone details in settings and check the software build version number.  It should appear as something like <em>TC5-RC7 112931</em>.</p>
<p><center><br />
<table>
<tr>
<td><a href="http://www.flickr.com/photos/39013214@N03/5075135008" title="View 'Initial bootloader' on Flickr.com"><img height="240" alt="Initial bootloader" border="0" style="display: inline;" src="http://farm5.static.flickr.com/4113/5075135008_1492d33db5_m.jpg" width="162"/></a></td>
<td><a href="http://www.flickr.com/photos/39013214@N03/5074536617" title="View 'Installing an image from SD card' on Flickr.com"><img height="240" alt="Installing an image from SD card" border="0" style="display: inline;" src="http://farm5.static.flickr.com/4012/5074536617_0f606f44e1_m.jpg" width="172"/></a></td>
<td><a href="http://www.flickr.com/photos/39013214@N03/5075135512" title="View 'Software information screen' on Flickr.com"><img height="240" alt="Software information screen" border="0" style="display: inline;" src="http://farm5.static.flickr.com/4111/5075135512_f439d0d7e8_m.jpg" width="168"/></a></td>
</tr>
</table>
<p></center></p>
<h3>Rooting the phone</h3>
<p>Now you have exploitable image installed it is time to root the phone.  This can be done using a back door.  Go to the home screen then type&#8230;</p>
<pre>
&lt;return&gt;
&lt;return&gt;
telnetd
&lt;return&gt;
</pre>
<p>&#8230;on the slide out keyboard.  This will look like a search in contacts for the name of <em>telnetd</em>.  In fact <em>telnetd</em> is the telnet daemon which allows you to connect to shell on the phone via telnet.  So now you need a telnet client.  Use the Android Market to download a telnet client.  Once installed you should be able to telnet to <em>localhost</em> and get a telnet prompt.</p>
<p>If all is well you have shell access as root (a &#8216;#&#8217; prompt).  Yay!</p>
<h3>Installing a recovery image</h3>
<p>Now we are going to install the recovery image we downloaded earlier. This will also give us permanent root access.  In the shell type&#8230;</p>
<pre>
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
cd sdcard
flash_image recovery recovery-RA-dream-v1.5.2.img
cat recovery-RA-dream-v1.5.2.img > /system/recovery.img
</pre>
<p>For those who are interested the lines do the following:</p>
<ol>
<li>Remounts the flash memory in read/write mode.  This allows you to save to the flash in the phone.</li>
<li>Navigate to the sdcard (which is mounted at /sdcard)</li>
<li>Flashes the recovery-RA-dream-v1.5.2.img image file in to the special recovery partition in the flash.</li>
<li>Copies the recovery-RA-dream-v1.5.2.img as /system/recovery.img (The Android busybox doesn&#8217;t have the copy command but cat with a pipe works just as well!).</li>
</ol>
<p>Once you&#8217;ve done this and have a prompt again you can reboot the phone.  This time if you hold down the home and power buttons when you power on the phone it will boot in to the recovery image.  You can use the short cuts or the track ball to select a menu item.</p>
<p>Now we are ready to install the new software.</p>
<p><a href="http://www.flickr.com/photos/39013214@N03/5074537061" title="View 'Custom recovery image menu' on Flickr.com"><img height="240" style="display:block; margin-left:auto; margin-right:auto;" alt="Custom recovery image menu" border="0" src="http://farm5.static.flickr.com/4071/5074537061_048eed984f_m.jpg" width="166"/></a></p>
<h2>New radio software and DangerSPL</h2>
<p>Now we have to repartition the flash to give enough room for the new platform.  This is the scary bit.  As I said before&#8230;</p>
<p><span style="text-color: #ff0000;"><strong>Finally I provide no warranty express or implied.  You are doing this to your phone.  If it goes wrong its your fault!  You have been warned!</strong></span></p>
<p>Read this page thoroughly <a href="http://wiki.cyanogenmod.com/index.php?title=Dream:DangerSPL">http://wiki.cyanogenmod.com/index.php?title=Dream:DangerSPL</a>.  If you&#8217;ve gotten this far you fulfil the prerequisites to install Danger/SPL.  You now need to download the appropriate radio image, place it in the root of your SD card and flash it.  Remember that number we noted down from the boot-loader screen at the start (<em>&#8220;RADIO-2.22.19.26I&#8221;</em> or similar).  You will need it now.</p>
<p>Boot in to recovery by holding down the home and power buttons. Select <em>&#8220;Flash zip from sdcard&#8221;</em>.  Select <em>update.zip</em> (if that&#8217;s the name of the file) from the file menu.  The image should be flashed and will then reboot the phone.  Reboot the phone again.</p>
<p>Take out the SD card and boot back in to the boot loader.  It should now appear as below.</p>
<p><a href="http://www.flickr.com/photos/39013214@N03/5074537429" title="View 'Updated Google G1 bootloader' on Flickr.com"><img height="240" style="display:block; margin-left:auto; margin-right:auto;" alt="Updated Google G1 bootloader" border="0" src="http://farm5.static.flickr.com/4043/5074537429_0bebc5256a_m.jpg" width="179"/></a></p>
<h2>Upgrading to FROYO and installing Google Apps</h2>
<p>All the hard work is now done.  You have a rooted phone with a nice recovery menu which allows you to flash anything that will fit.</p>
<p>To install the latest Android read this link: <a href="http://wiki.cyanogenmod.com/index.php?title=Dream:Installing_CyanogenMod_5%2B">http://wiki.cyanogenmod.com/index.php?title=Dream:Installing_CyanogenMod_5%2B</a>.</p>
<p>Clear out your SD card and download <a href="http://wiki.cyanogenmod.com/index.php?title=Latest_Version#DreamCM6">Froyo for the G1/Dream</a> and the <a href="http://wiki.cyanogenmod.com/index.php?title=Latest_Version#Google_Apps">Google <strong>Tiny</strong> Apps</a> (The G1 is a bit short on flash space so wont fit the all singing all dancing versions of the Google apps).  Boot in to recovery as before (home and power buttons) and again select <em>&#8220;Flash zip from sdcard&#8221;</em>.  Flash first Froyo and then the apps.</p>
<p>Finally reboot your phone.  You should now have Android 2.2 installed!</p>
<p><center><br />
<table>
<tr>
<td><a href="http://www.flickr.com/photos/39013214@N03/5074537651" title="View 'Cyanogen's Froyo (Android 2.2) booting' on Flickr.com"><img height="240" alt="Cyanogen's Froyo (Android 2.2) booting" border="0" style="display: inline;" src="http://farm5.static.flickr.com/4068/5074537651_fd8dc6e0e7_m.jpg" width="179"/></a></td>
<td><a href="http://www.flickr.com/photos/39013214@N03/5075136748" title="View 'Cyanogen's Froyo (Android 2.2) home screen.' on Flickr.com"><img height="240" alt="Cyanogen's Froyo (Android 2.2) home screen." border="0" style="display: inline;" src="http://farm5.static.flickr.com/4089/5075136748_9bb89e6b2c_m.jpg" width="179"/></a></td>
</tr>
</table>
<p></center></p>
<h2>Issues</h2>
<p>The only issue I had was an exception from the keyboard software the first time I used Froyo.  This can be solved using the <em>&#8220;Wipe&#8221;</em> menu in the recovery image.  I wiped data, cache and delvik cache (The latter generated an error I ignored) and then re-installed Froyo.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberspice.org.uk/blog/2010/10/22/upgrading-your-uk-t-mobile-google-g1-htc-dream-to-android-2-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing eclipse for multiple programming languages</title>
		<link>http://www.cyberspice.org.uk/blog/2009/11/20/installing_eclipse_for_multiple_programming_languages/</link>
		<comments>http://www.cyberspice.org.uk/blog/2009/11/20/installing_eclipse_for_multiple_programming_languages/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 14:39:59 +0000</pubDate>
		<dc:creator>cyberspice</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[CDT]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[JDT]]></category>
		<category><![CDATA[PDT]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.cyberspice.org.uk/blog/?p=378</guid>
		<description><![CDATA[I can do almost everything I need to do in OS X but just occasionally I need Linux. So I recently installed a VirtualBox VM running Ubuntu 9.10. I use Eclipse as my favoured IDE since I can run it on all three of the major OSes and it has good support for C, C++ [...]]]></description>
			<content:encoded><![CDATA[<p>I can do almost everything I need to do in OS X but just occasionally I need Linux.  So I recently installed a <a href="http://www.virtualbox.org/">VirtualBox</a> VM running <a href="http://www.ubuntu.com/">Ubuntu 9.10</a>.  I use <a href="http://www.eclipse.org/">Eclipse</a> as my favoured IDE since I can run it on all three of the major OSes and it has good support for C, C++ and Java which is what I use mostly for work.  So the next thing for me was to install Eclipse.  This is where things became interesting.<br />
<span id="more-378"></span><br />
My usual technique for installing Java and Eclipse on Linux is to grab the JDK from <a href="http://java.sun.com/">Sun&#8217;s site</a> and Eclipse from the Eclipse site. (Never try to run Eclipse on GNU Java, it just doesn&#8217;t work.)  I did this, installed Java, unpacked Eclipse and ran it.  It just didn&#8217;t seem to run right.  I suspected some Ubuntu 9.10 incompatibility since it was shortly after the release.  So I decided to use the Ubuntu packages for Sun&#8217;s Java and Eclipse.  It worked but the latter was somewhat minimalist.  </p>
<p>I really wanted to run &#8216;proper&#8217; Eclipse as released by Eclipse so it was time to Google for the problem and I found this <a href="http://mou.me.uk/2009/10/31/fixing-eclipse-in-ubuntu-9-10-karmic-koala/">link</a>.  It turns out that there is a bug in Eclipse to do with GTK which will be fixed in the next release.  However you can work around it.  Put the following shell script in to your Eclipse directory and when you run Eclipse using that and not the binary directly its fixed.</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">#!/bin/bash
export GDK_NATIVE_WINDOWS=1
/path/to/eclipse/eclipse</pre></div></div>

<p>Anyway that&#8217;s a bit of an aside.  So now the post proper, how do you install Eclipse for multiple languages?  This is what I did.</p>
<h3>Eclipse for C/C++</h3>
<p>Go to <a href="http://www.eclipse.org/downloads">http://www.eclipse.org/downloads</a>, select packages and grab Eclipse IDE for C/C++.  <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?tar">Untar</a> it somewhere suitable like /usr/local.  You will have to be superuser to untar there so use <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?sudo">sudo</a>.</p>
<h3>Java Development Tools</h3>
<p>These come from <a href="http://download.eclipse.org/eclipse/downloads">http://download.eclipse.org/eclipse/downloads</a>.  Scroll down the page until you find &#8216;JDT Runtime Binary&#8217; and select the latest version.  This will download a ZIP file containing all the additional Java classes which provide the Java Development Tools.</p>
<p>Eclipse ZIP files are typically one of two kinds.  Either a collection of files you unzip in the directory where you eclipse directory is or one which that&#8217;s an &#8216;All in 1&#8242; update site.  The latter is a zip file that you can install via the Eclipse IDE itself since it contains the appropriate site.xml file used by the Eclipse installer.</p>
<p>The Eclipse JDT ZIP is not an update site so you will need to unzip in it in the directory you untarred the original files.  I.e. /usr/local.  If there are any clashes (because the file already exists) just select &#8216;N&#8217; meaning don&#8217;t overwrite.  </p>
<p>Once you&#8217;ve unpacked start Eclipse (sudo /path/to/eclipse/eclipse.sh) to ensure it looks sane.  You run it as root until you&#8217;re completely installed so that the extensions are installed for all users.  Hopefully, if all is well, you should see C/C++, CVS and Java projects available in the Projects menu.  Leave Eclipse running.</p>
<h3>DLTK, EMF-XSD and GEF</h3>
<p>DLTK is the Dynamic Languages Toolkit extension; EMF is the Eclipse Modelling Framework extension and GEF is the Graphical Editing Framework.  These form dependencies on the Web Tools and the PHP Development Tools.  Download the &#8216;All in 1&#8242; update sites for these as follows.</p>
<h4>DLTK</h4>
<p>Go to <a href="http://download.eclipse.org/technology/dltk/downloads/">http://download.eclipse.org/technology/dltk/downloads/</a>.  Select &#8216;Stable&#8217; and then the &#8216;All in 1 Update Site&#8217; bundle.  Save the ZIP somewhere sensible.</p>
<h4>EMF-XSD</h4>
<p>Go to <a href="http://www.eclipse.org/modeling/emf/downloads/?project=emf">http://www.eclipse.org/modeling/emf/downloads/?project=emf</a>.  Select the &#8216;All in 1 Update&#8217; bundle and again save it.</p>
<h4>GEF</h4>
<p>Go to <a href="http://www.eclipse.org/gef/downloads/">http://www.eclipse.org/gef/downloads/</a> and select the &#8216;All in 1 Update&#8217; and save it.</p>
<h4>Installing</h4>
<p>In the help menu select &#8216;Install new software&#8217;.  The top right of the dialog that appears is an &#8216;Add&#8217; button which adds a new install site, i.e. a URI from which new software can be installed.  Click on it.</p>
<div style="text-align:center;"><img src="http://www.cyberspice.org.uk/blog/wp-content/uploads/2009/11/AddSitesButton.png" alt="AddSitesButton.png" border="0" width="228" height="178" /></div>
<p>A new dialog appears with two fields.  Type &#8216;DLTK Local&#8217; in the first (&#8216;local&#8217;) field.  This is the short name the install site will be known as locally.  Click on the &#8216;Archive&#8217; button and navigate to the DLTK ZIP file you downloaded.  Select it.  Then click the &#8216;OK&#8217; button in the new site dialog.</p>
<div style="text-align:center;"><img src="http://www.cyberspice.org.uk/blog/wp-content/uploads/2009/11/AddSiteDialog.png" alt="AddSiteDialog.png" border="0" width="561" height="223" /></div>
<p>Now Eclipse will display what software is available from your install site (i.e. the DLTK ZIP file).  If you expand the tree you will see a whole selection of different components.  Some will have &#8216;SDK&#8217; as part of the name.  These components are ones which allow you to develop the Eclipse components themselves.  You don&#8217;t need these if you are using Eclipse to develop regular code so ignore those.  For DLTK I selected the core component and support for Python and Ruby (since I want to learn them at some point).  </p>
<div style="text-align:center;"><img src="http://www.cyberspice.org.uk/blog/wp-content/uploads/2009/11/AvailableSoftware.png" alt="AvailableSoftware.png" border="0" width="437" height="340" /></div>
<p>Click &#8216;Next&#8217;.  All the dependencies should be there.  Eclipse may install additional packages which are dependencies on the one&#8217;s you&#8217;ve selected.  It may also show a licence agreement you need to agree to.  Click through until &#8216;Finish&#8217; is shown.  Click &#8216;Finish&#8217; to install the software.  Eclipse may want to restart at this point, everyone apart from Ubuntu 9.10 users can click &#8216;Restart&#8217; here.  Because of the bug Ubuntu 9.10 users should click &#8216;No&#8217; and exit and restart Eclipse manually using the shell script as described above.</p>
<p>Do the same for the EMF-XSD package and select &#8216;XSD-XL Schema Model, XSD Documentation and XSD Edit&#8217;.  (Probably not all needed but useful).  Install and restart as before.  Add the GEF ZIP to the install sites so that it can be found later but don&#8217;t bother installing anything if you don&#8217;t want to.  This seems to be needed by the Web Tools but I didn&#8217;t want anything from it myself.</p>
<p>You should now have Ruby and Python projects as well as DTD, XSD, etc. projects available in the projects menu.  Quit Eclipse.</p>
<h3>Web Tools Platform</h3>
<p>WTP provides support for developing HTML, JavaScript etc. and is needed by the PHP Developer Tools.  The download site is <a href="http://download.eclipse.org/webtools/downloads/">http://download.eclipse.org/webtools/downloads/</a>.  Select the latest release and then download the web tools platform, wtd, ZIP file.</p>
<p>Like the JDT ZIP this is not an install site so needs to be unpacked in the same directory as you unpacked Eclipse and the JDT ZIP.  You will need to be superuser as before.  Again there may be files that already exist so you have to stop them from being over written.</p>
<p>Restart Eclipse as superuser so that it can update its state.  Now you should have web tools projects available like JavaScript, HTML etc. in the new project menu.</p>
<h3>PHP Developer Tools</h3>
<p>Finally we can install PDT.  You can get the all in one update site from <a href="http://www.eclipse.org/pdt/downloads/">http://www.eclipse.org/pdt/downloads/</a> as a ZIP.  In Eclipse using the &#8216;Install New Software&#8217; dialog add it as an install site, select the PDT tools and install them.  Eclipse will install anything it needs from the other install sites you used earlier.  Finally restart Eclipse one last time.</p>
<p>Yay!  You should have Eclipse installed for a whole bucket load of languages.  Because you installed the plug-ins as superuser they are available for everyone on your system.  Finally, because you don&#8217;t want to have to do this all over again should something go wrong, tar up the entire Eclipse directory and put it somewhere safe.</p>
<div style="text-align:center;"><img src="http://www.cyberspice.org.uk/blog/wp-content/uploads/2009/11/ProjectMenu.png" alt="ProjectMenu.png" border="0" width="344" height="415" /></div>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberspice.org.uk/blog/2009/11/20/installing_eclipse_for_multiple_programming_languages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using PHP sessions from Java</title>
		<link>http://www.cyberspice.org.uk/blog/2009/03/19/using-php-sessions-from-java/</link>
		<comments>http://www.cyberspice.org.uk/blog/2009/03/19/using-php-sessions-from-java/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 16:02:45 +0000</pubDate>
		<dc:creator>cyberspice</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PHP Sessions]]></category>

		<guid isPermaLink="false">http://www.cyberspice.org.uk/blog/?p=141</guid>
		<description><![CDATA[This blog post is the first in a number of what I hope are useful coding related posts. I&#8217;ve been coding for many years in many languages on many platforms. I tend to solve issues using a combination of technologies so many of my posts will be based around combining technologies. Others will be basically [...]]]></description>
			<content:encoded><![CDATA[<p>This blog post is the first in a number of what I hope are useful coding related posts.  I&#8217;ve been coding for many years in many languages on many platforms.  I tend to solve issues using a combination of technologies so many of my posts will be based around combining technologies.  Others will be basically describing gotchas that I have found during development and how I solved or worked around them.  This post is about using PHP&#8217;s session handling from a Java client, i.e. not a browser.</p>
<p><span id="more-141"></span></p>
<h3>PHP session handling</h3>
<p><a href="http://uk3.php.net/session">PHP session handling</a> is a way of having persistent or stateful data across HTTP requests.  The simplest snippet of PHP to achieve this is shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Start the session</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create a new session object if its a new session</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'session'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'session'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MySessionObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I tend to use a PHP class to encapsulate the session data.  Using the code above a new instance of <em>MySessionObject()</em> is created if a session could not be restored.  Otherwise the instance is restored from the previous time the code was called.</p>
<p>PHP actually manages this using a session ID stored in a cookie.  (There is an alternative method of managing the session ID if cookies are disable in a browser but that&#8217;s beyond the scope of this post.)  The <em>session_start()</em> call will parse the value of the <strong>Cookie:</strong> HTTP request header field looking for a value similar to <strong>PHPSESSID=b330c9b604d371e5c88cd0919990f41c</strong>.  It will use the session ID value to retrieve the appropriate session data.  It will deserialise any stored values and store them in the  <em>$_SESSION</em> global array variable.</p>
<p>If the <strong>PHPSESSID</strong> does not exist <em>session_start()</em> will create a new empty session (and <em>$_SESSION</em>) and use <strong>Set-Cookie:</strong> in the HTTP response to pass the session ID cookie to the browser so it can be returned on the next request.  The session cookie is valid for the lifetime of the browser.</p>
<h3>The Java client</h3>
<p>However this post is about using PHP sessions from Java.  To make an HTTP request from Java use the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/HttpURLConnection.html"><em>HttpURLConnection</em></a> class.  This class does have a few gotchas which I will explain below but it is sufficient for our needs.  Typically I encapsulate the class in my own class to manage extra data.  The bones of such a class is as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyConnection <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> HTTP_HEADER_SET_COOKIE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;set-cookie&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">URL</span> url <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> cookies <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> MyConnection<span style="color: #009900;">&#40;</span><span style="color: #003399;">URL</span> url<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;">url</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> readCookies<span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span> con<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// ...</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> sendRequest<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> requestString<span style="color: #009900;">&#41;</span> 
		<span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, SocketTimeoutException <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// ...</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You create an instance of the class when you want to start communicating with the remote URL and it will store cookies between each communication session with the remote server.  The main work is carried out by the <em>sendRequest()</em> method which is shown below.</p>
<p><em>HttpURLConnection</em> is a little tricky to use due to the time at which the request is actually sent to the server.  If you set header fields (using <em>setRequestProperty()</em>) too late then they wont be included.  It is similar for other properties too.  </p>
<p>By default the class expects to have an empty body to the request sent to the server and returns the body of the response to the user by way of the input stream obtained using <em>getInputStream()</em>.  (Note that input and output here refers to data being input to the connection and output from the connection.  I.e. input in to the server and output from the server which may be the reverse of what you&#8217;re expecting.)  However you can tell the connection you intend providing a body to the request by calling <em>setDoOutput()</em> with a value of <strong>true</strong>.  In this case the output data is buffered and sent with the headers when <em>getInputStream()</em> is called.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> sendRequest<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> requestString<span style="color: #009900;">&#41;</span> 
	<span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, SocketTimeoutException <span style="color: #009900;">&#123;</span>
&nbsp;
	StringBuilder buffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">String</span> line<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the connection.  Java will create a new one each time.</span>
	<span style="color: #003399;">HttpURLConnection</span> con <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span><span style="color: #009900;">&#41;</span>url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Set the Cookie header</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>cookies <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>
		con.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cookie&quot;</span>, cookies<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;">// Set the content-size header</span>
	con.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-size&quot;</span>, 
		<span style="color: #003399;">Integer</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span>requestString.<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Set the request method.</span>
	con.<span style="color: #006633;">setRequestMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Enable both reading and writing to and from the connection.</span>
	con.<span style="color: #006633;">setDoInput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	con.<span style="color: #006633;">setDoOutput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the output stream</span>
	<span style="color: #003399;">OutputStreamWriter</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">OutputStreamWriter</span><span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Send the request</span>
	out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>requestString.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	out.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the input stream.  This is the point at which data is sent to </span>
	<span style="color: #666666; font-style: italic;">// and read from the server.</span>
	<span style="color: #003399;">BufferedReader</span> in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span>
		<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span>con.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// If the response from the server is OK handle the content of the response</span>
	<span style="color: #003399;">String</span> responseCode  <span style="color: #339933;">=</span> con.<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;">if</span> <span style="color: #009900;">&#40;</span>responseCode <span style="color: #339933;">==</span> <span style="color: #003399;">HttpURLConnection</span>.<span style="color: #006633;">HTTP_OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Read the cookies</span>
		<span style="color: #003399;">String</span> cookies <span style="color: #339933;">=</span> readCookies<span style="color: #009900;">&#40;</span>con<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>cookies <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;">this</span>.<span style="color: #006633;">cookies</span> <span style="color: #339933;">=</span> cookies<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Read the content</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>line <span style="color: #339933;">=</span> in.<span style="color: #006633;">readLine</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: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			buffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>line<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;">return</span> buffer.<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: #666666; font-style: italic;">// Error from server</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></pre></div></div>

<p>Basically the method obtains a connection and sets up important headers, the request type, sends the request string as the body of the request returning the response string to the caller.  This is a fairly typical use of the <em>HttpURLConnection</em> class.  The important bit with respect to this particular post is the cookie handling.  </p>
<p>In the code above, if the <em>cookies</em> variable is set then it is passed to the server as the value of a <strong>Cookie:</strong> header otherwise that header is omitted.  When an instance of the <em>MyConnection</em> class is created the <em>cookies</em> variable is initialised to null and is only set when a <strong>Set-Cookie:</strong> header is received from the server.  This is handled by the <em>readCookies()</em> method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> readCookies<span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span> con<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	StringBuilder cookieBuffer <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">String</span>        cookieField  <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">String</span>        headerName   <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<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;">1</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span>headerName <span style="color: #339933;">=</span> con.<span style="color: #006633;">getHeaderFieldKey</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</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> i<span style="color: #339933;">++</span><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>headerName.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>HTTP_HEADER_SET_COOKIE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			cookieField <span style="color: #339933;">=</span> con.<span style="color: #006633;">getHeaderField</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			cookieField <span style="color: #339933;">=</span> cookieField.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, 
				cookieField.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;;&quot;</span><span style="color: #009900;">&#41;</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>cookieBuffer <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>
				cookieBuffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;; &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;">else</span> <span style="color: #009900;">&#123;</span>
				cookieBuffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			cookieBuffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>cookieField<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;">if</span> <span style="color: #009900;">&#40;</span>cookieBuffer <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> cookieBuffer.<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;">else</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: #009900;">&#125;</span></pre></div></div>

<p>Annoyingly there isn&#8217;t a getter equivalent to <em>setRequestProperty()</em>.  You can only obtain headers provided in the response by index.  This means that you have to iterate through all the headers looking for the ones your interested in.  This is slightly simplified by the <em>getHeaderFieldKey()</em> which returns the header name, for example, <strong>Set-Cookie</strong> for the specified index.  So you iterate through the header names obtaining the header data for each of the names you are interested.  The value of the cookie is everything up to the first semi-colon.  The rest is meta-data.</p>
<p>However nothing is simple in this world.  It appears that the Android version of <em>HttpURLConnection</em> converts the header field name to lower case but the Sun version (in the JDK) does not.  So above I convert the header name to lowercase before comparison to ensure I&#8217;m always comparing like with like.  </p>
<p>Yet another gotcha is that you could have more than one <strong>Set-Cookie</strong> header but you can only call <em>setRequestProperty()</em> once for each header.  If you call it more than once you overwrite the previous value.  So I concatenate all the cookies received in to one cookie string separating them appropriately.</p>
<p>So combining everything the Java client creates an instance of <em>MyConnection</em> with the appropriate URL.  When <em>sendRequest()</em> is first called no cookies are set.  The PHP on the server calls <em>session_start()</em> that creates a new session.  The session ID is returned to the Java client, obtained by <em>readCookies()</em> and stored in the instance of <em>MyConnection</em>.  </p>
<p>Subsequent calls to <em>sendRequest()</em> will return the session ID back to the PHP running on the server for as long as the instance of <em>MyConnection</em> exists.  This way creating a new instance of <em>MyConnection</em> creating a new session on the server automatically.</p>
<p>So that&#8217;s how you use PHP session from a Java client.  I hope this post is useful and welcome any comments about content or style.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberspice.org.uk/blog/2009/03/19/using-php-sessions-from-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

