Archive for the ‘Tech’ Category

JAX-RS (using Apache CXF) in embedded Tomcat example

Tuesday, January 13th, 2015

I had serious trouble today firing up an embedded Tomcat server that serves up REST resources using JAX-RS via Apache CXF.

Here’s minimal example, hopefully saving you the same trouble:


import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;

public class HelloJaxRs
    public static void main( String[] args ) throws Exception
        Tomcat tomcat = new Tomcat();
        Context context = tomcat.addContext( "/", "" );

        Wrapper servlet = context.createWrapper();
        servlet.setName( "jaxrs" );
            "org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet" );


        servlet.setLoadOnStartup( 1 );
        context.addChild( servlet );
        context.addServletMapping( "/rest/*", "jaxrs" );




@Path( "/hello/{name}" )
public class Hello
    public String getThing( @PathParam( "name" ) String name )
        return "Hello, " + name;


<?xml version="1.0" encoding="UTF-8"?>
<project default="run">

    <target name="run" depends="compile">
        <mkdir dir="bin/tomcat.8080/webapps"/>
                <pathelement location="bin/"/>
                <fileset dir="lib"><include name="**/*.jar"/></fileset>

    <target name="compile">
        <mkdir dir="bin"/>
        <javac srcdir="src" destdir="bin">
                <fileset dir="lib"><include name="**/*.jar"/></fileset>
            <compilerarg value="-Werror"/>


To download the dependencies (or you can do something clever with Maven):

$ mkdir lib
$ cd lib
$ wget
$ tar -xzf apache-tomcat-7.0.57.tar.gz
$ wget
$ tar -xzf apache-cxf-3.0.3.tar.gz
$ cd ..

To run Tomcat:

$ ant

Then, in another terminal, you can see it’s working:

$ curl http://localhost:8080/rest/hello/andy
Hello, andy

Vim persistent buffer list combined with saved sessions

Thursday, January 8th, 2015

I like to use several saved Vim sessions to remember what files I was working on in a particular project.

I also like to have a list of buffers I have open on the left-hand side of the screen that is always visible and up-to-date.

Sometimes, the existence of a generated buffer like the list of buffers can confuse the session restore code, but I have found a way to make this work nicely using the Buffergator plugin and the built-in mksession command.

Here are the relevant snippets from my .vimrc.

I am a beginner: please comment if you see bugs or possible improvements.

" Prerequisites: install the Buffergator plugin

" Save session on exit, excluding problematic stuff like options

set sessionoptions=blank,buffers,curdir,tabpages,resize,winpos,winsize
au VimLeavePre * if v:this_session != '' | exec "mks! " . v:this_session | endif

" Open Buffergator when you press F5

nnoremap <F5> :BuffergatorOpen<CR>

" Various Buffergator options to make it persistent,
" and displayed how I like

let g:buffergator_autodismiss_on_select = 0
let g:buffergator_display_regime = "bufname"
let g:buffergator_sort_regime    = "filepath"
let g:buffergator_autoupdate = 1

" Suppress the error message about the Buffergator buffer
" when you restore a session

function! OpenBuffergatorIfItsBufferExists()
    if bufnr("[[buffergator-buffers]]") > ''
        normal o

autocmd VimEnter * call OpenBuffergatorIfItsBufferExists()

Now when I start vim with vim -S path/to/session.vim the session is restored and saved when I exit, with a persistent buffer list on the left, and no errors during the session restore.

Encoding URLs in Java

Thursday, December 4th, 2014

To encode a URL in Java, create a new instance of and then call toString() or toASCIIString() on it.

DO NOT use It is for HTML form encoding, not encoding URLs, despite its name.

Each part of a URL must be escaped differently, so use the multi-argument constructors for URI where you have multiple parts you want to stick together. You can pass in null for arguments you want to omit, and it works the way you’d hope.

This program:

class EncodeUrls
    public static void main( String[] args ) throws
        url( "http", "", "/x/y/x", "x=y", "pos" );


    private static void url(
        String scheme,
        String host,
        String path,
        String query,
        String fragment
    { uri = new
            scheme, host, path, query, fragment );

        System.out.println( "." + uri.toString() );
        System.out.println( "." + uri.toASCIIString() );




LibreOffice spell check not working in Lubuntu

Wednesday, November 12th, 2014

I installed LibreOffice into Lubuntu, but the spell checking didn’t work.

It turns out I need to install a dictionary for my locale:

sudo apt-get install myspell-en-gb

I needed to restart LibreOffice, and then I got spell checking as I typed.

Is it ok to represent dates as Unix time (seconds since the epoch) in JSON?

Thursday, November 6th, 2014


If your JSON contains integer numbers that represent Unix time (seconds since the “epoch”), and you parse your JSON to JavaScript, the range of integers that can accurately be represented is -9007199254740992 to 9007199254740992 (ref: EcmaScript standard, section 8.5).

The year 4000AD starts at unix time 64060588815, which is 9007135194152192 less than the maximum.

The year 1AD started at unix time -2177452800, which is 9007197077288192 more than the minimum.

So there are enough numbers.