<?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>Songbird Creations &#187; Development</title>
	<atom:link href="http://www.songbird-creations.com/archives/category/development/feed" rel="self" type="application/rss+xml" />
	<link>http://www.songbird-creations.com</link>
	<description>Software Creation and Consultancy</description>
	<lastBuildDate>Fri, 23 Oct 2009 14:11:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Wonderful Thing About Java</title>
		<link>http://www.songbird-creations.com/archives/33</link>
		<comments>http://www.songbird-creations.com/archives/33#comments</comments>
		<pubDate>Thu, 19 Feb 2009 16:24:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Applet]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PDF]]></category>
		<category><![CDATA[Printing]]></category>

		<guid isPermaLink="false">http://www.songbird-creations.com/?p=33</guid>
		<description><![CDATA[Fun and games developing Java Applets to print PDFs without user intervention.]]></description>
			<content:encoded><![CDATA[<p><strong>is Java&#8217;s a wonderful thing..</strong></p>
<p><em>Well, mostly anyway.</em></p>
<p>The wonderful world of Java applets is a funny old world. One of pitfalls and woe on one side and unparalleled power on the other. </p>
<p>Java has been around for a while and has a very substantial support base, which comes in handy when trying to get around some of the more annoying problems associated with Java development, well Applet development anyway. It runs on pretty much anything, part of it&#8217;s charm is that you don&#8217;t need to run on a Microsoft platform unlike .net</p>
<p>One of the best things is that as a language, it&#8217;s free and there are many free IDEs out there to help you to develop. One of the best I&#8217;ve seen so far is NetBeans v6.5 &#8211; An IDE originally written by Sun Micro systems, made open source whilst being heavily sponsored by Sun. It&#8217;s completely free, Cross platform and downloadable from the their site <a href="http://www.netbeans.org">www.netbeans.org </a><br />
Without wanting to spend too much time singing the praises of NetBeans, I have to mention that it&#8217;s PHP editing tools are pretty fine too and when coupled with the correct version of <a href="http://www.xdebug.org">xdebug </a>for your PHP runtime, make for a very powerful PHP development platform indeed.</p>
<p>We found a couple of minor issues which were noteworthy when developing Java Applets.</p>
<p>One of them being that the HTML standard has kind-of left Applets behind, deprecating the Applet tag for the more generic Object tag. Not a big problem, but slightly annoying and important if your web site is to be XHTML compliant. </p>
<p><strong>We had a simple task to solve for our client:</strong></p>
<p><em>Print PDF documents on a cross browser web page, without user intervention</em>. </p>
<p>The task in itself doesn&#8217;t sound too bad. PDFs are a well known standard for documents and there are numerous browser plug-ins we can use. The tricky part is the &#8220;<em>Without user intervention</em>&#8221; bit. </p>
<p>That suddenly throws all of the spanners into the machine, leaving a poor engineer with the task of putting it all back together again.. only this time making it spanner proof.<br />
Browsers are very protective of the printer. You can&#8217;t access it directly. Sure, you can print a web page,  but you can&#8217;t bypass the all-seeing eye of the Printer Dialog. </p>
<p>Because of this, numerous ActiveX components have been developed and are sold to get around this issue for web applications. The thing is, ActiveX is a Microsoft Only technology, for Windows and Internet Explorer. That goes against the Cross Platform, Cross Browser requirement.</p>
<p><strong>Java to the Rescue&#8230; mostly</strong></p>
<p>So we had to set about printing from the browser. Thankfully, Java does have the capability to talk to the printer directly. The only downside being that it keeps asking for permission to do so, rather like an over-polite child constantly asking to use the bathroom.. The temptation to lose all patience and say &#8220;Yes, for the 1000th time, just use the bathroom if you need it OK?&#8221; &#8211; which would be bad.</p>
<p>There is a way to instruct the Java runtime that it&#8217;s OK for it to use the bathroom.. I mean, Printer. You can modify the java.policy file in your runtime directory/lib/security folder to grant permissions to use the printer. A cheap and nasty solution in more than one way. </p>
<p>First of all, it meant having to modify the file on the machines of each user using the web application to grant access to the printer and secondly (and possibly more importantly) Every time you upgrade your Java runtime, it keeps a copy of the old one and installs the new one in a fresh directory.<br />
This means that you have to re-apply your cheap hack to the new runtime, whenever your users upgrade their Java.. Support Nightmare!</p>
<p><strong>Thankfully, there was a solution.</strong><br />
First of all.. Sign the Applet. You can self sign your applets but we chose to buy a certificate and sign it that way. It looks much better with a certificate from a trusted Certificate Authority, and they&#8217;re not expensive.</p>
<p>A signed applet is a trusted applet (assuming the user says &#8216;Yes&#8217;, they trust your applet) and has access to things like Printing and Files etc. Things a normal applet just doesn&#8217;t have (without above hack).</p>
<p>But we were still running into problems. The over-polite child.. I mean, Java.. was still asking to use the printer, each and every time we  initialized the applet and tried to print. Even though we&#8217;d checked the little box which was supposed to remember our selection. I&#8217;m not sure if this is a bug in the runtime or not, but that&#8217;s another thing entirely.</p>
<p><strong>Odd permissions issue &#8211; The day Logic took the day off</strong><br />
It turns out that the reason it kept asking, even with a signed applet was that the code being called to talk to the printer was being accessed from Javascript on the web page, and was therefore un-trusted. I&#8217;m unsure of the logic here, what was going through the minds of the Java Runtime developers.. a trusted applet should be a trusted applet irrespective of what is calling it, but it is what it is.</p>
<p>The way around that was to make sure that the Javascript was not directly accessing the printing method. </p>
<p>This was done using a separate thread, sitting waiting for a variable to be set.<br />
The public method accessed by the Javascript then only set a variable along with the printing details.<br />
The printing thread then picks this information up and prints the document. No problems.</p>
<p><strong>Are we nearly there yet?</strong><br />
Another issue was telling when the applet was loaded so that we could call it&#8217;s methods from the site. This turned out to be more problematic than we originally thought. The isActive method didn&#8217;t always work and we had to resort to getting the Applet to call a Javascript method on the page (if it existed) to inform the page that it was ready.</p>
<p><strong>..and Finally</strong></p>
<p>Now, figuring that all out took more time than writing the entire applet and if this was a few years ago, If google didn&#8217;t have access to forum threads where other people had solved these same problems, it would have taken a lot longer. In some ways, I can see why the .net framework has overtaken Java as the development platform of choice for business. It&#8217;s certainly easier to get up and running faster with .net but .net can&#8217;t do everything and Java shouldn&#8217;t be dismissed as old hat.</p>
<p>It&#8217;s very powerful, fast, cross-platform, cross-browser and free. These factors alone make it a language worth using.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.songbird-creations.com/archives/33/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
