<?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>Technical Notes</title>
	<atom:link href="http://blog.anthonychaves.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.anthonychaves.net</link>
	<description>Life is software and jujitsu</description>
	<lastBuildDate>Tue, 16 Feb 2010 22:15:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Why won&#8217;t my span respond to click events? Strings vs. DOM elements</title>
		<link>http://blog.anthonychaves.net/2010/02/16/why-wont-my-span-respond-to-click-events-strings-vs-dom-elements/</link>
		<comments>http://blog.anthonychaves.net/2010/02/16/why-wont-my-span-respond-to-click-events-strings-vs-dom-elements/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 22:11:44 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=185</guid>
		<description><![CDATA[I had another fun opportunity to work on a web app (I&#8217;ve been doing a lot of them lately) over the last week. This time I built on my recent JSON experience by writing a Rails app that only communicates in JSON. Rather than have the output built using Rails views the output is sent [...]]]></description>
			<content:encoded><![CDATA[<p>I had another fun opportunity to work on a web app (I&#8217;ve been doing a lot of them lately) over the last week.  This time I built on my recent JSON experience by writing a Rails app that only communicates in JSON.  Rather than have the output built using Rails views the output is sent as JSON to a JavaScript client in the user&#8217;s web browser.</p>
<p>Once in the web browser, the data has to be parsed and split up into different sentences based on what the user wants to do with the data.  Once the data is split into sentences it&#8217;s appended to divs that show the sentences in the web browser.  Some of these sentences have to be clickable, they must respond to a &#8220;click&#8221; event coming from the user.  This is completely ordinary in most JavaScript web apps.  The user clicks, the browser responds.  Imagine my surprise when my sentences weren&#8217;t responding to the click input.</p>
<p>I wasn&#8217;t surprised for very long, however.  By looking at how I prepared the sentence in the JavaScript code made it pretty obvious what was going on.</p>
<pre name="code" class="javascript">
data[index] = data[index] + ". &lt;span class=\"my_class\">" + choices[choiceIndex++] + "&lt;/span>";
</pre>
<p>This created the sentence that was to be clickable.  It&#8217;s just string data that creates a span with a class.  This is something jQuery can find by doing $(&#8220;.my_class&#8221;).  Once I have all the spans with that class I can add a click event to them.  Well, that&#8217;s true, but those spans have to be DOM objects before jQuery can find them.</p>
<p>The way I was doing it, those spans were just string data.  Sure, the browser displayed them with the correct style but jQuery was unable to find them because they were not internalized as DOM objects.  Opening a JavaScript console with Google Chrome would allow me to run the same code to make the spans respond to click events and it would actually work.  Chrome&#8217;s console reads the DOM after the page has been rendered and creates the DOM objects based on the rendered page when the spans are in place.  This is no good to me.  I can&#8217;t tell my customer to tell his customers to open a JavaScript console and run this cryptic command after loading the page.  </p>
<p>In order to get the behavior we want we have to create DOM objects using jQuery so jQuery can then find these objects to add the click behavior to them.  Rather than creating spans as string data we use the jQuery function to create a DOM element with the correct class and text.</p>
<pre name="code" class="javascript">
elm = $("&lt;span class=\"my_class\">" + choices[choiceIndex++] + ". &lt;/span>");
elm.click(function(){
                  alert("Hey! You got the right answer!");
                  document.location = "http://localhost:3000";
                });
</pre>
<p>The jQuery function, $( ), first looks for elements based on the class, id or attribute selector passed in to it.  If there is no selector then the function looks for any tags in the string passed in to it.  If there are tags, in our case span tags, jQuery creates the appropriate DOM element based on the content of those tags!  This is great!  Now I can pass in my string data and have jQuery return a DOM element that I then bind to the click function specified.</p>
<p>Adding the click function later also works now.  </p>
<pre name="code" class="javascript">
$(".my_class").click(function() {
                  alert("Hey! You got the right answer!");
                  document.location = "http://localhost:3000";
                });
</pre>
<p>I&#8217;m happy with the way I can build a DOM element using the jQuery function.  In order for jQuery to find the objects we look for those objects have to be DOM elements, not just string data appended to a div.  DOM elements respond to events, strings do not.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2010/02/16/why-wont-my-span-respond-to-click-events-strings-vs-dom-elements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>User authorization in RESTful Spring</title>
		<link>http://blog.anthonychaves.net/2010/02/06/user-authorization-in-restful-spring/</link>
		<comments>http://blog.anthonychaves.net/2010/02/06/user-authorization-in-restful-spring/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 17:46:00 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[interceptor]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=181</guid>
		<description><![CDATA[I&#8217;m having a lot of fun with the new RESTful features in Spring 3.0. I&#8217;m sure you could tell by the number of RESTful Spring posts lately. Last night I wrote a user authorization interceptor that makes sure there is a user associated with each resource request that requires it. I&#8217;m structuring most of my [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m having a lot of fun with the new RESTful features in Spring 3.0.  I&#8217;m sure you could tell by the number of RESTful Spring posts lately.</p>
<p>Last night I wrote a user authorization interceptor that makes sure there is a user associated with each resource request that requires it.  I&#8217;m structuring most of my new web apps in such a way that user authorization is required for almost all actions.  I had to make a choice of how to enforce user authorization in the app.  The two choices were between a Servlet filter and a HandlerInterceptor.</p>
<p>A Servlet Filter is part of the Java EE Servlet package.  Most of my Java web projects use Servlet Filters in some way so I wasn&#8217;t immediately opposed to using one to make sure there is a logged in user associated with a request.  Because I wanted possible errors to be properly rendered in my different view types using Spring I decided to use a HandlerInterceptor.</p>
<p>A HandlerInterceptor is a Spring interface that is pretty much analogous to a Servlet Filter in terms of structure, though it&#8217;s a little more focused in purposed.  The HandlerInterceptor is mostly concerned with text-based content, where Filters can be applied to any content type.  Filters are also more appropriate for setting headers and gzipping responses.  The Handler Interceptor is targeted for application logic.</p>
<p>This interceptor only checks to see that there is a user object associated with an resource request before passing it on to the handler (a controller).  In practice this may include more detailed authorization checks but for the example just checking for a user is fine.</p>
<pre name="code" class="java">
@Component
public class UserAuthorizationInterceptor extends HandlerInterceptorAdapter {

  private static final String UNAUTHORIZED_MSG = "You are not logged in.  You must log in or supply an API token.";

  @Override
  public boolean preHandle(HttpServletRequest request,
                        HttpServletResponse response,
                        Object handler) throws Exception {

    String uri = request.getRequestURI();
    User user = (User) request.getSession().getAttribute("user");

    if (user != null || uri.indexOf("login") != -1) {
      return true;
    } else {
      response.sendError(HttpServletResponse.SC_FORBIDDEN, UNAUTHORIZED_MSG);
      return false;
    }
  }
}
</pre>
<p>Initially I wanted any error messages to be rendered by my Atom, RSS and JSON views.  Any error message generated by the application should come back to the user in the requested data format.  But this is a RESTful app.  The REST philosophy already addresses the only error message that should come out of an authorization check with <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">HTTP status codes</a>.  The only error message that should come out of this interceptor is an &#8220;unauthorized&#8221; message.  HTTP supplies the 401 status code to indicate a user is unauthorized.  </p>
<p>The implication of using the 401 code is that the user should use HTTP authentication, either basic or digest.  That&#8217;s not appropriate for an application using OpenID and API tokens for user authentication.  Instead the 403 status code is more appropriate.  This means the client is forbidden from accessing the requested resource.  The server understood the request but the client is not allowed to access the resource, which is exactly the case when there is no logged in user associated with the request.  </p>
<p>Because HTTP already provides plenty of status codes for things that can go wrong we should rely on those codes when building RESTful applications.  There is no reason to supply an application-generated error message when the protocol supplies everything we need to deal with the problem.  The REST philosophy actually makes it much easier to write web apps because we&#8217;re using HTTP as it is intended to be used which takes a lot of responsibility off of our application code.  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2010/02/06/user-authorization-in-restful-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring 3.0 Web MVC and JSON</title>
		<link>http://blog.anthonychaves.net/2010/02/01/spring-3-0-web-mvc-and-json/</link>
		<comments>http://blog.anthonychaves.net/2010/02/01/spring-3-0-web-mvc-and-json/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 14:24:55 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[web mvc]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=175</guid>
		<description><![CDATA[Over the weekend I got a chance to use Spring 3.0&#8242;s MappingJacksonJsonView and ContentNegotiatingViewResolver. I&#8217;m very happy with how easy it was to integrate with my code. I didn&#8217;t have to do very much at all to get JSON out of my web app. Why would I want JSON out of the app instead of [...]]]></description>
			<content:encoded><![CDATA[<p>Over the weekend I got a chance to use Spring 3.0&#8242;s MappingJacksonJsonView and ContentNegotiatingViewResolver.  I&#8217;m very happy with how easy it was to integrate with my code.  I didn&#8217;t have to do very much at all to get JSON out of my web app.</p>
<p>Why would I want JSON out of the app instead of HTML?  Interoperability is one reason.  Now instead of being bound to one web app, my own, my data can be shuttled to other apps in a commonly understood notation without screen scraping.  Of course we can also use Atom and RSS for data that can be massaged into those formats with the ContentNegotiatingViewResolver as well.  For stuff that can&#8217;t coerced into standardized XML, JSON is a great choice.</p>
<p>If you don&#8217;t know what JSON is, it&#8217;s basically a JavaScript text representation of a map that drills down into your object hierarchy if you have one.  Let&#8217;s say I want to get my user information.  The JSON representation is:</p>
<pre name="code" class="js">
{ "firstname":"Anthony", "lastname":"Chaves", "userid":"123456", "address":{ "street":"A Fake Street", "city": "Bradford", "state":"MA" } }
</pre>
<p>This JSON is valid JavaScript and parsed by the library of your choice.  The first three fields are just string data.  The address field is another map that contains data about my address.  This is produced because my Java class User has a field of class Address.  Address contains the street, city and state fields.  </p>
<p>To get the JSON output we should use a MappingJacksonJsonView.  This view uses the Jackson library to map our Java objects to JSON.  We  need the Jackson core and mapper jars on our classpath to make this view work.  I just copied them into my WEB-INF/lib directory.  </p>
<p>It should be noted that I could not get any combination of configuration and Jackson jar files to work using Glassfish 2.1.1.  Deploying the app failed due to a java.lang.VerifyError with the message &#8220;Cannot extend a final class.&#8221;  Duh.  The same app worked right from the start on Tomcat 6.0.24.  </p>
<p>Because we want to preserve the functionality of the web app and its HTML output we can&#8217;t cut out the UrlBasedViewResolver completely.  We want the app to function as normal when we get requests for .html files but return JSON when we get requests for .json files.  So user.html would be used to get the HTML output of a user page and user.json would be used to get the above JSON output for a different app.  </p>
<p>Spring 3.0 has a ContentNegotiatingViewResolver that does just this.  Based on the client accept header and the file extension, this view resolver determines the best view resolver to pass the request on to after the controller does its processing.  </p>
<pre name="code" class="xml">
&lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  &lt;property name="mediaTypes">
    &lt;map>
      &lt;entry key="html" value="text/html"/>
      &lt;entry key="json" value="application/json"/>
    &lt;/map>
  &lt;/property>
  &lt;property name="viewResolvers">
    &lt;list>
      &lt;bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        &lt;property name="prefix" value="/WEB-INF/jsp/"/>
        &lt;property name="suffix" value=".jsp"/>
      &lt;/bean>
    &lt;/list>
  &lt;/property>
  &lt;property name="defaultViews">
    &lt;list>
      &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        &lt;property name="prefixJson" value="true"/>
      &lt;/bean>
    &lt;/list>
  &lt;/property>
&lt;/bean>
</pre>
<p>For text/html content the ContentNegotiatingViewResolver sends requests on to the old UrlBasedViewResolver after the controller method returns.  For application/json or application/javascript requests the ContentNegotiatingViewResolver passes the request on to the MappingJacksonJsonView.  This view parses the object tree into JSON and gives us the output we want.  </p>
<p>The object tree is based on the objects in the Spring Web MVC model.  This means we have to add them to a ModelMap object in the controller.  In an HTML-based web app we might store data in the HttpSession object.  For JSON we need it in the ModelMap.  Let&#8217;s say we&#8217;re adding a tag to something.</p>
<pre name="code" class="java">
@RequestMapping(method=RequestMethod.GET)
public String getBookmarksWithTag(@RequestParam(value="tag") String tag, HttpSession session, ModelMap model) {
  User user = (User) session.getAttribute("user");

 doSomethingUseful(user);

  model.addAttribute(user);
  return "tags";
}
</pre>
<p>By placing the User object into the ModelMap, Spring has a reference to it when it comes time to make some JSON.  We still return a string, &#8220;tags&#8221;, so the UrlBasedViewResolver still returns the same jsp for text/html content.  After this controller method returns, Spring takes the result and sends it to the ContentNegotiatingViewResolver.  The &#8220;tags&#8221; return value is not used if the request is sent to the MappingJacksonJsonView.  It has no use for jsps.  If the request were for text/html the &#8220;tags&#8221; string would be used by the UrlBasedViewResolver.</p>
<p>Like I said, I&#8217;m happy with the new REST support in Spring 3.0 Web MVC.  It&#8217;s certainly lagging behind Rails in trendy features but it&#8217;s far more flexible overall.  There is a certain &#8220;style&#8221; to writing web apps that Rails guys have.  I&#8217;m convinced it&#8217;s just as easy to use this style in Spring as it is to do it in Rails but the huge amount of extraneous documentation and classes in Spring makes it more difficult than it should be to get down to business.  I&#8217;m not making a statement about the Spring framework itself, it&#8217;s certainly possible to do everything I want and need.  I just haven&#8217;t seen a good style guide or cookbook for RESTful web apps.  Maybe it&#8217;s because some of the features are still so new, being just added to the 3.0 branch.  Maybe we just haven&#8217;t found the right idioms yet.  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2010/02/01/spring-3-0-web-mvc-and-json/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Login Tokens with Java Servlets</title>
		<link>http://blog.anthonychaves.net/2010/01/28/login-tokens-with-java-servlets/</link>
		<comments>http://blog.anthonychaves.net/2010/01/28/login-tokens-with-java-servlets/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 02:32:04 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=164</guid>
		<description><![CDATA[If you don&#8217;t want to read the whole thing, here is the short version: I wrote a servlet filter to log a user in from a token cookie that does not contain a user name and password. It is released under the GPLv3 license. It&#8217;s on github. Please use it, please test it, please send [...]]]></description>
			<content:encoded><![CDATA[<p>If you don&#8217;t want to read the whole thing, here is the short version: I wrote a servlet filter to log a user in from a token cookie that does not contain a user name and password.  It is released under the GPLv3 license.  It&#8217;s on github.  Please use it, please test it, please send feedback and bug fixes.</p>
<p>Let&#8217;s talk about user authentication in web apps.  When a web app keeps track of my data, whether it&#8217;s bookmarks, book orders or bank data, I expect to log in with a reasonably secured username and password.  If I&#8217;m authenticated the web app sets up a cookie to track my session.  There are two parts to a session, the client side &#8211; a cookie is placed in my browser with a session id, and the server side &#8211; the app server uses a map with my user data keyed by my session id stored in the cookie sent back to me.</p>
<p>Once my browser has a session cookie it sends that cookie back to the web app with every HTTP request I make.  The app server uses the session id in that cookie to load my user data and, say, add a book to my shopping cart.  My user data is updated in the session and the web page I requested is sent back to the browser.  The next time I make a request for a web page the session cookie is sent back to the web app and again my user data is loaded from the session map.  Though this approach violates a strict RESTful architecture it&#8217;s the best we&#8217;ve got for now.</p>
<p>As long as I keep requesting web pages while my session is active I keep delaying the session timeout on the app server.  A session typically times out on an app server due to the cost of keeping all user data in memory in a session map.  With limited memory we have to timeout inactive user sessions so that the app server can serve active users more effectively (i.e. without crashing).  If I wait 5 minutes between web requests I&#8217;m likely to find my bank session timed out and I have to log in again.  When ordering books that timeout period may be 30 minutes or longer.  It seems like the more valuable the data, the shorter the session timeout.  This is to prevent another user from coming in and reusing a session tracking cookie for &#8220;bad things&#8221; like transferring all my money to his account.</p>
<p>This brings up the point that though I still have a session id in my browser, the session on the server side no longer exists.  What if I wasn&#8217;t done shopping for books and computer parts?  I have to log in again and start a new session &#8211; my browser gets a new cookie with a new session id and the app server starts a new session keyed by my session id.  My user data, hopefully my up-to-date shopping cart, is loaded into my new session and I may continue shopping with another 30 minute or so timeout.  If I keep requesting web pages my session timeout keeps moving to expire 30 minutes after my last page request.  I say 30 minutes but it could be any arbitrary period.  </p>
<p>Expiring user sessions is a good practice because it forces users to authenticate themselves every so often.  One user doesn&#8217;t always use one browser and one browser doesn&#8217;t always serve one person.  But what if that statement is close enough to true that it might as well be?  What if I&#8217;ve got 5 browsers where I&#8217;m the only user?  Sometimes I don&#8217;t want to be required to use the login form every time I use a web app.  </p>
<p>GMail can remember my login for two weeks.  Yahoo! Mail can, too.  I don&#8217;t have to log in every time I got to those web apps.  I wanted the same functionality for a Java-based web app I wrote and I couldn&#8217;t find anything to do something similar through the search engines.  The Java packages I found stored the user&#8217;s login information in the cookie in plain text.  The next time the user goes to the web app the form data is auto-populated by the plain-text username and password stored in the cookie.  This is a very bad &#8220;security&#8221; practice and the user *still* needs to visit a login page.  </p>
<p>Rather than storing a username and password in a cookie we can do a little better.  We can store a &#8220;token&#8221; in the cookie that is associated with the user.  This token does not store the user&#8217;s username or password, either in plain-text or encrypted. Instead the token is generated by the web app and placed in a cookie that does not track the user&#8217;s session.  This means the web app sends one more cookie, the login token, in addition to the session tracking cookie.</p>
<p>Any time after a user is authenticated we can set the login token cookie.  This could be as part of the login process if a login form has a &#8220;remember me&#8221; check box or at the user&#8217;s explicit request any time after authentication.  How does this work in practice?  Let&#8217;s look at some code.  First we&#8217;ll look at the TokenService class.  </p>
<pre name="code" class="java">
@Service
public class TokenService {

  @PersistenceUnit(unitName="bookmarksPU")
  EntityManagerFactory emf;

  public String setupNewLoginToken(User user) {
    PersistentLoginToken tonaken = new PersistentLoginToken();
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();
    User u = em.find(User.class, user.getId());
    token.setUser(u);
    em.persist(token);
    em.getTransaction().commit();

    return token.getId();
  }

  public User loginWithToken(String tokenId) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    PersistentLoginToken token = (PersistentLoginToken) em.find(PersistentLoginToken.class, tokenId);

    User user = null;
    if (token != null) {
      user = token.getUser();
      em.remove(token);
      em.getTransaction().commit();
    } else {
      em.getTransaction().rollback();
      //TODO this is a forgery attempt
      throw new RuntimeException("Attempted login token cookie forgery");
    }
    return user;
  }
}
</pre>
<p>TokenService#setupNewLoginToken is called when we have a User object &#8211; sometime after authentication or when the user requests it.  TokenService#loginWithToken is called in a servlet filter before an HTTP request even makes it to our code.  In order for this to work we need to tie a login token to a user.  We do that through PersistentLoginToken.</p>
<pre name="code" class="java">
@Entity
@Table(name="persistentLoginTokens")
public class PersistentLoginToken {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO, generator="uuid-hex")
  private String id;

  @ManyToOne
  private User user;

  public void setUser(User user) {
    this.user = user;
  }

  public User getUser() {
    return user;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getId() {
    return id;
  }
}
</pre>
<p>The table required (or generated) by this entity has an id column and a user_id column.  We have our JPA provider generate a 128-bit UUID to serve as the token id.  The user id is obtained from the user associated with this token.  Notice how we do not constrain the number of valid tokens a user can have at any particular time.  This means I can have valid login tokens across 5, 10 or 100+ browsers.  Each browser would get a new unique token when I log in using that browser.  </p>
<p>Back to the setupNewLoginToken method &#8211; this works by creating a new PersistentLoginToken object and associating it with a user.  When it PersistentLoginToken object is saved the JPA provider generates its UUID and makes an entry in the database.  Now that the PersistentLoginToken has a generated UUID we put that UUID into the loginToken cookie and add that cookie to the HttpResponse object.  We set the cookie&#8217;s expiration time to 1 week, though this can be set to whatever your web app requires.</p>
<p>This login token cookie is sent back to the browser at the end of this response.  The browser then sends this loginToken cookie to the web app on every subsequent request.  Though this is useless when I have an active session, I really want this cookie around when my session expires on the server side or my session tracking cookie expires on the browser side.  Let&#8217;s see what happens when I don&#8217;t have an active session and I visit the web app.</p>
<pre name="code" class="java">
@Component
public class PersistentLoginFilter implements Filter {

  @PersistenceUnit(unitName="webappPU")
  EntityManagerFactory emf;

  @Override
  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    Cookie tokenCookie = getCookieByName(httpRequest.getCookies(), "loginToken");

    HttpSession session = httpRequest.getSession();
    User user = (User) session.getAttribute("user");

    if (user == null &#038;&#038; tokenCookie != null) {
      user = tokenService.loginWithToken(tokenCookie.getValue());
      String tokenValue = tokenService.setupNewLoginToken(user);

      httpRequest.getSession().setAttribute("user", user);
      tokenCookie.setMaxAge(0);
      httpResponse.addCookie(tokenCookie);

      tokenCookie = new Cookie("loginToken", tokenValue);
      tokenCookie.setPath("/bookmarks");
      tokenCookie.setMaxAge(168 * 60 * 60);
      httpResponse.addCookie(tokenCookie);
    }

    chain.doFilter(httpRequest, httpResponse);
  }
// other methods omitted
}
</pre>
<p>The doFilter method looks for a User associated with the current web session.  The absence of a User and the presence of a login token cookie indicate it&#8217;s time to perform a user lookup and login based on the token cookie.  After finding the user based on a valid token the filter gives the user a new login token that is valid for another week.  This may or may not be ok for your web app.  I just didn&#8217;t feel like writing the code to set expiration of the new cookie to the remaining time on the original cookie.  </p>
<p>With our User in hand, we set it on the current session and let the request finish processing.  All this code is available at <a href="http://github.com/anthonychaves/bookmarks">http://github.com/anthonychaves/bookmarks</a> under the GPLv3 license.  Feel free to modify it, use it and submit feedback.  I know it&#8217;s not as generic as it could be &#8211; feel free to use it as a template for your own code.  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2010/01/28/login-tokens-with-java-servlets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bidirectional relationships &#8211; owning and inverse sides</title>
		<link>http://blog.anthonychaves.net/2009/12/16/bidirectional-relationships-owning-and-inverse-sides/</link>
		<comments>http://blog.anthonychaves.net/2009/12/16/bidirectional-relationships-owning-and-inverse-sides/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 03:11:18 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=157</guid>
		<description><![CDATA[I used to have a hard time remembering which side was which in a bidirectional relationship using ORM. I was confused because the owning side was not the side I thought it was when thinking about a relationship. Let&#8217;s say we have a Customer that has many Orders. The English language semantics suggest to me [...]]]></description>
			<content:encoded><![CDATA[<p>I used to have a hard time remembering which side was which in a bidirectional relationship using ORM.  I was confused because the owning side was not the side I thought it was when thinking about a relationship.  Let&#8217;s say we have a Customer that has many Orders.  The English language semantics suggest to me that the Customer side of the relationship is the owning side.  It &#8220;has many&#8221; Orders, right?</p>
<p>But every ORM framework will tell you that the &#8220;many&#8221; side of the relationship should be the owning side and the &#8220;one&#8221; side is the inverse.  Why?  Let&#8217;s look at some Java class definitions using JPA.</p>
<pre name="code" class="Java">
@Entity
public class Customer implements Serializable {
	@Id
	public String id;

	public String name;
	public String emailAddress;

	@OneToMany
	List&lt;Order> order = new ArrayList&lt;Order>();

	@Version
	public int version;

}
</pre>
<p>A Customer has a one-to-many relationship with Order.</p>
<pre name="code" class="java">
@Entity
@Table(name="orders")
public class Order implements Serializable {
	@Id
	public String id;

	@ManyToOne
	Customer customer;

}
</pre>
<p>And the Order references the customer to which it belongs.  Here is our bi-directional relationship.  But we don&#8217;t know why the Order is the owning-side of the relationship yet.  If we were to create a database schema based on these classes we would get three tables:</p>
<pre>
mysql> describe Customer;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | varchar(255) | NO   | PRI | NULL    |       |
| emailAddress | varchar(255) | YES  |     | NULL    |       |
| name         | varchar(255) | YES  |     | NULL    |       |
| version      | int(11)      | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+

mysql> describe customer_orders;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| CUSTOMER_ID | varchar(255) | YES  | MUL | NULL    |       |
| ORDER_ID    | varchar(255) | YES  | MUL | NULL    |       |
+-------------+--------------+------+-----+---------+-------+

mysql> describe orders;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | varchar(255) | NO   | PRI | NULL    |       |
| CUSTOMER_ID | varchar(255) | YES  | MUL | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
</pre>
<p>The tables that map to our entities look fine.  We also get a join table to map from Customer to Order.  Even though the Order class can refer directly to its Customer the Customer must look at the join table to get to its Orders.</p>
<p>The SQL query generated by the ORM to get the Orders associated with a customer would join the customer_orders table to get a list of order ids associated with that customer, then join the orders table on those order ids.  There is a more direct way we can get there, especially since the orders table already references its customer id.  We need to indicate to the JPA runtime that the Customer can reference a column on the orders table.  We do that with the mappedBy attribute in @OneToMany.</p>
<pre name="code" class="Java">
@Entity
public class Customer implements Serializable {
	@Id
	public String id;

	public String name;
	public String emailAddress;

	@OneToMany(mappedBy="customer")
	List&lt;Order> order = new ArrayList&lt;Order>();

	@Version
	public int version;

}
</pre>
<p>When we add the mappedBy attribute on the @OneToMany annotation the JPA runtime knows that there is a field in the related object named customer.  The column this field maps to is the appropriate foreign key to use to find associated Orders.  Creating the database schema with this change to the Customer class @OneToMany annotation we get only two tables.</p>
<pre>
mysql> describe Customer;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | varchar(255) | NO   | PRI | NULL    |       |
| emailAddress | varchar(255) | YES  |     | NULL    |       |
| name         | varchar(255) | YES  |     | NULL    |       |
| version      | int(11)      | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> describe orders;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | varchar(255) | NO   | PRI | NULL    |       |
| CUSTOMER_ID | varchar(255) | YES  | MUL | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
</pre>
<p>Now the JPA runtime knows that the customer_id column on the orders table can map between an Order and a Customer.  The owning-side of a bidirectional relationship is the one with the foreign key column on the table &#8211; the orders table.  The orders table owns the relationship because it is responsible for the column that maps between Customer and Order.  The &#8220;owning-side&#8221; is not a statement about a Customer has many Orders, the logical relationship between entities.  It is a statement about which underlying table is responsible for the mapping data between two tables and the entities mapped by those tables.  </p>
<p>I was just reminded of my initial questions about why we use these terms when I wrote a @OneToMany(mappedBy=&#8221;&#8230;&#8221;) a few days ago.  As always I&#8217;m open to comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2009/12/16/bidirectional-relationships-owning-and-inverse-sides/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Thoughts on the Economics of the AWS Cloud</title>
		<link>http://blog.anthonychaves.net/2009/12/12/thoughts-on-the-economics-of-the-aws-cloud/</link>
		<comments>http://blog.anthonychaves.net/2009/12/12/thoughts-on-the-economics-of-the-aws-cloud/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 14:48:03 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[amazon web services]]></category>
		<category><![CDATA[aws]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=153</guid>
		<description><![CDATA[A couple days ago Amazon published a short whitepaper on the considerations one might make when building a data center vs. using Amazon&#8217;s web services. In my experience most people use EC2 and S3 for their app, and the more adventurous people branch out into Amazon&#8217;s other services. Most people need storage and compute power, [...]]]></description>
			<content:encoded><![CDATA[<p>A couple days ago Amazon published a <a href="http://awsmedia.s3.amazonaws.com/The_Economics_of_the_AWS_Cloud_vs_Owned_IT_Infrastructure.pdf">short whitepaper</a> on the considerations one might make when building a data center vs. using Amazon&#8217;s web services.  In my experience most people use EC2 and S3 for their app, and the more adventurous people branch out into Amazon&#8217;s other services.  Most people need storage and compute power, fewer people need Elastic Map Reduce.  </p>
<p>Financially, for a small scale production or testing platform using the Amazon Web Services makes a lot of sense for a company whose core business is not IT-centric.  Running a couple m1.small EC2 instances and paying for a backup to S3 or EBS is cheaper than purchasing a box and running the app in colo.</p>
<p>Since the start of the AWS platform I had the opinion that, as one starts using more and more EC2 instances and S3 storage space, there comes a point where it would be cheaper to bring the IT platform in house and hire a staff to run it instead of running on AWS forever.  I have no idea where that point is, nor do I know of anyone with credibility who does.  I thought the dollar amount where in-housing is cheaper is quite high.</p>
<p>I say those things in the past tense.  At the risk of being accused of falling victim to Amazon propaganda, after reading the Economics of the AWS Cloud paper I think the expense of bringing a production app in-house for financial reasons alone is too great to overcome the benefits of using Amazon web services.  What Amazon provides in terms of reliability, redundancy, management and scale can not be overcome unless you plan to compete with Amazon.  This completely ignores in-housing due to compliance and auditing.  AWS is not an option in that case.</p>
<p>The paper makes the argument that we can hit closer to 100% server utilization than an owned IT infrastructure.  Though I&#8217;m not sure I buy this argument, idle CPU and unused memory are idle wherever they are, using a pay-as-you-go service implies someone might be more aware of hardware utilization because using more costs more in EC2.  I guess the comparison is owning 10 boxes at 5% utilization vs. renting 2 EC2 instances at 60% utilization.  Over 5 years renting EC2 instances at full price is cheaper than the capital expense of buying those 10 boxes.  </p>
<p>The Power Efficiency and Enabling Redundancy sections of the paper are particularly important.  Running a data center costs more than just the price of the computers in it.  Power and cooling isn&#8217;t free.  Rack space isn&#8217;t free.  The investments Amazon can afford to make in improving power efficiency and data center design is much greater than most other data center owners can afford.  If you can afford to make this investment you&#8217;re already operating at Amazon-scale &#8211; why aren&#8217;t you competing with them?</p>
<p>Based on the go-big-or-go-home data center, who should build data centers?  CDNs, hosting providers, colo facilities and business that are subject to regulation?  Is that it?  As someone who has been using AWS and other hosting providers for years for small customers and doing hardware recommendations for existing, poorly designed, non-regulated company data centers for large customers, I&#8217;m partial to the AWS/hosted approach if there is not already hardware that can do the job on site.  Hardware procurement in large companies is a nightmare and hasn&#8217;t changed since I started doing this.</p>
<p>So where are we headed with AWS?  Are new tech startups going to forego hardware purchases and use AWS as their infrastructure?  In the last year I have worked with a lot of people making that choice.  What about existing small businesses?  I haven&#8217;t done a lot of work where people are migrating away from their colo&#8217;d software and I don&#8217;t expect them to, at least until the next upgrade cycle.  Upgrade cycle &#8211; that&#8217;s another thing Amazon handles &#8220;in the cloud&#8221; for us.  Certainly a small business needs to be aware of the AWS option and I don&#8217;t have a lot of experience with the IT companies that small businesses out-source to.</p>
<p>Of course I expect (non-regulated) big business to continue building poorly designed data centers based on use-it-or-lose-it budgets.  &#8220;Our team has 30 boxes sitting idle and we need to order 10 more!&#8221;  </p>
<p>These were just some of my thoughts while reading the paper.  Leave a comment with any feedback or your own thoughts on the Economics of the AWS Cloud paper.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2009/12/12/thoughts-on-the-economics-of-the-aws-cloud/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Wiring RESTful web services with Spring</title>
		<link>http://blog.anthonychaves.net/2009/12/10/wiring-restful-web-services-with-spring/</link>
		<comments>http://blog.anthonychaves.net/2009/12/10/wiring-restful-web-services-with-spring/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 01:31:50 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=143</guid>
		<description><![CDATA[I&#8217;ve recently been using Spring 3.0M1 to make some RESTful APIs. While working on the project a question came up a few times from different people: how do the &#60;jee:jndi-lookup/>, &#60;context:annotation-config/> and &#60;context:component-scan/> beans relate to each other? I&#8217;ve got some sample code that I use as a reference when this question comes up. Let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been using Spring 3.0M1 to make some RESTful APIs.  While working on the project a question came up a few times from different people: how do the &lt;jee:jndi-lookup/>, &lt;context:annotation-config/> and &lt;context:component-scan/> beans relate to each other?  I&#8217;ve got some sample code that I use as a reference when this question comes up.</p>
<p>Let&#8217;s start with the &lt;jee:jndi-lookup/> bean.  Since a RESTful API probably runs in a web container we need to look up a data source, persistence unit or persistence context.  We define the persistence unit in WEB-INF/classes/META-INF/persistence.xml.</p>
<pre name="code" class="Xml:nocontrols">
&lt;?xml version="1.0"?>
&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  &lt;persistence-unit name="bookmarksPU" transaction-type="RESOURCE_LOCAL">
    &lt;provider>org.apache.openjpa.persistence.PersistenceProviderImpl&lt;/provider>
    &lt;non-jta-data-source>jdbc/bookmarks&lt;/non-jta-data-source>
    &lt;class>net.anthonychaves.bookmarks.models.User&lt;/class>
    &lt;class>net.anthonychaves.bookmarks.models.Bookmark&lt;/class>
  &lt;/persistence-unit>
&lt;/persistence>
</pre>
<p>The bookmarksPU persistence unit is registered with the container when the webapp is deployed.  It looks up the jdbc/bookmarks datasource and uses it for access to the database.  Setting up this datasource is up to you based on your container.</p>
<p>The bookmarksPU is also bound to a JNDI name, persistence/bookmarksPU.  We can inject this persistence unit into our webapp in the form of an EntityManagerFactory after registering a bean in our Spring container.  We do this in our Spring configuration file by looking up the persistence unit in JNDI.</p>
<pre name="code" class="Xml:nocontrols">
&lt;jee:jndi-lookup id="bookmarksPU" jndi-name="persistence/bookmarksPU"/>
</pre>
<p>In our service layer we have a class UserService.</p>
<pre name="code" class="Java:nocontrols">
@Service
public class UserService {

  @PersistenceUnit(unitName="bookmarksPU")
  EntityManagerFactory emf;

  public void saveUser(User user) {
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(user);
    em.getTransaction().commit();
  }

  public User findUser(String name) {
    EntityManager em = emf.createEntityManager();
    Query query = em.createQuery("select u from User u where u.name = ?1")
                    .setParameter(1, name);
    em.getTransaction().begin();
    User user = (User) query.getSingleResult();
    em.getTransaction().rollback();
    return user;
  }
}
</pre>
<p>Notice two things about this class: it is annotated with the org.springframework.stereotype.Service annotation and its EntityManagerFactory is annotated with the javax.persistence.PersistenceUnit annotation.    We want Spring to deal with both of these annotations.  We want the component, the class annotated by @Service, registered as a Spring bean without doing it manually in XML.  We also want Spring to inject an instance of EntityManagerFactory bound to the persistence/bookmarksPU persistence unit.  </p>
<p>One of the nice things about Spring is that we can rely on these annotations so we don&#8217;t have to create long XML configuration files.  We still need the XML config to tell Spring which classes it should examine for these annotations.  Enter the &lt;context:annotation-config/> and &lt;context:component-scan/> beans.  </p>
<p>Each of these beans implicitly creates instances of classes that implement the BeanPostProcessor interface.  The post processor created by the &lt;context:annotation-config/> bean that is most important to our case is the PersistenceAnnotationPostProcessorBean.  This bean finds the bookmarksPU bean created when we performed the JNDI lookup and injects it into the emf field annotated with @PersistenceUnit.</p>
<p>The &lt;context:annotation-config/> bean takes care of the javax.persistence annotation but it does not create a bean definition for the UserService class.  To do that we need the &lt;context:component-scan/> bean.  This bean scans the specified base package for classes annotated with @Component and its subclasses, which includes @Service.  By annotating the UserClass as a @Service we are spared from configuring it by hand in XML.</p>
<p>The &lt;context:component-scan/> bean only searches for annotations relevant to the current application context.  In this case we must define the service layer outside of the *-servlet.xml WebApplicationContext.  The @Controller annotation is valid in a WebApplicationContext where a @Service annotation is not.  We should instead keep our service layer configuration separate from the webapp configuration.  In services.xml we have our services defined.</p>
<pre name="code" class="Xml:nocontrols">
&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/jee

                           http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

  &lt;jee:jndi-lookup id="bookmarksPU" jndi-name="persistence/bookmarksPU"/>

  &lt;!-- here we need component-scan for the services and annotation-config for the persistence unit -->
  &lt;context:component-scan base-package="net.anthonychaves.bookmarks"/>
  &lt;context:annotation-config/>

&lt;/beans>
</pre>
<p>A WebApplicationContext inherits all the bean definitions from any imported ApplicationContexts.  If we import the services.xml file in our *-servlet.xml file we will inherit the fully wired UserService bean created by the BeanPostProcessor beans in that ApplicationContext.  We have a controller that needs an instance of UserService.</p>
<pre name="code" class="Java:nocontrols">
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;

	@Autowired
	ImageCaptchaService icservice;

	@RequestMapping(value="/new", method=RequestMethod.GET)
	public String newUser(ModelMap model) {
		model.addAttribute(new User());
		return "user_new";
	}

	@RequestMapping(method=RequestMethod.POST)
	public String createUser(@ModelAttribute("user") User user,
							 HttpSession session,
							 @RequestParam("j_captcha_response") String captchaResponse) {

		boolean validResponse = icservice.validateResponseForID(session.getId(), captchaResponse);
		if (validResponse) {
		  userService.saveUser(user);
		  session.setAttribute("user", user);
			return "redirect:/b/user";
		} else {
			return "redirect:/b/user/new";
		}
	}

	@RequestMapping(method=RequestMethod.GET)
	public String user(HttpSession session) {
	  if (session == null || session.getAttribute("user") == null) {
	    return "redirect:/b/user/new";
	  }

		User user = (User)session.getAttribute("user");
	  return "user";
	}

	@RequestMapping(method=RequestMethod.POST, value="/login")
	public String login(@RequestParam("name") String username, HttpSession session) {
	  User user = userService.findUser(username);
	  session.setAttribute("user", user);
    return "redirect:/b/user";
	}

	public void setUserService(UserService userService) {
	  this.userService = userService;
	}
}
</pre>
<p>By specifying the UserService in UserController as @Autowired we expect an AutowiredAnnotationBeanPostProcessor to inject an instance of UserService into an instance of this class.  Both the &lt;context:annotation-config/> and &lt;context:component-scan/> beans create instances of AutowiredAnnotationBeanPostProcessor.  The &lt;context:annotation-config/> bean creates a PersistenceAnnotationBeanPostProcessor which is not important to us here.  We don&#8217;t have any persistence units to inject.  </p>
<p>The &lt;context:component-scan/> bean creates an AutowiredAnnotationBeanPostProcessor and it also registers a bean definition for the UserController.  @Controller is a subclass of @Component and has several annotations that would only be used in a class annotated with @Controller.  The &lt;context:component-scan/> bean takes care of mapping any routes and parameters specified by the @RequestMapping, @RequestParam and other @Controller-related annotations.  </p>
<p>Our bookmarks-servlet.xml file needs only the &lt;context:component-scan/> bean defined to get our web classes up and running.  The full bookmarks-servlet.xml file is very simple.</p>
<pre name="code" class="Xml:nocontrols">
&lt;?xml version="1.0" encoding="UTF-8"?>
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/jee

                           http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

  &lt;import resource="services.xml"/>

  &lt;!-- component-scan creates implicit AutowiredBeanPostProcessor,
             we don't need PersistenceAnnotationPostProcessor -->
  &lt;context:component-scan base-package="net.anthonychaves.bookmarks"/>

  &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    &lt;property name="prefix" value="/WEB-INF/jsp/"/>
    &lt;property name="suffix" value=".jsp"/>
  &lt;/bean>

  &lt;bean id="captchaService" class="net.anthonychaves.bookmarks.service.CaptchaServiceSingleton"
        factory-method="getInstance"/>

&lt;/beans>
</pre>
<p>This should hopefully make it very easy for anyone to quickly create at least a skeleton RESTful API with Spring.  Comments?  Let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2009/12/10/wiring-restful-web-services-with-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting started with Mechanize</title>
		<link>http://blog.anthonychaves.net/2009/07/16/getting-started-with-mechanize/</link>
		<comments>http://blog.anthonychaves.net/2009/07/16/getting-started-with-mechanize/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 01:33:51 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=134</guid>
		<description><![CDATA[I think this will start a short series on black-box webapp testing.  This is just the first in a series and we&#8217;ll add quite a bit of content to it in the next week or so. A weeks back I helped a development team set up a testing environment for their Ruby on Rails webapp.  [...]]]></description>
			<content:encoded><![CDATA[<p><em>I think this will start a short series on black-box webapp testing.  This is just the first in a series and we&#8217;ll add quite a bit of content to it in the next week or so.</em></p>
<p>A weeks back I helped a development team set up a testing environment for their Ruby on Rails webapp.  The webapp is about 18 months old and had exactly zero tests.  Reverifying its intended behavior was a full time job for some of the developers on the team because there was no way to prove any behavior worked as intended at any given time.  Worse, behavior verification was a manual process and obviously error-prone.<br />
<span id="more-134"></span><br />
Repeatable tests that prove the software is correct are of high value to this customer.  Testing their application had to be easy.  Writing tests and running them had to be easy.  For an existing webapp with little to no test coverage a black-box testing approach provides a  lot of return for the effort.  Black-box testing allows behavior testing.  We test  webapp behavior by making GET and POST requests to the known URLs the webapp exposes.</p>
<p>Before we set up a test environment for the existing webapp we must decide how to exercise its behavior.  There are a plenty of HTTP libraries available for different languages and platforms.  When you don&#8217;t have any tests any testing is better than nothing.  Don&#8217;t be afraid if your initial testing framework doesn&#8217;t match your webapp language and platform.  We can just as easily test a JSF webapp hosted on Windows with a Ruby client on Linux.  In fact, diversity in the test environment is a good thing.  Multiple black-box clients show how a webapp will perform in the wild.</p>
<p>Automated webapp testing with the Ruby Mechanize library is very easy.  The Ruby environment with RubyGems is required to use the Mechanize library.  There are instructions for setting up a Ruby environment on the <a href="http://rubyonrails.org/download">Ruby on Rails getting started page</a>.  Follow the instructions for Ruby and RubyGems.</p>
<p>Installing the Mechanize gem is easy.  On Mac OS X 10.5 and Linux the command is:<br />
sudo gem install mechanize</p>
<p>Once you install Mechanize you probably want to play around with it.  You can do so with irb, the interactive Ruby interpreter.  Start irb on the command line by typing irb.  We write Ruby programs directly in irb and irb executes each line as we enter it.  Think of it like a bash shell for Ruby.</p>
<p>Initially irb loads only the core Ruby libraries.  We need to load Mechanize manually with the &#8216;require&#8217; keyword.</p>
<pre name="code" class="ruby">
>> require 'mechanize'
</pre>
<p>I like API documentation in front of me when I code.  The documentation for the version of Mechanize <em>you&#8217;re using</em> is on your local box if you installed it with the gem install command as it appeared a few paragraphs up.  Start the gem documentation server by typing &#8216;gem server&#8217; on the command line.  Now visit <a href="http://localhost:8808">http://localhost:8808</a> in your web browser.  Click the rdoc link in the Mechanize section for the API doc.</p>
<p>The fun stuff happens in the WWW::Mechanize class.  Let&#8217;s go back to irb and create an instance of this class:</p>
<pre name="code" class="ruby">
>> agent = WWW::Mechanize.new
</pre>
<p>Now we have an instance of an object that makes HTTP GET and POST requests.  Start up your webapp and we can make a request.  Starting with a GET request we call the get method on the Mechanize object.  The get method returns an instance of WWW::Mechanize::Page.</p>
<pre name="code" class="ruby">
>> page.header.class
=> WWW::Mechanize::Headers
>> page.header
=> {"last-modified"=&gt;"Tue, 16 Jun 2009 02:41:30 GMT", "connection"=&gt;"close", "date"=&gt;"Fri, 17 Jul 2009 00:42:43 GMT", "content-type"=&gt;"text/html", "content-length"=&gt;"7466"}
>> page.body.class
=> String
>> page.body
</pre>
<p>Go ahead and enter page.body.  I omit the output here because it is a little long.  What you should see is the HTML of the web page you requested with the agent.get(&#8230;) method.  agent.get(&#8230;) assembles the HTTP headers and makes an HTTP GET request.  This method crosses the network if your webapp runs on a different box.  You can test remote webapps just as easily as local.</p>
<p>Play around with the API.  Next time we&#8217;ll cover HTTP POST requests.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2009/07/16/getting-started-with-mechanize/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Nati Shalom to speak at Boston Scalability Group next Wednesday</title>
		<link>http://blog.anthonychaves.net/2009/02/11/nati-shalom-to-speak-at-boston-scalability-group-next-wednesday/</link>
		<comments>http://blog.anthonychaves.net/2009/02/11/nati-shalom-to-speak-at-boston-scalability-group-next-wednesday/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 18:02:52 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[scalability]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=130</guid>
		<description><![CDATA[There is an interesting BostonSUG meeting next week at the IBM Innovation center in Waltham, MA.  GigaSpaces CTO Nati Shalom will speak on cloud-based infrastructure, space-based architecture, scalability with latency in mind and more.  BostonSUG web site for more details: http://www.bostonsug.org/2009/02/09/nati-shalom-speaking-on-february-18/ I&#8217;ll be there!]]></description>
			<content:encoded><![CDATA[<p>There is an interesting BostonSUG meeting next week at the IBM Innovation center in Waltham, MA.  GigaSpaces CTO Nati Shalom will speak on cloud-based infrastructure, space-based architecture, scalability with latency in mind and more.  BostonSUG web site for more details: http://www.bostonsug.org/2009/02/09/nati-shalom-speaking-on-february-18/</p>
<p>I&#8217;ll be there!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2009/02/11/nati-shalom-to-speak-at-boston-scalability-group-next-wednesday/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dummy Code (Quick&#8217;n&#039;Dirty vs. Engineered)</title>
		<link>http://blog.anthonychaves.net/2009/02/04/dummy-code-quickndirty-vs-engineered/</link>
		<comments>http://blog.anthonychaves.net/2009/02/04/dummy-code-quickndirty-vs-engineered/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 22:34:13 +0000</pubDate>
		<dc:creator>Anthony Chaves</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[career]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://blog.anthonychaves.net/?p=125</guid>
		<description><![CDATA[When creating software, two people will never write the same implementation of a method or system of non-trivial design.  Creating software is a problem solving process and there are usually many ways to solve one problem.  The solutions may differ in elegance and efficiency while giving the same output for a given set of inputs.  [...]]]></description>
			<content:encoded><![CDATA[<p>When creating software, two people will never write the same implementation of a method or system of non-trivial design.  Creating software is a problem solving process and there are usually many ways to solve one problem.  The solutions may differ in elegance and efficiency while giving the same output for a given set of inputs.  A correct solutions is a correct solution regardless of implementation.<span id="more-125"></span></p>
<p>Building software modules within a team requires communicating, at a minimum,  method or class signatures and a general idea of the functionality.  This may include a detailed design document clearly specifying method signature and a pseudo-code implementation or method stubs and comments about what the method should do right in the source file.  One thing that should never be used for communicating a signature and functionality is dummy code.</p>
<p>Dummy code looks like real code.  It is real code but it rarely works.  Even if it compiles it doesn&#8217;t produce the correct result when run.  Nor are there any tests that prove it (in)correct.  Dummy code exists only for the sake of its author&#8217;s gratification.</p>
<p>Rather than talk about the desired functionality through some more productive means the dummy code author thinks he is helpfully providing a guesstimate of correct implementation.  What he has actually done is provide a jumbled mess of non-working code cleverly disguised as a working part of the project when he checks it in to the SCM.  The dummy code author thinks someone else will come along and provide the correct implementation &#8220;later&#8221;.</p>
<p>What he doesn&#8217;t realize is that his dummy code is almost indistinguishable from code that should be part of the project.  The next person to try to reuse this code is in for hours of WTF moments trying to figure out WTF the code is supposed to do and why it&#8217;s got code branching five levels deep.</p>
<p>At my first post-college job I had a wonderful mentor.  Thys (pronounced <em>Tays</em>, like &#8220;taste&#8221; without the second &#8216;t&#8217;) and I were talking one day when he asked me a question.  &#8220;Anthony, if you were given a task of writing software that would only be run once would you do it quick-and-dirty or with a well-measured, engineered approach?&#8221;</p>
<p>How bad could &#8220;quick-and-diry&#8221; be if the software would run only once?  I thought about it for a moment before answering.  Quick and dirty.</p>
<p>Thys said he would never write quick and dirty software.  Doing so figuratively creates a  monkey on the developerment team&#8217;s back.  The software that only needs to run once inevitably needs to run a second time and a third time and eventually run regularly.  Thys helped me realize that software evolves and escapes.  Useful software will run indefinitely.  Any time and effort saved on the initial write will be lost 10 times (I made that number up.  It is actually higher.  Any want to provide real data or examples?) over on the maintenance, bug fixes and rewrites.</p>
<p>Writing dummy code is writing software quick and dirty without any idea of the implementation details.  Bad code and bad algorithms.  The worst of both worlds.  It&#8217;s been a long time since Thys and I had that conversation but the years have proven him right again and again.  Any code worth checking in to SCM is worth writing well.  That includes unit tests.</p>
<p>Dummy code has no place in SCM.  If you&#8217;re not going to correctly implement a method you stub out then don&#8217;t provide an implementation at all.  Leave some comments around the method stub if you have to.  Open a ticket in the issue tracking system to let someone know the method needs an implementation and describe the input and output.  Let the implementer determine which algorithms and data structures to use.</p>
<p>Dummy code is a hazard to software projects.  It violates the principle of least astonishment.  Next time you think about writing some remember that the next person that uses the code will waste time figuring out why it doesn&#8217;t work as expected.</p>
<p>If you check code in to your SCM where other people can pull it, take a look at what you&#8217;re checking in.  Does it work?  Does it work well?  Are there tests that prove it?  Helping someone by coding less is sometimes the best help of all.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anthonychaves.net/2009/02/04/dummy-code-quickndirty-vs-engineered/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

