Monday 21 November 2011

Converting a Long into a Date/Time (MySQL)

In MySQL, sometimes people store timestamps as long values (time since epoch, etc.)

In order to turn that into a date, you need to do the following in MySQL:

SELECT FROM_UNIXTIME(event_time) FROM MY_TABLE


or if you like a date nicely formatted, then:

SELECT DATE_FORMAT(FROM_UNIXTIME(event_time), '%d/%m/%y %h:%i%p') FROM MY_TABLE


NB: This assumes that your event_time is in seconds. If you are using Java, for example, then you will have probably stored your timestamp in millisconds (as kicked out by System,currentTimeMillis() ) so in this instance, MySQL will rather rudely just return NULL.

To fix this, simply divide by 1000 first as follows:


SELECT FROM_UNIXTIME(event_time/1000) FROM MY_TABLE


or if you like a date nicely formatted, then:

SELECT DATE_FORMAT(FROM_UNIXTIME(event_time/1000), '%d/%m/%y %h:%i%p') FROM MY_TABLE

Thursday 23 June 2011

Why don't my spring security tags work ?

A good question indeed ... but first the background.

I was working on a quick app, using Struts 2, Spring, Spring Security, and SiteMesh (3 Alpha 2 for those that care - it's actually pretty good for an 'alpha' tag).

Specifically, I hadn't used the Spring Security taglib before, so I couldn't understand why the tags I was specifying just weren't doing anyrhing. It was as if they were being ignored.

It turn's out, after some significant head scrathing, and swearing that I had fallen foul of web.xml filter ordering. You have to have the SiteMesh filter *after* the Spring Security one, as follows:

... other web.xml stuff

 
 
     springSecurityFilterChain
     org.springframework.web.filter.DelegatingFilterProxy
 
 
 
     springSecurityFilterChain
     /*
 


 
 
         sitemesh
         org.sitemesh.config.ConfigurableSiteMeshFilter
 

 
   
         sitemesh
         /*
   

... other web.xml stuff

If you put them in the order above, ev erything works swimmingly, and the tags are honoured.

Hope that helps.

Tuesday 24 May 2011

Making Struts 2 and JSTL share message resources nicely.

It doesn't happen often, but occasionally ... you need to use your Struts 2 message resources in a jsp that is not part of a struts flow.

Normally, you have declared your struts 2 message resources (I put mine in global scope, i.e. global.properties at the root of my /WEB-INF/classes), and you use them as follows:

...

...


But in a simple JSP, you don't have access to these, so what do you do ? The obvious, simple solution is:

...

<%@ taglib prefix="fmt" 
           uri="http://java.sun.com/jsp/jstl/fmt" %>

...



...


Now, you might expect your text to appear right ? bzzzzt, Wrong! you get:

??some.random.text??

Now, why does this happen ? The answer is pretty simple, JSTL doesn't know about your struts message resources, you need to tell it about them.

This can be achieved with the following snippet in your web.xml:


  javax.servlet.jsp.jstl.fmt.localizationContextglobal
.


But be warned, as far as I can tell you can only specify one context, and thus you cannot use the struts 2 package style property cascading.

I personally use global propertiesm so this doesn't affect me.

Tuesday 8 March 2011

Why doesn't EL work in my JSPs ... answered

So, you're working away on a relatively legacy application ... and you decide that as you're running the application in a new-ish (think Tomcat 6 level) servlet container, you might as well actually use some JSTL.

Makes sense right ? Course it does !

So you go and add it, all good. Then you pre-compile your jsps and deploy, and your met with:

${user.name}

in your jsp ... Hmmmm what could be the matter ?!?!? (actually it was more WTF, but hey)

Well, it turns out that, the DTD referenced in the application web.xml was out of date (was referencing 2.3), and hence none of the EL functionality is enabled by default !!

The solution is to either update your web.xml to reflect your container, OR if you can't follow the directions in the link below to add EL support.

Frankly, the mapping of servlet containers to JSP and JSTL versions, and associated jars, is not trivial. Fortunately, someone else has done a fantastic job of pulling all the information together:

http://tech.zhenhua.info/2009/01/version-matching-of-jsp-jstl-and.html

Thank you Gerald !

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.