Skip to content
OpenDDR edited this page Nov 30, 2011 · 1 revision

Introduction

In this tutorial we show how to integrate OpenDDR-Java in a sample Web App.
Identification is performed in a servlet filter.
For simplicity we identify only four properties: vendor, model, displayWidth and displayHeight all from the Device aspect.
The identified properties are added as attribute in the HTTPServletRequest.
We use Netbeans as IDE and JavaEE 5 as platform.

Integration Steps

  • Create a new Web Application (New Project → Java Web → Web Application). In this sample WebApp we do not rely on any additional framework.
  • Add the needed libraries to the Web App (right click on the project → Properties → Libraries → Add Jar/Folder). We need W3C DDR-Simple-Api.jar, OpenDDRSimpleApi.jar, slf4j-api-1.6.1.jar and commons-lang-2.1.jar.
  • Add oddr.properties file in /WEB-INF directory. In the properties file set the file system paths properties for the relative files as showed below:

oddr.ua.device.builder.path=/PATH_TO_FILE/BuidlerDataSource.xml
oddr.ua.device.datasource.path=/PATH_TO_FILE/DeviceDataSource.xml
oddr.ua.device.builder.patch.paths=/PATH_TO_FILE/BuilderDataSourcePatch.xml
oddr.ua.device.datasource.patch.paths=/PATH_TO_FILE/DeviceDataSourcePatch.xml
oddr.ua.browser.datasource.path=/PATH_TO_FILE/BrowserDataSource.xml
ddr.vocabulary.core.path=/PATH_TO_FILE/coreVocabulary.xml
oddr.vocabulary.path=/PATH_TO_FILE/oddrVocabulary.xml
oddr.limited.vocabulary.path=/PATH_TO_FILE/oddrLimitedVocabulary.xml
oddr.vocabulary.device=http://www.openddr.org/oddr-vocabulary
oddr.threshold=70
A basic explanation of the properties:
  • oddr.ua.device.builder.path: Path of the file that explain how to identify the devices. In this, for each builder, are specified the devices id that the builder handle and the identification rules
  • oddr.ua.device.datasource.path: Path of the device datasource
  • oddr.ua.device.builder.patch.paths: Path of the patch file for the builder file
  • oddr.ua.device.datasource.patch.paths: Path of the patch file for the device data source
  • oddr.ua.browser.datasource.path: Path of the browser data source
  • ddr.vocabulary.core.path: Path of the W3C vocabulary file
  • oddr.vocabulary.path: Path of OpenDDR vocabulary
  • oddr.limited.vocabulary.path: Path of the reduced vocabulary. This vocabulary is usefull to limitate the memory load. It can be safely left unspecified.
  • oddr.vocabulary.device: IRI of the default vocabulary. It is the target namespace specified in a vocabulary
  • oddr.threshold: Identification threshold. It is used to balance the request evaluation time and identification matching.
  • Create new package org.openddr.samplewebapp.filter (Right click on Source Packages → new java Package) and create new java class IdentificationFilter. In the follow the source code of the filter:

package org.openddr.samplewebapp.filter;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.openddr.simpleapi.oddr.ODDRService;
import org.openddr.simpleapi.oddr.model.ODDRHTTPEvidence;
import org.w3c.ddr.simple.Evidence;
import org.w3c.ddr.simple.PropertyRef;
import org.w3c.ddr.simple.PropertyValue;
import org.w3c.ddr.simple.PropertyValues;
import org.w3c.ddr.simple.Service;
import org.w3c.ddr.simple.ServiceFactory;
import org.w3c.ddr.simple.exception.NameException;

public class IdentificationFilter implements Filter {

    private Service identificationService = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Initialize filter");
        Properties initializationProperties = new Properties();
        ServletContext context = filterConfig.getServletContext();                

        try {
            initializationProperties.load(context.getResourceAsStream("/WEB-INF/oddr.properties"));
            identificationService = ServiceFactory.newService("org.openddr.simpleapi.oddr.ODDRService", initializationProperties.getProperty(ODDRService.ODDR_VOCABULARY_IRI), initializationProperties);

        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        PropertyRef vendorRef;
        PropertyRef modelRef;
        PropertyRef displayWidthRef;
        PropertyRef displayHeightRef;

        try {
            vendorRef = identificationService.newPropertyRef("vendor");
            modelRef = identificationService.newPropertyRef("model");
            displayWidthRef = identificationService.newPropertyRef("displayWidth");
            displayHeightRef = identificationService.newPropertyRef("displayHeight");

        } catch (NameException ex) {
            throw new RuntimeException(ex);
        }

        PropertyRef[] propertyRefs = new PropertyRef[] {vendorRef, modelRef, displayWidthRef, displayHeightRef};
        Evidence e = new ODDRHTTPEvidence();
        e.put("User-Agent", ((HttpServletRequest)request).getHeader("User-Agent"));

        try {
            PropertyValues propertyValues = identificationService.getPropertyValues(e, propertyRefs);
            PropertyValue vendor = propertyValues.getValue(vendorRef);
            PropertyValue model = propertyValues.getValue(modelRef);
            PropertyValue displayWidth = propertyValues.getValue(displayWidthRef);
            PropertyValue displayHeight = propertyValues.getValue(displayHeightRef);

            if (vendor.exists() && model.exists() && displayWidth.exists() && displayHeight.exists()) {
                ((HttpServletRequest)request).setAttribute("vendor", vendor.getString());
                ((HttpServletRequest)request).setAttribute("model", model.getString());
                ((HttpServletRequest)request).setAttribute("displayWidth", displayWidth.getInteger());
                ((HttpServletRequest)request).setAttribute("displayHeight", displayHeight.getInteger());

            }

        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        chain.doFilter(request, response);
    }

    public void destroy() {
        System.out.println("Destroy Filter");
    }
}
In the init method we configure our OpenDDR Service. First of all we load the properties file from /WEB-INF/oddr.properties. After that we create our Service by W3C ServiceFactory in DDR-Simple-Api.jar. There are three argument:
  • The class name of OpenDDR Service
  • The default vocabulary IRI (in the example we use our oddr-vocabulary)
  • The properties file (oddr.properties)
    After the initialization the OpenDDR Service is ready to use.
In the doFilter method for each request (see the configuration of web.xml in the next step) we get four properties from the OpenDDR Service: vendor, model, displayWidth and displayHeight. First of all we create four PropertyRef. As you can see we do not specify the vocabulary IRI and the aspect so we consider the default vocabulary (as specified in oddr.vocabulary.device property) and the default aspect of the properties; in the oddr-vocabulary the default aspect for these properties is the “device”. All the PropertyRef objects are inserted in the propertyRefs array. Keep in mind that retrive a propertyRefs array is mutch more efficent that multiple identification on the same evidence. The next step is the creation of the evidence from the HTTP header “User-Agent”. During the identification the evidence is evaluated, and the requested propertyRef are retrived as properyValues. From the PropertyValues object it is possible to extract any PropertyValue object requested. If exist a valid value for the properties these are added as attribute in the http request. Now the identification filter has achieved its goal, and the chain.doFilter() method is called.
  • Configure the filter in the web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <filter>
        <filter-name>IdentificationFilter</filter-name>
        <filter-class>org.openddr.samplewebapp.filter.IdentificationFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>IdentificationFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
As you can se the identification filter is mapped for all the request sent to the Web App.
  • Modify index.jsp.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>OpenDDR Sample</title>
    </head>
    <body>
        <h1>${vendor} ${model} resolution: ${displayWidth} x ${displayHeight}</h1>
    </body>
</html>
The identified properties can be accessed as attribute of the request parameter.

Now you can deploy and run the example Web Application.

Clone this wiki locally