<?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, 23 Jul 2010 10:25:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 [...]]]></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 [...]]]></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>
