<?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>Jayway Team Blog &#187; Mattias Hellborg Arthursson</title>
	<atom:link href="http://blog.jayway.com/author/mattiasarthursson/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jayway.com</link>
	<description>Sharing Experience</description>
	<lastBuildDate>Sat, 28 Jan 2012 15:53:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Environment-Specific Configuration of Spring Applications</title>
		<link>http://blog.jayway.com/2010/10/21/environment-specific-configuration-of-spring-applications/</link>
		<comments>http://blog.jayway.com/2010/10/21/environment-specific-configuration-of-spring-applications/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 18:31:04 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[spring environment configuration property-placeholder]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=6495</guid>
		<description><![CDATA[Over and over I see projects struggling with the problem of parameters in property files that change in different environments; after all that's pretty much the idea with the property files anyway - to make it easy to reconfigure the system. The PropertyPlaceholderConfigurer lets us externalize configuration parameters from the XML configuration to property files, [...]]]></description>
			<content:encoded><![CDATA[<p>Over and over I see projects struggling with the problem of parameters in property files that change in different environments; after all that's pretty much the idea with the property files anyway - to make it easy to reconfigure the system.</p>
<p>The <code>PropertyPlaceholderConfigurer</code> lets us externalize configuration parameters from the XML configuration to property files, and that's excellent, but I've seen (and implemented myself) so many different cumbersome attempts on environment configuration it's not even funny.</p>
<p>The problem is this: We'll want to have one test-environment version of the property file bundled with the application source code to simplify starting the app in test mode from inside the IDE. So how do we reconfigure the application for production? Extract the war file, replace the property file with the production version, and re-pack the war? No, we really want to avoid this. Another, even worse solution is to have a number of different versions of the property files and then using different build profiles including the "correct" versions.</p>
<p>As it turns out there's a perfectly simple, obvious, but surprisingly hard-to-google solution to this problem: You probably know that it's possible to specify several property files when configuring the <code>PropertyPlaceholderConfigurer</code>; well it turns out that a property redefined in a later file will <b>override</b> a setting from a previous file. This allows us to do the following:</p>
<pre class="xml">&nbsp;
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;context:property-placeholder</span>
    <span style="color: #000066;">ignore-resource-not-found</span>=<span style="color: #ff0000;">&quot;true&quot;</span>
    <span style="color: #000066;">location</span>=<span style="color: #ff0000;">&quot;WEB-INF/myApp.properties,file:///etc/myApp/myApp.properties&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;</pre>
<p>Note the <code>ignore-resource-not-found</code> attribute, which is needed to so that not all files need to be in place. Now consider that the bundled property file, <code>WEB-INF/myApp.properties</code> has the following contents:</p>
<pre class="java">&nbsp;
db.<span style="color: #006600;">url</span>=dbc:jtds:sqlserver:<span style="color: #808080; font-style: italic;">//testdb.example.com/mydb</span>
db.<span style="color: #006600;">user</span>=testuser
db.<span style="color: #006600;">password</span>=testpass
&nbsp;</pre>
<p>and that there is no file at <code>/etc/myApp/myApp.properties</code> on your development machine. When we start the application in the IDE, it will load and run with the test settings.</p>
<p>If we now place an environment-specific property file at <code>/etc/myApp/myApp.properties</code> with the following contents:</p>
<pre class="java">&nbsp;
db.<span style="color: #006600;">url</span>=dbc:jtds:sqlserver:<span style="color: #808080; font-style: italic;">//proddb.example.com/mydb</span>
db.<span style="color: #006600;">user</span>=produser
db.<span style="color: #006600;">password</span>=somethingreallysecret
&nbsp;</pre>
<p>these are the settings that will be used when the application is started on that machine. This is what we'll do on the production machine. And what's even better: it won't even be your responsibility - you just need to document where the property file should be placed and which properties that can be specified and then leave it for operations to actually make it so.</p>
<p>Simple as that.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/10/21/environment-specific-configuration-of-spring-applications/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>REST and XML using Spring MVC and Groovy</title>
		<link>http://blog.jayway.com/2010/04/09/rest-and-xml-using-spring-mvc-and-groovy/</link>
		<comments>http://blog.jayway.com/2010/04/09/rest-and-xml-using-spring-mvc-and-groovy/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 14:32:10 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[hateoas]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=5319</guid>
		<description><![CDATA[There's one particular thing Groovy is really good for and that is working with XML. When I started playing around with the REST support in the latest version of Spring MVC I wanted to try using Spring for the controller infrastructure and delegate to Groovy for producing XML responses. It turned out this wasn't as [...]]]></description>
			<content:encoded><![CDATA[<p>There's one particular thing Groovy is really good for and that is working with XML. When I started playing around with the REST support in the latest version of Spring MVC I wanted to try using Spring for the controller infrastructure and delegate to Groovy for producing XML responses. It turned out this wasn't as easy as expected.</p>
<p>The built-in support for rendering XML from RESTful web services in Spring is built on top of the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/oxm.html">Marshaller</a> abstraction originally from Spring Web Services (now moved to Spring core). This is a pretty nice abstraction if you want to use one of the existing Object-XML marshalling frameworks (JAXB, Castor, XMLBeans, etc.), but if you want to move out of that box you're pretty much stranded.</p>
<p>I really do want to move out of the box because I think the control and readability greatly improves if you do your XML marshalling in Groovy. It's quite common that the same domain object will need to be marshalled differently depending on the scenario (an object will e.g. quite probably need to be marshalled differently in a detail view than in a list). Also, it is not uncommon that more complicated application logic will be required when rendering the XML; a common example of this is URI generation in 'linked' REST applications (<a href="http://en.wikipedia.org/wiki/HATEOAS">HATEOAS</a>). Hence, Groovy is the perfect choice: Very little code is needed, you are able to include some logic if necessary, and the code written exactly reflects the XML that will be produced. </p>
<h3>The Building Blocks</h3>
<p>I started out by defining a simple interface for a completely generic Marshaller:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> SimpleMarshaller <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> marshal<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Writer</span></a> writer, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> o<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>I now needed a View implementation to render Model objects using the <code>SimpleMarshaller</code> interface (much of this code is actually borrowed from the Spring <code>MarshallingView</code> - Apache license rocks!):</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SimpleMarshallingView <span style="color: #000000; font-weight: bold;">extends</span> AbstractView <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> DEFAULT_MODEL_KEY = <span style="color: #ff0000;">&quot;objectToMarshal&quot;</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">boolean</span> AUTOMATIC_FLUSH = <span style="color: #000000; font-weight: bold;">true</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> DEFAULT_CHARSET = <span style="color: #ff0000;">&quot;utf8&quot;</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> SimpleMarshaller marshaller;
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> SimpleMarshallingView<span style="color: #66cc66;">&#40;</span>SimpleMarshaller marshaller<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">marshaller</span> = marshaller;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">void</span> renderMergedOutputModel<span style="color: #66cc66;">&#40;</span>Map&lt;String, Object&gt; model, HttpServletRequest request,
      HttpServletResponse response<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a> outputStream = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APrintWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">PrintWriter</span></a> writer = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APrintWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">PrintWriter</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AOutputStreamWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">OutputStreamWriter</span></a><span style="color: #66cc66;">&#40;</span>outputStream, DEFAULT_CHARSET<span style="color: #66cc66;">&#41;</span>, AUTOMATIC_FLUSH<span style="color: #66cc66;">&#41;</span>;
    marshaller.<span style="color: #006600;">marshal</span><span style="color: #66cc66;">&#40;</span>writer, objectToRender<span style="color: #66cc66;">&#40;</span>model<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    response.<span style="color: #006600;">setContentType</span><span style="color: #66cc66;">&#40;</span>getContentType<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    response.<span style="color: #006600;">setContentLength</span><span style="color: #66cc66;">&#40;</span>outputStream.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    FileCopyUtils.<span style="color: #006600;">copy</span><span style="color: #66cc66;">&#40;</span>outputStream.<span style="color: #006600;">toByteArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, response.<span style="color: #006600;">getOutputStream</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> objectToRender<span style="color: #66cc66;">&#40;</span>Map&lt;String, Object&gt; model<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> o = model.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>DEFAULT_MODEL_KEY<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>o == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AIllegalArgumentException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">IllegalArgumentException</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Model does not contain the expected key '&quot;</span> + DEFAULT_MODEL_KEY + <span style="color: #ff0000;">&quot;'&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> o;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Next I created an abstract <code>SimpleMarshaller</code> implementation for marshalling using a Groovy <code>MarkupBuilder</code>:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> AbstractMarkupBuilderSimpleMarshaller <span style="color: #000000; font-weight: bold;">implements</span> SimpleMarshaller <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">void</span> marshal<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AWriter+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Writer</span></a> writer, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> o<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    MarkupBuilder builder = <span style="color: #000000; font-weight: bold;">new</span> MarkupBuilder<span style="color: #66cc66;">&#40;</span>writer<span style="color: #66cc66;">&#41;</span>;
    marshalToBuilder<span style="color: #66cc66;">&#40;</span>builder, o<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #993333;">void</span> marshalToBuilder<span style="color: #66cc66;">&#40;</span>MarkupBuilder builder, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> o<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Now all that is left is to create a new Groovy View implementation for specific XML response you want, e.g.:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> UserXmlMarshallingView <span style="color: #000000; font-weight: bold;">extends</span> SimpleMarshallingView <span style="color: #66cc66;">&#123;</span>
  @Autowired
  <span style="color: #000000; font-weight: bold;">public</span> UserXmlMarshallingView<span style="color: #66cc66;">&#40;</span>UriConstructionService uriConstructionService<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> UserXmlMarshaller<span style="color: #66cc66;">&#40;</span>uriConstructionService<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    setContentType<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;application/xml&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> UserXmlMarshaller <span style="color: #000000; font-weight: bold;">extends</span> AbstractMarkupBuilderSimpleMarshaller <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> UriConstructionService uriConstructionService;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> UserXmlMarshaller<span style="color: #66cc66;">&#40;</span>UriConstructionService uriConstructionService<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">uriConstructionService</span> = uriConstructionService;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> marshalToBuilder<span style="color: #66cc66;">&#40;</span>MarkupBuilder builder, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> user<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      builder.<span style="color: #ff0000;">'user'</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span>: user.<span style="color: #006600;">name</span>, <span style="color: #ff0000;">'email'</span>: user.<span style="color: #006600;">email</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        user.<span style="color: #006600;">orders</span>.<span style="color: #006600;">each</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #ff0000;">'order-ref'</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dateCreated'</span>: it.<span style="color: #006600;">dateCreated</span>.<span style="color: #006600;">format</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'yyyy-MM-dd hh:mm:ss'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">'uri'</span>: uriConstructionService.<span style="color: #006600;">orderUri</span><span style="color: #66cc66;">&#40;</span>it<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Note above that we included some processing logic as well - we get a formatted date and we also make use of a service for producing URIs for referencing sub-entities.</p>
<p>Each individual View implementations now needs to be registered in the <code>DispatcherServlet</code>'s ApplicationContext:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.jayway.demo.xml.UserXmlMarshallingView&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
...
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.view.BeanNameViewResolver&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
&nbsp;</pre>
<p>Since we have defined a <code>BeanNameViewResolver</code> we can now refer to these Views using their bean names when pointing them out from a Controller:</p>
<pre class="java">&nbsp;
@Controller
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserController <span style="color: #66cc66;">&#123;</span>
  @Autowired
  <span style="color: #000000; font-weight: bold;">private</span> Repo repo;
&nbsp;
  @RequestMapping<span style="color: #66cc66;">&#40;</span>value = <span style="color: #ff0000;">&quot;/users/{id}&quot;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">public</span> ModelAndView user<span style="color: #66cc66;">&#40;</span>@PathVariable <span style="color: #993333;">int</span> id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    User user = repo.<span style="color: #006600;">userById</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;user&quot;</span>, SimpleMarshallingView.<span style="color: #006600;">DEFAULT_MODEL_KEY</span>, user<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<h3>Infrastructure</h3>
<p>Obviously, for the above to work we need organize our code appropriately and make sure that our build system properly builds the groovy files. This turned out to be surprisingly easy (using maven). All groovy code should be placed under <code>src/main/groovy</code>. Then maven needs to be configured to build the groovy files using the <a href="http://docs.codehaus.org/display/GMAVEN/Building+Groovy+Projects">gmaven</a> plugin. Also, make sure you include the groovy libs in your dependency list:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.codehaus.groovy<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>groovy-all<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>1.7.1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<h3>Conclusion</h3>
<p>With the above setup it's easy to quickly implement (and test) new marshalling views for the different resources in a REST application. Even though this was used in a rather small proof-of-concept project my general feeling is that the approach will work pretty well. My next step with this will be to add some simplifications for working with HTTP status codes (the approach to HTTP response codes with the <code>@ResponseCode</code> annotation in Spring MVC feels insufficient for many cases).</p>
<p>Finally it should be noted that the approach with a completely generic marshaller makes it easy to expand this to working with other representations as well, e.g. JSON (although the JSON support in Groovy is still a little bit shaky).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2010/04/09/rest-and-xml-using-spring-mvc-and-groovy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring Security For Real with Grails</title>
		<link>http://blog.jayway.com/2009/11/23/spring-security-for-real-with-grails/</link>
		<comments>http://blog.jayway.com/2009/11/23/spring-security-for-real-with-grails/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 18:41:34 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring security]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=2248</guid>
		<description><![CDATA[Spring Security is one of the basic building blocks I use pretty much every time I'm constructing a web application. It's a very mature and incredibly powerful security framework, one of its main benefits being its versatility. There are hooks and plugs everywhere, allowing you to extend and combine basically any way you want. Now, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://static.springsource.org/spring-security/site/">Spring Security</a> is one of the basic building blocks I use pretty much every time I'm constructing a web application. It's a very mature and incredibly powerful security framework, one of its main benefits being its versatility. There are hooks and plugs everywhere, allowing you to extend and combine basically any way you want.</p>
<p>Now, if you want to apply security in a Grails application you are typically pointed in the direction of the <a href="http://grails.org/plugin/acegi">Grails Acegi Plugin</a>, which does a rather decent job at applying basic security to your Grails application. It quickly falls short however when you need to start doing something more than the bare basics (which you pretty much always need to do); even though the plugin is based on Spring Security, far from everything in the original framework is supported in the plugin, and hooking in custom components is pretty much out of the question. In addition to this, the Acegi Plugin is haunted by a couple of pretty annoying bugs.</p>
<p>Bottom line: for any real-world scenario you will most likely want to fall back to the original, i.e. use the original Spring Security framework in your Grails application. Since Grails is Spring-based it shouldn't be all that much work to set that up, right? Well basically yes, but as I set out do to it I ran into a number of problems before I got it right, so I thought I might as well line out the steps and pitfalls.</p>
<h4>1. Download and Install the Spring Security jars</h4>
<p>Typically, for the basic setup you should need only the <code>spring-security-core.jar</code> and <code>spring-security-core-tiger.jar</code>, but depending on your requirements you might need to include more of the Spring Security binaries. Place the jars in your <code>lib</code> directory of your Grails application.</p>
<h4>2. Install Templates</h4>
<p>Spring Security is based on an HTTP filter chain, which needs to be declared in the <code>WEB-INF/web.xml</code> file of the web application. This file is normally automatically generated for you by Grails, but for the event that you need more control (such as this occasion) you can have the default file generated for you to edit. The command for this is <code>grails install-templates</code>. This will generate a number of files, and the <code>web.xml</code> will be ready for editing under <code>src/templates/war</code>.</p>
<h4>3. Add the Spring Security Filter Chain</h4>
<p>There will be a number of filters defined in the <code>web.xml</code> file already. Add the Spring Security filter after the other filter definitions, but before the filter-mapping entries (all the filter definitions need to be placed before the filter-mapping ones, or else evil things will happen with any additional filters generated by Grails and we'll get in trouble when we deploy in tomcat).</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>springSecurityFilterChain<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.springframework.web.filter.DelegatingFilterProxy<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p>Now, after the other filter-mapping entries, add the filter-mapping for the Spring Security filter:</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>springSecurityFilterChain<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<h4>4. Spring Security Configuration </h4>
<p>Now we're ready to add the Spring Security configuration XML. Note that this configuration <b>needs</b> to be placed in <code>grails-app/conf/spring/resources.xml</code>. I initially tried to put it in <code>WEB-APP/WEB-INF/applicationContext.xml</code> but due to the Grails ApplicationContext loading magic that attempt failed spectacularly. We'll start out with a minimal Spring Security configuration just to get things going; for more information the configuration topic I'll refer to the <a href="http://static.springsource.org/spring-security/site/docs/2.0.x/reference/springsecurity.html">reference documentation</a>.</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span>
        <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
        <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd&quot;</span>
        <span style="color: #000066;">xmlns:sec</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/security&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:http<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:intercept-url</span> <span style="color: #000066;">pattern</span>=<span style="color: #ff0000;">&quot;/**&quot;</span> <span style="color: #000066;">access</span>=<span style="color: #ff0000;">&quot;ROLE_USER&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:http-basic</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sec:http<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:authentication-provider<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:user-service<span style="font-weight: bold; color: black;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:user</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mattias&quot;</span> <span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;12345&quot;</span> <span style="color: #000066;">authorities</span>=<span style="color: #ff0000;">&quot;ROLE_USER&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
                <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sec:user-service<span style="font-weight: bold; color: black;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sec:authentication-provider<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/beans<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre>
<p><b>Another note of caution here:</b> If there is anything incorrect in your <code>resources.xml</code> Grails will <b>happily and silently ignore this</b> and go ahead and start anyway. Therefore, whenever you start doing stuff with your own custom Spring configuration in a Grails app it is imperative to make sure to configure your logging so that Spring warning and error messages are logged properly or you'll be completely in the dark trying to figure out what went wrong.</p>
<h4>All done</h4>
<p>As it turns out this wasn't as bad as expected. You're now all set to unleash the full power of Spring Security on your Grails application.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/11/23/spring-security-for-real-with-grails/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Setting Up Grails Projects in IntelliJ Idea</title>
		<link>http://blog.jayway.com/2009/09/06/setting-up-grails-projects-in-intellij-idea/</link>
		<comments>http://blog.jayway.com/2009/09/06/setting-up-grails-projects-in-intellij-idea/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 17:24:01 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[buildconfig]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[idea]]></category>
		<category><![CDATA[intellij]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[setup]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1927</guid>
		<description><![CDATA[Since I've started doing some stuff in Grails lately I've virtually been forced to start looking for an alternative IDE. I've been an Eclipse guy for the last five years or so; ever since I got out of the JBuilder swamp. Unfortunately, the only IDE with decent support for Grails seems to be Idea (yes [...]]]></description>
			<content:encoded><![CDATA[<p>Since I've started doing some stuff in Grails lately I've virtually been forced to start looking for an alternative IDE. I've been an Eclipse guy for the last five years or so; ever since I got out of the JBuilder swamp. Unfortunately, the only IDE with decent support for Grails seems to be Idea (yes I tried NetBeans, but it was so dreadfully slow it wasn't even funny). Now, even though it is possible to get it to work, it was very far from painless before it was all set up. Since I had problems, and since I had a really hard time finding information about this I figured I might as well share what I had to do to make it work.</p>
<p>Part of the problem was (I guess) that the projects were created outside of Idea, so they had to be imported. Another thing that turned out to be a big problem was that there are a number of inter-dependencies between the projects (set up as plugin dependencies in <code>BuildConfig.groovy</code>). Now, here are the steps I had to go through to make a multi-sub-project Grails project working in IntelliJ Idea:</p>
<ul>
<li>First of all, create an empty top-level project (actively uncheck the 'Create Module' checkbox).</li>
<li>Then, for each of the sub projects, create a new module. The tricky part here is that it turned out that you need to select 'Create from scratch..' option and select the root directory of each Grails app as module directory (and name). Then, just select 'Grails module', and you're done (for now)</li>
<li>Now we need to set up the build paths manually. For each of the modules that have local libraries included in the <code>lib</code> directory, that directory needs to be added manually as a jar directory dependency of that module. Make sure you check the 'export' checkbox as well, so that the binaries are included when you build and when if this module is referenced from another module (see below).</li>
<li>Next step is to manually remove and re-add each of the required plugins in each module. Be sure to re-add them one at a time; it didn't work to add multiple plugins at once</li>
<li>Now if you have inter-dependencies between your modules (e.g. one app uses a plugin you made yourself, set up in <code>BuildConfig.groovy</code>), these need to be set up as module dependencies.</li>
<li>Finally we're done. Now, double check the build path in the module settings window and make sure nothing is marked red. Try to build the project and you should be all set.</li>
</ul>
<p>Obviously, the bad thing here is that it's an incredible amount of work involved in getting everything up and working. The good thing however is that once you have it all set up it works really well. Code navigation and even debugging a remote Grails application works as a charm. That said, I'm still not comfortable with how Idea works. It just feels awkward somehow. I can't wait for when the SpringSource guys get this working in Spring IDE so that I can get back to my IDE of choice.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/09/06/setting-up-grails-projects-in-intellij-idea/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Configuring Timeout with Apache HttpClient 4.0</title>
		<link>http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/</link>
		<comments>http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 15:49:23 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[4.0]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[httpclient]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[timeout]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=1174</guid>
		<description><![CDATA[Great news everyone: just recently an all-new version of Apache HttpClient was released. HttpClient is now part of the new initiative Apache HttpComponents, which seems to aim for a complete approach to Http programming in Java; server side as well as client side. I've used Commons HttpClient in older versions on several occasions in the [...]]]></description>
			<content:encoded><![CDATA[<p>Great news everyone: just recently an all-new version of <a href="http://hc.apache.org/httpcomponents-client/index.html">Apache HttpClient</a> was released. HttpClient is now part of the new initiative <a href="http://hc.apache.org/">Apache HttpComponents</a>, which seems to aim for a complete approach to Http programming in Java; server side as well as client side.</p>
<p>I've used Commons HttpClient in older versions on several occasions in the past and have found it to be extremely useful. I've only just started working with the new version, but there seems to quite a lot of good stuff in there. For example I'm thrilled to see that the Template pattern, heavily used in Spring, has now made its way to the Apache world (e.g. <a href="http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/ResponseHandler.html">ResponseHandler</a>, a callback interface to be used with <a href="http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/HttpClient.html">HttpClient</a> for handling results).</p>
<p>Now, configuration of <code>HttpClient</code> has always been a little bit tricky. There are so many features in there, and most of these are configured using generic parameters (<code>HttpParams</code>), basically name-value pairs of a setting identifier and its value. This has not been made that much easier in the new version.</p>
<p>In my current project I was struggling to find the appropriate parameters for configuring timeouts with the new version of the framework. It turned out to be quite difficult to find clear information on this - the documentation on the 4.0 version of <code>HttpClient</code> is still quite sparse. Since it took me a while to find out how to do this I figured I wasn't alone, so here goes:</p>
<p>My original solution for this problem - being completely unable to find the proper way to do it - was to use the hard-coded property values and set them as parameters in <code>HttpParams</code>. Fortunately, a reader with signature 'BoD' (see below) knew the right way to do this and was kind enough to share. </p>
<p>Right, the code looks as follows:</p>
<pre class="java">&nbsp;
DefaultHttpClient httpClient = <span style="color: #000000; font-weight: bold;">new</span> DefaultHttpClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
HttpParams params = httpClient.<span style="color: #006600;">getParams</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
HttpConnectionParams.<span style="color: #006600;">setConnectionTimeout</span><span style="color: #66cc66;">&#40;</span>httpParams, connectionTimeoutMillis<span style="color: #66cc66;">&#41;</span>;
HttpConnectionParams.<span style="color: #006600;">setSoTimeout</span><span style="color: #66cc66;">&#40;</span>httpParams, socketTimeoutMillis<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>Simple as that - hard to understand why that should take me so long to figure out <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Thanks again to 'BoD' for setting me straight on this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Simple Authentication Using Spring LDAP</title>
		<link>http://blog.jayway.com/2009/02/02/simple-authentication-using-spring-ldap/</link>
		<comments>http://blog.jayway.com/2009/02/02/simple-authentication-using-spring-ldap/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 20:00:46 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[1.3.0]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[bind]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[ldap]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring ldap]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=870</guid>
		<description><![CDATA[It's with great pleasure that we can now finally announce the final 1.3.0 version of Spring LDAP. It's been a while since we've made a major release, but there's quite a bit in this one to make up for it. Among the highlights of this release are the improvements in the authentication area, which is [...]]]></description>
			<content:encoded><![CDATA[<p>It's with great pleasure that we can now finally announce the final 1.3.0 version of <a href="http://www.springframework.org/ldap">Spring LDAP</a>. It's been a while since we've made a major release, but there's quite a bit in this one to make up for it. Among the highlights of this release are the improvements in the authentication area, which is the intended focus of this post.</p>
<h3>Simple LDAP Authentication</h3>
<p>One of the most requested pieces of functionality in Spring LDAP has been a means to perform simple authentication. We have previously hesitated to include this, not finding any logical place to put it. In this release however we got a couple of suggestions on suitable API additions that enabled us to attack this from a different angle, in the end resulting in explicit methods in LdapTemplate for this purpose.</p>
<h4>Background</h4>
<p>The problem with authentication in LDAP is that it normally requires two separate steps: First you need to find the principal to authenticate in the LDAP tree, typically performing an LDAP search based on e.g. a user name. A new LDAP connection will then be acquired, authenticating it using the Distinguished Name of the found entry (normally referred to as an 'LDAP Bind').</p>
<h5>Example</h5>
<p>Consider the LDAP tree below:<br />
<img src="http://blog.jayway.com/wordpress/wp-content/uploads/2009/02/ldaptree.gif" alt="Ldap Tree" title="Ldap Tree" width="303" height="276" class="size-full wp-image-871" /><br />
Let us say a user identifying himself as 'John Doe' is trying to log into our system. We would execute a search from the top of the LDAP tree using a search filter like <code>(&(objectclass=person)(cn=John Doe))</code>. The search would return one single entry, from which we would extract the absolute DN; <code>cn=John Doe, ou=company1, c=Sweden, dc=jayway, dc=se</code>. This DN would then be used for authenticating a new LDAP connection to the server, thus validating the password supplied by the user.</p>
<h4>New Spring LDAP Authentication API</h4>
<p>While the above has indeed been possible to do using previous versions of Spring LDAP, it has required quite a lot of work and resulted in rather messy code. Spring LDAP 1.3.0 adds a couple of methods to LdapTemplate, making the authentication procedure very straightforward:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> authenticate<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AName+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Name</span></a> base, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> filter, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> password<span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> authenticate<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AName+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Name</span></a> base, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> filter, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> password, AuthenticatedLdapEntryContextCallback callback<span style="color: #66cc66;">&#41;</span></pre>
<p>The first method performs exactly the procedure described above, returning <code>true</code> or <code>false</code> depending on the outcome. The second method goes one step further, allowing us to perform any operation on the authenticated LDAP connection. Focusing on the simplest case, a standard authentication method using Spring LDAP would look something like the following:</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> login<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> password<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
  AndFilter filter = <span style="color: #000000; font-weight: bold;">new</span> AndFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  filter.<span style="color: #006600;">and</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EqualsFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;objectclass&quot;</span>, <span style="color: #ff0000;">&quot;person&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">and</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> EqualsFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;cn&quot;</span>, username<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">return</span> ldapTemplate.<span style="color: #006600;">authenticate</span><span style="color: #66cc66;">&#40;</span>DistinguishedName.<span style="color: #006600;">EMPTY_PATH</span>, filter.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, password<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>Simple, clean and to the point, especially compared to the mess that used to be required (won't linger on those nasty details here). Obviously however, using a Spring library we will be required to write a few lines of XML as well:</p>
<pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.ldap.core.support.LdapContextSource&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;ldap://url.to.ldap.server:389&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;userDn&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;uid=admin,ou=system&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;adminpassword&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ldapTemplate&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.ldap.core.LdapTemplate&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myAuthenticator&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.example.MyAuthenticatingClass&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #808080; font-style: italic;">&lt;!-- Assuming constructor injection of LdapTemplate instance in your authentication class --&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;ldapTemplate&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>A couple of comments on the suggested solution:</p>
<ul>
<li>The search needs to return exactly one result entry. In the example above, if there would be more than one person entry in the tree with <code>cn</code> 'John Doe' (which would be perfectly legal according to schema regulations), the call to <code>authenticate</code> would fail.</li>
<li>In actual implementations the attribute to use for identification will likely be e.g. <code>uid</code> or <code>sAMAccountname</code> (in Active Directory). Both of these attributes have uniqueness enforced throughout the entire tree by the LDAP server.</li>
<li> The method only returns <code>true</code> or <code>false</code>; thus the actual reason for failing will not be visible to the caller. The reason will however be logged, which might be useful useful when tracking down problems with search filters and such.</li>
<li>A common reason for confusion in LDAP searches is the <code>base</code> parameter, which is used for pointing out where in the LDAP tree to start searching. Referring again to the potential problem where several users might have the same <code>cn</code>; in that case these entries would have to be located in different subtrees. The search could then be narrowed by specifying a different base DN to the <code>authenticate</code> method, e.g. <code>c=Sweden, dc=jayway, dc=com</code></li>
</ul>
<p><b>Note: </b>While the provided methods will handle the simple task of authentication for you it is likely that your actual security requirements go way past plain authentication (e.g. authorization, web integration, etc.). The realm of security is a very complex one, which is the reason you should carefully consider your actual requirements - if they appear to go beyond simple authentication you should definitely consider using <a href="http://www.springsecurity.org">Spring Security</a> instead. (Obviously, under the covers Spring LDAP would be used for the actual authentication anyway).</p>
<p>That said, for many systems the API provided with Spring LDAP will be quite sufficient.</p>
<h3>Other improvements in Spring LDAP 1.3.0</h3>
<p>As compared to the 1.2.1 version of Spring LDAP, 1.3.0 includes more than 50 fixes, varying from internal modifications and minor improvements to important bug fixes and significant functionality additions. The full list of modifications can be viewed in the <a href="http://static.springframework.org/spring-ldap/docs/1.3.x/changelog.txt">the changelog</a>.</p>
<h3>About Spring LDAP</h3>
<p>Spring LDAP is a Java library for simplifying LDAP operations, based on the pattern of Spring's JdbcTemplate. The framework relieves the user of common chores, such as looking up and closing contexts, looping through results, encoding/decoding values and filters, and more. The library is free, open source, and distributed under the Apache Licence version 2. </p>
<p>For more information on the Spring LDAP project, including downloads, maven usage, as well as project reference and API documentation, refer to its <a href="http://www.springsource.org/ldap">project home page</a> on springsource.org. Support and enhancement requests will be answered in the <a href="http://forum.springframework.org/forumdisplay.php?f=40">Spring LDAP Forum</a> at Spring Community Forums.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2009/02/02/simple-authentication-using-spring-ldap/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>Getting Sound Working Properly on Ubuntu 8.10</title>
		<link>http://blog.jayway.com/2008/11/10/getting-sound-to-work-on-ubuntu-810ut/</link>
		<comments>http://blog.jayway.com/2008/11/10/getting-sound-to-work-on-ubuntu-810ut/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 10:35:58 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[alsa]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[sound]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[ubuntu 8.10]]></category>
		<category><![CDATA[youtube]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=380</guid>
		<description><![CDATA[Ever since I upgraded to Ubuntu 8.10 on my laptop (Dell Precision M90) I've been having problems with getting the sound to work properly. I could get some applications working if I would change the sound default from ALSA to OSS, but it wouldn't work in Firefox (e.g. youtube etc.) Seems this is not an [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I upgraded to Ubuntu 8.10 on my laptop (Dell Precision M90) I've been having problems with getting the sound to work properly. I could get some applications working if I would change the sound default from ALSA to OSS, but it wouldn't work in Firefox (e.g. youtube etc.)</p>
<p>Seems this is not an uncommon problem, but the solution was not easy to find - there are lots of proposed solutions on different forums and I've tried several until I finally found the one that worked. It turned out that the culprit in my setup was pulse audio. <a href="http://ubuntuforums.org/showthread.php?t=973637">The solution that worked for me</a> was simply to remove that:</p>
<pre>
killall pulseaudio
sudo apt-get remove pulseaudio
sudo apt-get install esound esound-clients libao2
sudo rm /etc/X11/Xsession.d/70pulseaudio
</pre>
<p>After the above I just changed back to ALSA in sound settings, restarted Firefox and everything worked like a charm. Note that executing the <code>apt-get remove</code> statement above will display a prompt to stating that it will remove Ubuntu desktop. This is just a meta-package so it will NOT remove the ubuntu desktop for real. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/11/10/getting-sound-to-work-on-ubuntu-810ut/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>What&#8217;s New in Spring LDAP 1.3</title>
		<link>http://blog.jayway.com/2008/10/27/whats-new-in-spring-ldap-13/</link>
		<comments>http://blog.jayway.com/2008/10/27/whats-new-in-spring-ldap-13/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 16:22:56 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring ldap]]></category>
		<category><![CDATA[tls]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=369</guid>
		<description><![CDATA[We recently released Spring LDAP 1.3.0.RC1. This long awaited release contains a number of new features and bug fixes. In this post I'll highlight some of the changes, pointing out some of my favorite Spring LDAP features. Simple Authentication Mechanism By far the most requested feature for inclusion in Spring LDAP has been the ability [...]]]></description>
			<content:encoded><![CDATA[<p>We recently released <a href="http://www.springframework.org/ldap">Spring LDAP</a> 1.3.0.RC1. This long awaited release contains a number of new features and bug fixes. In this post I'll highlight some of the changes, pointing out some of my favorite Spring LDAP features.</p>
<h3>Simple Authentication Mechanism</h3>
<p>By far the most requested feature for inclusion in Spring LDAP has been the ability to easily perform simple authentication using the library. While you would typically like to use <a href="http://static.springframework.org/spring-security/site/index.html">Spring Security</a> to implement full-blown application security many of our users have expressed the need to just do the authentication part, not having to worry about the full Spring Security framework. This is now explicitly supported with a new method in the <code>ContextSource</code> interface: <code>getContext(String principal, String password)</code>. This means that in order to do simple user authentication you would write something like the following:</p>
<pre class="java">&nbsp;
...
<span style="color: #000000; font-weight: bold;">private</span> SimpleLdapTemplate ldapTemplate;
<span style="color: #000000; font-weight: bold;">private</span> ContextSource contextSource;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setLdapTemplate<span style="color: #66cc66;">&#40;</span>SimpleLdapTemplate ldapTemplate<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">ldapTemplate</span> = ldapTemplate;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setContextSource<span style="color: #66cc66;">&#40;</span>ContextSource contextSource<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">contextSource</span> = contextSource;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> authenticate<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> userName, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> password<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	EqualsFilter filter = <span style="color: #000000; font-weight: bold;">new</span> EqualsFilter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;uid&quot;</span>, userName<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Actual filter will differ depending on LDAP Server and schema</span>
	List&lt;String&gt; results = ldapTemplate.<span style="color: #006600;">search</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>, filter.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>,
			<span style="color: #000000; font-weight: bold;">new</span> DnContextMapper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>results.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IncorrectResultSizeDataAccessException<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, results.<span style="color: #006600;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADirContext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">DirContext</span></a> ctx = <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
		ctx = contextSource.<span style="color: #006600;">getContext</span><span style="color: #66cc66;">&#40;</span>results.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, password<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
	<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #66cc66;">&#123;</span>
		LdapUtils.<span style="color: #006600;">closeContext</span><span style="color: #66cc66;">&#40;</span>ctx<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> DnContextMapper <span style="color: #000000; font-weight: bold;">extends</span>
		AbstractParameterizedContextMapper&lt;String&gt; <span style="color: #66cc66;">&#123;</span>
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> doMapFromContext<span style="color: #66cc66;">&#40;</span>DirContextOperations ctx<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> ctx.<span style="color: #006600;">getNameInNamespace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The required XML configuration for this:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ldapTemplate&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.ldap.core.LdapTemplate&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.ldap.core.support.LdapContextSource&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;ldap://my.ldap.server&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;base&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;dc=mycompany, dc=com&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;userDn&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;cn=Administrator, ou=system&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;secret&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dummy&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;se.jayway.web.Dummy&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ldapTemplate&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;ldapTemplate&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<h3>LDAP Data Management</h3>
<p>Working with data in LDAP is usually tedious and verbose. Spring LDAP relieves the programmer from explicitly worrying about the details of the underlying Attributes and encapsulates all data regarding an LDAP entry in the <code>DirContextAdapter</code> class. You can get the Attributes of an entry using a <code>DirContextAdapter</code> in a <code>ContextMapper</code> (or <code>ParameterizedContextMapper</code> like above), or you can use the Attribute management capabilities in <code>DirContextAdapter</code> to help you when performing updates or creating entries.</p>
<p>This has been one of the key features from Spring LDAP from the very beginning, and the API has been improved further in this release; particularly a new <code>bind</code> has been added in Spring LDAP, enabling even simpler standard repository code using Spring LDAP:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> create<span style="color: #66cc66;">&#40;</span>Person p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	p.<span style="color: #006600;">setDn</span><span style="color: #66cc66;">&#40;</span>buildDn<span style="color: #66cc66;">&#40;</span>p<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	DirContextOperations ctx = <span style="color: #000000; font-weight: bold;">new</span> DirContextAdapter<span style="color: #66cc66;">&#40;</span>p.<span style="color: #006600;">getDn</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	setAttributeValues<span style="color: #66cc66;">&#40;</span>p, ctx<span style="color: #66cc66;">&#41;</span>;
	ldapTemplate.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span>ctx<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> update<span style="color: #66cc66;">&#40;</span>Person p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	DirContextOperations ctx = ldapTemplate.<span style="color: #006600;">lookupContext</span><span style="color: #66cc66;">&#40;</span>p.<span style="color: #006600;">getDn</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	setAttributeValues<span style="color: #66cc66;">&#40;</span>p, ctx<span style="color: #66cc66;">&#41;</span>;
	ldapTemplate.<span style="color: #006600;">modifyAttributes</span><span style="color: #66cc66;">&#40;</span>ctx<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> setAttributeValues<span style="color: #66cc66;">&#40;</span>Person p, DirContextOperations ctx<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	ctx.<span style="color: #006600;">setAttributeValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;description&quot;</span>, p.<span style="color: #006600;">getDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	ctx.<span style="color: #006600;">setAttributeValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;telephoneNumber&quot;</span>, p.<span style="color: #006600;">getPhone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #808080; font-style: italic;">// Set more attribute values here.</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p><code>DirContextAdapter</code> now also supports entries that represent referrals. This means that if you configure your <code>ContextSource</code> to follow referrals (setting the <code>referral</code> property to <code>follow</code> and properly configuring DNS settings so that the server names of the referrals can be resolved) you can get the server URL from any <code>DirContextAdapter</code> resulting from referrals.</p>
<h3>TLS Connections</h3>
<p>It is a very common setup that the LDAP server is configured only to accept TLS connections. This has previously not been supported by Spring LDAP, but due to some internal rework in <code>AbstractContextSource</code> it is now possible to perform some more elaborate authentication and connection negotiation logic by supplying a <code>DirContextAuthenticationStrategy</code> implementation to the <code>ContextSource</code>. To enable TLS connections you will supply a <code>DefaultTlsDirContextAuthenticationStrategy</code> to your <code>LdapContextSource</code>:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.ldap.core.support.LdapContextSource&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;ldap://my.ldap.server&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;base&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;dc=mycompany, dc=com&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;userDn&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;cn=Administrator, ou=system&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;secret&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;authenticationStrategy&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span>
			<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.ldap.core.support.DefaultTlsDirContextAuthenticationStrategy&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>Authentication customization has previously often been done by subclassing <code>LdapContextSource</code>. The recommended approach from Spring LDAP 1.3 is to create a custom <code>DirContextAuthenticationStrategy</code> implementation to suit your need. This would be useful for e.g. LDAP Proxy Authentication or similar functionality.</p>
<h3>Major Bug Fixes and Other Changes</h3>
<p>Some interesting bug fixes are included in Spring LDAP. Also, some default behavior has been changed; the most important stuff is listed here:</p>
<h4>Distinguished Name toString representation</h4>
<p>It has long been requested that the Spring LDAP <code>DistinguishedName</code> <code>toString</code> representation should be changed. The <code>toString</code> representation has previously been focused on the readability of the string, adding spaces between the RDNs to make it less compact, e.g.:<br />
<code>cn=John Doe, ou=Some Company, c=Sweden</code><br />
Several users have been complaining that their DN representations have been compact and that the discrepancy has been causing problems:<br />
<code>cn=John Doe,ou=Some Company,c=Sweden</code><br />
We have changed the default string representation to the compact one in the 1.3 release. If your system should require the old, 'spaced' format, you can change the default by setting the system property <code>org.springframework.ldap.core.spacedDnFormat</code> to <code>true</code>.</p>
<h4>Built-in JNDI Connection Pooling</h4>
<p>The <code>pooled</code> property of <code>ContextSource</code> has previously defaulted to <code>true</code>, enabling the built-in Java LDAP connection pooling by default. However the built-in LDAP connection pooling suffers from several deficiencies (most notable there is no way of doing connection validation and configuration is cumbersome), which is why we decided to change the default to <code>false </code>. If you require connection pooling we strongly recommend using the Spring LDAP <a href="http://static.springframework.org/spring-ldap/docs/1.3.x/apidocs/org/springframework/ldap/pool/factory/PoolingContextSource.html"><code>PoolingContextSource</code></a> instead.</p>
<h4>The Dreaded '\' in Distinguished Names Problem</h4>
<p>Java JNDI cannot handle '\' in the Distinguished Names of entries in an LDAP tree. If they do, the DN returned from JNDI will be invalid, which previously caused Spring LDAP to throw an exception. We now work around this bug.</p>
<h3>Downloads</h3>
<p>We obviously want people to use our stuff. Here are the relevant links:<br />
<a href="http://dist.springframework.org/milestone/LDAP/spring-ldap-1.3.0.RC1.zip">Binaries</a>(<a href="http://dist.springframework.org/milestone/LDAP/spring-ldap-1.3.0.RC1.zip.sha1">sha</a>)<br />
<a href="<br />
http://dist.springframework.org/milestone/LDAP/spring-ldap-1.3.0.RC1-with-dependencies.zip">Binary With Dependencies</a>(<a href="http://dist.springframework.org/milestone/LDAP/spring-ldap-1.3.0.RC1-with-dependencies.zip.sha1">sha</a>)<br />
<a href="http://static.springframework.org/spring-ldap/docs/1.3.x/apidocs/">Javadocs</a><br />
<a href="http://static.springframework.org/spring-ldap/docs/1.3.x/reference/pdf/spring-ldap-reference.pdf">Reference docs</a></p>
<h3>Maven Users</h3>
<p>Since this is a release candidate it is not published to the main maven repository. It is however available from the Spring Framework milestone repository:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;repositories<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;repository<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;id<span style="font-weight: bold; color: black;">&gt;</span></span></span>spring-milestone<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/id<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;name<span style="font-weight: bold; color: black;">&gt;</span></span></span>Spring Portfolio Milestone Repository<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url<span style="font-weight: bold; color: black;">&gt;</span></span></span>http://s3.amazonaws.com/maven.springframework.org/milestone<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/repository<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/repositories<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>The maven dependencies are as follows:</p>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.springframework.ldap<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>spring-ldap-core<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>1.3.0.RC1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<pre class="xml">&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.springframework.ldap<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/groupId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>spring-ldap-core-tiger<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/artifactId<span style="font-weight: bold; color: black;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;version<span style="font-weight: bold; color: black;">&gt;</span></span></span>1.3.0.RC1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/version<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/dependency<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<h3>Summary</h3>
<p>In addition to the above there's quite a number of minor feature enhancements and bug fixes. The full change log can be found <a href="http://static.springframework.org/spring-ldap/docs/1.3.x/changelog.txt">here</a>. We're obviously very interested in getting your feedback. Please post any comments you might have on the <a href="http://forum.springframework.org/forumdisplay.php?f=40">Sping LDAP Support Forum</a>. Bugs should be submitted to the <a href="http://jira.springframework.org/browse/LDAP">Spring Framework Jira System</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/10/27/whats-new-in-spring-ldap-13/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Testing Among the Clouds, Part 2</title>
		<link>http://blog.jayway.com/2008/10/20/testing-among-the-clouds-part-2/</link>
		<comments>http://blog.jayway.com/2008/10/20/testing-among-the-clouds-part-2/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 14:22:56 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[automated testing]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring ldap]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[typica]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=357</guid>
		<description><![CDATA[In a recent post I wrote about the particular problems we've been having with integration testing the Spring LDAP project and the use we've made of Amazon EC2 for solving these problems. In this post I'll present the implementation details. Prerequisites In order to keep this reasonably brief I'll have to refer to the getting [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a target="_blank" href="http://blog.jayway.com/2008/09/11/testing-among-the-clouds/">recent post</a> I wrote about the particular problems we've been having with integration testing the <a target="_blank" href="http://springframework.org/ldap">Spring LDAP</a> project and the use we've made of <a target="_blank" href="http://aws.amazon.com/ec2/">Amazon EC2</a> for solving these problems. In this post I'll present the implementation details.</p>
<h3>Prerequisites</h3>
<p>In order to keep this reasonably brief I'll have to refer to the <a target="_blank" href="http://docs.amazonwebservices.com/AmazonEC2/gsg/2006-06-26/">getting started guide</a> for information on how to get going with Amazon EC2.</p>
<h3>A FactoryBean to Launch EC2 Instances</h3>
<p>What we want to achieve here is to launch an EC2 instance transparently and independently of the actual test code. Ideally, the test code should be oblivious of the target server and we want to externalize those details to external configuration. We will use Spring's excellent JUnit test support to automatically wire the integration test setup and make sure that the target server is up and running before the actual test code is running.</p>
<p>A powerful way to transparently perform complex initialization logic when using Spring is the <a target="_blank" href="http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-extension-factorybean"><code>FactoryBean</code></a> concept. We will create a <code>FactoryBean</code> implementation to launch the EC2 image and set up our target resource:</p>
<pre class="java">AbstractEc2InstanceLaunchingFactoryBean.<span style="color: #006600;">java</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> AbstractEc2InstanceLaunchingFactoryBean <span style="color: #000000; font-weight: bold;">extends</span> AbstractFactoryBean <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">int</span> INSTANCE_START_SLEEP_TIME = <span style="color: #cc66cc;">1000</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span> DEFAULT_PREPARATION_SLEEP_TIME = <span style="color: #cc66cc;">30000</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Log log = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span>AbstractEc2InstanceLaunchingFactoryBean.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> imageName;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> awsKey;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> awsSecretKey;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> keypairName;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> groupName;
  <span style="color: #000000; font-weight: bold;">private</span> Instance instance;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">long</span> preparationSleepTime = DEFAULT_PREPARATION_SLEEP_TIME;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setImageName<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> imageName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">imageName</span> = imageName;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setAwsKey<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> awsKey<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">awsKey</span> = awsKey;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setAwsSecretKey<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> awsSecretKey<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">awsSecretKey</span> = awsSecretKey;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setKeypairName<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> keypairName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">keypairName</span> = keypairName;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setGroupName<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> groupName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">groupName</span> = groupName;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setPreparationSleepTime<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">long</span> preparationSleepTime<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">preparationSleepTime</span> = preparationSleepTime;
  <span style="color: #66cc66;">&#125;</span>
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> createInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006600;">hasLength</span><span style="color: #66cc66;">&#40;</span>imageName, <span style="color: #ff0000;">&quot;ImageName must be set&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006600;">hasLength</span><span style="color: #66cc66;">&#40;</span>awsKey, <span style="color: #ff0000;">&quot;AwsKey must be set&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006600;">hasLength</span><span style="color: #66cc66;">&#40;</span>awsSecretKey, <span style="color: #ff0000;">&quot;AwsSecretKey must be set&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006600;">hasLength</span><span style="color: #66cc66;">&#40;</span>keypairName, <span style="color: #ff0000;">&quot;KeyName must be set&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006600;">hasLength</span><span style="color: #66cc66;">&#40;</span>groupName, <span style="color: #ff0000;">&quot;GroupName must be set&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      log.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Launching EC2 instance for image: &quot;</span> + imageName<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      Jec2 jec2 = <span style="color: #000000; font-weight: bold;">new</span> Jec2<span style="color: #66cc66;">&#40;</span>awsKey, awsSecretKey<span style="color: #66cc66;">&#41;</span>;
      LaunchConfiguration launchConfiguration = <span style="color: #000000; font-weight: bold;">new</span> LaunchConfiguration<span style="color: #66cc66;">&#40;</span>imageName<span style="color: #66cc66;">&#41;</span>;
      launchConfiguration.<span style="color: #006600;">setKeyName</span><span style="color: #66cc66;">&#40;</span>keypairName<span style="color: #66cc66;">&#41;</span>;
      launchConfiguration.<span style="color: #006600;">setSecurityGroup</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACollections+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Collections</span></a>.<span style="color: #006600;">singletonList</span><span style="color: #66cc66;">&#40;</span>groupName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      ReservationDescription reservationDescription = jec2.<span style="color: #006600;">runInstances</span><span style="color: #66cc66;">&#40;</span>launchConfiguration<span style="color: #66cc66;">&#41;</span>;
      instance = reservationDescription.<span style="color: #006600;">getInstances</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>!instance.<span style="color: #006600;">isRunning</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; !instance.<span style="color: #006600;">isTerminated</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          log.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Instance still starting up; sleeping &quot;</span> + INSTANCE_START_SLEEP_TIME + <span style="color: #ff0000;">&quot;ms&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a>.<span style="color: #006600;">sleep</span><span style="color: #66cc66;">&#40;</span>INSTANCE_START_SLEEP_TIME<span style="color: #66cc66;">&#41;</span>;
          reservationDescription = jec2.<span style="color: #006600;">describeInstances</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACollections+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Collections</span></a>.<span style="color: #006600;">singletonList</span><span style="color: #66cc66;">&#40;</span>instance.<span style="color: #006600;">getInstanceId</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
          instance = reservationDescription.<span style="color: #006600;">getInstances</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance.<span style="color: #006600;">isRunning</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          log.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;EC2 instance is now running&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>preparationSleepTime &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
              log.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Sleeping &quot;</span> + preparationSleepTime + <span style="color: #ff0000;">&quot;ms allowing instance services to start up properly.&quot;</span><span style="color: #66cc66;">&#41;</span>;
              <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a>.<span style="color: #006600;">sleep</span><span style="color: #66cc66;">&#40;</span>preparationSleepTime<span style="color: #66cc66;">&#41;</span>;
              log.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Instance prepared - proceeding&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
          <span style="color: #000000; font-weight: bold;">return</span> doCreateInstance<span style="color: #66cc66;">&#40;</span>instance.<span style="color: #006600;">getDnsName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AIllegalStateException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">IllegalStateException</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Failed to start a new instance&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">abstract</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> doCreateInstance<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> ip<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a>;
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> destroyInstance<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> ignored<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">instance</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      log.<span style="color: #006600;">info</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Shutting down instance&quot;</span><span style="color: #66cc66;">&#41;</span>;
      Jec2 jec2 = <span style="color: #000000; font-weight: bold;">new</span> Jec2<span style="color: #66cc66;">&#40;</span>awsKey, awsSecretKey<span style="color: #66cc66;">&#41;</span>;
      jec2.<span style="color: #006600;">terminateInstances</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ACollections+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Collections</span></a>.<span style="color: #006600;">singletonList</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">instance</span>.<span style="color: #006600;">getInstanceId</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>This superclass makes use of the <a target="_blank" href="http://code.google.com/p/typica/">typica</a> library (<a target="_blank" href="http://mvnrepository.com/artifact/com.google.code.typica/typica">also available in maven</a>) to launch the EC2 instance and delegates to <code>doCreateInstance</code> for creating the target resource that we will use in our test case. In our setup we want to create a <code>ContextSource</code>, which is the Spring LDAP equivalent of a <code>DataSource</code>:</p>
<pre class="java">ContextSourceEc2InstanceLaunchingFactoryBean.<span style="color: #006600;">java</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContextSourceEc2InstanceLaunchingFactoryBean <span style="color: #000000; font-weight: bold;">extends</span> AbstractEc2InstanceLaunchingFactoryBean <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> base;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> userDn;
  <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> password;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">Class</span> getObjectType<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> ContextSource.<span style="color: #000000; font-weight: bold;">class</span>;
  <span style="color: #66cc66;">&#125;</span>
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> doCreateInstance<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> dnsName<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006600;">hasText</span><span style="color: #66cc66;">&#40;</span>userDn<span style="color: #66cc66;">&#41;</span>;
    LdapContextSource instance = <span style="color: #000000; font-weight: bold;">new</span> LdapContextSource<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    instance.<span style="color: #006600;">setUrl</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ldap://&quot;</span> + dnsName<span style="color: #66cc66;">&#41;</span>;
    instance.<span style="color: #006600;">setUserDn</span><span style="color: #66cc66;">&#40;</span>userDn<span style="color: #66cc66;">&#41;</span>;
    instance.<span style="color: #006600;">setPassword</span><span style="color: #66cc66;">&#40;</span>password<span style="color: #66cc66;">&#41;</span>;
    instance.<span style="color: #006600;">setBase</span><span style="color: #66cc66;">&#40;</span>base<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    instance.<span style="color: #006600;">afterPropertiesSet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">return</span> instance;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setBase<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> base<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">base</span> = base;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setUserDn<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> userDn<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">userDn</span> = userDn;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setPassword<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> password<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">password</span> = password;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Note that the <code>AbstractEc2InstanceLaunchingFactoryBean</code> will wait for the instance to start up properly. It will also wait an additional period of time once the instance is up and running to allow for all relevant services to start up properly. Finally it will  automatically close down the launched instance once it is properly shut down (which it will be when executing using the Spring automated test support).</p>
<h3>Test Case Configuration</h3>
<p>We now have the infrastructure to have the EC2 instance launched transparently from a Spring Application Context. All we need to do is configure it:</p>
<pre class="xml">testContext.xml
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;location&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;ldap.properties&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;systemPropertiesModeName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;SYSTEM_PROPERTIES_MODE_OVERRIDE&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.ldap.ContextSourceEc2InstanceLaunchingFactoryBean&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;awsKey&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${aws.key}&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;awsSecretKey&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${aws.secret.key}&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;imageName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${aws.ami}&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;groupName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${aws.security.group}&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;keypairName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${aws.keypair}&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;base&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;dc=jayway,dc=se&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;userDn&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${userDn}&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${password}&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;ldapTemplate&quot;</span>
  <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.ldap.core.LdapTemplate&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;contextSource&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>We are referring to <code>ldap.properties</code> for the actual settings:</p>
<pre>
userDn=cn=admin,dc=jayway,dc=se
password=secret
base=dc=jayway,dc=se
aws.ami=ami-56ec083f
aws.keypair=spring-ldap-keypair
aws.security.group=spring-ldap
</pre>
<p>The referenced AMI is a public image specifically set up for our test cases. It has OpenLDAP installed and has been pre-populated with the test data that our test cases expect.</p>
<p>Note that we are not specifying any property value for <code>aws.key</code> and <code>aws.secret.key</code>, as this is the Amazon account access information. That will be the account settings of the individual developer and should be supplied at runtime using system properties (e.g. <code>mvn -Daws.key=xxxxxxx -Daws.secret.key=xxxxxx test</code>).</p>
<h3>Performing the Test</h3>
<p>Now all we need to do is use Spring's automated test support to start our test:</p>
<pre class="java">SomeLdapITest.<span style="color: #006600;">java</span>
@ContextConfiguration<span style="color: #66cc66;">&#40;</span>locations = <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">&quot;testContext.xml&quot;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeLdapITest <span style="color: #000000; font-weight: bold;">extends</span> AbstractJUnit4SpringContextTests <span style="color: #66cc66;">&#123;</span>
  @Autowired
  <span style="color: #000000; font-weight: bold;">private</span> LdapTemplate tested;
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testSomething<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// Use the automatically injected LdapTemplate instance to perform a test.</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<h3>Set to Go</h3>
<p>With the above you should be all set to go to start using this powerful approach to integration testing. The actual code is available with the 1.3.0.RC1 release of Spring LDAP; links to downloads and documentation available <a href="http://www.springframework.org/ldap">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/10/20/testing-among-the-clouds-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Remoting with Security and SSL</title>
		<link>http://blog.jayway.com/2008/09/30/spring-remoting-with-security-and-ssl/</link>
		<comments>http://blog.jayway.com/2008/09/30/spring-remoting-with-security-and-ssl/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 19:15:17 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring remoting]]></category>
		<category><![CDATA[spring security]]></category>
		<category><![CDATA[ssl]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=311</guid>
		<description><![CDATA[One of my favorite features of the Spring Framework is the Spring Remoting part, which enables you to expose any bean in a Spring Application Context as a remote service over HTTP. It's fast, it's easy, and it's really, really simple. Basic Spring Remoting Configuration In the general situation all you need to do is [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favorite features of the Spring Framework is the Spring Remoting part, which enables you to expose any bean in a Spring Application Context as a remote service over HTTP. It's fast, it's easy, and it's really, really simple.</p>
<h3>Basic Spring Remoting Configuration</h3>
<p>In the general situation all you need to do is create a DispatcherServlet (just as you would with any Spring MVC application), add an Exporter on the server side and reference a ProxyFactoryBean on the client.<br />
On the server side:</p>
<pre class="xml">web.xml
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;context-param<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>contextConfigLocation<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param-value<span style="font-weight: bold; color: black;">&gt;</span></span></span>/WEB-INF/applicationContext.xml<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param-value<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/context-param<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;listener<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;listener-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.springframework.web.context.ContextLoaderListener<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/listener-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/listener<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>demo<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.springframework.web.servlet.DispatcherServlet<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;init-param<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>contextConfigLocation<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;param-value<span style="font-weight: bold; color: black;">&gt;</span></span></span>/WEB-INF/demo-servlet.xml<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/param-value<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/init-param<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;load-on-startup<span style="font-weight: bold; color: black;">&gt;</span></span></span>1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/load-on-startup<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>demo<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/servlet-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<pre class="xml">demo-servlet.xml - exposes bean 'helloService' as remote service
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;/hello&quot;</span>
    <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;service&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;helloService&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceInterface&quot;</span>
      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;se.jayway.demo.server.HelloService&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>On the client side:</p>
<pre class="xml">clientContext.xml
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;helloService&quot;</span>
  <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceUrl&quot;</span>
      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;https://remote-host:8080/security-remoting/hello&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceInterface&quot;</span>
      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;se.jayway.demo.server.HelloService&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>Now, in the client application all you need to do is ask for the '<code>helloService</code>' bean and you will be handed a proxy that talks to the target service on the server without the server or the client knowing anything about it.</p>
<h3>Securing the Remote Service</h3>
<p>Now, in many cases you'll want to apply some security restrictions on the exposed HTTP service. Being in the Spring world the natural choice for this purpose will be Spring Security. Far from the complications of its predecessor Acegi, Spring Security configuration is now a matter of very few lines of XML code: </p>
<pre class="xml">web.xml
...
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>springSecurityFilterChain<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>org.springframework.web.filter.DelegatingFilterProxy<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-class<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>springSecurityFilterChain<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-name<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/url-pattern<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/filter-mapping<span style="font-weight: bold; color: black;">&gt;</span></span></span>
...
&nbsp;</pre>
<pre class="xml">demo-servlet.xml - additions to the original file above; default with one hard coded user
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;beans</span>
  <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span>
  <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
  <span style="color: #000066;">xmlns:sec</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/security&quot;</span>
  <span style="color: #000066;">xsi:schemaLocation</span>=
        <span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans 
&nbsp;
http://www.springframework.org/schema/beans/spring-beans.xsd
&nbsp;
http://www.springframework.org/schema/security
&nbsp;
         http://www.springframework.org/schema/security/spring-security-2.0.xsd&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
...
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:http</span> <span style="color: #000066;">realm</span>=<span style="color: #ff0000;">&quot;Hello App&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:http-basic</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:intercept-url</span> <span style="color: #000066;">pattern</span>=<span style="color: #ff0000;">&quot;/**&quot;</span> <span style="color: #000066;">access</span>=<span style="color: #ff0000;">&quot;ROLE_USER&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sec:http<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:authentication-provider<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:user-service<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;sec:user</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;someuser&quot;</span> <span style="color: #000066;">password</span>=<span style="color: #ff0000;">&quot;somepassword&quot;</span> <span style="color: #000066;">authorities</span>=<span style="color: #ff0000;">&quot;ROLE_USER&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sec:user-service<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/sec:authentication-provider<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
<p>Note that we're defining the Spring Security XML schema in the schema definition.</p>
<pre class="xml">clientContext.xml
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;helloService&quot;</span>
  <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceUrl&quot;</span>
      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;https://remote-host:8080/security-remoting/hello&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceInterface&quot;</span>
      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;se.jayway.demo.server.HelloService&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;httpInvokerRequestExecutor&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.context.httpinvoker.AuthenticationSimpleHttpInvokerRequestExecutor  /&gt;</span>
  <span style="color: #009900;">&lt;/property&gt;</span>
<span style="color: #009900;">&lt;/bean&gt;</span>
</span></pre>
<p>The <code>AuthenticationSimpleHttpInvokerRequestExecutor</code> will make sure that any Spring Security applied on the client side will be transferred to the server side using Basic HTTP Authentication. The filters and the XML configuration on the server side will make sure the Authentication headers are inspected and checked against the valid principals and credentials.</p>
<h3>Applying SSL</h3>
<p>As most of you probably know, Basic HTTP Authentication is pretty much the same thing as sending the authentication information over the network in plain text. This is why you will typically want to use encrypted connections whenever you are working with this type of authentication. This gets us into the core of this post, because it's here it becomes tricky.</p>
<p>In the ideal world you would just configure your web server to expose the service over HTTPS, change the target URL on the client side and be on your way. The reality however is slightly more complicated. </p>
<p>The problem is that you the built-in <code>HttpURLConnection</code> class on which the <code>AuthenticationSimpleHttpInvokerRequestExecutor</code> relies is very picky when it comes to certificates. What you want to do when working with SSL in Spring Remoting is to use the <code>CommonsHttpInvokerRequestExecutor</code>, which relies on Commons HttpClient - a more flexible and capable HTTP client. Now, the problem with this is that then you cannot use the <code>AuthenticationSimpleHttpInvokerRequestExecutor</code> anymore - they plug into the <code>HttpInvokerProxyFactoryBean</code> at the same extension point.</p>
<p>It boils down to this: if you want to use Spring Remoting and Spring Security over SSL you will need to implement your own <code>HttpInvokerRequestExecutor</code>:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BasicAuthenticationCommonsHttpInvokerRequestExecutor <span style="color: #000000; font-weight: bold;">extends</span>
  CommonsHttpInvokerRequestExecutor <span style="color: #66cc66;">&#123;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">protected</span> PostMethod createPostMethod<span style="color: #66cc66;">&#40;</span>HttpInvokerClientConfiguration config<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AIOException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span>
    PostMethod postMethod = <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">createPostMethod</span><span style="color: #66cc66;">&#40;</span>config<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    Authentication auth =
        SecurityContextHolder.<span style="color: #006600;">getContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getAuthentication</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>auth != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <span style="color: #66cc66;">&#40;</span>auth.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> &amp;&amp;
          <span style="color: #66cc66;">&#40;</span>auth.<span style="color: #006600;">getCredentials</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> base64 = auth.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot;:&quot;</span> + auth.<span style="color: #006600;">getCredentials</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
      postMethod.<span style="color: #006600;">setRequestHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Authorization&quot;</span>, <span style="color: #ff0000;">&quot;Basic &quot;</span> +
          <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#40;</span>Base64.<span style="color: #006600;">encodeBase64</span><span style="color: #66cc66;">&#40;</span>base64.<span style="color: #006600;">getBytes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> postMethod;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Now all you need to do is specify this implementation as <code>HttpInvokerRequestExecutor</code> for your client ProxyFactoryBean and you're all set:</p>
<pre class="xml">clientContext.xml
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;helloService&quot;</span>
  <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceUrl&quot;</span>
      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;https://remote-host:8080/security-remoting/hello&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceInterface&quot;</span>
      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;se.jayway.demo.server.HelloService&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;httpInvokerRequestExecutor&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;se.jayway.demo.security.BasicAuthenticationCommonsHttpInvokerRequestExecutor&quot;</span>  <span style="font-weight: bold; color: black;">/&gt;</span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/09/30/spring-remoting-with-security-and-ssl/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Testing Among the Clouds</title>
		<link>http://blog.jayway.com/2008/09/11/testing-among-the-clouds/</link>
		<comments>http://blog.jayway.com/2008/09/11/testing-among-the-clouds/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 08:53:26 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[automated testing]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring ldap]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[typica]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=77</guid>
		<description><![CDATA[One of the major challenges we've been facing in the Spring LDAP project is to make certain that the library works together with different LDAP servers. Different servers behave differently in certain situations; some functionality might only be supported on select servers, etc. In the ideal situation we would run our automated test suite against [...]]]></description>
			<content:encoded><![CDATA[<p>One of the major challenges we've been facing in the <a title="Spring LDAP" href="http://springframework.org/ldap" target="_blank">Spring LDAP</a> project is to make certain that the library works together with different LDAP servers. Different servers behave differently in certain situations; some functionality might only be supported on select servers, etc. In the ideal situation we would run our automated test suite against a multitude of target platforms for each change made to the code. Each developer would be able to run the test suite locally against each of these platforms, and the <a href="http://build.springframework.org:8085/browse/LDAP" target="_blank">continuous integration server</a> would automatically run them on each commit. This is the key element to automated testing: the automated tests must be relevant, and they must be quickly and easily run at any time, by any member of the team (or - in this case, being an open source project - by anyone that might have checked the source out from subversion). These conditions are imperative for any type of automated tests - if the conditions are not fulfilled the tests will not be run at all and consequently they will die.</p>
<p>In the perfect world, each developer would at all times have access to a server park with all of these different server versions installed. Reality is however almost always a different question: Being an Open Source project with limited funding and developers located throughout the world this has been impossible for us, up until now. You're probably already guessing: The answer is in the Clouds.</p>
<p>Having used <a href="http://www.amazon.com/gp/browse.html?node=201590011" target="_blank">Amazon EC2</a> in a couple of customer projects, we suddenly realized this is the answer to our problems. For those of you not completely familiar with the concept, the general idea of Amazon EC2 is that you configure one or several <strong>machine images</strong>, installing whatever software and data you might need on each image. You will then start an <strong>instance</strong> from one of these pre-configured images, which will in effect start up a virtual computer somewhere in the amazon cloud, a computer that will then be yours to play around with in any way you might like until you're finished with it and just shut the instance down. All of this is available at a very reasonable pricing - you pay only (a very modest amount) for the time that your instances have been running.</p>
<p>The concept suits us perfectly: we want a number of isolated environments, each with a known start state where we can run our test suite against a particular server setup. So that's what we've started to do: we have now created an EC2 image with an Open LDAP installation, pre-loaded with the data that our test cases expect. We then devised support classes to have the JUnit test cases automatically start an appropriate instance (using the excellent <a href="http://code.google.com/p/typica/" target="_blank">typica</a> library), run the tests against the started virtual machines, and shut down the instance after the tests are finished (regardless of the outcome - we don't want any stray instances running around costing us money <img src='http://blog.jayway.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ). We still have some more work to do with setting up more images for other server setups, but we're hoping to get that work going as we move on...</p>
<p>I'm planning to dig a little bit deeper into the details of the setup and the code involved in a later post. In the meantime: if you're really interested, feel free to check the code out from the <a href="https://springframework.svn.sourceforge.net/svnroot/springframework/spring-ldap/trunk/mvn-build" target="_blank">svn repo</a> and have a look at what we've done so far. The relevant code for this is in the test/integration-tests-openldap directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2008/09/11/testing-among-the-clouds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring LDAP – LDAP Programming in Java Made Simple</title>
		<link>http://blog.jayway.com/2006/10/01/spring-ldap-%e2%80%93-ldap-programming-in-java-made-simple/</link>
		<comments>http://blog.jayway.com/2006/10/01/spring-ldap-%e2%80%93-ldap-programming-in-java-made-simple/#comments</comments>
		<pubDate>Sun, 01 Oct 2006 15:51:08 +0000</pubDate>
		<dc:creator>Mattias Hellborg Arthursson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[jayview]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring ldap]]></category>

		<guid isPermaLink="false">http://blog.jayway.com/?p=3809</guid>
		<description><![CDATA[The Java Naming and Directory Interface (JNDI) is for LDAP programming what Java Database Connectivity (JDBC) is for SQL programming. There are several similarities between JDBC and JNDI/LDAP (Java LDAP). Despite being two completely different APIs with different pros and cons, they share a number of less flattering characteristics: They require extensive plumbing code, even [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Java Naming and Directory Interface (JNDI) is for LDAP programming what Java Database Connectivity (JDBC) is for SQL programming. There are several similarities between JDBC and JNDI/LDAP (Java LDAP). Despite being two completely different APIs with different pros and cons, they share a number of less flattering characteristics:<br />
</strong></p>
<ul>
<li> They require extensive plumbing code, even to perform the simplest of tasks.
<li> All resources need to be correctly closed, no matter what happens.
<li> Exception handling is difficult.
</ul>
<p>The above points often lead to massive code duplication in common usages of the<br />
APIs. As we all know, code duplication is one of the worst code smells. All in all, it<br />
boils down to this: JDBC and LDAP programming in Java are both incredibly dull<br />
and repetitive.<br />
Spring JDBC, a part of the Spring framework, provides excellent utilities for sim-<br />
plifying SQL programming. We need a similar framework for Java LDAP program-<br />
ming. </p>
<h2>The Traditional Way </h2>
<p>Consider, for example, a method that should search some storage for all persons and<br />
return their names in a list. Using JDBC, we would create a connection and execute<br />
a query using a statement. We would then loop over the result set and retrieve the<br />
column we want, adding it to a list. By contrast, using Java LDAP, we would create a<br />
context and perform a search using a search filter. We would then loop over the re-<br />
sulting naming enumeration and retrieve the attribute we want, adding it to a list.<br />
The traditional way of implementing this person name search method in Java<br />
LDAP is this: </p>
<pre>public class TraditionalPersonDaoImpl implements
         PersonDao {
   public List getAllPersonNames() {
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,
         ”com.sun.jndi.ldap.LdapCtxFactory”);
      env.put(Context.PROVIDER_URL,
         ”ldap://localhost:389/dc=jayway,dc=se”);
      DirContext ctx;
      try {
         ctx = new InitialDirContext(env);
      } catch (NamingException e) {
         throw new RuntimeException(e);
      }
      LinkedList list = new LinkedList();
      NamingEnumeration results = null;
      try {
         SearchControls controls =
            new SearchControls();
         controls.setSearchScope(
            SearchControls.SUBTREE_SCOPE);
         results = ctx.search(
            ””, ”(objectclass=person)”, controls);
         while (results.hasMore()) {
            SearchResult searchResult =
               (SearchResult) results.next();
            Attributes attributes =
               searchResult.getAttributes();
            Attribute attr = attributes.get(”cn”);
            String cn = (String) attr.get();
            list.add(cn);
         }
      } catch (NameNotFoundException e) {
         // The base context was not found.
         // Just clean up and exit.
      } catch (NamingException e) {
         throw new RuntimeException(e);
      } finally {
         if (results != null) {
            try {
               results.close();
            } catch (Exception e) {
               // Never mind this.
            }
         }
         if (ctx != null) {
            try {
               ctx.close();
            } catch (Exception e) {
               // Never mind this.
            }
         }
      }
      return list;
   }
} </pre>
<p>The method above produces a list containing the cn (common name) attribute of<br />
the person objects found in the database. It is important to always close the naming<br />
enumeration and the context. The same procedural manner of programming needs<br />
to be used whether you want to search, create, update or delete data. It’s easy to see<br />
how this leads to massive code duplication. </p>
<h2>Introducing Spring LDAP </h2>
<p>The above made us realize the need for help, which is why we created the Spring<br />
LDAP project. It’s a library for simpler LDAP programming in Java, built on the<br />
same principles as Spring JDBC. It was recently included as a Spring Framework<br />
sub project.<br />
Spring LDAP completely eliminates the need to worry about creating and closing<br />
<strong>DirContext</strong> and looping through <strong>NamingEnumeration</strong>. It also provides a more<br />
comprehensive unchecked exception hierarchy, built on Spring’s <strong>DataAccessEx-<br />
ception</strong>. As a bonus, it also contains classes for dynamically building LDAP filters<br />
and distinguished names. An LDAP filter corresponds somewhat to the WHERE<br />
clause in a SQL SELECT statement. A distinguished name (dn) can be seen as a<br />
handle or the path to a specific object in the LDAP database. If the dn is available,<br />
an object can be looked up directly, rather than having to be searched for.<br />
This article will present the core features of Spring LDAP and demonstrate how<br />
LDAP programming really can be made simple. </p>
<h2>Searches using AttributesMapper </h2>
<p>In this example, we will use an <strong>AttributesMapper</strong> to easily build a list of all common<br />
names of all person objects. This is exactly what we did in the earlier example using<br />
the traditional way. </p>
<pre>public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   public void setLdapTemplate(LdapTemplate ldapTemplate) {
      this.ldapTemplate = ldapTemplate;
   }
   public List getAllPersonNames() {
      return ldapTemplate.search(
         ””, ”(objectclass=person)”,
         new AttributesMapper() {
            public Object mapFromAttributes(Attributes attrs)
               throws NamingException {
               return attrs.get(”cn”).get();
            }
         });
   }
}
</pre>
<p>The inline implementation of <strong>AttributesMapper</strong> just gets the desired attribute<br />
value from the <strong>Attributes</strong> and returns it. Internally, Spring LDAP iterates over<br />
all entries found, calling the given <strong>AttributesMapper</strong> for each entry, and collects<br />
the results in a list. The list is then returned by the search method.<br />
Note that the <strong>AttributesMapper</strong> implementation could easily be modified to<br />
return a full Person object: </p>
<pre>public class PersonDaoImpl implements PersonDao {
   private LdapTemplate ldapTemplate;
   ...
   private static class PersonAttributesMapper
      implements AttributesMapper {
      public Object mapFromAttributes(Attributes attrs)
         throws NamingException {
         Person person = new Person();
         person.setFullName((String)attrs.get(”cn”).get());
         person.setLastName((String)attrs.get(”sn”).get());
         person.setDescription((String)attrs.get(”description”).get());
         return person;
      }
   }
   public List getAllPersons() {
      return ldapTemplate.search(
         ””, ”(objectclass=person)”,
         new PersonAttributesMapper();
   }
}
</pre>
<h2>Dynamically Building Distinguished Names </h2>
<p>The standard <strong>Name</strong> interface represents a generic name, which is basically an or-<br />
dered sequence of components. The <strong>Name</strong> interface also provides operations on that<br />
sequence; e.g., add or remove. Spring LDAP provides an implementation of the<br />
<strong>Name</strong> interface: <strong>DistinguishedName</strong>. Using this class greatly simplifies building<br />
distinguished names, especially considering the sometimes complex rules regarding<br />
escapings and encodings. The following example illustrates how <strong>Distinguished-<br />
Name</strong> can be used to dynamically construct a distinguished name:<br />
public static final String BASE_DN = ”dc=jayway, dc=se”; </p>
<pre>...
protected Name buildDn(Person p) {
   DistinguishedName dn = new DistinguishedName(BASE_DN);
   dn.add(”c”, p.getCountry());
   dn.add(”ou”, p.getCompany());
   dn.add(”cn”, p.getFullname());
   return dn;
}
</pre>
<p>Assuming that a Person has the following attributes: </p>
<ul>
<li> <strong>country:</strong> Sweden
<li> <strong>company:</strong> Some Company
<li> <strong>fullname:</strong> Some Person<br />
then the result of the code above would be the following dn: <strong>cn=Some<br />
Person, ou=Some Company, c=Sweden, dc=jayway, dc=se </strong>
</ul>
<h2>Managing Attributes Using the DirContextAdapter </h2>
<p>As demonstrated above, values in LDAP are managed using <strong>Attributes</strong>. Working<br />
with <strong>Attributes</strong> is as dull and verbose as the rest of the standard LDAP API. This<br />
is why the Spring LDAP library provides the <strong>DirContextAdapter</strong>.<br />
Whenever a context is found in the LDAP tree, Spring LDAP will use its <strong>At-<br />
tributes</strong> to construct a <strong>DirContextAdapter</strong>. This enables us to use a <strong>Con-<br />
textMapper</strong> instead of an <strong>AttributesMapper</strong> to transform found values: </p>
<pre>public class PersonDaoImpl implements PersonDao {
   ...
   private static class PersonContextMapper
      implements ContextMapper {
      public Object mapFromContext(Object ctx) {
         DirContextAdapter context = (DirContextAdapter)ctx;
         Person p = new Person();
         p.setFullName(context.getStringAttribute(”cn”));
         p.setLastName(context.getStringAttribute(”sn”));
         p.setDescription(context.getStringAttribute(”description”));
         return p;
      }
   }
   public List getAllPersons() {
      return ldapTemplate.search(
         ””, ”(objectclass=person)”,
         new PersonContextMapper();
   }
}
</pre>
<p>While useful in searches and lookups, the <strong>DirContextAdapter</strong> is especially use-<br />
ful when creating and updating data, which will be described below. </p>
<h2>Binding Data </h2>
<p>Inserting data in Java LDAP is called binding. In order to do that, a distinguished<br />
name that uniquely identifies the new entry is required. The following example<br />
shows how data is bound using Spring LDAP:</p>
<pre>public void create(Person p) {
   Name dn = buildDn(p);
   DirContextAdapter context = new DirContextAdapter(dn);
   context.setAttributeValues(”objectclass”,
      new String[] {”top”, ”person”});
   context.setAttributeValue(”cn”, p.getFullname());
   context.setAttributeValue(”sn”, p.getLastname());
   context.setAttributeValue(”description”,
      p.getDescription());
   ldapTemplate.bind(dn, context, null);
}
</pre>
<h2>Unbinding Data</h2>
<p>Removing data in Java LDAP is called unbinding. A distinguished name (dn) is re-<br />
quired to identify the entry, just as in the binding operation. The following example<br />
shows how data is unbound using Spring LDAP:<br />
public class PersonDaoImpl implements PersonDao { </p>
<pre>   ...
   public void delete(Person p) {
      Name dn = buildDn(p);
      ldapTemplate.unbind(dn);
   }
}
Modifying Data
In Java LDAP, data is usually modified using modifyAttributes. Using the mo-
difyAttributes method you need to keep track of the changes and construct a
ModificationItem array with the updated data - once again very tedious work.
Luckily, DirContextAdapter does this for us:
public void update(Person p) {
   Name dn = buildDn(p);
   DirContextAdapter context =
      (DirContextAdapter)ldapTemplate.lookup(dn);
   context.setAttributeValues(”objectclass”,
      new String[] {”top”, ”person”});
   context.setAttributeValue(”cn”, p.getFullname());
   context.setAttributeValue(”sn”, p.getLastname());
   context.setAttributeValue(”description”,
      p.getDescription());
   ldapTemplate.modifyAttributes(dn,
      context.getModificationItems());
}
</pre>
<h2>Conclusion </h2>
<p>We have seen how Spring LDAP can provide substantial improvements compared<br />
to code using JNDI directly. There is no longer any need for creating contexts manu-<br />
ally or looping through naming enumerations. Nor is there any risk of forgetting to<br />
close any of those.<br />
By now it should be clear that the Spring LDAP library will be of great help for<br />
any Java project that communicates with LDAP. Further information is available on<br />
the Spring LDAP home page, including a full reference manual, API documenta-<br />
tion, and links for downloading the library. </p>
<p><em>Mattias Arthursson and Ulrik Sandberg</em></p>
<h2>Resources</h2>
<p>Spring LDAP home page<br />
<a href="http://www.springframework.org/ldap">http://www.springframework.org/ldap</a><br />
Sun's JNDI pages:<br />
<a href="http://java.sun.com/products/jndi/">http://java.sun.com/products/jndi/</a></p>
<p><em>Originally published in <a href="http://jayway.se/jayview">JayView</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jayway.com/2006/10/01/spring-ldap-%e2%80%93-ldap-programming-in-java-made-simple/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

