So… new city, new website… again!

Hey all,

I have changed cities and websites again! A bit of a coincidence that I have done both at the same time again. I have migrated off of Drupal and onto Tumblr. Drupal has been good to me but I just do not have time to maintain it on my server anymore especially since I only had a single Website running on it. I really like Tumblr as a platform and it is super convenient so that is why I am using it for now.

You will notice that I was able to import all my old blog posts. Tumblr does not import blog posts natively so I wrote a Web app to do it for me. I posted it to my GitHub, Blog Teleporter. It is a pretty simple app that crawls Drupal blogs (for now) and posts them to Tumblr. There are a few things I need to tighten up on it but it runs end to end and can be easily extended to crawl other blog platforms. I think I am going to add in one more platform before I declare it a version 1.0. You might notice that all of my old posts have a new tag, teleporter. My app added it so you can easily see which posts you have imported. That allows for easy rollbacks on Tumblr in case there is a problem importing. 

That is basically why I have not made any posts in the past few months. The combination of me getting a new job, handing off the old one, and promising myself to not post until I migrated off of Drupal did it. There are a lot of goodies in my Blog Teleporter though and I will be posting about them shortly.

If you want to migrate your blog onto Tumblr and my Blog Teleporter does not support your platform, just let me know. I am happy to add it as another feature.

Using Dependency Injection To Incorporate A/B Testing Into Your Applications

I posted this article to my company’s tech blog a few weeks back. I am reposting it here because… well… I wrote it :-) 

http://tech.gilt.com/post/8391205906/using-dependency-injection-to-incorporate-a-b-testing

If you work at an e-commerce company, chances are you’ve probably come across the term “A/B testing”. We all know that it has something to do with testing out new features on users and seeing which ones are “better”. A/B testing is really the practice of comparing the effect that a feature (“treatment” in statistical terms) has on different groups of users. For instance, you might be developing a new navigation bar for your Website. You can see if it improves how long users stay on your site by testing it on a group of users (A) and comparing them to users in another group (B) that are using the original navigation bar. Again in statistical lingo, group B would be considered your “control group”. It’s important to note that you can test a variety of navigation bars here and not violate the principles behind A/B testing since your treatment is the type of bar you are exposing.

Now you are probably wondering how dependency injection (DI) factors into A/B testing. DI is the design principle where your objects are assigned references to their dependencies at runtime. Generally speaking, objects using DI refer to their dependencies through interfaces or super classes so they are indifferent to how those dependencies are actually implemented and instantiated. There are a few frameworks in Java that leverage DI. For the work that we do at Gilt, we use Java Spring. The example in this post is in Spring but it’s general enough that it can be applied to other frameworks.

So now that we know what DI is, let’s apply it to make A/B testing a part of your applications. In A/B testing, groups of users are exposed to a given treatment. That means we need to have a mapping between groups of users and the treatment that they are receiving. Java Spring makes that very easy; in your Spring configuration, you can create such a mapping using the map tag. For example:

<util:map id="ABTestMapping">
  <entry>
    <key>
      <value>group 1</value>
    </key>
    <ref bean="baseline_nav" />
  </entry>
  <entry>
    <key>
      <value>group 2</value>
    </key>
    <ref bean="test_nav1" />
  </entry>
  <entry>
    <key>
      <value>group 3</value>
    </key>
    <ref bean="test_nav2" />
  </entry>
</util:map>
 
<bean id="baseline_nav" class="com.gilt.examples.BaselineNavBar"/>
<bean id="test_nav1"    class="com.gilt.examples.NewNavBar1"/>
<bean id="test_nav2"    class="com.gilt.examples.NewNavBar2"/>

So in this map, our keys are different test groups of users and their corresponding values are the different navigation bar we will expose to them. This example is a bit contrived but it illustrates the point quite well. Instead of having to code A/B testing into your application, you can externalize it in a configuration. Java Spring instantiates this map and your application can use it to determine which navigation bar a user should see. A nifty trick in Spring is to use the import tag in your configuration. 

<import resource="file:test_buckets.xml"/>

This tag allows you to put your A/B testing configuration into a separate file which is very powerful. Doing so allows you to change which groups receive various treatments without having to recompile or redeploy your code. 

Simple. Fast. Very Useful.

External Spring Config Files

Most of the time when you are creating a Spring app, you end up packaging the XML config files with the war/jar. Sometimes though, it is quite beneficial to have a configuration file external to your built package. That allows you to configure your Spring app without having to rebuild or redeploy it; trust me, your system engineers/admins will love you for that. Using an external spring config is quite easy. You can use the import tag.

For example:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   <import resource="file:/path/to/external/config.xml"/>
</beans>

The above Spring config will import /path/to/external/config.xml. Having an external file will allow you to configure beans without having to rebuilding your main war/jar. Furthermore, an external Spring config allows you to create map configurations, something that you cannot do in traditional unix style property files. Below is an example map instance you can create in your external config:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
   <util:map id="sampleMap">
     <entry>
        <key>
           <value>Hello</value>
        </key>
           <value>World</value>
      </entry>
   </util:map>
</beans>

Programatically logging a user out in Spring Security

So I use Spring Security to handle user authentication in most of my Web applications. Every now and then, you need to log a user out programmatically. For example, users perform some sort of operation that redirects to a success page and logs them out. Logging a user out is quite simple. You need use the logout method for the relevant LogoutHandlers in your application. You are always going to have to use the SecurityContextLogoutHandler. I generally use the “remember me token” so I also have to use the PersistentTokenBasedRememberMeServices. Below is a sample method that you could have in your controller.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.ui.logout.SecurityContextLogoutHandler;
import org.springframework.security.ui.rememberme.PersistentTokenBasedRememberMeServices; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SampleController {
   @RequestMapping(value="/some_page.htm")
   public String somePage (HttpServletRequest request, HttpServletResponse response) {
        /* some business logic code */
      Authentication auth = SecurityContextHolder.getContext().getAuthentication();
      if (auth != null){    
         new SecurityContextLogoutHandler().logout(request, response, auth);
         new PersistentTokenBasedRememberMeServices().logout(request, response, auth);
      }
   }
}

Using the Spring Expression Language with static properties

One of the things that I love about Spring 3 is the Spring Expression Language (SPEL). At first glance, SPEL does not appear to give you that much; with it, you can execute Java expressions in your Spring config files. This capability, however, is awesome when have properties that take values defined by static variables. For example, how days of the week are defined in the Calendar class:

Calendar.MONDAY
Calendar.TUESDAY
Calendar.WEDNESDAY
Calendar.THURSDAY
Calendar.FRIDAY
Calendar.SATURDAY
Calendar.SUNDAY

Prior to SPEL, if you wanted to set an integer property to Calendar.MONDAY, you had to know and specify the actual integer value for it. Using SPEL, can simply use the following snippet instead; you need to add the org.springframework.expression-3.X.X.jar to your classpath as well.

<property name="dayOfWeek" value="<entry key="#{T(java.util.Calendar).MONDAY}" />

The PhD is done!

Well, that title is slightly deceiving. I am almost done. I successfully defended my thesis earlier this month and pasted in my revisions. I am just waiting to get my committee’s approval before I can official declare that I am done. So yes, that means I can be referred to as Dr. Chris. I am not too sure about using the prefix but I am definitely glad to be done.

Any interesting aside; so I have been doing a lot of Spring development. I have now seen applications that use JSP pages and applications that use Apache Velocity templates. From what I can see, you can actually configure a Spring Web app to use pages that are external to the actual WAR file. There isn’t a whole lot of difference between Velocity and JSP really expect that if condition statements are a little more cumbersome in JSP since you have to use choose, when, otherwise statements and I haven’t really found a good editor for Velocity. In truth, I have to say that I prefer Velocity templates over JSPs though it is because Velocity really forces you to keep your business logic to your Java code as oppose to in your HTML. You can use JSP in the say way but there is nothing stopping you from dumping a whole ton of Java code in there.

I know I have been kind of spotty with my blogging. I’m new at it and have been busy getting wrapping up school and getting settled (excuses I know). I’m going to starting up a new side project with Dr. Toms at Dalhousie so I should have some interesting coding tid bits to post and maybe some user search behavior results.

New City, New Framework

Hey all,

I have just relocated to New York with my wife. I got what looks to be a cool job here and she will be continuing on with her school. It’s been a busy month with moving and settling in but we are getting there.

In my down town, I have started getting back into my usual hackery. I have been reading Craig Walls’ book “Spring in Action”. It is a pretty good book actually. Some people on Amazon have criticized it for a lack of code examples but really, it is a good solid book that explains how Spring works and how to use it properly. If you understand Java, then you don’t really need coding examples, just an explanation of how Spring works.

I am new to the Spring framework though it has been around for many years. It is an interesting framework that helps you build Java systems designed around dependency injection, as known as inversion of control. Essentially, it promotes the development of loosely coupled object which in turn makes them very modular. It also promotes aspect orient programming which allows for modularization of things like logging and security; stuff that is a pain in the butt to do and susceptible to bugs. I am looking forward to using at my new job and making a couple new research search system for my old lab at Dal; logging user behavior was always a pain to do so it should be interesting to see if I can make good use of Spring there.

I am sort of behind of my Web posts due to moving and what not. I actually got a few papers accepted for publication which is a new and happy change from my usual rejections. I will post up the references shortly.