Monday 2 July 2012

Customising Mou PDF exports

I recently discovered MarkDown, specifically through Mou (sorry Mac only, but there are plenty of windows editors - just Google)

One thing that took a little investigation (and a very fast response from the developer), was how to customise the PDF output - i.e. how to change the default styling.

The answer is really simple, go to Preferences -> CSS -> Edi, and open the CSS file.

Then you just use the @print media type, so for example, the following snippet will make the output text black:

@media print
{
    body
    {
        color: black;
    }
}


Don't forget to reload your CSS afterwards for it to take effect !

Tuesday 7 February 2012

Adding password controlled access to a CloudControl PHP deployment

I have recently spent some time evaluating cloud-based PHP hosting suppliers (which is another conversation entirely ... stay tuned).

One of the issues I came across, which is arguably trivial in a traditional apache/PHP environment was to add BASIC AUTH authentication to a site using .htaccess, and .htpasswd type configuration.

I will assume that anyone reading this has already mastered the art of CloudControl based deployment. This article will only cover what you need to do to make a .htpasswd based BASIC AUTH solution work.

DISCLAIMER:
BASIC AUTH is not a prefect solution for user authentication. Always prefer use of HTTPS-based connections, and implement proper authentication and authorization mechanisms in your application. CloudControl offer https based traffic as standard, e.g. https://<deployment>.<app>.cloudcontrolled.com, or https://<app>.cloudcontrolled.com if you are on the default deployment.

As I am quite new to this environment, I had to chat with their support guys (via email), but I have to say the experience was very positive. Responses where timely, and technically precise - good work guys !

So, to implement this strategy, you need to follow these steps:

  • (Maybe) Change the way you layout your deployment in the repository. In order for this process to work you need to (if you don't already do this) move the files in your staging location.repository to a sub-folder, thus allowing 'private' files, such as a .htpasswd to be securely stored.

What you might have before would look something like this:

 <repo root>
|
+-------- index.php
|
+-------- ... other stuff ...

What you need to do is move everything to a sub-folder, e.g. publc/ so the structure looks like this:

 <repo root>
|
+-------- public/
|
+-------- index.php
|
+-------- ... other stuff ...

  • Create a .ccconfig.yaml file to describe the deployment layout. A full description of how this file works can be found here: https://www.cloudcontrol.com/documentation/platform-components/config-file, but for the purposes of what we are doing here, we only need the minimal configuration as shown below. This file should be saved into the root of your staging location/repository.

BaseConfig:
WebContent: /public

Your staging location/repository should then look something like:

 <repo root>
|
+-------- public/
| |
| +-------- index.php
| |
| +-------- ... other stuff ...
|
+-------- .ccconfig

  • Create a suitable .htpasswd file. If you need help with this, there is a site that may be of use: http://www.htaccesstools.com/htpasswd-generator/. What you generate should look something like the example below when you are done. This file should be saved into the root of your staging location/repository.

test:$apr1$wk6zMZHa$Qzs5wtkncXeXlUjaBboro0


Your staging location/repository should now look something like:


 <repo root> /
|
+-------- public/
| |
| +-------- index.php
| |
| +-------- ... other stuff ...
|
+-------- .ccconfig
|
+-------- .htpasswd

  • Build a suitable .htaccess file. If you need help with this, there is a site that may be of use: http://www.htaccesstools.com/htaccess-authentication/. What you generate should look something like the example below when you are done. This file should be saved into the public sub-folder of your staging location/repository.


NB: I am assuming that you do not already have a .htaccess - if you do, then append these lines to the top of the file.

NB: Replace the depXXX xml with your deployment id. This can be found in the CloudControl console. Look under the application deployment page, and you will see a string starting with depXXX in the SFTP location that is given.

AuthType Basic
AuthName "My Protected Area"
AuthUserFile /data/local/depXXX/current/.htpasswd
Require valid-user

Your staging location/repository should now look something like this:


 <repo root> /
|
+-------- public/
| |
| +-------- .htaccess
| |
| +-------- index.php
| |
| +-------- ... other stuff ...
|
+-------- .ccconfig
|
+-------- .htpasswd


  • Deploy !! Perform your normal deployment routine from the base of your staging location/repository. Again, I am assuming you already know how to do this.

Thats it ! Worked a charm for me, and wasn't documented anywhere so I thought best to write it down before I forget ;)

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