Friday 21 January 2011

Wildcards in Struts2 actions

Wildcards, and their possible applications in Struts2 action mapping are both diverse and powerful. More so than a lot of the content out there give them credit for, and more than I thought the were.

Rather thatn attempt to explain it all here, just go read the article here: https://cwiki.apache.org/WW/wildcard-mappings.html

there is also an article on what to do about ambiguous matches here: http://stackoverflow.com/questions/1742076/what-can-i-do-about-ambigous-wildcard-patterns-in-struts

Wednesday 19 January 2011

Help debugging Struts 2

They say you learn something everyday, but today I genuinely did !

It turns out that there is a plugin included in the Struts 2 distributation that lets you browse the loaded configuration. This includes:

  • Configuration settings
  • Loaded jars
  • Namespaces
  • Mapped actions for each namespace
  • Interceptor stacks
  • Validation hooks 
  • and many other useful things.

So, to make it work, you simply drop struts-config-browser-plugin-2.x.x.jar into your application path, and restart your app. You can then go visit: http://yourhost/yourapp/config-browser/index and there it is !

NB: If you are using sitemesh or equivalent with a global /* decorator, you may see some odd rendering. I had to change my config around a bit to prevent sitemesh decorating these pages.

Warning: This may seem obvious, but ... make sure you don't put this out into production (or even QA for that matter).

The full details on how this works can be found here: http://struts.apache.org/2.2.1/docs/debugging-struts.html

Monday 17 January 2011

Taming the Spring JavaMailSender / GMail beastie

Programatically sending email in Spring (I'm talking about Spring 3.0.x here) really isn't too hard, you just wire up the JavaMailSenderImpl and off you go, right ?

Well, yes ... except when you want to use a GMail type account.

It's not that it's hard, it's just that there's a lot of examples out there that nearly work .... so here's the defintive guide that worked for me.

We'll get onto the spring configuration in a minute, but first a word on jars. To make this work you really need the latest and greatest JavaMail (1.4.3), and (if you don't have it JAF (1.1.1)). Go here for mail goodness: http://www.oracle.com/technetwork/java/index-jsp-139225.html

Now, onto the configuration. NB: This is only the spring config, I'm not going into any detail about how to use the MailSender, if you need help with that, go google.

There are two different configurations, depending on the google mail type you are using. If you are using Google Apps mail (think http://mail.google.com/a/yourdomain.co.uk), then this is for you:


     
    
    
    
    
    
        
            true
            true
        
    



If you are using good ol' GMail, then this is for you:


     
    
    
    
    
    
        
            true
            true
        
    


Oh, and here's a final nugget to keep you going ... If it's all going south, and you need to peek under the proverbial, then add this to the javaMailProperties section, and you'll get protocol level debug to STDOUT - which can be really helpful.


    
        ...
        true
        ...
    

Tuesday 11 January 2011

Syntax Highlighting in Blog posts

I wanted to know how/if I could apply some pretty syntax formatting and highlighting to code snippets in blog posts.

If you want to know how, go look here: http://www.craftyfella.com/2010/01/syntax-highlighting-with-blogger-engine.html

Making NOT NULL columns nullable again in Firebird

I came across the need to make a NOT NULL column nullable again recently, and was somewhat suprised to find that it's actually a little harder than you might think in Firebird SQL.

take the following ficticious example:

CREATE TABLE pointless
(
    widget VARCHAR(40) NOT NULL
);

now, in, say Oracle, I would simply do this:

ALTER TABLE pointless MODIFY COLUMN widget VARCHAR(40);

et voila !!

Turns out you can't do that in Firebird, so I had to do a little digging. It turns out that Firebird internally handles NOT NULL columns as constraints, and you have to drop the constraint to remove the not null clause.

The full explanation of what to do, and why is here: http://www.firebirdsql.org/manual/nullguide-alter-pop-tables.html#nullguide-make-column-nullable but for those of you masses that just can't wait, try this:

SELECT rc.rdb$constraint_name
FROM rdb$relation_constraints rc
JOIN rdb$check_constraints cc
ON rc.rdb$constraint_name = cc.rdb$constraint_name
WHERE rc.rdb$constraint_type = 'NOT NULL'
AND rc.rdb$relation_name = 'tablename'
AND cc.rdb$trigger_name = 'fieldname'

Substituting the table and column names as appropriate. The resulting row will give you the internal constraint name that you can simply drop as follows:

ALTER TABLE tablename DROP CONSTRAINT constraintname

Job done !

Sunday 2 January 2011

XSRF protection using Struts 2

I was recently ferretting around for different ways to handle XSRF protection in Struts 2.
(an explanation of this can be found here)

I was curious to see what other people are doing around this, but it seems that (as I expected) the general best practice still seems to be around unique token submission - which is fine.

At the same time. it dawned on me that I've not come across too much on how to do this with Struts 2.
But, fear not faithful readers, this blog entry here http://nickcoblentz.blogspot.com/2008/11/csrf-prevention-in-struts-2.html sums it up nicely.

Incidently, this guy also deals with several other Struts 2 topics in his blog, worth a look.