Custom 404 page template in Drupal

A simple snippet to define a custom 404 and 403 theme suggestion in Drupal:

function YOURTHEME_preprocess_page(&$vars) {
  $header = drupal_get_http_header("status");
  if($header == "404 Not Found") {
    $vars['theme_hook_suggestions'][] = 'page__404';
  if($header == "403 Forbidden") {
    $vars['theme_hook_suggestions'][] = 'page__403';

The template file should be named page--404.tpl.php and clear the cache and this should work.


Adding users

In a previous post I’ve showed some code to add nodes to Drupal, in this blog I will show that the same can be done with user objects.

$roles = array(
   5 => 'visitor'

$fields = array(
    'name' => $username,
    'mail' => $email,
    'pass' => $password,
    'status' => 1,
    'init' => 'email address',
    'roles' => $roles
//the first parameter is left blank so a new user is created
$account = user_save('', $fields);

// $account will contain the new created user object

The fields are rather simple and easy to understand what they do:

  • name; loginname of the user
  • mail; email address
  • pass; plaintext password, will be hashed on save
  • status; 1 for an active user and 0 for an blocked user
  • roles; an array with the roles to assign to the user. The number and name is different per Drupal installation

So it is rather simple to add an user in Drupal, as it should be 🙂

An overview of REST

REST is maybe the most wrongly used word within the web world. It has became synonym with any sort of API that can be used of HTTP. As this frustrates me sometimes I will try to explain what REST is about and it is very simple and elegant in the basis.

REST stands for Representational State Transfer and is an architectural style to connect multiple components with each other. It defines some constraints to which a RESTful api should adhere. The constraints will be explained below:
Continue reading An overview of REST

Type safety

Consider the method findByDate that executes a query to find something by the year and month. One way of implementing this method in Java can be with two parameters year & month, both of type int. But this has some problems with passing valid arguments to methods and statically type checking.

public function findByDate(int year, int month) {
  // do stuff

Continue reading Type safety

MySQL regular expressions

Just found out MySQL can also use regular expression in the WHERE and I used it to match all valid URL’s and to find invalid URL’s.

To find all rows that match the regular expression

SELECT * FROM campaign
WHERE url REGEXP "^((https?|ftp)://|(www|ftp)\.)[a-z0-9-]+(\.[a-z0-9-]+)+([/?].*)?$";

To find all rows that not match the regular expression

SELECT * FROM campaign 
WHERE url NOT REGEXP "^((https?|ftp)://|(www|ftp)\.)[a-z0-9-]+(\.[a-z0-9-]+)+([/?].*)?$";

Websockets in Java EE

With the JSR-356 spec websockets can be easily created and run within a Java EE container. I am using TomEE as a very lightweight and easy to install and deploy container but every Java EE 7 container should work, thanks to the EE specs.

I’m showing some easy to understand example of the Java POJO and some Javascript example code, together with some explanation about what is does and how it should work.
Continue reading Websockets in Java EE

OpenCL in Java

My first experiment with OpenCL was in XCode on Mac and I was wondering if there is also support for Java. My C/C++ skills are not what they used to be and I feel more comfortable with Java. Luckily there is a binding for Java which is called Javacl and I have tried to make the shortest working example to learn how OpenCL is working and how the library must be used. I’m sharing this example because I found that there are not many simple short examples.

At the end of the post you find the complete sourcecode I’ve used and I will describe the code in a few blocks to make clear what it does and why. You should be familiar with some of the OpenCL terminology, this helps in understanding the code.

Continue reading OpenCL in Java

Using ETags

With an Etag (or Entity Tag) it is possible to check with the server if a resource has changed the last time it is received. The server sends an Etag in the headers of the request when a resource is received. A md5 sum of the content is a good candidate for a Etag because it changes when the content changes. It could be useful when the resource is not static but it will not change very often. The controller in a web application can perform the ETag comparison and decide when to send a the resource or a not modified. Continue reading Using ETags

Some personal notes on Software Engineering, Robotics, Programming etc..