<?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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>willnorris.com &#187; java</title>
	<atom:link href="http://willnorris.com/tag/java/feed" rel="self" type="application/rss+xml" />
	<link>http://willnorris.com</link>
	<description>there&#039;s more to life than this</description>
	<lastBuildDate>Tue, 15 May 2012 21:57:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-beta3-20574</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Java OpenID Library - Target Audience</title>
		<link>http://willnorris.com/2009/12/java-openid-library-target-audience</link>
		<comments>http://willnorris.com/2009/12/java-openid-library-target-audience#comments</comments>
		<pubDate>Thu, 24 Dec 2009 20:02:48 +0000</pubDate>
		<dc:creator>Will Norris</dc:creator>
				<category><![CDATA[identity]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[openid]]></category>
		<category><![CDATA[shibboleth]]></category>

		<guid isPermaLink="false">http://willnorris.com/?p=895</guid>
		<description><![CDATA[One of the decisions that has to be made, or at least considered, early in the design of any software project is identifying your target audience. This is especially true of libraries that are designed to be integrated into other applications. Who do you expect to be using this library, and how do you expect [...]]]></description>
			<content:encoded><![CDATA[<p>One of the decisions that has to be made, or at least considered, early in the design of any software project is identifying your target audience.  This is especially true of libraries that are designed to be integrated into other applications.  Who do you expect to be using this library, and how do you expect them to make use of it?  Is it something like <a href="http://logging.apache.org/log4j/">log4j</a> that can be dropped into place and used with just a few lines of additional code?  Or is it something that is intended to be integrated into a larger system, requiring the developer using the library to provide additional logic and business rules to get things working?  Something that might require a non-trivial amount of effort, depending on the needs of the use-case.  There is no right or wrong answer, and oftentimes it&#8217;s somewhere in between, but it&#8217;s something that must be considered.</p>

<p>Some of the best software libraries I&#8217;ve used address both ends of the spectrum.  There&#8217;s a common adage in software development (and I&#8217;m sure it goes back farther than that): &#8220;make the common things easy, and the hard things possible&#8221;.  First, you don&#8217;t want to make things any harder than necessary for the majority of users that are just using the basic functionality of a library.  If they don&#8217;t care about customizing and tweaking every little aspect of it, then the way they interact with the library should be relatively simple and straightforward.  But for those users that have unique needs, the library should allow them to configure it in such a way to accommodate that.  It is certainly my goal to address both extremes in the Shibboleth OpenID library, but it will happen in phases.</p>

<p>The first phase will address the edge-cases, those users of the library that tend to have unique needs and requirements.  That may seem backwards, but I assure you it isn&#8217;t.  First of all, the really practical reason for starting here is that Shibboleth is itself an edge case.  The reason I chose not to use the existing Java OpenID libraries in the first place was that they didn&#8217;t adequately conform to the way Shibboleth needed them to work.  But from a design perspective, I&#8217;ve found that this approach tends to yield better results anyway.</p>

<h2>Small Pieces Loosely Coupled</h2>

<p>I&#8217;ve learned that in order to make a system really flexible and modular, it&#8217;s best to architect it that way from the very beginning.  You have to decide where the logical divisions of labor are within the system, and then translate that into the code itself.  Each component should be relatively self-contained, it&#8217;s purpose should be clear, and its interface (the way it interacts with other components) should be separated from its actual implementation.  Sometimes these components are obvious, and there are clear places where the code should be divided.  But more often than not, its a judgement call.  Architecture is often harder than actual construction, whether you&#8217;re talking about software or brick and mortar.  It requires a lot of creativity because you&#8217;re often working from a blank canvas, but it also requires that your plans are grounded in what is actually possible.  Architectural plans are worthless if they can&#8217;t actually be implemented in the real world in a practical way.  By no means do I think that I&#8217;ve found the best architecture for this library, because it&#8217;s always subjective.  Fortunately, I&#8217;ve had <a href="http://opensaml.org/">similar libraries</a> that I&#8217;ve borrowed from heavily for inspiration, as well as much smarter developers that I work with to bounce ideas off of.</p>

<p>At its core, this library is an OpenID messaging library.  It is capable of converting between generic HTTP messages and strongly typed OpenID objects that developers can work with.  My last two posts have talked about this in detail.  The library also provides the additional logic for implementing the OpenID specification, things like Diffie Hellman key exchange and OpenID message signing.  What the library does <strong>not</strong> do is tell you how you must compose these pieces into a working system.  That&#8217;s because there isn&#8217;t just one way to do it.  It greatly depends on the application that is wanting to add OpenID support.  If you&#8217;re using a Java framework like <a href="http://tapestry.apache.org/">Tapestry</a> or <a href="http://java.sun.com/javaee/javaserverfaces/">JSF</a>, perhaps you have other processing that happens to the message before the OpenID library gets involved.  How does the user get authenticated and where do the user attributes come from?  I have no idea&#8230; that&#8217;s up to your application to decide.  At this level, the library makes no assumptions (or at least as few as possible) about how all of these small pieces should be coupled together.</p>

<p>If this sounds like a lot of work left up to the user just for something as simple as OpenID, you&#8217;re right&#8230; it is a lot of work.  But when you really need that level of control, it&#8217;s important that the library support that.</p>

<h2>Addressing the Common Case</h2>

<p>So what about everyone else, all the &#8220;mere mortals&#8221; who <em>don&#8217;t</em> need that much control, and just want to add OpenID support to some application using the default configuration?  At a high level, I&#8217;d love to have a Servlet Filter that you can drop in front of your application, configure a few small things, and have it <strong>just work</strong> as an OpenID relying party.  I&#8217;ve always been a huge fan of the Tapestry framework, so I&#8217;d love to have a Tapestry component that can be dropped in just as easily.  All of these things are possible by building a layer that sits on top of those individual components in the library, and arranges them in a prescribed way.</p>

<p>Now, I don&#8217;t anticipate that a drop-in Servlet Filter will ever be a part of the core library, because I don&#8217;t think it belongs there.  It would be a separate deliverable unto itself that simply relies on the library to do all of OpenID work.  This also means that the Filter wouldn&#8217;t necessarily need to come from me, anyone could write it and make it available.  I don&#8217;t imagine that the core library itself will ever have everything that the &#8220;common case&#8221; users will need.  And I&#8217;m okay with that, because I&#8217;m not building an OpenID product, I&#8217;m building an OpenID library.</p>

<h2>Current Status</h2>

<p>This is by no means a complete picture of the Shibboleth OpenID library, but it should give you a rough idea.  It identifies some of the larger components of the libraries, and some of the interdependencies.</p>

<p><a href="http://www.flickr.com/photos/wnorris/4211756452/"><img src="http://farm3.static.flickr.com/2518/4211756452_3e7d278f3a.jpg" alt="OpenID Java Software Stack (v1)" width="364" height="256" /></a></p>

<p>The orange blocks are pieces that are basically complete and present in the current library.  All of the message handling is complete for OpenID 2.0 message, as well as three of the most popular message extensions (SReg, AX, and PAPE).  Additionally, association management is done, and a very simple AssociationStore is provided (though it needs a little improvement).  The security layer is complete insofar as signing and verifying signed messages.  The yellow blocks represent pieces that are not yet complete, but will be included in the core library in the future.  The discovery component is still up in the air a little bit because it&#8217;s not completely clear if we&#8217;ll be using XRD, XRDS, or both.  The portions of the security layer that depend on discovery are, of course, waiting on the completion of the discovery stack.  Those pieces include everything that an application would need to construct an OpenID provider or relying party.  They implement the full OpenID protocol.</p>

<p>But those components alone leave a lot to be filled in by the application using the library.  It says nothing about how an incoming HttpServletRequest object is converted into an OpenID Message.  The application would be responsible for instantiating the specific objects and wiring them together to actually get a working AssociationManager.  And for applications that wish to have control over these specific aspects of an OpenID flow, this is a good thing.  The next layer up on the stack however, the yellow &#8220;Managers&#8221; block, will provide simple Manager objects that wire things together in a prescribed way.  Most users of the library will deal with the Manager layer, and probably nothing else.  Only when they have specific needs will it be necessary to dig any deeper.</p>

<p>Now this last layer is actually nothing special&#8230; it&#8217;s a very common pattern, and both <a href="http://openid4java.org/">OpenID4Java</a> and <a href="http://code.google.com/p/joid/">Joid</a> work in very similar ways.  I point it out only to note that while this layer will be part of the core library in a future release, it isn&#8217;t right now.  Much of the code that will likely make up these components has already been written, but in the form of the <a href="https://spaces.internet2.edu/display/SHIB2/IdP+OpenID">Shibboleth IdP extension</a>.  For the last few months I&#8217;ve been simultaneously building both a generic OpenID library, as well as an actual product that makes use of the library.   One of the tougher ongoing challenges while doing this is in deciding which of the two projects a particular bit of code goes into.  Much of the time it&#8217;s clear, but when in doubt, I&#8217;ll put something into the Shibboleth extension rather than the library.  If anything, I want to err on the side of keeping the library &#8220;pure&#8221; so to speak.  To not accidentally bake any assumptions into the library itself that might limit its flexibility.  One of my focuses after the holidays will be in identifying which pieces need to be refactored from the Shibboleth extension back into the core library in order to build out that management layer.</p>

<p>(And in case it&#8217;s not clear, the final layer in grey in the stack above are other pieces that will make use of the OpenID library, but will likely not be part of the library itself.)</p>
]]></content:encoded>
			<wfw:commentRss>http://willnorris.com/2009/12/java-openid-library-target-audience/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java OpenID Library - Configuration and Custom Messages</title>
		<link>http://willnorris.com/2009/11/java-openid-library-configuration-and-custom-messages</link>
		<comments>http://willnorris.com/2009/11/java-openid-library-configuration-and-custom-messages#comments</comments>
		<pubDate>Mon, 16 Nov 2009 04:34:00 +0000</pubDate>
		<dc:creator>Will Norris</dc:creator>
				<category><![CDATA[identity]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[internet2]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[openid]]></category>

		<guid isPermaLink="false">http://willnorris.com/?p=893</guid>
		<description><![CDATA[I previously described how message handling works in the Internet2 OpenID library, and how each OpenID message type requires a half dozen or so classes to handle everything. While this may seem like overkill to some, one of the nice things about this separation of logic is that it makes it quite simple to provide [...]]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://willnorris.com/2009/11/java-openid-library-design-message-handling">previously described</a> how message handling works in the Internet2 OpenID library, and how each OpenID message type requires a half dozen or so classes to handle everything.  While this may seem like overkill to some, one of the nice things about this separation of logic is that it makes it quite simple to provide custom implementations of specific kinds of messages.  While this was not specifically a core requirement of the library, it was an added bonus of the design, and just seemed like a good thing to support.  I want to talk about it here, because it illustrates how this portion of the library is configured, which will be important to understand later.</p>

<h2>Central Registry</h2>

<p>As we mentioned, every OpenID message type has a number of supporting classes.  Let&#8217;s take the authentication request message as an example.  You have:</p>

<ul>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/AuthenticationRequest.java?view=markup">AuthenticationRequest</a> &#8212; interface that defines this specific OpenID message type</li>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/impl/AuthenticationRequestImpl.java?view=markup">AuthenticationRequestImpl</a> &#8212; default implementation of this message type</li>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/impl/AuthenticationRequestBuilder.java?view=markup">AuthenticationRequestBuilder</a> &#8212; builder of AuthenticationRequest instances</li>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/impl/AuthenticationRequestMarshaller.java?view=markup">AuthenticationRequestMarshaller</a> &#8212; converts AuthenticationRequest message into a ParameterMap</li>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/impl/AuthenticationRequestUnmarshaller.java?view=markup">AuthenticationRequestUnmarshaller</a> &#8212; converts ParameterMap into an AuthenticationRequest message</li>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/impl/AuthenticationRequestValidator.java?view=markup">AuthenticationRequestValidator</a> &#8212; this wasn&#8217;t talked about last time, but is responsible for validating that an AuthenticationRequest message contains all of the requisite parameters</li>
</ul>

<p>All classes except for the actual message implementation must be thread-safe, as only a single instance is maintained by the library (technically they don&#8217;t follow a singleton pattern, but only one instance is typically used).  All of these are stored in central registries, so that they can be retrieved to marshall or unmarshall a message as needed.  Each one has it&#8217;s own factory that allows registering and looking up of specific implementations:</p>

<ul>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/MessageBuilderFactory.java?view=markup">MessageBuilderFactory</a></li>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/io/MessageMarshallerFactory.java?view=markup">MessageMarshallerFactory</a></li>
<li><a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/message/io/MessageUnmarshallerFactory.java?view=markup">MessageUnmarshallerFactory</a></li>
<li>MessageValidatorFactory (not yet implemented as of this writing)</li>
</ul>

<p>MessageValidator implementations are registered based on the message class that it validates. For the other three factories, implementations are registered based on a <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/namespace/QName.html">QName</a> which consists of the OpenID protocol namespace URI, and the value of the mode parameter.  Yes, there are three OpenID message types that don&#8217;t actually have a &#8216;mode&#8217; parameter, but I&#8217;ll save that discussion for another post.  Also, the QName here doesn&#8217;t exactly represent a namespaced parameter name like it does in the <a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/common/ParameterMap.java?view=markup">ParameterMap</a>, instead it is just a container for a namespace URI and a string value.  Perhaps this is technically a misuse of the QName object, but it&#8217;s working fine for now.  A static instance of each factory is available from the <a href="http://svn.middleware.georgetown.edu/view/java-openid/trunk/src/main/java/edu/internet2/middleware/openid/Configuration.java?view=markup">Configuration</a> class.</p>

<h2>Message Flow (redux)</h2>

<p>So now let&#8217;s go through a message flow like we did last time, and look at how each of the factories are used.  (At the time of this writing, I&#8217;m still working on hooking in the MessageValidators, so I won&#8217;t be talking much about that).</p>

<p>Remember that when a message comes in, it is in some kind of transport specific encoding.  Depending on how the message was received and the format it is in, an appropriate MessageDecoder is used to convert it into a ParameterMap.  The next step is to find an appropriate MessageUnmarshaller to convert this ParameterMap into an actual Message object.  The MessageUnmarshallerFactory has a <code>getUnmarshaller(ParameterMap)</code> method that will lookup exactly what we need.  Once we have an unmarshaller, we can call its <code>unmarshall(ParameterMap)</code>.  This method is responsible for building an appropriate Message object, and then populating it based on the data provided in the ParameterMap.  Internally, the unmarshaller uses the MessageBuilderFactory to find an appropriate MessageBuilder using the <code>getBuilder(ParameterMap)</code> method.  Once the correct builder is obtained, its <code>buildObject()</code> method is called to get an instance of the Message object.  This instance is then populated using data from the ParameterMap and returned.  (If anyone wants to volunteer a flow chart that illustrates this, I&#8217;d be greatly appreciative!)</p>

<p>When it comes time to send a message back out, the MessageMarshallerFactory&#8217;s <code>getMarshaller(Message)</code> method is called to get the correct MessageMarshaller for a given message.  The marshaller&#8217;s <code>marshall(Message)</code> method is called and returns a ParameterMap, and that is passed through an appropriate MessageEncoder to send it out on the wire.</p>

<h2>Custom Implementations</h2>

<p>The library comes with default implementations for all of this, so a user can simply choose to ignore all of this plumbing and be just fine.  But just in case you <strong>do</strong> want to customize part of this, how would you go about doing so?  Simply by registering them with the appropriate factory.  Let&#8217;s say you want to provide your own AssociationRequest implementation for whatever reason.  But maybe you don&#8217;t necessarily care to customize the way the data is unmarshalled into and marshalled out of the object&#8230; the default implementations for those are fine.  You would of course have your custom AssociationRequest:</p>

<pre><code>public class MyAssociationRequest implements AssociationRequest {
    /* implementation here */
}
</code></pre>

<p>Then to make sure that your custom implementation is built instead of the default implementation provided by the library, you would also need to provide a MessageBuilder:</p>

<pre><code>public class MyAssociationRequestBuilder implements 
             MessageBuilder&lt;AssociationRequest&gt; {

    public AssociationRequest buildObject() {
        /* build and return an instance of MyAssociationRequest */
    }
}
</code></pre>

<p>Then register your message builder:</p>

<pre><code>MessageBuilder myBuilder = new MyAssociationRequestBuilder();
QName qname = new QName(OpenIDConstants.OPENID_20_NS, AssociationRequest.MODE);
Configuration.getMessageBuilders().registerBuilder(qname, myBuilder);
</code></pre>

<p>Once your builder is registered, it will be used to build AssociationRequest objects for all incoming messages of that type.  However, the default marshaller and unmarshaller for that type will continue to be used&#8230; you don&#8217;t need to worry about that.  And once I get the validators hooked in, that will <em>just work</em> as well with your custom class.  Or, you could provide your own Validators if you like. You can customize as much or as little of the library as you want.</p>

<p>I don&#8217;t imagine that anyone will want to provide custom message implementations very often, but the option is most certainly there.  What is far more likely is providing a custom message extension like Attribute Exchange or PAPE.  That works in very much the same way, which I&#8217;ll explain next.</p>
]]></content:encoded>
			<wfw:commentRss>http://willnorris.com/2009/11/java-openid-library-configuration-and-custom-messages/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java OpenID Library Design - Message Handling</title>
		<link>http://willnorris.com/2009/11/java-openid-library-design-message-handling</link>
		<comments>http://willnorris.com/2009/11/java-openid-library-design-message-handling#comments</comments>
		<pubDate>Sat, 14 Nov 2009 00:16:52 +0000</pubDate>
		<dc:creator>Will Norris</dc:creator>
				<category><![CDATA[identity]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[internet2]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[joid]]></category>
		<category><![CDATA[openid]]></category>
		<category><![CDATA[openid4java]]></category>
		<category><![CDATA[shibboleth]]></category>

		<guid isPermaLink="false">http://willnorris.com/?p=888</guid>
		<description><![CDATA[This past June I contracted with Internet2 to work on adding OpenID support to the Shibboleth Identity Provider. I had actually started to work on this over a year prior while working at USC. At the time there were (and still are) two primary OpenID libraries in Java, Verisign&#8217;s JOID, and Sxip&#8217;s OpenID4Java. I spent [...]]]></description>
			<content:encoded><![CDATA[<p>This past June I contracted with <a href="http://internet2.edu/">Internet2</a> to work on adding OpenID support to the <a href="http://shibboleth.internet2.edu/">Shibboleth</a> Identity Provider.  I had actually started to work on this over a year prior while working at USC.  At the time there were (and still are) two primary OpenID libraries in Java, Verisign&#8217;s <a href="http://code.google.com/p/joid/">JOID</a>, and Sxip&#8217;s <a href="http://code.google.com/p/openid4java/">OpenID4Java</a>.  I spent a fair amount of time looking at both libraries, but ultimately decided they weren&#8217;t going to work for what Shibboleth needed.  There were architectural issues with the existing libraries, which I pointed out in <a href="http://groups.google.com/group/openid4java/browse_thread/thread/f0775348b3b7f3f/f93d22fe21a6e37e">my post</a> to the OpenID4Java mailing list.  But there were also significant design decisions that I felt could be improved upon, so I began work on a new OpenID library in Java.  Now that this library is nearing a usable state, I wanted to talk about some of the architectural decisions that were made, and how it differs from the existing Java libraries for OpenID.</p>

<p>Let me first preface this by clarifying that I&#8217;m not saying the existing OpenID libraries are not usable.  Quite the contrary, I know that the OpenID4Java library is used for AOL&#8217;s OpenID provider, on Google&#8217;s Blogger, as part of Sun&#8217;s <a href="https://opensso.dev.java.net/">OpenSSO</a>, and countless other projects.  Additionally, JOID powers Verisign&#8217;s very usable <a href="https://pip.verisignlabs.com/">PIP</a>.  There is no question that they work for many use cases.  However, they lack the clean architecture I was looking for, which can really only be corrected by starting from a blank canvas.</p>

<p>(I&#8217;m not sure how many posts this will take, or how sensical the order of things will be, but better to go ahead and get it written down in <em>some</em> form.)</p>

<h2>Message Handling Flow</h2>

<p>One of the most immediate differences you&#8217;ll see in the Internet2 library is the very clear separation of logic in the message handling code.  I wanted the core message objects to be simple Java beans that provide access to strongly typed properties, and nothing more.  When I&#8217;m processing an OpenID message, I don&#8217;t want to be thinking about how that message was encoded during transit.  Additionally, I don&#8217;t want to duplicate code if at all possible, so there needs to be one very clear place where any particular process is implemented.  To achieve this, messages are transformed into three distinct formats as they are being processed.</p>

<p>When a message comes in to an OpenID provider, it is in some kind of transport specific format.  Typically that will be a URL-encoded string that is taken either from an HTTP POST request body, or from an HTTP GET request query string.  Alternately, it may be a Map retrieved by calling <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getParameterMap()">ServletRequest.getParameterMap</a>.  This transport specific format needs to first be converted into some kind of common intermediary format so that the next step in the process can deal with all messages in the same way, regardless of transport method.  In the Internet2 library, this common format is a ParameterMap.</p>

<h3>ParameterMap</h3>

<p>A <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/common/ParameterMap.java?view=markup">ParameterMap</a> is simply a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedHashMap.html">LinkedHashMap</a> with <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/namespace/QName.html">QName</a> keys, String values, and a little additional logic.  Why QNames for keys?  Aren&#8217;t those for XML?  Yes they are, but they actually work beautifully for OpenID message parameters as well.  You see, an OpenID message is really just a collection of namespace qualified parameters, and can be quite easily represented in XML.  (Yes, this is a little bit of a rabbit trail, but it&#8217;s interesting nonetheless).  Let&#8217;s start with a really simple KVF encoded OpenID message:</p>

<pre><code>openid.ns:http://specs.openid.net/auth/2.0
openid.mode:checkid_setup
openid.claimed_id:http://example.com/
openid.identity:http://example.com/
openid.ns.sreg:http://openid.net/extensions/sreg/1.1
openid.sreg.required:email,fullname
</code></pre>

<p>Yeah it has no signature, etc, but that&#8217;s not the point.  What might this look like in XML?</p>

<pre><code>&lt;message xmlns="http://specs.openid.net/auth/2.0" 
         xmlns:sreg="http://openid.net/extensions/sreg/1.1"&gt;
    &lt;mode&gt;checkid_setup&lt;/mode&gt;
    &lt;claimed_id&gt;http://example.com/&lt;/claimed_id&gt;
    &lt;identity&gt;http://example.com/&lt;/identity&gt;
    &lt;sreg:required&gt;email,fullname&lt;/sreg:required&gt;
&lt;/message&gt;
</code></pre>

<p>See how cleanly it maps?  This is no accident.  This is a very common pattern for handling namespace qualified parameters.  First you assign your namespace to an alias, then you use that alias as a prefix for any parameters that are part of that namespace.  The simple registration &#8216;required&#8217; parameter name has three parts: there&#8217;s the base parameter name (&#8220;required&#8221;), the namespace alias (&#8220;sreg&#8221;), and the actual namespace URI which is declared separately (&#8220;http://openid.net/extensions/sreg/1.1&#8221;).  A Java QName object consists of three parts: a namespace URI, a local part, and a namespace prefix.  Slightly different terms, but <strong>exactly</strong> the same concepts.</p>

<p>Okay, so back to our OpenID library.  We&#8217;ve taken our transport specific encoding, passed it through an appropriate <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/message/encoding/MessageDecoder.java?view=markup">MessageDecoder</a>, and ended up with a ParameterMap.  Before we move on, I want to point out one more thing about the parameters in a ParameterMap.  None of the parameter names contain the &#8220;openid.&#8221; prefix.  This prefix is specific to messages that are encoded using <a href="http://openid.net/specs/openid-authentication-2_0.html#rfc.section.4.1.2">URL Form encoding</a>, since that&#8217;s the only way to identify which parameters are part of the OpenID message.  One of the jobs of the <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/message/encoding/impl/URLFormCodec.java?view=markup">URLFormCodec</a> is to strip this prefix as messages come in, and add the prefix as messages go out.  The message encoder and decoder is the <strong>only</strong> part of the entire library that knows anything about this prefix, and quite frankly it&#8217;s the only part that should.</p>

<p>Okay, so now that we have our ParameterMap, it needs to be converted into an actual message object, which is the job for a MessageUnmarshaller.</p>

<h3>Unmarshalling messages</h3>

<p><a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/message/io/MessageUnmarshaller.java?view=markup">Message unmarshallers</a> are responsible for taking a ParameterMap and using it to populate a specific kind of message object.  Remember the desire for message objects to have strongly typed properties?  The corresponding unmarshaller for that message type is the one and only place that needs to worry with how the parameter passed on the wire gets converted into that strong type.  For example, <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/message/AssociationRequest.java?view=markup">AssociationRequest</a> messages may include the Diffie-Hellman public key of the OpenID relying party.  Java provides a very specific object just for that called <a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/crypto/interfaces/DHPublicKey.html">DHPublicKey</a>, so that&#8217;s what we want our AssociationRequest object to use.  Parameters can only be passed as strings during transit, so the <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/message/impl/AssociationRequestUnmarshaller.java?view=markup">AssociationRequestUnmarshaller</a> (and nothing else) is responsible for knowing how to convert that string into a DHPublicKey.</p>

<p>Similarly, Attribute Exchange fetch requests may include a list of required attributes it wants for a user.  These attributes are identified by URIs, so Attribute Exchange does it&#8217;s own aliasing similar to the namespace declarations we saw above.  This way, the &#8220;ax.required&#8221; message parameter need only contain a comma-separated list of attribute aliases rather than the full namespace URIs.  But when you get right down to it, these aliases are just an optimization that is used during transport.  Really all that&#8217;s being represented is a list of attributes URIs.  This is why the <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/extensions/ax/FetchRequest.java?view=markup">FetchRequest</a> object in the Internet2 library exposes this particular message parameter simply as a List of attribute URIs.  It&#8217;s the <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/FetchRequestUnmarshaller.java?view=markup">FetchRequestUnmarshaller</a> that is responsible for taking the AX message parameters, dereferencing the attribute aliases, and populating the FetchRequest object appropriately.</p>

<h3>Reversing the process</h3>

<p>What about returning OpenID response messages?  We just do the same process in reverse.  The message object is passed through an appropriate <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/message/io/MessageMarshaller.java?view=markup">MessageMarshaller</a> which populates a ParameterMap.  And the ParamerMap is in turn passed through a <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/message/encoding/MessageEncoder.java?view=markup">MessageEncoder</a> that produces some kind of transport specific format.  That may be a Key-Value form encoded string, as is the case with direct responses, it may be a URL suitable for redirecting the user to, or it may be an HTML response to use for HTML form submission.</p>

<h2>Uniformity over brevity</h2>

<p>Depending on how you separate them, there are roughly nine different message types in the core OpenID 2.0 spec, and for each of these message types, the Internet2 library has five files that handle the processing.  There&#8217;s the message interface, the concrete implementation, the message builder (which I didn&#8217;t actually talk about in this post), the message marshaller, and the message unmarshaller.  At times all these files may seem needlessly verbose, especially when you see that some of them are only a few lines long.  It turns out that this separation doesn&#8217;t necessarily result in more lines of code, just that the code is broken up into smaller chunks.  Besides, the goal here is not conciseness.  The goal is uniformity and predictability in how messages are processed, as well as clean, logical separation of duties.  When every message is processed in exactly the same way, bugs tend to expose themselves much earlier in the process, and strange edge cases are far rarer.  When things are logically separated, it makes the overall architecture much easier to understand.  And perhaps more importantly, it makes it possible to fully understand one part of the library without needing to be concerned with others.  You can go in and look at the code for <a href="https://github.com/willnorris/java-openid/blob/master/src/main/java/edu/internet2/middleware/openid/security/SecurityUtils.java?view=markup">signing messages</a>, and not have to wade through code dealing with transport encodings.</p>
]]></content:encoded>
			<wfw:commentRss>http://willnorris.com/2009/11/java-openid-library-design-message-handling/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A class needed by class &lt;&#8230;&gt; cannot be found: org/apache/tools/ant/Task</title>
		<link>http://willnorris.com/2005/03/classnotfound</link>
		<comments>http://willnorris.com/2005/03/classnotfound#comments</comments>
		<pubDate>Tue, 22 Mar 2005 13:36:46 +0000</pubDate>
		<dc:creator>Will Norris</dc:creator>
				<category><![CDATA[technology]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://willnorris.com/wordpress/2005/03/a-class-needed-by-class-cannot-be-found-orgapachetoolsanttask</guid>
		<description><![CDATA[(I spent quite a few hours fighting this one, so hopefully Google will help others find this page&#8230;) After toying around with maven a bit, I discovered that none of my ant tasks would run. I hadn&#8217;t made any changes to anything in $ANT_HOME, but all of sudden things broke. What I had done without [...]]]></description>
			<content:encoded><![CDATA[<p>(I spent quite a few hours fighting this one, so hopefully Google will help
others find this page&#8230;)  After toying around with maven a bit, I discovered
that none of my ant tasks would run.  I hadn&#8217;t made any changes to anything in
$ANT_HOME, but all of sudden things broke.  What I had done without thinking
about it was to place <code>woproject.jar</code> in <code>/Library/Java/Extensions</code>.  This jar
contains the classes to create a few Ant task definitions.  So here&#8217;s the
rule&#8230;</p>

<blockquote>
  <p>Never place jars that are necessary for Ant task definitions in your Java
  classpath (<code>/System/Library/Java/Extensions</code>, <code>/Library/Java/Extensions/</code>,
  <code>~/Library/Java/Extensions</code>)</p>
</blockquote>

<p>The problem is that Java tries to load these jars before it has loaded the Ant
jars that they rely on.  These jars that contain task definitions should either
be placed in the Ant classpath (<code>$ANT_HOME/lib</code>, <code>~/.ant/lib</code>) or in your
project&#8217;s lib directory and referenced directly using the <code>classpath</code> attribute
of the <code>taskdef</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://willnorris.com/2005/03/classnotfound/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>OKI OSID Unit Testing</title>
		<link>http://willnorris.com/2005/01/oki-osid-unit-testing</link>
		<comments>http://willnorris.com/2005/01/oki-osid-unit-testing#comments</comments>
		<pubDate>Tue, 04 Jan 2005 06:31:37 +0000</pubDate>
		<dc:creator>Will Norris</dc:creator>
				<category><![CDATA[technology]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[osid]]></category>

		<guid isPermaLink="false">http://willnorris.com/wordpress/2005/01/oki-osid-unit-testing</guid>
		<description><![CDATA[As part of hedmaster I&#8217;ve tried to implement as many of the OKI Specs as have made sense. I&#8217;m working on writing extensive JUnit test cases to thoroughly test hedmaster, and I got to thinking about writing a test suite to check for adherence to the OKI Specs. Because the OSID interfaces are designed to [...]]]></description>
			<content:encoded><![CDATA[<p>As part of <a href="http://willnorris.com/projects/hedmaster">hedmaster</a> I&#8217;ve tried to implement as many of the <a href="http://www.okiproject.org/specs">OKI Specs</a>
as have made sense.  I&#8217;m working on writing extensive JUnit test cases to
thoroughly test hedmaster, and I got to thinking about writing a test suite to
check for adherence to the OKI Specs.  Because the OSID interfaces are designed
to enable &#8220;drop in replacement&#8221; of each component, this shouldn&#8217;t be too
difficult.  There would be a test case for each OsidManager that would create
and manipulate objects and simply test that they are instances of the
appropriate OSID.  The user would then write their own test case that passed
their specific implementation of each OsidManger to OsidUnitTest.</p>

<p>As the OSIDs gradually gain acceptance, I think some simple automation like
this would really help people develop proper implementations.</p>
]]></content:encoded>
			<wfw:commentRss>http://willnorris.com/2005/01/oki-osid-unit-testing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

