Skip to content
accelerando

Category Archives: Webdevelopment

Creating a better PathMatcher for Spring 3

09-Mar-11

Spring 3 has excellent support for mapping URLs to @Controller methods through the @RequestMapping annotation. This works quite well and i especially like the fact having the mapping right next to the method and not in some other config file like routes.rb.

My goal was to have urls like http://foobar.com/resource, http://foobar.com/resource.html, http://foobar.com/resource.zip etc. This is no problem at all thanks to the ContentNegotiatingViewResolver.

The solution has only one draw back: The format is not known to the controller. Yes, this shouldn’t be a controller concern in most cases but what if you have a format that you don’t want to be available to all users? Maybe an nice zip download of your resources? Handling authentication in a view? I don’t think so.

So my first attempt looked like this

@RequestMapping("/resource.{format}")
public String resource(
		final @PathVariable String format,
		final HttpServletRequest request,
		final Model model
)

That didn’t work because it wouldn’t work for the default text/html resource http://foobar.com/resource so i added

@RequestMapping("/resource")
public String resource(
		final Model model
) {
  this.resource('html', model);
}

That worked for http://foobar.com/resource but not for http://foobar.com/resource.zip… “format” was always html. Hmmm…

After much googling and reading StackOverflow.com i found the “useDefaultSuffixPattern” option on org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping. If set to true (which is the default) the mapping “/resource/” will also map to “/resource/” and “/resource.*”. Although both useful i tried disabling it through my spring-cfg.xml like

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    	<property name="order" value="0" />
    	<property name="useDefaultSuffixPattern" value="false" />
</bean>

Enter the next problem: First, i didn’t work either. Second, all urls where mapped twice. Without the default suffix pattern and with. I spend 2 hours trying to locate the place where the spring config was loaded twice. In the end it was that one line that caused me trouble:

  <mvc:annotation-driven/>

That tag enables a lot of stuff in Spring 3, like the @Controller programming model and many other goodies. What it also does is establishing an AnnotationHandlerMapping that cannot be overwritten. So the next thing i did was browsing through the Spring sources to see what it does and redid with Spring beans in my config file (code follows later).

With that implemented, my urls still didn’t work, for all cases the URL without .{format} was called.

As i was already deep down in the Spring sources i had a look at the default path parser and matcher called AntPathMatcher. There is nothing wrong with the parsing code but the “getPatternComparator” method that “Given a full path, returns a Comparator suitable for sorting patterns in order of explicitness.” had some flaws, at least for my use case.

It sorts the patterns by explicitness and that explicitness is (among others) defined by how many placeholders for path variables are present. So my “/resource” is more explicit that “/resource.{format}”. With that in mind, i extend the path matcher like so:

This PathMatcher delegates most of his methods to the default AntPathMatcher but overwrites the getPatternComparator. If you have a look at the sources you’ll see that it is also partly copied. In the last else branch you’ll see that i sort both patterns by length, strip the default suffix (.*) and check wether the longer pattern starts with the other one. If it does i check wether the difference is just a .{format} (hardcoded). If that’s true, than the pattern with the format suffix is more explicit. Otherwise, i’ll use the default algorithm.

To get this to work, you cannot use the mvc:annotation-driven tag as the PathMatcher is a property of the AnnotationMappingHandler which in turn cannot be overwritten. So to get the same functionality like in Spring 3.0.5 with my PathMatcher use

As you can see i left the useDefaultSuffixPattern option enabled as it works very well with my PathMatcher and i didn’t want to care about mapping “/resource”, “/resource/” etc…

I really hope that the gists will save someone some time. I cannot imagine that i’m the only one having this kind of requirement. The solution is really simple but the way to it was not that easy.

HTML Entity Character Lookup

16-Jun-07

On Retrax Bloggy i found a great widget for looking up HTML Entities:

HTML Entity Character Lookup

Best of all: the widget doesn’t need a online connection. The whole thing works like a charm, great!

I wanna visit some spamers with a chrome axe…

06-Jun-07

and replay a book by Bret Easton Ellis. Or at least smack them in the face.

This blog received “just” ~1500 Comment- and trackbackspams since last thursday. On the 2nd place is my dailyfratze project and right behind, planet-punk.

The quintessence: You must do some math if you wanna comment here, trackbacks are validated and i’ve joined Project Honeypot. If there are any questions regarding the latter, just ask me. A handfull of plugins, more internet traffic etc. pp. just to keep idiots destroying modern mediums of communication.

Best of all: My own URL planet-punk.de just got blacklisted by Akismet. Damn it. I’m really curious how that happened. I really never ever spammed anything. *sigh*

Projektdokumentationen und Anwendungsdesign

19-May-07

In den letzten Tagen habe ich einige ganz interessante, deutsche Projektdokumentationen gefunden.

Zum einen die zur Zeiterfassung Mite gehörende Diplomarbeit, die hier zum Download angeboten wird.
Zum anderen einen Aufsatz von Thomas Bachem, einem der Macher von sevenload.de

Ich finde beide Dokumentationen hochgradig interessant zu lesen, nichts desto habe ich einige Anmerkungen und Gedanken dazu:

Mite ist ein Projekt mit Ruby on Rails. Die Macher hatten am Anfang Lastprobleme, konnten das aber durch einen Umzug auf einen performanteren Server lösen.

Sevenload ist ein PHP Projekt. Leider nutze ich es nicht so häufig wie Youtube, daher kann ich keine definitive Aussage zur Geschwindigkeit treffen. Dennoch frage ich mich, ob es wirklich nötig ist, in einem Grundlagenartikel direkt mit kontrollierten Redundanzen für die einfachsten Sachen wie “durchschnittliche Bewertung eines Bildes” loszulegen? Ich meine, bin ich der einzige, der so etwas für Überflüssig hält? Letzten Endes ist es ein Einzeiler in SQL, der mit korrekter Indexerstellung kein DBMS in die Knie zwingen sollte:

SELECT avg(rating)/COUNT(*), rateable_id FROM ratings GROUP BY rateable_id ORDER BY 1 ASC;

Das dann noch mit einem inner join über die zu bewertenden Dinger verknüpft und gut.

Welcher Ansatz würde ich wählen? Ich selber würde jederzeit Standards vorziehen, im obigen Fall auf ein sauberes ER <->Objekt Mapping und auf Normalisierung in der DB (witzigerweise erwähnt Thomas Bachem das im nächsten Absatz bzgl. Tagging Schema) setzen. In anderen Worten: Lieber den Railsweg gehen und sauberes Design erhalten und dann im Zweifelsfall etwas mehr Hardware hinter her werfen.

Tatsächlich redundate Informationen zu speichern würde ich generell nicht ausschliessen, in diesem Fall allerdings schon. Ich denke, wenn man soweit unten bereits diesen Bedarf hat, wird es eng mit Optimierungen, wenn die Luft unter Last dünner wird.

Ruby On Rails native MySQL Bindings Vs. RMagick

02-Jan-07

I’m writing this post in english in hope that more people find it useful…

Some times ago i really had bad problems installing the MySQL Gem 2.7 with Ruby 1.8.2 or 1.8.5 in conjunction with Rails 1.1.6 on Mac OS X 10.4

Compilation failed with:

Building native extensions. This could take a while…
mysql.c: In function ‘Init_mysql’:
mysql.c:2015: error: ‘ulong’ undeclared (first use in this function)
mysql.c:2015: error: (Each undeclared identifier is reported only once
mysql.c:2015: error: for each function it appears in.)
mysql.c:2015: error: parse error before numeric constant
mysql.c:2018: error: parse error before numeric constant
make: *** [mysql.o] Error 1
mysql.c: In function ‘Init_mysql’:
mysql.c:2015: error: ‘ulong’ undeclared (first use in this function)
mysql.c:2015: error: (Each undeclared identifier is reported only once
mysql.c:2015: error: for each function it appears in.)
mysql.c:2015: error: parse error before numeric constant
mysql.c:2018: error: parse error before numeric constant
make: *** [mysql.o] Error 1
ruby extconf.rb install mysql — –with-mysql-dir=/usr/local/mysql
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lz… yes
checking for mysql_query() in -lmysqlclient… yes
checking for mysql_ssl_set()yes
checking for mysql.h… yes
creating Makefile

The gem would then install with Successfully installed mysql-2.7. Creepy!!! But that damn thing just didn’t work.

After quite some googling i found this one:

Running Rails on OS X with MySQL 5.0.24

It’s all about puting a little

#ifndef ulong 
#define ulong unsigned long
#endif

somewhere in “/usr/include/stdlib.h”.

This tipp is still necessary for MySQL 5.0.24+. Thanks again mate!!

But here the trouble starts….

I put the define in a nice little conditional just in case but bah… It would come down to hunt me…

For my project DailyFratze.de i also need RMagick. Again, the gem (1.14.1, 1.14.0 and 1.13) failed to compile but didn’t tell (on runtime it said “require “RMagick” LoadError: No such file to load — RMagick.so” … ) and installation from source did fail as well with:

setup.rb:655:in `command': system("make") failed (RuntimeError)
from setup.rb:664:in `make'
from setup.rb:1258:in `setup_dir_ext'
from setup.rb:1532:in `__send__'
from setup.rb:1532:in `traverse'
from setup.rb:1530:in `dive_into'
from setup.rb:1530:in `traverse'
from setup.rb:1534:in `traverse'
from setup.rb:1533:in `each'
... 8 levels...
from setup.rb:826:in `__send__'
from setup.rb:826:in `invoke'
from setup.rb:772:in `invoke'
from setup.rb:1578
make: *** [all] Error 1

Damn! After banging my head against the walls, reinstalling ImageMagick and all it’s depencies either direct from source, via i-installer and finally as mentioned here i took a break, visited some porn sites and stuff like that and though, hmm… stdlib.h….

I removed the little define and bam! It’s that easy, RMagick compiles just fine…

From the forums i found i guess other people with the same error message may have the same problem as i had…

I really wish installing a ruby on rails environment would be a less pain in the ass…. somewhere near as easy as developing with rails.

Close
E-mail It