title: Adding custom RequestCondition's in Spring mvc 3.1
body: <p>I have a Spring mvc (3.1.1) app, and I want to define conditions beyond what's available in RequestMapping. I have a couple of things I want to use it for.</p>
  
  <p>First, it would be nice if I could show a different home page for different user types:</p>
  
  <pre><code>@Controller
  public class HomepageController {
  
      @RequestMapping(value = "/")
      @CustomCondition(roles = Guest.class)
      public String guestHome() { /*...*/ }
  
      @RequestMapping(value = "/")
      @CustomCondition(roles = Admin.class)
      public String adminHome() { /*...*/ }
  
  }
  </code></pre>
  
  <p>Second, I want the app to function both as a web site and as a REST service (e.g. for mobile apps), so I'd want to let the website access both html and json actions, and let the service (different subdomain) only access json actions (some kind of <code>@CustomCondition(web = true)</code> which only matches website urls)</p>
  
  <p>Can this work for any of the two uses I'm planning?</p>
  
  <p>I found very little documentation about custom conditions, but I did find <a href="https://github.com/rstoyanchev/spring-mvc-31-demo" rel="noreferrer">one example</a> that <a href="https://github.com/rstoyanchev/spring-mvc-31-demo/tree/master/src/main/java/org/springframework/samples/mvc31/requestcondition/mvc" rel="noreferrer">implements custom conditions</a> which might be what I want, but it uses a <code>@Configuration</code> class instead of the XML configuration which I'm using and I don't want to move my entire spring xml definitions to a <code>@Configuration</code> class.</p>
  
  <p>Can I define a customMethodCondition for <code>RequestMappingHandlerMapping</code> in the XML?</p>
  
  <p>I tried subclassing <code>RequestMappingHandlerMapping</code> and override <code>getCustomMethodCondition</code>, to return my custom <code>RequestCondition</code>, but it didn't work - <code>getMatchingCondition()</code> in my condition didn't fire.</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I read a little more, and it looks like <code>RequestMappingHandlerMapping</code> is a new class (since ver 3.1).</p>
  
  <p>What happens in my app is that the @Configuration that tries to override and thereby redefine the <code>requestMappingHandlerMapping</code> bean actually works, but the url mappings (<code>@RequestMapping</code> methods in <code>@Controller</code>s) seem to <em>get processed twice</em>, once by the subclass <code>ExtendedRequestMappingHandlerMapping</code> and once by the original <code>RequestMappingHandlerMapping</code> --first with a custom condition, and then again without it.</p>
  
  <p>Bottom line is my custom conditions are simply ignored.</p>
  
  <p>This is supposed to be an advanced pattern, but IMO it should be quite common...</p>
  
  <p>Comments anyone?</p>