<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rough Book &#187; Programming and Development</title>
	<atom:link href="http://vivin.net/category/nerdy-stuff/computers-nerdy-stuff/programming-and-development-computers-nerdy-stuff/feed/" rel="self" type="application/rss+xml" />
	<link>http://vivin.net</link>
	<description>random musings of just another computer nerd</description>
	<lastBuildDate>Wed, 02 May 2012 18:20:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Indenting XML and HTML from vim</title>
		<link>http://vivin.net/2012/01/17/indenting-xml-and-html-from-vim/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2012/01/17/indenting-xml-and-html-from-vim/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 23:32:28 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[editors]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1831</guid>
		<description><![CDATA[vim has an awesome feature, using which you can pipe a range through an external command. This is pretty useful if you&#8217;re opening up an un-indented or poorly-indented XML or HTML file. If you want to indent your entire file, simply do the following: :%!tidy -i -xml -q The -i option tells tidy that it [...]]]></description>
			<content:encoded><![CDATA[<p>vim has an awesome feature, using which you can pipe a range through an external command. This is pretty useful if you&#8217;re opening up an un-indented or poorly-indented XML or HTML file. If you want to indent your entire file, simply do the following:</p>
<pre class="brush: php">
:%!tidy -i -xml -q
</pre>
<p>The <span class="code-snippet">-i</span> option tells tidy that it needs to indent the content, <span class="code-snippet">-xml</span> tells tidy that the content is well-formed XML, and <span class="code-snippet">-q</span> puts tidy into &#8220;quiet mode&#8221; where extraneous information is suppressed. You can also specify ranges like so:</p>
<pre class="brush: php">
:40, 74!tidy -i -xml -q
</pre>
<p>This indents content between lines 40 and 74 (both lines inclusive). You can also do:</p>
<pre class="brush: php">
:., .+50!tidy -i -xml -q
</pre>
<p>This indents the current line and the next 50 lines. You can also do the same for HTML:</p>
<pre class="brush: php">
:%!tidy -i -xml -q
</pre>
<p>You can of course, supply additional parameters to tidy to customize the indenting.</p>
<pre class="brush: php">
:%!tidy -i -q
</pre>
<br /><a href="http://vivin.net/?p=1831#comments" title="Comments on &quot;Indenting XML and HTML from vim&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1831" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1831&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2012/01/17/indenting-xml-and-html-from-vim/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1831" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1831" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1831&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Implementing pinch-zoom and pan/drag in an Android view on the canvas</title>
		<link>http://vivin.net/2011/12/04/implementing-pinch-zoom-and-pandrag-in-an-android-view-on-the-canvas/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/12/04/implementing-pinch-zoom-and-pandrag-in-an-android-view-on-the-canvas/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 23:21:49 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Operating Systems]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[drag]]></category>
		<category><![CDATA[dragging]]></category>
		<category><![CDATA[gestures]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[pan]]></category>
		<category><![CDATA[panning]]></category>
		<category><![CDATA[pinch-zoom]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[zoom]]></category>
		<category><![CDATA[zooming]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1806</guid>
		<description><![CDATA[I was trying to get pinch-zoom and panning working on an Android view today. Basically I was trying to implement the same behavior you see when you use Google Maps (for example). You can zoom in and pan around until the edge of the image, but no further. Also, if the image is fully zoomed [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to get pinch-zoom and panning working on an Android view today. Basically I was trying to implement the same behavior you see when you use Google Maps (for example). You can zoom in and pan around until the edge of the image, but no further. Also, if the image is fully zoomed out, you can&#8217;t pan the image. Implementing the pinch-zoom functionality was pretty easy. I found <a href="http://stackoverflow.com/questions/5216658/pinch-zoom-for-custom-view" title="Pinch Zoom on Android">an example on StackOverflow</a>. I then wanted to implement panning (or dragging) as well. However, I wasn&#8217;t able to easily find examples and tutorials for this functionality. I started with <a href="http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-5-implementing-the-drag-gesture/1789?tag=content;siu-container" title="Drag">this example</a> that comes from the third edition of the <a href="http://pragprog.com/book/eband3/hello-android" title="Hello, Android!">Hello, Android!</a> book but I didn&#8217;t get too far. So I started playing around a little bit with the events and started writing some code from scratch (using the example from <i>Hello, Android!</i>) so that I could have a better idea of what was happening.</p>
<p>As I mentioned before, getting zoom to work was pretty easy. Implementing panning/dragging was the hard part. The major issues I encountered and subsequently fixed were the following:</p>
<ol>
<li>Panning continues indefinitely in all directions.</li>
<li>When you zoom and then pan, stop, and then start again, the view jerks to a new position instead of panning from the existing position.</li>
<li>Excessive panning towards the left and top can be constrained, but panning towards the right and bottom is not so easily constrained.</li>
</ol>
<p>Once I fixed all the problems, I figured that it would be nice to document it for future reference, and I also think it would be a useful resource for others who have the same problem. Now a little disclaimer before I go any further: I&#8217;m not an Android expert and I&#8217;m really not that great with graphics; I just started it learning to program for Android this semester for one of my Masters electives. So there might be a better way of doing all this, and if there is, please let me know! Also, if you want to skip all the explanations and just see the code, you can <a href = "http://vivin.net/2011/12/04/implementing-pinch-zoom-and-pandrag-in-an-android-view-on-the-canvas/8/">skip to the last page</a>.</p>
<p><span id="more-1806"></span></p>
<p>Let&#8217;s start with the simple stuff first, that is implementing pinch-zoom. To implement pinch-zoom, we make use of the <a href="http://developer.android.com/reference/android/view/ScaleGestureDetector.html" title="ScaleGestureDetector"><span class="code-snippet">ScaleGestureDetector</span></a> class. This class helps you detect the pinch-zoom event. Using it is pretty simple:</p>
<pre class="brush: java">
public class ZoomView extends View {

    private static float MIN_ZOOM = 1f;
    private static float MAX_ZOOM = 5f;

    private float scaleFactor = 1.f;
    private ScaleGestureDetector detector;

    public ZoomView(Context context) {
        super(context);
        detector = new ScaleGestureDetector(getContext(), new ScaleListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.scale(scaleFactor, scaleFactor);

        // ...
        // your canvas-drawing code
        // ...

        canvas.restore();
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            scaleFactor *= detector.getScaleFactor();
            scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));
            invalidate();
            return true;
        }
    }
}
</pre>
<p>Your view class has four private members: <span class="code-snippet">MIN_ZOOM</span>, <span class="code-snippet">MAX_ZOOM</span>, <span class="code-snippet">detector</span>, and <span class="code-snippet">scaleFactor</span>. The first two are static constants that define the maximum and minimum zoom allowed. The third is of type <span class="code-snippet">ScaleGestureDetector</span> and does all the heavy lifting as far as zooming is concerned. The fourth member holds the scaling factor i.e., a number that represents the amount of &#8220;zoom&#8221;.</p>
<p>Now what does this do?</p>
<p>In the constructor, you initialize the detector. The constructor to <span class="code-snippet">ScaleGestureDetector</span> takes two parameters: the current context, and a listener. Our listener is defined inside the class <span class="code-snippet">ScaleListener</span> which extends the abstract class <span class="code-snippet">ScaleGestureDetector.SimpleOnScaleGestureListener</span>. Inside the <span class="code-snippet">onScale</span> method, we get the current scale factor from the detector. We then check to see if it is greater or smaller than our upper and lower bounds. If so, we make sure that it we limit the value to be between those bounds. We then call <span class="code-snippet">invalidate()</span> which forces the canvas to redraw itself.</p>
<p>The actual scaling happens inside the <span class="code-snippet">onDraw</span> method. There, we save the canvas, set its scaling factor (which is the one we got from the detector), draw anything we need to draw, and then restore the canvas. What happens now is that anything you draw on the canvas is now scaled by the scaling factor. This is what gives you the &#8220;zoom&#8221; effect.</p>
<br /><a href="http://vivin.net/?p=1806#comments" title="Comments on &quot;Implementing pinch-zoom and pan/drag in an Android view on the canvas&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1806" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1806&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/12/04/implementing-pinch-zoom-and-pandrag-in-an-android-view-on-the-canvas/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1806" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1806" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1806&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Akṣi: Handwritten-digit-recognizing neural-network</title>
		<link>http://vivin.net/2011/11/24/ak%e1%b9%a3i-handwritten-digit-recognizing-neural-network/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/11/24/ak%e1%b9%a3i-handwritten-digit-recognizing-neural-network/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 19:26:03 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[artificial intelligence]]></category>
		<category><![CDATA[machine-learning]]></category>
		<category><![CDATA[neural-networks]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1803</guid>
		<description><![CDATA[I&#8217;ve always been interested in Neural Networks (ever since I first found out about them around 10 years ago). However, I never got a chance to learn about them or write one of my own; this is something I&#8217;ve wanted to do for some time. I got the opportunity this semester when my professor in [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always been interested in Neural Networks (ever since I first found out about them around 10 years ago). However, I never got a chance to learn about them or write one of my own; this is something I&#8217;ve wanted to do for some time. I got the opportunity this semester when my professor in my advanced data-structures class told us that we could pick any topic we liked, for a semester project. I thought that this would be the perfect time for me to learn about neural networks and create one of my own.</p>
<p>The end result was <a href="http://aksi-neural-network.appspot.com/" title="Akṣi Handwritten-digit-recognizing neural-network" target="_blank">Akṣi</a>, a neural network that recognizes hand-written digits. I&#8217;ve hosted it using Google&#8217;s App Engine. Please check it out!</p>
<br /><a href="http://vivin.net/?p=1803#comments" title="Comments on &quot;Akṣi: Handwritten-digit-recognizing neural-network&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1803" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1803&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/11/24/ak%e1%b9%a3i-handwritten-digit-recognizing-neural-network/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1803" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1803" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1803&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Setting the content type to text/plain for a JSON response from a Spring controller</title>
		<link>http://vivin.net/2011/11/07/setting-the-content-type-to-textplain-for-a-json-response-from-a-spring-controller/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/11/07/setting-the-content-type-to-textplain-for-a-json-response-from-a-spring-controller/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 20:30:19 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Nerdy Stuff]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax fie upload]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring 3]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1779</guid>
		<description><![CDATA[I was using a jQuery plugin called a ajaxfileupload to upload a file through AJAX. Technically what the plugin does isn&#8217;t AJAX. It creates a hidden form and an iframe, and then submits the form using the iframe as the target. The iframe will then end up with the response from the server. This response [...]]]></description>
			<content:encoded><![CDATA[<p>I was using a jQuery plugin called a <a href="http://www.phpletter.com/Our-Projects/AjaxFileUpload/" title="ajaxfileupload">ajaxfileupload</a> to upload a file through AJAX. Technically what the plugin does isn&#8217;t AJAX. It creates a hidden form and an iframe, and then submits the form using the iframe as the target. The iframe will then end up with the response from the server. This response is then read-in by the plugin and handled appropriately. In my case I was using a controller action that would return JSON (using the <span class="code-snippet">.action</span> extension). The action uses Spring&#8217;s <span class="code-snippet">MappingJacksonJSONView</span> that returns JSON with a content type of <span class="code-snippet">application/json</span> (as it should). This works perfectly in Chrome, however in both Firefox and IE, the user is presented with a dialog box that asks them to download the JSON response. This is obviously not what I wanted. The reason this is happening is because the response is being directly submitted to the iframe (and therefore, the page). That is, it&#8217;s not coming through via the <span class="code-snippet">XMLHttpRequest</span> object. So IE and FF don&#8217;t know what to do with it and assume that it is something the user would want to download. The solution to this problem is to set the content-type to <span class="code-snippet">text/plain</span>. This wasn&#8217;t as straightforward as I thought it would be. </p>
<p>Initially I was going to call the <span class="code-snippet">render(&#8230;)</span> method of <span class="code-snippet">MappingJacksonJsonView</span> but that didn&#8217;t work because the content-type had already been set to <span class="code-snippet">application/json</span>. The solution I came up with was to duplicate some of the code (ugh) inside <span class="code-snippet">MappingJacksonJsonView</span> to get the JSON as a string and to then write that to the response:</p>
<pre class="brush: java">

@RequestMapping
public void processFileUpload(HttpServletResponse response, Model model, ...) {

    ...

    //Set the content-type and charset of the response
    response.setContentType(&quot;text/plain&quot;);
    response.setCharacterEncoding(&quot;UTF-8&quot;);

    //I need to use another OutputStream here; I cannot use the response&#039;s OutputStream because that will cause errors
    //later on when the JSP needs to render its content (recall that getOutputStream() can only be called exactly once
    //on a response). Therefore I&#039;m writing the data to a ByteArrayOutputStream and then writing the byte array from
    //the ByteArrayOutputStream to the response manually.

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectMapper objectMapper = new ObjectMapper();
    JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(byteArrayOutputStream, JsonEncoding.UTF8);

    //Before I can convert the data into JSON, I will need to filter some attributes out of the model (namely BindingResult)
    Map&lt;String, Object&gt; result = new HashMap&lt;String, Object&gt;();

    for(Map.Entry&lt;String, Object&gt; entry : model.asMap().entrySet()) {
        if(!(entry.getValue() instanceof BindingResult)) {
            result.put(entry.getKey(), entry.getValue());
        }
    }

    objectMapper.writeValue(generator, result);
    response.getWriter().write(new String(byteArrayOutputStream.toByteArray(), &quot;UTF8&quot;));
}
</pre>
<p>This still seems a little hacky to me. A possible improvement is to annotate the action with <span class="code-snippet">@ResponseBody</span> and return the JSON as a string without involving the response at all. If anyone has a better solution, I&#8217;m all ears!</p>
<br /><a href="http://vivin.net/?p=1779#comments" title="Comments on &quot;Setting the content type to text/plain for a JSON response from a Spring controller&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1779" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1779&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/11/07/setting-the-content-type-to-textplain-for-a-json-response-from-a-spring-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1779" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1779" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1779&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Popularity Contest WordPress plugin breaks RSS feed</title>
		<link>http://vivin.net/2011/10/23/popularity-contest-wordpress-plugin-breaks-rss-feed/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/10/23/popularity-contest-wordpress-plugin-breaks-rss-feed/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 17:24:51 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1773</guid>
		<description><![CDATA[I&#8217;m using a somewhat old plugin (it hasn&#8217;t been updated since &#8217;09) called Popularity Contest to show the popularity of my posts. However, I noticed that it was breaking my RSS feed. This is due to the fact that the plugin doesn&#8217;t properly escape the ampersand character inside an image URL. Instead of &#38;#38; it [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using a somewhat old plugin (it hasn&#8217;t been updated since &#8217;09) called <a href="http://wordpress.org/extend/plugins/popularity-contest/" title="Popularity Contest" target="_blank">Popularity Contest</a> to show the popularity of my posts. However, I noticed that it was breaking my RSS feed. This is due to the fact that the plugin doesn&#8217;t properly escape the ampersand character inside an image URL. Instead of &#38;#38; it uses just &#38;. You can fix this by changind line number 2272 to:</p>
<pre class="brush: php">
$str .= &#039;&lt;img src=&quot;&#039;.site_url(&#039;?ak_action=api_record_view&amp;#38;id=&#039;.$post-&gt;ID.&#039;&amp;#38;type=feed&#039;).&#039;&quot; alt=&quot;&quot; /&gt;&#039;;
</pre>
<br /><a href="http://vivin.net/?p=1773#comments" title="Comments on &quot;Popularity Contest WordPress plugin breaks RSS feed&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1773" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1773&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/10/23/popularity-contest-wordpress-plugin-breaks-rss-feed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1773" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1773" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1773&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Fixing Maven 3.0.3&#8242;s dependency-resolution performance-regression</title>
		<link>http://vivin.net/2011/07/20/fixing-maven-3-0-3s-dependency-resolution-performance-regression/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/07/20/fixing-maven-3-0-3s-dependency-resolution-performance-regression/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 23:27:07 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Nerdy Stuff]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[aether]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[dependency resolution]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven 3]]></category>
		<category><![CDATA[performance regression]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1757</guid>
		<description><![CDATA[TL; DR; version: Maven 3.0.3 has a performance-regression while resolving dependencies. This is because it uses version 1.11 of aether. The problem has been fixed in version 1.12 of aether, but a version of maven with this library is not available. I built maven from source with the 1.12 version of aether, so use maven-3.0.3-with-aether-1.12.zip [...]]]></description>
			<content:encoded><![CDATA[<p><strong>TL; DR; version:</strong> Maven 3.0.3 has a performance-regression while resolving dependencies. This is because it uses version 1.11 of aether. The problem has been fixed in version 1.12 of aether, but a version of maven with this library is not available. I built maven from source with the 1.12 version of aether, so use <span style="font-family: courier new; font-weight: bold"><a href="http://vivin.net/pub/maven-3.0.3-with-aether-1.12.zip">maven-3.0.3-with-aether-1.12.zip</a></span> if you have the same problem.</p>
<p><strong>The whole story:</strong> At work we use Maven to build our project. Over the last few months, we started noticing a disparity in build times between different developers even though the hardware they were using was similar to each other (Core i7&#8242;s with 6 Gb RAM). Initially we suspected that it might have to do with SSD performance-degradation. Some of us were using SSD&#8217;s that didn&#8217;t support TRIM and others didn&#8217;t have TRIM enabled. I was one of the few with the former problem. I secure-erased my drive but still saw no performance benefits. I even got a new drive and even with that I didn&#8217;t see much of an improvement either. </p>
<p>We finally realized that the build-time disparity was related to the version of maven some developers were using. On maven 3.0b1, build times are much faster compared to 3.0.3. In 3.0.3 there is a performance regression when maven tries to build the dependency graph. For example, using 3.0b1 our project built in 3 minutes and 30 seconds, whereas using 3.0.3, the build times were upwards of 9 minutes. Long build-times take away from the amount of productive coding-time a developer has.</p>
<p>I was determined to find the reason for this performance regression and did some investigation by looking at the source for 3.0b1 and 3.0.3. In 3.0b1, maven uses its own code to resolve dependencies and build the dependency graph, whereas in 3.0.3 it uses the aether library. For more background information on the matter, take a look at <a href="http://maven.40175.n5.nabble.com/Maven-3-builds-take-much-longer-to-run-in-3-0-3-than-in-3-0b1-td4612863.html">this post</a> on the maven developer&#8217;s mailing list and <a href="https://jira.codehaus.org/browse/MNG-5125">this</a> JIRA issue.</p>
<p>Long story short, to get better build times, maven needs to use version 1.12 of aether. I downloaded the maven source and edited the <span style="font-family: courier new; font-weight: bold">pom.xml</span> file to use version 1.12 of aether. I then built maven from source and got a deployable version that uses the newer aether library. When I tested it out, the build times were comparable to 3.0b1.</p>
<p>I initially thought that version 3.0.4 of maven would include version 1.12 of aether. But it turns out that there were licensing changes and so the maven developers are discussing whether to include it. In the meantime, you can use <a href="http://vivin.net/pub/maven-3.0.3-with-aether-1.12.zip">this version</a> of maven that I built from source, which includes version 1.12 of aether. It&#8217;s a zip file and you can install it like you would normally install maven.</p>
<br /><a href="http://vivin.net/?p=1757#comments" title="Comments on &quot;Fixing Maven 3.0.3&#8242;s dependency-resolution performance-regression&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1757" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1757&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/07/20/fixing-maven-3-0-3s-dependency-resolution-performance-regression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1757" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1757" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1757&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Generating API Documentation from XML using XSLT</title>
		<link>http://vivin.net/2011/07/09/generating-api-documentation-from-xml-using-xslt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/07/09/generating-api-documentation-from-xml-using-xslt/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 18:39:04 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Nerdy Stuff]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XSLT]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[infusionsoft]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1748</guid>
		<description><![CDATA[I work at Infusionsoft, and we offer our customers API access. Visibility and access to the various tables and their fields is controlled by an XML file on our end. Naturally, our customers require user-friendly documentation that tells them what tables and fields they can access and in what manner. Previously, a former developer had [...]]]></description>
			<content:encoded><![CDATA[<p>I work at Infusionsoft, and we offer our customers API access. Visibility and access to the various tables and their fields is controlled by an XML file on our end. Naturally, our customers require user-friendly documentation that tells them what tables and fields they can access and in what manner. Previously, a former developer had written a maven goal that would generate the API documentation from the XML file. Unfortunately this was something that he did on his own and the code wasn&#8217;t in our subversion repository. When that developer left, we decided to take a look at the code to see if we could continue generating the documentation using the maven goal. We determined that the original solution though helpful, involved a lot of work in Java simply to generate API documentation. This was when I suggested using XSLT as it would be a remarkably lightweight solution and it is also perfectly suited to this task. My colleagues agreed and so I decided to go ahead with the task. There was one slight problem though. I had very little experience with XSLT! But how hard could it be? I love learning new things anyway!<br />
<span id="more-1748"></span><br />
As it turned out, it wasn&#8217;t all that hard (this doesn&#8217;t mean I am an expert; I&#8217;m still a novice!). I went through a few tutorials and examples and in about an hour or so I had a basic idea of the language (especially its functional aspect). It took me about a day to write out the entire XSLT document (tweaks and all). Most the time was spent looking up instructions in XSLT to do certain things (string operations, date formatting, etc.).</p>
<p>Here&#8217;s an example of what I had to work with, from the XML document (this is not the exact document; it&#8217;s just an example that follows the structure):</p>
<pre class="brush: xml">
&lt;tables&gt;
 &lt;table name=&quot;Contact&quot; access=&quot;EDAR&quot;&gt;
  &lt;fields&gt;
   &lt;field name=&quot;FirstName&quot; /&gt;
   &lt;field name=&quot;LastName&quot; /&gt;
   ...
   &lt;field name=&quot;LastUpdated&quot; type=&quot;DateTime&quot; access=&quot;R&quot; /&gt;
  &lt;/fields&gt;
 &lt;/table&gt;
&lt;/tables&gt;
</pre>
<p>As you can see, the structure is pretty simple. As far as the resulting API documentation was concerned, what I wanted was an <span style="font-family:courier; font-weight:bold">index.html</span> that contained links to other pages. These other pages should be structured so that there is an HTML page for each table. This page will contain information about the table fields, as well as their access modes (E: Edit, D: Delete, A: Add, R: Read). As it turns out, in XSLT 1.0, you cannot create separate documents. You can only transform one document into another document. In XSLT 2.0 however, you can transform one document into multiple documents using the <span style="font-family:courier; font-weight:bold">xsl:result-document</span> instruction.</p>
<p>The XSLT that I&#8217;ve written is pretty straightforward (if you know XSLT; even if you don&#8217;t, you can kinda get the gist of it from the instructions). The only thing I had trouble with was translating the access-mode letters into their expanded forms. To do this, I had to write a recursive template that looks at each character (there are <a href="http://stackoverflow.com/questions/5628671/xslt-splitting-a-string-and-doing-something-with-each-character">other ways</a>). This is where the functional aspect of XSLT comes into play. Once I started thinking of XSLT like Lisp, it became much easier to write my solution because I wasn&#8217;t fighting the language. Anyway, here is my solution:</p>
<pre class="brush: xslt">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;xsl:stylesheet version=&quot;2.0&quot;
                xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;

 &lt;!--
  XSLT document that transforms Infusionsoft&#039;s API Field-Access XML into HTML pages
  Author: Vivin Suresh Paliath
  Date: 4/11/2011
 --&gt;

 &lt;xsl:output method=&quot;text&quot; /&gt;
 &lt;xsl:output method=&quot;html&quot; indent=&quot;yes&quot; name=&quot;html&quot; /&gt;

 &lt;xsl:template name=&quot;translateAccessModes&quot;&gt;

  &lt;xsl:param name=&quot;accessModes&quot; /&gt;

  &lt;xsl:if test=&quot;string-length($accessModes) &gt; 0&quot;&gt;

   &lt;xsl:variable name=&quot;accessMode&quot; select=&quot;substring($accessModes, 1, 1)&quot; /&gt;
   &lt;xsl:choose&gt;
    &lt;xsl:when test=&quot;$accessMode=&#039;E&#039;&quot;&gt;Edit &lt;/xsl:when&gt;
    &lt;xsl:when test=&quot;$accessMode=&#039;D&#039;&quot;&gt;Delete &lt;/xsl:when&gt;
    &lt;xsl:when test=&quot;$accessMode=&#039;A&#039;&quot;&gt;Add &lt;/xsl:when&gt;
    &lt;xsl:when test=&quot;$accessMode=&#039;R&#039;&quot;&gt;Read &lt;/xsl:when&gt;
    &lt;xsl:otherwise&gt;Unrecognized Access Mode: &lt;xsl:value-of select=&quot;$accessMode&quot; /&gt; &lt;/xsl:otherwise&gt;
   &lt;/xsl:choose&gt;

   &lt;xsl:call-template name=&quot;translateAccessModes&quot;&gt;
    &lt;xsl:with-param name=&quot;accessModes&quot; select=&quot;substring($accessModes, 2, string-length($accessModes))&quot; /&gt;
   &lt;/xsl:call-template&gt;

  &lt;/xsl:if&gt;

 &lt;/xsl:template&gt;

 &lt;xsl:template match=&quot;/&quot;&gt;
  &lt;xsl:result-document href=&quot;tables/index.html&quot; format=&quot;html&quot;&gt;
   &lt;html&gt;
    &lt;xsl:comment&gt; Generated via XSLT on &lt;xsl:value-of select=&quot;format-dateTime(current-dateTime(), &#039;[F], [MNn] [D], [Y] at [h]:[m01] [P]&#039;)&quot; /&gt; &lt;/xsl:comment&gt;
    &lt;head&gt;
     &lt;title&gt;Infusionsoft Table-Access Documentation&lt;/title&gt;
     &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
    &lt;/head&gt;
    &lt;body&gt;
     &lt;h2&gt;Database Tables &amp;amp; Fields&lt;/h2&gt;
     &lt;div id=&quot;tables&quot;&gt;
      &lt;ul&gt;
       &lt;xsl:for-each select=&quot;//table&quot;&gt;
        &lt;xsl:variable name=&quot;filename&quot; select=&quot;concat(&#039;tables/&#039;, @name, &#039;.html&#039;)&quot; /&gt;
        &lt;xsl:variable name=&quot;tableAccessModes&quot;&gt;
         &lt;xsl:choose&gt;
          &lt;xsl:when test=&quot;@access!=&#039;&#039;&quot;&gt;
           &lt;xsl:call-template name=&quot;translateAccessModes&quot;&gt;
            &lt;xsl:with-param name=&quot;accessModes&quot; select=&quot;@access&quot; /&gt;
           &lt;/xsl:call-template&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;Edit Delete Add Read&lt;/xsl:otherwise&gt;
         &lt;/xsl:choose&gt;
        &lt;/xsl:variable&gt;
        &lt;li&gt;
         &lt;xsl:element name=&quot;a&quot;&gt;
          &lt;xsl:attribute name=&quot;href&quot;&gt;&lt;xsl:value-of select=&quot;concat(@name, &#039;.html&#039;)&quot; /&gt;&lt;/xsl:attribute&gt;
          &lt;xsl:value-of select=&quot;@name&quot; /&gt;
         &lt;/xsl:element&gt;
        &lt;/li&gt;

        &lt;xsl:result-document href=&quot;{$filename}&quot; format=&quot;html&quot;&gt;
         &lt;html&gt;
          &lt;xsl:comment&gt; Generated via XSLT on &lt;xsl:value-of select=&quot;format-dateTime(current-dateTime(), &#039;[F], [MNn] [D], [Y] at [h]:[m01] [P]&#039;)&quot; /&gt; &lt;/xsl:comment&gt;
          &lt;head&gt;
           &lt;title&gt;&lt;xsl:value-of select=&quot;@name&quot; /&gt; Table (Access Modes: &lt;xsl:value-of select=&quot;substring($tableAccessModes, 1, string-length($tableAccessModes) - 1)&quot; /&gt;)&lt;/title&gt;
           &lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
          &lt;/head&gt;
          &lt;body&gt;
           &lt;h2&gt;&lt;xsl:value-of select=&quot;@name&quot; /&gt;&lt;/h2&gt;
           &lt;table&gt;
            &lt;tr&gt;
             &lt;th&gt;Field Name&lt;/th&gt;
             &lt;th&gt;Type&lt;/th&gt;
             &lt;th&gt;Access&lt;/th&gt;
            &lt;/tr&gt;

            &lt;xsl:for-each select=&quot;./fields/field&quot;&gt;

             &lt;xsl:variable name=&quot;fieldType&quot;&gt;
              &lt;xsl:choose&gt;
               &lt;xsl:when test=&quot;@type!=&#039;&#039;&quot;&gt;&lt;xsl:value-of select=&quot;@type&quot; /&gt;&lt;/xsl:when&gt;
               &lt;xsl:otherwise&gt;String&lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
             &lt;/xsl:variable&gt;

             &lt;xsl:variable name=&quot;accessModes&quot;&gt;
              &lt;xsl:choose&gt;
               &lt;xsl:when test=&quot;@access!=&#039;&#039;&quot;&gt;
                &lt;xsl:call-template name=&quot;translateAccessModes&quot;&gt;
                 &lt;xsl:with-param name=&quot;accessModes&quot; select=&quot;@access&quot; /&gt;
                &lt;/xsl:call-template&gt;
               &lt;/xsl:when&gt;
               &lt;xsl:otherwise&gt;&lt;xsl:value-of select=&quot;$tableAccessModes&quot; /&gt;&lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
             &lt;/xsl:variable&gt;

             &lt;xsl:variable name=&quot;rowClass&quot;&gt;
              &lt;xsl:choose&gt;
               &lt;xsl:when test=&quot;(position() mod 2) = 0&quot;&gt;even&lt;/xsl:when&gt;
               &lt;xsl:otherwise&gt;odd&lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
             &lt;/xsl:variable&gt;

             &lt;xsl:element name=&quot;tr&quot;&gt;
              &lt;xsl:attribute name=&quot;class&quot;&gt;&lt;xsl:value-of select=&quot;$rowClass&quot; /&gt;&lt;/xsl:attribute&gt;
              &lt;td&gt;&lt;xsl:value-of select=&quot;@name&quot; /&gt;&lt;/td&gt;
              &lt;td&gt;&lt;xsl:value-of select=&quot;$fieldType&quot; /&gt;&lt;/td&gt;
              &lt;td&gt;&lt;xsl:value-of select=&quot;$accessModes&quot; /&gt;&lt;/td&gt;
             &lt;/xsl:element&gt;
            &lt;/xsl:for-each&gt;

           &lt;/table&gt;

           &lt;h2&gt;&lt;a href = &quot;index.html&quot;&gt;Back to list of tables&lt;/a&gt;&lt;/h2&gt;

          &lt;/body&gt;
         &lt;/html&gt;
        &lt;/xsl:result-document&gt;

       &lt;/xsl:for-each&gt;
      &lt;/ul&gt;
     &lt;/div&gt;
    &lt;/body&gt;
   &lt;/html&gt;
  &lt;/xsl:result-document&gt;
 &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p>That&#8217;s all there is to it! To actually use the XSLT document to generate the API HTML files, I used <a href="http://saxon.sourceforge.net/saxon6.5.4/">Saxon</a>, which supports XSLT 2.0. This is how I used it:</p>
<pre class="brush: php">
java -cp saxon9he.jar net.sf.saxon.Transform -t -s:api_field_access.xml -xsl:api_doc.xslt
</pre>
<p>So if you&#8217;re an Infusionsoft customer or a developer who is reading the API documentation, you now know how it was generated!</p>
<br /><a href="http://vivin.net/?p=1748#comments" title="Comments on &quot;Generating API Documentation from XML using XSLT&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1748" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1748&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/07/09/generating-api-documentation-from-xml-using-xslt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1748" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1748" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1748&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Implementing JSONP in Spring MVC 3.0.x</title>
		<link>http://vivin.net/2011/07/01/implementing-jsonp-in-spring-mvc-3-0-x/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/07/01/implementing-jsonp-in-spring-mvc-3-0-x/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 16:33:22 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Nerdy Stuff]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring 3]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1732</guid>
		<description><![CDATA[In Spring 3, it&#8217;s very easy to get a view to return JSON. However, there is no built-in mechanism to return JSONP. I was able to find a pretty good tutorial That uses Spring&#8217;s DelegatingFilterProxy. I implemented this solution and I got it to work, but I didn&#8217;t like the fact that I had to [...]]]></description>
			<content:encoded><![CDATA[<p>In Spring 3, it&#8217;s very easy to get a view to return JSON. However, there is no built-in mechanism to return <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a>. I was able to find a <a href="http://jpgmr.wordpress.com/2010/07/28/tutorial-implementing-a-servlet-filter-for-jsonp-callback-with-springs-delegatingfilterproxy/">pretty good tutorial</a> That uses Spring&#8217;s <span style="font-family: courier new; font-weight: bold">DelegatingFilterProxy</span>. I implemented this solution and I got it to work, but I didn&#8217;t like the fact that I had to create a separate filter and a bunch of other classes just for that. I also wanted to use a specific extension (<span style = "font-family: courier new; font-weight: bold">.jsonp</span>) for JSONP just to make it more explicit. Spring uses <span style="font-family: courier new; font-weight: bold">MappingJacksonJsonView</span> to return JSON. I figured that I could extend this view and have it return JSONP instead of JSON.<br />
<span id="more-1732"></span></p>
<p>Although I pulled my hair out for an hour or two (I was getting a 404 when hitting my controller with with a <span style="font-family: courier new; font-weight: bold">.jsonp</span> extension; I&#8217;ll explain why, later), the resulting solution is actually pretty simple. First, you need to extend <span style="font-family: courier new; font-weight: bold">MappingJacksonJsonView</span> to create your own view. Since <span style="font-family: courier new; font-weight: bold">MappingJacksonJsonView</span> already handles the creation of JSON, all we need to do is pad it with our callback. The way to do this is to override the <span style="font-family: courier new; font-weight: bold">MappingJacksonJsonView#render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)</span> method (This method is actually defined in <span style="font-family: courier new; font-weight: bold">AbstractView</span>, a Spring-provided abstract class that you can override in your own view). The full source is below:</p>
<pre class="brush: java">
package net.vivin.mvc.spring.view.jsonp;

import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class MappingJacksonJsonpView extends MappingJacksonJsonView {

	/**
	 * Default content type. Overridable as bean property.
	 */
	public static final String DEFAULT_CONTENT_TYPE = &quot;application/javascript&quot;;

    @Override
    public String getContentType() {
        return DEFAULT_CONTENT_TYPE;
    }

 	/**
	 * Prepares the view given the specified model, merging it with static
	 * attributes and a RequestContext attribute, if necessary.
	 * Delegates to renderMergedOutputModel for the actual rendering.
	 * @see #renderMergedOutputModel
	 */
    @Override
	public void render(Map&lt;String, ?&gt; model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        if(&quot;GET&quot;.equals(request.getMethod().toUpperCase())) {
            @SuppressWarnings(&quot;unchecked&quot;)
            Map&lt;String, String[]&gt; params = request.getParameterMap();

            if(params.containsKey(&quot;callback&quot;)) {
                response.getOutputStream().write(new String(params.get(&quot;callback&quot;)[0] + &quot;(&quot;).getBytes());
                super.render(model, request, response);
                response.getOutputStream().write(new String(&quot;);&quot;).getBytes());
                response.setContentType(&quot;application/javascript&quot;);
            }

            else {
                super.render(model, request, response);
            }
        }

        else {
            super.render(model, request, response);
        }
    }
}
</pre>
<p>The <span style="font-family: courier new; font-weight: bold">DEFAULT_CONTENT_TYPE</span> public static property and the <span style="font-family: courier new; font-weight: bold">getContentType()</span> method are important. Spring uses this method (among other things) to find the best candidate view. I spent at least an hour trying to figure out why I was getting 404&#8242;s. It was because I hadn&#8217;t overridden the property and the accessor to return the correct content-type. Going back to the <span style="font-family: courier new; font-weight: bold">render</span> method itself, you can see that it&#8217;s pretty simple. First I make sure that the request method is <span style="font-family: courier new; font-weight: bold">GET</span>. Then I get the request parameter-map and check for the <span style="font-family: courier new; font-weight: bold">callback</span> parameter. If this parameter exists, I write out the name of the callback with an open parenthesis. Then I call the parent class&#8217;s <span style="font-family: courier new; font-weight: bold">render</span> method. This method belongs to <span style="font-family: courier new; font-weight: bold">MappingJacksonJsonView</span> and will write out JSON to the response. Finally, I write out a closing parenthesis and a semicolon to the response and set the content type to <span style="font-family: courier new; font-weight: bold">application/javascript</span>. So as you can see, I&#8217;ve converted JSON into JSONP.</p>
<p>The next thing you need to do is include an entry for your new view in the <span style="font-family: courier new; font-weight: bold">springmvc-servlet.xml</span> file. This part also took a little work. I wasn&#8217;t sure how to include my new view within the <span style="font-family: courier new; font-weight: bold">ContentNegotiatingViewResolver</span> bean. But <a href="http://www.rickherrick.com/?q=node/63">this post</a> proved to be remarkably helpful. The post actually talks about returning XML or JSON (based on the extension), but I was able to adapt it to return JSON or JSON. Here&#8217;s what it looks like:</p>
<pre class="brush: xml">
    &lt;bean class=&quot;org.springframework.web.servlet.view.ContentNegotiatingViewResolver&quot;&gt;
        &lt;property name=&quot;favorPathExtension&quot; value=&quot;true&quot;/&gt;
        &lt;property name=&quot;mediaTypes&quot;&gt;
            &lt;map&gt;
                &lt;entry key=&quot;json&quot; value=&quot;application/json&quot;/&gt;
                &lt;entry key=&quot;jsonp&quot; value=&quot;application/javascript&quot;/&gt;
            &lt;/map&gt;
        &lt;/property&gt;
        &lt;property name=&quot;defaultViews&quot;&gt;
            &lt;list&gt;
                &lt;bean class=&quot;org.springframework.web.servlet.view.json.MappingJacksonJsonView&quot;/&gt;
                &lt;bean class=&quot;net.vivin.mvc.spring.view.jsonp.MappingJacksonJsonpView&quot;/&gt;
            &lt;/list&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
</pre>
<p>If you notice, the <span style="font-family: courier new; font-weight: bold">value</span> property for <span style="font-family: courier new; font-weight: bold">jsonp</span> has been set to <span style="font-family: courier new; font-weight: bold">application/javascript</span>. Spring uses this value and checks it against the view class to figure out the best candidate view. If you didn&#8217;t override the <span style="font-family: courier new; font-weight: bold">DEFAULT_CONTENT_TYPE</span> property and the associated getter, you would get a 404.</p>
<p>Finally, you need a <span style="font-family: courier new; font-weight: bold">url-pattern</span> entry under <span style="font-family: courier new; font-weight: bold">servlet-mapping</span> in your <span style="font-family: courier new; font-weight: bold">web.xml</span> file so that the new <span style="font-family: courier new; font-weight: bold">.jsonp</span> extension is recognized:</p>
<pre class="brush: xml">
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;springmvc&lt;/servlet-name&gt;
        &lt;url-pattern&gt;*.json&lt;/url-pattern&gt;
        &lt;url-pattern&gt;*.jsonp&lt;/url-pattern&gt;
        ...
    &lt;/servlet-mapping&gt;
</pre>
<p>That&#8217;s pretty much it. With this code in place, you can hit your controller with <span style="font-family: courier new; font-weight: bold">http://my.domain.com/controllerName/actionName.jsonp?callback=myCallback</span> and you&#8217;ll get your data back as JSONP! I thought this solution was pretty simple and a little less invasive than creating a filter. Comments/suggestions/criticisms for improvement are welcome!</p>
<br /><a href="http://vivin.net/?p=1732#comments" title="Comments on &quot;Implementing JSONP in Spring MVC 3.0.x&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1732" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1732&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/07/01/implementing-jsonp-in-spring-mvc-3-0-x/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1732" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1732" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1732&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Integrating Regula with Spring 3.0.x MVC</title>
		<link>http://vivin.net/2011/02/21/integrating-regula-with-spring-3-0-x-mvc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/02/21/integrating-regula-with-spring-3-0-x-mvc/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 01:50:20 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[annotation-based form-validation]]></category>
		<category><![CDATA[annotation-based validation]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[bean-validation]]></category>
		<category><![CDATA[form-validation]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript form-validation]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[regula]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1680</guid>
		<description><![CDATA[A little less than a year ago, I released Regula, an annotation-based form-validation written in Javascript. The source and documentation are available on GitHub. I started working on the integration on and off throughout most of last year. At the end of the year, I had a pretty good integration going, where you could annotate [...]]]></description>
			<content:encoded><![CDATA[<p>A little less than a year ago, I <a href="http://vivin.net/2011/03/30/regula-an-annotation-based-form-validator-written-in-javascript/">released</a> Regula, an annotation-based form-validation written in Javascript. The <a href="https://github.com/vivin/regula">source</a> and <a href="https://github.com/vivin/regula/wiki">documentation</a> are available on GitHub. I started working on the integration on and off throughout most of last year. At the end of the year, I had a pretty good integration going, where you could annotate fields with Hibernate Validator annotations, and the corresponding Regula validation-code would be generated on the client side. Of course, I wasn&#8217;t done yet because what I had was simply a demo project and I had to figure out a good way to distribute the whole thing; I was able to finish up the packaging and distribution today. With minimal setup, you should be able to get started with Regula and Spring. You don&#8217;t need to go through this post to figure out how to use the integration. This post is mostly about <i>how</i> I accomplished the integration (I don&#8217;t go into all the details; just the important bits). As far as actually using it, I will make a blog post about it later.</p>
<p>The <a href="https://github.com/vivin/regula-spring">source</a> for the integration is also hosted on GitHub. My approach towards translating validation constraints from the server-side to the client-side was two-fold: gather validation constraints from the object and represent it in a canonical form. Using the canonical form, generate Javascript code that uses Regula for validation. To do this, I created a service that examines a domain object and gathers all information regarding its properties and validation constraints. The service returns this information in a canonical form, that I then inserted into the model. On the client-side, I had a tag that used the canonical form and outputted Javascript that uses the Regula framework. Initially, I was calling the service explicitly from an action in the controller. Later, in an effort to make the integration less-invasive and more seamless, I used an aspect-oriented approach with interceptors. In fact, that&#8217;s where I&#8217;d like to start.<br />
<span id="more-1680"></span><br />
I created an annotation called <span style="font-family:courier">@ValidateClientSide</span> that marks a domain object as requiring client-side validation. Then, I created an interceptor called <span style="font-family:courier">ClientSideValidationInterceptor</span> that will call the validation service and gather validation information:</p>
<br /><a href="http://vivin.net/?p=1680#comments" title="Comments on &quot;Integrating Regula with Spring 3.0.x MVC&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1680" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1680&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/02/21/integrating-regula-with-spring-3-0-x-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1680" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1680" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1680&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Packaging and distributing taglibs in a JAR</title>
		<link>http://vivin.net/2011/02/21/packaging-and-distributing-taglibs-in-a-jar/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/02/21/packaging-and-distributing-taglibs-in-a-jar/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 21:23:35 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[custom taglibs]]></category>
		<category><![CDATA[custom tags]]></category>
		<category><![CDATA[distributing]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jstl]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[packing]]></category>
		<category><![CDATA[taglibs]]></category>
		<category><![CDATA[tags]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1673</guid>
		<description><![CDATA[This is more of a &#8220;note to self&#8221; than a &#8220;how to&#8221;. If you&#8217;re trying to distribute tag files in a JAR, you need to put them under /META-INF/tags. You then need to create a TLD file that you also put under /META-INF/tags. If you have tags or functions that you created in Java, and [...]]]></description>
			<content:encoded><![CDATA[<p>This is more of a &#8220;note to self&#8221; than a &#8220;how to&#8221;.</p>
<p>If you&#8217;re trying to distribute tag files in a JAR, you need to put them under <span style="font-family:courier">/META-INF/tags</span>. You then need to create a TLD file that you also put under <span style="font-family:courier">/META-INF/tags</span>. If you have tags or functions that you created in Java, and want to distribute them alongside the tag files, you need to reference them in the TLD and package them in the same JAR (goes without saying).</p>
<p>If you want to do the same thing in maven, the location for the tag files and the tld file is different; you need to put them in <span style="font-family:courier">src/main/resources/META-INF/tags</span>. Then you can run <span style="font-family:courier">mvn package</span> and maven will create a JAR with your tags.<br />
<span id="more-1673"></span><br />
My directory structure in my maven project looks like this:</p>
<pre>
src
`-- main
    |-- java
    |   `-- net
    |       `-- vivin
    |           `-- regula
    |               `-- validation
    |                   `-- util
    |                       `-- TagStringUtils.java
    `-- resources
        `-- META-INF
            `-- tags
                |-- generate-validation.tag
                `-- regula-spring.tld
</pre>
<p>And my TLD file looks like this:</p>
<pre class="brush: xml">
&lt;taglib&gt;
 &lt;tlib-version&gt;1.0&lt;/tlib-version&gt;
 &lt;short-name&gt;regula-spring&lt;/short-name&gt;
 &lt;uri&gt;http://regula-spring&lt;/uri&gt;
 &lt;jsp-version&gt;2.0&lt;/jsp-version&gt;

 &lt;tag-file&gt;
   &lt;name&gt;generate-validation&lt;/name&gt;
   &lt;path&gt;/META-INF/tags/generate-validation.tag&lt;/path&gt;
 &lt;/tag-file&gt;

 &lt;function&gt;
   &lt;name&gt;matches&lt;/name&gt;
   &lt;function-class&gt;net.vivin.regula.validation.util.TagStringUtils&lt;/function-class&gt;
   &lt;function-signature&gt;boolean matches(java.lang.String, java.lang.String, java.lang.String)&lt;/function-signature&gt;
 &lt;/function&gt;

 &lt;function&gt;
   &lt;name&gt;replaceUsingRegex&lt;/name&gt;
   &lt;function-class&gt;net.vivin.regula.validation.util.TagStringUtils&lt;/function-class&gt;
   &lt;function-signature&gt;java.lang.String replaceUsingRegex(java.lang.String, java.lang.String, java.lang.String, java.lang.String)&lt;/function-signature&gt;
 &lt;/function&gt;
&lt;/taglib&gt;
</pre>
<br /><a href="http://vivin.net/?p=1673#comments" title="Comments on &quot;Packaging and distributing taglibs in a JAR&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1673" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1673&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/02/21/packaging-and-distributing-taglibs-in-a-jar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1673" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1673" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1673&#38;type=feed" medium="image" />
	</item>
	</channel>
</rss>

