Spark + Freemarker basic example

Today, we are going to develop a little web page with a form using Spark and Freemarker. The purpose is simply to know how they are used together and how to manage GET and POST request. In addition, I am going to add Bootstrap to learn how to combine Freemarker with CSS.

As its web page describes “Spark – A tiny Sinatra inspired framework for creating web applications in Java 8 with minimal effort“. You can see a previous example I implemented in this blog called: Quick REST demo with Spark.

In the same way, as its web page describes “FreeMarker is a “template engine”; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates

The project has these requirements:

  • JDK 8
  • Maven
  • IDE. I have used IntelliJ IDEA CE, but you can use NetBeans, Eclipse or your favorite one.

The final structure for our project will be:

./pom.xml
./src/main/java/com/wordpress/binarycoders/SayMyName.java
./src/main/resources/css/bootstrap.css
./src/main/resources/templates/form.ftl
./src/main/resources/templates/result.ftl

The first step is to create a simple maven java project, obtaining in this way a pom.xml and a basic project structure. We should assign groupId, artifactId and version.

After that, we should edit our pom.xml file to add the dependencies that we are going to use to implement our example:

<dependency&gt;
    <groupId&gt;com.sparkjava</groupId&gt;
    <artifactId&gt;spark-core</artifactId&gt;
    <version&gt;2.1</version&gt;
</dependency&gt;
 
<dependency&gt;
    <groupId&gt;org.freemarker</groupId&gt;
    <artifactId&gt;freemarker</artifactId&gt;
    <version&gt;2.3.22</version&gt;
</dependency&gt;

As I have said at the beginning of this post, Freemarker is a template engine, then, the next step is to write the templates. We are going to write two templates:

  • form.ftl: This is going to be the first page of our application and it is going to show us a form to introduce “user” and “email“.
  • result.ftl: This page is going to show us the information introduced in the form.

The first template: form.ftl

<html&gt;
    <head&gt;
        <title&gt;Welcome</title&gt;
 
        <link href="css/bootstrap.css" rel="stylesheet" /&gt;
    </head&gt;
    <body&gt;
        <form class="form-inline" method="POST" action="/sait"&gt;
            <div class="form-group"&gt;
                <label for="name"&gt;Name</label&gt;
                <input type="text"
                       class="form-control"
                       id="name"
                       name="name"
                       placeholder="John Doe"&gt;
            </div&gt;
            <div class="form-group"&gt;
                <label for="email"&gt;Email</label&gt;
                <input type="email"
                       class="form-control"
                       id="email"
                       name="email"
                       placeholder="john.doe@example.org"&gt;
            </div&gt;
            <button type="submit" class="btn btn-default"&gt;Send invitation</button&gt;
        </form&gt;
    <body&gt;
</html&gt;

The second template: result.ftl

<html&gt;
    <head&gt;
        <title&gt;Said</title&gt;
    </head&gt;
    <body&gt;
        <h1&gt; Your name is ${name}</h1&gt;
        <br&gt;
        <h1&gt; Your email is ${email}</h1&gt;
    </body&gt;
</html&gt;

It is very important to remember in this point that to be allowed to recover our form parameters in the Java code we need to add the name attribute to our inputs. It is not enough with the id attribute.

In this point, we should add Bootstrap CSS file to our project.

And finally, we need to implement our main class that it is going to process the GET and the POST requests.

package com.wordpress.binarycoders;
 
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import spark.Spark;
 
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
 
public class SayMyName {
 
    public static void main(String[] args) {
        final Configuration configuration = new Configuration(new Version(2, 3, 0));
        configuration.setClassForTemplateLoading(SayMyName.class, "/");
 
        Spark.get("/", (request, response) -&gt; {
 
            StringWriter writer = new StringWriter();
 
            try {
                Template formTemplate = configuration.getTemplate("templates/form.ftl");
 
                formTemplate.process(null, writer);
            } catch (Exception e) {
                Spark.halt(500);
            }
 
            return writer;
        });
 
        Spark.post("/sait", (request, response) -&gt; {
            StringWriter writer = new StringWriter();
 
            try {
                String name = request.queryParams("name") != null ? request.queryParams("name") : "anonymous";
                String email = request.queryParams("email") != null ? request.queryParams("email") : "unknown";
 
                Template resultTemplate = configuration.getTemplate("templates/result.ftl");
 
                Map<String, Object&gt; map = new HashMap<&gt;();
                map.put("name", name);
                map.put("email", email);
 
                resultTemplate.process(map, writer);
            } catch (Exception e) {
                Spark.halt(500);
            }
 
            return writer;
        });
    }
}

Now, we only need to run this main class and… that’s all. We can go to our browser and introduce the URL:

http://localhost:4567

We should see our form with our Bootstrap style. If we introduce a username and an email, and we submit the form, we will see in the next page two messages with our form data.

Probably, as you have notice, we have not needed a server and this is because the Spark framework have an embedded Jetty server.

See you.

Spark + Freemarker basic example

Customizing our connections with proxies

Nowadays, it is clear that Java / Java EE are mainly back end technologies. In this role, it is quite common for a Java / Java EE application to connect to different systems to collect data or perform different operations, using REST web services, SOAP web services, sockets or other technologies that allow different systems to exchange information without a human being interference.

In complex systems, in addition to the big number of external and internal connections the system performs, we need to consider that sometimes we need to use one or more different proxies to reach the destiny.

Sometime the objects we are using to create and manage connections allow us to add this proxy information inside, but not always, and this creates always a big headache. We usually end, wrapping our object inside others that support a proxy addition.

Today, I have another suggestion that, in some cases, can do our lives easier. This option is to extend the ProxySelector class. This class has been around for a long time, you can find the API page for different Java version:

The idea is to extend this class and implement its abstract methods and assign this extended class as the default class for our system. Each time that a connection is going to be performed, the method List<Proxy> select(URI uri) is going to be called. In this method, we can decide if we need to add a proxy or not to this connection.

The resultant code should be something like that:

package com.wordpress.binarycoders.proxy;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
 
public class CustomProxySelector extends ProxySelector {
 
    private static final int DEFAULT_PORT = 80;
 
    private final ProxySelector defaultProxySelector;
 
    private Proxy proxy;
 
    private List<Proxy&gt; proxyList = new ArrayList<Proxy&gt;();
 
    public CustomProxySelector(String proxyHost, Integer proxyPort) {
 
        defaultProxySelector = ProxySelector.getDefault();
 
        proxy = new Proxy(Proxy.Type.HTTP,
                          new InetSocketAddress(proxyHost,
                                                (proxyPort == null) ?
                                                 DEFAULT_PORT :
                                                 proxyPort));
        proxyList.add(proxy);
    }
 
    @Override
    public List<Proxy&gt; select(URI uri) {
 
        if (uri == null) {
            throw new IllegalArgumentException("URI cannot be null.");
        }
 
        if (needsProxy(uri)) {
            return proxyList;
        }
 
        return def.select(uri);
    }
 
    @Override
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
 
        if ((uri == null) || (sa == null) || (ioe == null)) {
            throw new IllegalArgumentException("Arguments cannot be null.");
        }
 
        defaultProxySelector.connectFailed(uri, sa, ioe);
    }
 
    private boolean needsProxy(URI uri) {
        return uri.getHost().contains("urlNeedsProxy");
    }
}

As you can see, we only need a few lines to make it works. Obviously, we can make as complex as you want the method to determinate if a connection needs a proxy or not.

To use this class, we only need to add a simple line to the code in the start point of our application:

ProxySelector.setDefault(new CustomProxySelector(proxyHost, proxyPort));

And that’s all. Now with a few tests we can check if our solution works fine or not.

See you.

Note: Take care if your application share the server with other applications. Remember to test all of them just in case.

Customizing our connections with proxies

Adding a library to our maven repository

Sometimes the maven repositories don’t have all the libraries we need and, we need to add a concrete library to our repository. Usually, this is because there is a problem with its license, something similar or only because it is a library that we have built for our own projects. In both cases, the steps to add the library to our own repository are quite simple.

For this example, we are going to use the library example-1.0-jar and the information about this library is going to be:

  • groupId: org.example
  • artifactId: example
  • version: 1.0

To add this library to our repository, we only need to execute a simple command:

mvn install:install-file -Dfile=<libraryName&gt; -DgroupId=<groupId&gt; -DartifactId=<artifactId&gt; -Dversion=<version&gt; -Dpackaging=jar

With our example data, the command should look like this:

mvn install:install-file -Dfile=example-1.0.jar -DgroupId=org.example -DartifactId=example -Dversion=1.0 -Dpackaging=jar

Finally, we only need to add our new dependency in our maven file, and that’s all.

<dependency&gt;
    <groupId&gt;org.example</groupId&gt;
    <artifactId&gt;example</artifactId&gt;
    <version&gt;1.0</version&gt;
</dependency&gt;

See you.

Adding a library to our maven repository

Quick REST demo with Spark

What is Spark? If we take a look to the project’s web page, it says something like “Spark is a simple and lightweight Java web framework built for rapid development“. In other words, it proposes us a simple way to build web applications without dealing with XML or annotations.

I recognize I do not usually use it but I have found it very useful when I need to prepare some REST services for a quick demo. Do not missunderstanding me, I think that use the Java EE platform is amazing and you can develop with it very fast, but you need a server, a couple of classes with the JAX-RS configuration and a few annotations to make everything work. With Spark, you only need to add a dependency in you .pom file and write a few lines of code.

For the example, I am going to implement a little application offering REST services with the CRUD operations for a User. This entity is going to have:

  • id
  • name
  • surname

In first place, we should create a java application in our favorite IDE using maven.

In second place, we are going to add the correct maven dependency for the Spark library.

<dependencies&gt;
    <dependency&gt;
        <groupId&gt;com.sparkjava</groupId&gt;
        <artifactId&gt;spark-core</artifactId&gt;
        <version&gt;2.1</version&gt;
    </dependency&gt;
</dependencies&gt;

In third place, we should create our entity object.

public class User {
    private String id;
    private String name;
    private String surname;
     
    // Getters and setters
}

Now, we need to start to write our main class. In this main class we are going to add a couple of things:

  • A Map to simulate our data store.
  • A main method that it is going to contain our REST services.

The main class looks like:

public class Users {
    private static Map<String, User&gt; users = new HashMap<String, User&gt;();
 
    public static void main(String[] args) {
        final Random random = new Random();
         
        // Services here
    }
}

Now, we are going to start with the services.

/users?name=Foo&surname=Bar (POST)

Spark.post("/users", (request, response) -> {
    String name = request.queryParams("name");
    String surname = request.queryParams("surname");
 
    User user = new User(name, surname);
    int id = random.nextInt(Integer.MAX_VALUE);
     
    users.put(String.valueOf(id), user);
 
    response.status(201); // 201 Created
     
    return id;
});

/users/:id (GET)

Spark.get("/users/:id", (request, response) -> {
    User user = users.get(request.params(":id"));
    if (user != null) {
        return "Name: " + user.getName() + ", Surname: " + user.getSurname();
    } else {
        response.status(404); // 404 Not found
        return "User not found.";
    }
});

/users/:id (PUT)

Spark.put("/users/:id", (request, response) -> {
    String id = request.params(":id");
    User user = users.get(id);
    if (user != null) {
        String newName = request.queryParams("name");
        String newSurname = request.queryParams("surname");
        if (newName != null) {
            user.setName(newName);
        }
        if (newSurname != null) {
            user.setSurname(newSurname);
        }
        return "User with id '" + id + "' has been updated.";
    } else {
        response.status(404); // 404 Not found
        return "User not found.";
    }
});

/users/:id (DELETE)

Spark.delete("/users/:id", (request, response) -> {
    String id = request.params(":id");
 
    User user = users.remove(id);
 
    if (user != null) {
        return "User with id '" + id + "'has been deleted";
    } else {
        response.status(404); // 404 Not found
        return "User not found.";
    }
});

/users (GET)

Spark.get("/users", (request, response) -> {
    String ids = "";
 
    for (String id : users.keySet()) {
        ids += id + " ";
    }
     
    return ids;
});

Now, we only need to execute our application and check if everything is working correctly. To do this, we can use cURL or our browser with the URL:

http://localhost:4567/users

The next steps are on you. Implement a front-end with Javascript or consuming the services with a mobile app or… whatever you prefer.

See you.

Quick REST demo with Spark

Increasing Java Encryption Strength

If you are a Java developer and you have worked with encryption, I am sure that you have notice the limit of 128-bit keys that the default JDK has. I do not know exactly why this restriction exists, it looks like related with some US laws. Fortunately, Java supports bigger keys but not out of the box, we need to perform a couple of actions to achieve this. The 128-bit limit sounds a little small and dated.

If you try to execute your code with a bigger key than 128-bit, you should receive and exception like:

java.security.InvalidKeyException:Illegal Key Size

The way to solve this, if we want to work with bigger keys, for example with a 256-bit AES encryption, it is to install the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for JDK/JRE.

Oracle offers the necessary .jar files in its downloads page in the Additional Resources section. In the previous link you can find the versions for JDK/JRE 8 and JDK/JRE 7.

After download and unzip the downloaded file (jce_policy-8.zip for JDK/JRE 8) you will have two new .jar files plus a README.txt file:

  • local_policy.jar
  • US_export_policy.jar

Now, you only need to copy these two .jar files to the correct path in your system. The correct path for default installations should be:

<java-home&gt;/lib/security  [Unix]
<java-home&gt;\lib\security  [Windows]

With these few steps, now, you only need to restart your applications to have support for bigger keys.

If you need more information, you can read the README.txt file.

See you

Tip: Remember that the maximum key length permitted by policy can be different from the maximum key length permitted by algorithms

Increasing Java Encryption Strength