Home

Awesome

OpenDDR-Java

Dear OpenDDR user,

In the follow the description of directory tree:

A basic explanation of the properties in oddr.properties:

This is a java example on how to integrate OpenDDR in a servlet Filter:

<pre><code> 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"); } } </code></pre>