<?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>Jabbering Giraffe &#187; http</title>
	<atom:link href="http://happygiraffe.net/blog/tag/http/feed/" rel="self" type="application/rss+xml" />
	<link>http://happygiraffe.net/blog</link>
	<description></description>
	<lastBuildDate>Wed, 19 Oct 2011 10:40:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>REST Web Services in Java</title>
		<link>http://happygiraffe.net/blog/2008/09/13/rest-web-services-in-java/</link>
		<comments>http://happygiraffe.net/blog/2008/09/13/rest-web-services-in-java/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 21:43:43 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[rest]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1383</guid>
		<description><![CDATA[I&#8217;ve been taking a look at jersey today. It&#8217;s an implementation of JSR 311, which is a proposed spec for implementing REST-like web services in Java. I started with the tutorial, which is quite frankly pretty bloody simple. @Path&#40;&#34;/helloworld&#34;&#41; public &#8230; <a href="http://happygiraffe.net/blog/2008/09/13/rest-web-services-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been taking a look at <a href="https://jersey.dev.java.net/">jersey</a> today.  It&#8217;s an implementation of <a href="https://jsr311.dev.java.net/">JSR 311</a>, which is a proposed spec for implementing REST-like web services in Java.  I started with the <a href="https://jersey.dev.java.net/use/getting-started.html">tutorial</a>, which is quite frankly pretty bloody simple.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/helloworld&quot;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldResource <span style="color: #009900;">&#123;</span>
    @GET
    @Produces<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/plain&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getClichedMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The javadoc for the annotations explains what they do in some more detail (<a href="https://jsr311.dev.java.net/nonav/releases/0.9/javax/ws/rs/Path.html">Path</a>, <a href="https://jsr311.dev.java.net/nonav/releases/0.9/javax/ws/rs/GET.html">GET</a>, <a href="https://jsr311.dev.java.net/nonav/releases/0.9/javax/ws/rs/Produces.html">Produces</a>).</p>
<p>The tutorial also includes a method of running that standalone using the <a href="https://grizzly.dev.java.net/">grizzly HTTP connector</a>.  But I&#8217;m a stick-in-the-mud.  I want it to run under <a href="http://tomcat.apache.org/tomcat-6.0-doc/index.html">tomcat</a>.  So how do I do that?  Again, it turns out to be fairly simple.  Just add the jersey-supplied integration servlet to your <code>web.xml</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Dispatcher<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.sun.jersey.spi.container.servlet.ServletContainer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.sun.jersey.config.property.packages<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>net.happygiraffe.jerseytest<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/init-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Dispatcher<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I specified an extra parameter to say which package I want scanned for resources.  Otherwise Jersey will scan everything in <code>/WEB-INF/classes</code> and <code>/WEB-INF/lib</code> by default.  Anyway, this works a treat:</p>
<pre>
% curl -i http://localhost:8080/jerseytest/hello
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Transfer-Encoding: chunked
Date: Sat, 13 Sep 2008 20:44:41 GMT

hello world
</pre>
<p>I&#8217;m going to play with this some more.  The API feels quite nice.  Particularly when compared to <a href="http://www.restlet.org/">Restlets</a>, which I found a little bit <em>too</em> uniform.  Mind you, the <a href="http://blog.noelios.com/2008/04/01/restlet-11-m3-released/">latest restlet code has its own version of JSR 311 support</a>, so it may still be useful.  It&#8217;s also actually worth checking out the <a href="https://jsr311.dev.java.net/drafts/spec20080827.pdf">JSR-311 spec</a>, as it&#8217;s quite readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/09/13/rest-web-services-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Varnish</title>
		<link>http://happygiraffe.net/blog/2006/10/03/varnish/</link>
		<comments>http://happygiraffe.net/blog/2006/10/03/varnish/#comments</comments>
		<pubDate>Tue, 03 Oct 2006 10:11:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[http]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2006/10/03/varnish/</guid>
		<description><![CDATA[I listened to an interview about Varnish this morning. It was on the excellent bsdtalk podcast. Varnish is an attempt to write a high performance HTTP accelerator (reverse proxy). It grew out of frustrations with the performance of squid. Because &#8230; <a href="http://happygiraffe.net/blog/2006/10/03/varnish/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I listened to an interview about <a href="http://varnish-cache.org/">Varnish</a> this morning.  It was on the excellent <a href="http://bsdtalk.blogspot.com/2006/10/bsdtalk072-interview-with-poul-henning.html">bsdtalk</a> podcast.  Varnish is an attempt to write a high performance <span class="caps">HTTP</span> accelerator (reverse proxy).  It grew out of frustrations with the performance of <a href="http://www.squid-cache.org/">squid</a>.  Because it&#8217;s focussing on a much smaller problem space, it&#8217;s much simpler to use.  Anybody who&#8217;s wrestled with squid config files before will sympathize.</p>
<p>It was primarily written by <a href="http://people.freebsd.org/~phk/">phk</a>, one of the major FreeBSD contributors.</p>
<p>But what I found really interesting was the little nugget about the configuration of Varnish.  They wanted the configuration to be a <acronym title="Domain Specific Language">DSL</acronym>, so you can easily do things like make caching decisions based on <span class="caps">HTTP</span> headers and <span class="caps">URL</span> matching and so on.  But the implementation surprised me: they compile the config file to C, compile it to a shared library and then load it into the Varnish process.  So it&#8217;s damned fast, as you don&#8217;t have the overhead of interpreting bytecodes like you would with embedded Ruby or Perl.</p>
<p>It still makes me a little nervous about the availability of compilers on a production server (it&#8217;s generally considered bad security practice).  But you can always compile on an identical machine elsewhere.</p>
<p>Overall, it seems like an interesting tool for helping web sites to perform.  I&#8217;ll try to give it some proper attention as soon as I get a chance.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2006/10/03/varnish/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PUTting an URL</title>
		<link>http://happygiraffe.net/blog/2006/02/09/putting-an-url/</link>
		<comments>http://happygiraffe.net/blog/2006/02/09/putting-an-url/#comments</comments>
		<pubDate>Thu, 09 Feb 2006 22:20:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2006/02/09/putting-an-url/</guid>
		<description><![CDATA[Today, somebody at work wanted to PUT some data to an URL. It&#8217;s part of an internal web service we use. Should be easy write. In Perl, it&#8217;s pretty trivial. my $ua = LWP::UserAgent-&#62;new(); my $req = PUT 'http://example.com/dest', Content &#8230; <a href="http://happygiraffe.net/blog/2006/02/09/putting-an-url/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today, somebody at work wanted to <span class="caps">PUT</span> some data to an <span class="caps">URL</span>.  It&#8217;s part of an internal web service we use.  Should be easy write.  In Perl, it&#8217;s pretty trivial.</p>
<pre>
my $ua = LWP::UserAgent-&gt;new();
my $req = PUT 'http://example.com/dest', Content =&gt; $ARGV[0];
my $resp = $ua-&gt;request( $req );
die $resp-&gt;status_line, "\n" unless $resp-&gt;is_success;
</pre>
<p>That takes the first command line argument and PUTs it <code>http://example.com/dest</code>.  Pretty simple stuff.  But compare this to the Java way, which is what my colleague was attempting.</p>
<pre>
public class HttpPutTest {
    public static void main(String[] args) throws IOException {
        URL dest = new URL("http://example.com/dest");
        HttpURLConnection conn = (HttpURLConnection) dest.openConnection();
        conn.setRequestMethod("PUT");
        conn.setDoOutput(true);
        conn.getOutputStream().write(args[0].getBytes());
        conn.connect();
        if (conn.getResponseCode() &lt; 200 || conn.getResponseCode() &gt; 299)
            throw new IOException(conn.getResponseMessage());
    }
}
</pre>
<p>Firstly, as with all Java, the sheer verbosity is <em>staggering</em>.  But there are two things of interest to me here.</p>
<ol>
<li>The request and response are conflated into one object, an UrlConnection (or variation thereof).  There&#8217;s no way to retrieve header values on the request once they&#8217;ve been set.</li>
<li>Setting the content is done through the most contorted route.  Firstly, we have to tell the connection that it&#8217;s to expect output (why? I&#8217;m doing output things below, it should work it out).  Secondly, we have to fetch an OutputStream, marshal our input into bytes and then write those bytes to the stream.</li>
</ol>
<p>It&#8217;s that OutputStream that gets me.  Oh, I can see why you&#8217;d need it occasionally.  But 90% of the time when you&#8217;re doing a <span class="caps">PUT</span> or a <span class="caps">POST</span>, the amount of data is tiny, and you have it all up front.  So you don&#8217;t <em>need</em> to stream it to the server.  You&#8217;re paying the price for it 100% of the time, even though it&#8217;s only needed 1% of the time, if that.  Really, how hard would a <code>setContent()</code> method be?</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2006/02/09/putting-an-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

