Correct Usage of Java 8 Optional

Optional is a new type of Java 8. I saw it before with scala and was quite impressed. You know Tony Hoare’s “billion-dollar mistake”: the null reference. How many NullPointerExceptions have you yet? The type Optional wants to fix that. With Optional you won’t get any NullPointerExceptions anymore. But it is important to use Optional the right way. So don’t disappoint Hoare and take the chance to fix the billion-dollar mistake.

Return values

If you have a method with a result which sometimes may be empty, don’t return null. Even if you add a comment to your method. They will not always read it.

* @return an object or else null.
public Object getSomething() {
  if (haveObject) {
    return object;
  } else {
    return null;

You should tell your user and the compiler that they can not be sure to get an object or not. Use the Optional:

* @return Optional of Object
public Optional<Object> getSomething() {
  if (haveObject) {
    return Optional.of(new Object());
  } else {
    return Optional.empty();

Only do if not null

Consuming an optional value you probably want to do something only if a value is present. You could do it by using the method isPresent():

public void doOnlyIfPresentComplicated() {
  Optional<Object> something = logic.getSomething();
  if (something.isPresent()) {

But this is not much of a progress compared to writing if (something != null). This is where the lambda expressions of optional come into play. Using ifPresent() you can write this much better:

 public void doOnlyIfPresent() {

Avoid nested null checks

Sometimes you have several optional values in a row. Only if all optionals return a value, you want to return an http 200. Http 404 should be returned if somewhere in the chain there is no value. Again there is a complicated way:

public Response avoidNullChecksComplicated() {
  Optional<Object> something = logic.getSomething();
  if (something.isPresent()) {
    Optional<Object> otherOptional = logic.otherOptional(something.get());
    if (otherOptional.isPresent()) {
      Optional<Object> lastOptional = logic.lastOptional(otherOptional.get());
      if (lastOptional.isPresent()) {
        return Response.ok(lastOptional.get()).build();
  return Response.status(Status.NOT_FOUND).build();

Or you avoid the nested if statements and simply use flatMap and method references:

 public Response avoidNullChecks() {
   return logic.getSomething() //
     .flatMap(this::getOtherOptional) //
     .flatMap(this::getLastOptional) //
     .map(this::getOKResponse) //

 private Optional<Object> getOtherOptional(Object o) {
   return logic.otherOptional(o);

 private Optional<Object> getLastOptional(Object o) {
   return logic.lastOptional(o);

 private Response getOKResponse(Object o) {
   return Response.ok(o).build();

 private Response get404Response() {
   return Response.status(Status.NOT_FOUND).build();

I think it is much better than the nested if statements. If you use all Optionals the right way, your code stays readable and you can fix some of the “billion-dollar mistake”.

Why I don’t like IBM RTC as Source Control Management

In a previous post I already mentioned why the architecture of IBM RTC will never scale like git. As a developer you use an SCM and probably a project management tool on a daily basis. Even small annoying things make you feel bad and are time consuming. In this post I list some reasons why a move away from RTC to git will save real money for your organisation and make your work easier.

Maven integration

IBM RTC is not prepared to work with hierarchical structured projects. Maven is a widely used build and dependency management tool. In some cases it is important to have a hierarchical structure of your projects. Especially if you work with parent and child projects. Unfortunately RTC hinders you to check out parent and child projects in the same workspace. You can make it run but have to do some workarounds. There is also a good blog post about it by Mike MacDonagh (Working with Maven multi-module projects in RTC Jazz SCM with m2). He talks about RTC v3.x but it is basically the same as with v5.x.

There are no reasons why you couldn’t do it with git. git does not know anything about a project. It is not it’s business. RTC seams to manage things that it shouldn’t.

IDE integration

All current IDEs have git support in their basic versions. I don’t know of any Java-IDE which comes with IBM RTC integration out of the box. This means you have to install it manually. In case of Eclipse it means to download about 500 MB(!) of plugins. With every new version of Eclipse you have to wait for the new IBM plugin before you can use the new features. You got the same situation with Visual Studio, IntelliJ and NetBeans.

Comparing file versions between branches / streams

git_branch_compareWith git you can easily compare file versions from different releases in different branches. The history view shows all very clearly (image on the right side).

In IBM RTC you can’t do that. It is a recommendation from IBM to create a new stream for every release. At first sight it might look pretty but if you really work with it you see the disadvantages:

  • It is complicated to merge streams.
  • There is really no way to compare your file from one stream with another stream until you load both streams.
  • It is even impossible to see if the file changed in another branch.


Some people say the merging process of git was better than the one with IBM RTC. Maybe I will post a direct and detailed comparison between those two tools later.

Complicated commits / delivery

This is a quick comparison between the git way and the RTC way.

The git way

Either you use the console:

git pull
git commit -m 'your comment'
git push origin master

Or you use the awesome Git Staging view from Eclipse:

To commit and push there are only three steps:

  1. Drag and drop the Files to your staging area
  2. Add a comment
  3. Click the Button “Commit and Push”

The RTC way

IBM RTC didn’t manage to create such a smooth view. Every action needs lots of server calls because you don’t have a local repository. They added a lot of asynchronous calls which lets your view update randomly. You sometimes wait for ages depending on how big your component, stream, change set or history is.

You need to do make at least five steps. It includes context menus, keyboard strokes and tree navigation:

  1. check-in your changes (3 clicks)
  2. Open the outgoing folder under your stream and find your newly created change set.
  3. Either click on it once and press “F2” or wait a second and perform another click.
  4. type your comment
  5. Deliver (another 2 clicks)
  6. Sometimes you have to answer some complicated questions about not checked-in changes.
  7. Then some asynchronous tasks start running and mostly after a second or two everything is delivered.

Working offline

As you have no local repository every interaction with RTC requires a server call. It is not designed to work offline. Git always had that use case in mind.

Git ecosystem

Git is very popular, widely used and extendable with hooks and triggers. Therefore many open source projects emerged around it. Just a brief list:

  • “Social coding”. The most popular git repository. I social web ui with issue tracking and many other integrations.
  • gitblit: Github like open source server ready to host on your local infrastructure.
  • Project management web application with git integration.
  • Jenkins / Travis: Build servers best integrated with git.
  • Atlassian Confluence: Team collaboration for software development.

You need really good reasons to make a decision against this tools and for a proprietary SCM.

Licenses and hardware resources

IBM RTC is expensive and resource consuming. I am probably not allowed to tell you the price you have to pay. But believe me it is expensive. To keep RTC running you need:

  • RTC-Server on a heavy weight Websphere
  • An expensive database like DB/2 (or MSSQL or Oracle although not recommended)

Git needs:

  • A filesystem for your repositories

You will install another tool like those mentioned above if you need issue tracking or project management. They will mostly run with an apache2 or apache tomcat and a MySQL database server.

Other issues or feedback?

Let me know if you got further pros or cons of RTC or git. I will update this post as I stumble upon more points.

How to add custom headers to Apache Camel Restlet

We use Apache Camel to glue services together. Camel Restlet can help you to translate a bean call into a REST call and vice versa. As we started using it we came to a need to add custom headers to a REST call. We thought it must be an easy job but it ended up digging deep into the Restlet source code to find a solution.

But first: Thanks to Lukas Eberli for his digging. He finally found the solution!

To be honest we didn’t read the documentation first. We just had a look at the API. I’m sure you work the same way. But what you get if you inspect the Camel interface ProducerTemplate is a massive amount of methods (more than 80!). OK, let’s just concentrate on the 10 (!) overloads of sendBodyAndHeaders – or should I use requestBodyAndHeaders? This two have both a parameter headers of the type Map. But don’t think you could just put your own headers into this map. That won’t work. Your headers will just disappear somewhere deep in Restlets code. The only possibility to put your own headers into the http request is this:

// some needed imports
import org.restlet.util.Series;
import org.restlet.engine.http.header.HeaderConstants;

// create the header map as an argument for
// the producer template:
Map<String, Object> headers = new HashMap<>();

// create the header series where you put your
// custom headers

<header>userDefinedHeaders = new Series
userDefinedHeaders.add(new Header("x-my-header", myHeaderValue));</header></header>// tell Restlet to add custom headers by using the
headers.put(HeaderConstants.ATTRIBUTE_HEADERS, userDefinedHeaders);

// finally call the send method
producerTemplate.sendBodyAndHeaders(endpoint, body, headers);

Unfortunately we could not find any documentation pointing to this. Only digging into the code of Restlet reveals this features. I am also disappointed of the giant interface ProducerTemplate. The lack of types puts you into type casting everything. I thought apache guys would do better than this. There is obviously a lot to improve…

@font-face not working with Internet Explorer and HTTP-Header Pragma=no-cache

We recently spent a whole day trying to fix an Internet Explorer related bug. If you got the same problem you can find our solution here.

Our phenomenon

We included a special font with the css property @font-face. That worked quite well for all browsers. Until we used SSL for HTTP encryption. IE could not load the fonts anymore. By examining the HTTPS traffic with Fiddler we finally found out that the HTTP header “pragma=no-cache” did the difference. So if you want to user @font-face and deliver your site with HTTPS you must never ever set the HTTP header “pragma=no-cache”. As soon as we got rid of this HTTP header, IE behaved the way it did before.

I created a test site to reproduce the problem:

Edit 2014-10-04:

There are others who report that the headers “Vary” or “Cache-Control=no-cache” confused IE.

How we fixed the problem

In our case we use following stack:

  • Wildfly 8.1 as application server
  • Undertow 1.1 as web server
  • BASIC authentication

Unfortunately Undertow 1.1 introduced the feature to disable caching for secured resources (commit). Since then all resources are delivered with pragma=no-cache which are secured with BASIC authentication. So since we updated to Undertow 1.1 IE did not work properly anymore. The only way to fix it was not to secure the fonts. We could not find any way to overrule the new default behaviour of Undertow 1.1.

Still upset about IE

We are still upset about the behaviour of the Internet Explorer! Why on earth do they always manage to screw things up like that? If you watch the communication between IE and your server you see following:

  1. IE requests the font from the server.
  2. As soon as the server starts delivering the resource, IE closes the connection. Probably because it suddenly detects that it should get the resource from cache.
  3. This behaviour apparently destroys the cache content. So IE can’t access the font.
  4. IE tries to retrieve the next declared font but with the same wrong pattern.
  5. At the end it all leads to no font and a messy web site.

It is definitely a bug in Internet Explorer. Secured content should not be cached. But IE just can’t cope with it. We therefore filed a bug at IE Developer Cannel: We’re curious if this is going to be fixed…

Edit 2015-04-21:

Sorry for the bad news. They didn’t fix it. They just changed status to “Won’t fix” and “Closed”. Although there are other people reporting the same issue.

Create custom launchers for my eclipse installations

Launchers in linux are *.desktop files which describe an startable desktop application. Desktop environments like KDE, Gnome or Xfce use those files to create their start menus. Also my favorite application starter Kupfer uses the same information source. Most of the applications are installed using apt-get from a distribution like Ubuntu. They always install their own launchers for all users on that machine. But if you install for example eclipse distributions in your home directory, you have to create the .desktop files on your own. To get the best result I do it like this:

Read More