Scala Meet WURFL. WURFL Meet Scala.

Scala Meet WURFL. WURFL Meet Scala.

Scala_logo

It’s a great pleasure for ScientiaMobile to host this post from Filippo De Luca. In addition to having been one of the lead engineers behind the modern WURFL Java API in the past, Filippo is an expert of the Scala Programming Language. Filippo advised ScientiaMobile on the benefits of Scala and on the architecture of a WURFL Java API for Scala that feels native to adopters of this great new enterprise language.
– Luca Passani, CTO @ScientiaMobile

 

 

The web today is very different from the one of eight years ago, when I was one the engineers behind the WURFL Java API. We have become accustomed to big numbers:

  1. Active Facebook daily users are about 900 million
  2. YouTube has more than 4 billion video views per day
  3. Google is serving about 12K billion searches per month.

 

More interesting, mobile traffic represents more than 50% of the total traffic of Internet nowadays. It is clear that the requirements of a web application written five years ago, especially a mobile one, are very different from the requirements of an application written today. Web applications are moving away from the concept of multi-apps containers, such as Tomcat, towards a more self-contained model such the Play! framework. And even further, with virtualization, to have several dedicated virtual machines. We need to scale more and to scale fast.

This evolution has not spared programming languages. What we were accustomed to using has become obsolete for the new generation of reactive applications. If we want to exploit a multicore architecture, we need a way to easily parallelize operations. This explains the recent spotlight over functional languages: Haskel, Erlang, Clojure… and Scala, of course, as they enable writing code that scales without the pain of dealing with race conditions and lockings.

The Scala language, in particular, is generally gathering consensus among Java developers, mostly because it allows a gradual migration from objects to a functional programming style, and it does so by providing a hybrid solution. The availability of very powerful frameworks, such as the Play! framework, Akka and Spray.io has triggered Scala’s widespread adoption.

In this context, providing a compatibility layer for WURFL (an API used by for mobile web applications ) is a natural development.  While the classic Java API could be used as well, the traditional object-oriented Java style does not fit very elegantly with the programming style that Scala promotes.
The WURFL Scala API is a thin layer on top of the Java API made possible by the Java interoperability that Scala offers. While not fully implemented in Scala, the “Scala wrapper”  offers a very clean and simple interface that should feel natural to Scala programmers.

If you are not familiar with Scala, I suggest you start here. Once you are familiar with Scala, using WURFL from Scala will be a breeze. The following code illustrates the use of the new API.

import com.scientiamobile.wurfl.Wurfl
import net.sourceforge.wurfl.core.GeneralWURFLEngine
import scala.util._

val wurflEngine = new GeneralWURFLEngine("wurfl.xml")
val wurfl = new Wurfl(wurflEngine)

wurfl.setFilter(
"can_assign_phone_number",
"marketing_name",
"brand_name","model_name",
"is_smarttv",
"is_wireless_device",
"device_os",
"device_os_version",
"is_tablet",
"ux_full_desktop",
"pointing_method",
"preferred_markup",
"resolution_height",
"resolution_width",
"xhtml_support_level",
"max_image_width",
"max_image_height"
)
val userAgent = "Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
var device = wurfl.deviceForRequest(userAgent)

println(device.matchType)
println(device.id)
val maxImageSize = for {
  maxImageWidth <- device.capability("max_image_width")
  maxImageWidthInPixels <- Try(maxImageWidth.toInt)
  maxImageHeight <- device.capability("max_image_height")
  maxImageHeightInPixels <- Try(maxImageHeight.toInt)
} yield {
  (maxImageWidthInPixels, maxImageHeightInPixels)
}