@GET
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public List<Message> getMessages(@javax.ws.rs.core.Context HttpHeaders requestHeaders) throws SQLException
    {
        int id = -1, limit = -1;
        
        MultivaluedMap<String,String> headers = requestHeaders.getRequestHeaders() ;
        
        if (headers.containsKey("X-Last-Received"))
        {
            id = Integer.parseInt(headers.get("X-Last-Received").get(0)) ;
        }

        if (headers.containsKey("X-Limit"))
        {
            limit = Integer.parseInt(headers.get("X-Limit").get(0)) ;
        }
        
        Connection connection = null;
        PreparedStatement statement = null;

        try
        {
            connection = dataSource.getConnection();

            if (id < 0)
            {
                statement = connection.prepareStatement("SELECT * FROM messages ORDER BY ts ASC");
            }
            else if (limit < 0)
            {
                statement = connection.prepareStatement("SELECT * FROM messages WHERE id > ? ORDER BY ts ASC");
                statement.setInt(1, id);
            }
            else
            {
                statement = connection.prepareStatement("SELECT * FROM messages WHERE id > ? ORDER BY ts ASC LIMIT ?") ;
                statement.setInt(1, id);
                statement.setInt(2, limit);
            }

            ResultSet results = statement.executeQuery();

            List<Message> messages = new ArrayList<Message>();

            while (results.next())
            {
                messages.add(new Message(results.getInt("id"), results.getString("author"), results.getString("content"), results.getTimestamp("ts")));
            }

            return messages;
        }
        finally
        {
            if (statement != null)
            {
                try
                {
                    statement.close();
                }
                catch (SQLException e)
                {
                    /* At least should be logged. */
                }
            }

            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (SQLException e)
                {
                    /* At least should be logged. */
                }
            }
        }
    }