Git Product home page Git Product logo

openddr-java's Introduction

OpenDDR-Java

Dear OpenDDR user,

In the follow the description of directory tree:

  • lib: contains necessary libraries to run OpenDDR-Simple-API;
  • src: contains java source code and the oddr.properties file. Please configure the oddr.properties file with the right path of the resources.

A basic explanation of the properties in oddr.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
  • oddr.ua.operatingSystem.datasource.path: Path of the operating system 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.

This is a java example on how to integrate OpenDDR in a servlet 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");
	    }
	}

openddr-java's People

Contributors

openddr avatar wlk avatar

Stargazers

arturcampos avatar

Watchers

Elio avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.