Author: wkeil Date: Mon Dec 29 02:54:46 2014 New Revision: 1648283 URL: http://svn.apache.org/r1648283 Log: DMAP-111: Improve Web Examples
Task-Url: https://issues.apache.org/jira/browse/DMAP-111 Modified: devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/ODDRService.java devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/AndroidDeviceBuilder.java devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/OrderedTokenDeviceBuilder.java devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/identificator/DeviceIdentificator.java Modified: devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/ODDRService.java URL: http://svn.apache.org/viewvc/devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/ODDRService.java?rev=1648283&r1=1648282&r2=1648283&view=diff ============================================================================== --- devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/ODDRService.java (original) +++ devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/ODDRService.java Mon Dec 29 02:54:46 2014 @@ -97,8 +97,8 @@ public class ODDRService implements Serv public static final String ODDR_UA_OPERATINGSYSTEM_DATASOURCE_STREAM_PROP = "oddr.ua.operatingSystem.datasource.stream"; public static final String ODDR_THRESHOLD_PROP = "oddr.threshold"; public static final String ODDR_VOCABULARY_IRI = "oddr.vocabulary.device"; - private static final String ODDR_API_VERSION = "0.2-SNAPSHOT"; - private static final String ODDR_DATA_VERSION = "2014"; + private static final String ODDR_API_VERSION = "1.0.0-SNAPSHOT"; + private static final String ODDR_DATA_VERSION = "1.0.2-SNAPSHOT"; private static final int ODDR_DEFAULT_THRESHOLD = 70; private String defaultVocabularyIRI = null; private DeviceIdentificator deviceIdentificator = null; @@ -181,7 +181,6 @@ public class ODDRService implements Serv if (oddrUaDeviceBuilderPatchPaths != null && oddrUaDeviceBuilderPatchPaths.trim().length() != 0) { oddrUaDeviceBuilderPatchPathArray = oddrUaDeviceBuilderPatchPaths.split(","); - } else { oddrUaDeviceBuilderPatchPathArray = new String[0]; } @@ -190,7 +189,6 @@ public class ODDRService implements Serv if (oddrUaDeviceDatasourcePatchPaths != null && oddrUaDeviceDatasourcePatchPaths.trim().length() != 0) { ooddrUaDeviceDatasourcePatchPathArray = oddrUaDeviceDatasourcePatchPaths.split(","); - } else { ooddrUaDeviceDatasourcePatchPathArray = new String[0]; } @@ -213,14 +211,12 @@ public class ODDRService implements Serv if (oddrThreshold == null || oddrThreshold.trim().length() == 0) { this.threshold = ODDR_DEFAULT_THRESHOLD; - } else { try { this.threshold = Integer.parseInt(oddrThreshold); if (this.threshold <= 0) { this.threshold = ODDR_DEFAULT_THRESHOLD; } - } catch (NumberFormatException x) { this.threshold = ODDR_DEFAULT_THRESHOLD; } @@ -313,15 +309,24 @@ public class ODDRService implements Serv parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { + try { + stream.close(); + } catch (IOException ie) { + logger.warn("", ie); + } throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { + try { + stream.close(); + } catch (IOException ie) { + logger.warn("", ie); + } throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } try { parser.parse(stream, deviceDatasourceHandler); - } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + ooddrUaDeviceDatasourcePatchPathArray[i])); @@ -422,9 +427,19 @@ public class ODDRService implements Serv parser = SAXParserFactory.newInstance().newSAXParser(); } catch (ParserConfigurationException ex) { + try { + stream.close(); + } catch (IOException ie) { + logger.warn("", ie); + } throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } catch (SAXException ex) { + try { + stream.close(); + } catch (IOException ie) { + logger.warn("", ie); + } throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalStateException("Can not instantiate SAXParserFactory.newInstance().newSAXParser()")); } @@ -433,14 +448,11 @@ public class ODDRService implements Serv } catch (SAXException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not parse document: " + oddrUaDeviceBuilderPatchPathArray[i])); - } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new RuntimeException("Can not open " + ODDR_UA_DEVICE_DATASOURCE_PATH_PROP + " :" + oddrUaDeviceBuilderPatchPathArray[i])); - } - + } try { stream.close(); - } catch (IOException ex) { logger.warn("", ex); } @@ -495,7 +507,6 @@ public class ODDRService implements Serv } else { stream = new FileInputStream(new File(oddrUaOperatingSystemDatasourcePaths)); } - } catch (IOException ex) { throw new InitializationException(InitializationException.INITIALIZATION_ERROR, new IllegalArgumentException("Can not open " + ODDR_UA_OPERATINGSYSTEM_DATASOURCE_PATH_PROP + " " + oddrUaOperatingSystemDatasourcePaths)); } @@ -550,11 +561,6 @@ public class ODDRService implements Serv return; } - @Deprecated - public String getAPIVersion() { - return ODDR_API_VERSION; - } - public String getDataVersion() { return ODDR_DATA_VERSION; } @@ -850,7 +856,6 @@ public class ODDRService implements Serv JexlContext jc = new MapContext(); Object o = e.evaluate(jc); ret.addProperty(new ODDRPropertyValue(o.toString(), vocabularyProperty.getType(), propertyRef)); - } catch (Exception ex) { throw new RuntimeException(ex); } Modified: devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/AndroidDeviceBuilder.java URL: http://svn.apache.org/viewvc/devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/AndroidDeviceBuilder.java?rev=1648283&r1=1648282&r2=1648283&view=diff ============================================================================== --- devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/AndroidDeviceBuilder.java (original) +++ devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/AndroidDeviceBuilder.java Mon Dec 29 02:54:46 2014 @@ -33,6 +33,7 @@ public class AndroidDeviceBuilder extend private static final String BUILD_HASH_REGEXP = ".*Build/([^ \\)\\(]*).*"; private Pattern buildHashPattern = Pattern.compile(BUILD_HASH_REGEXP); private Map<String, Device> devices; + private static final String[] FUZZY_TOKEN_REGEXPS = { "SM-T5\\d\\d" }; public AndroidDeviceBuilder() { super(); @@ -42,7 +43,6 @@ public class AndroidDeviceBuilder extend public boolean canBuild(UserAgent userAgent) { if (userAgent.containsAndroid()) { return true; - } else { return false; } @@ -51,17 +51,16 @@ public class AndroidDeviceBuilder extend public Device build(UserAgent userAgent, int confidenceTreshold) { System.out.println("Building"); System.out.println("UA: " + userAgent.getCompleteUserAgent() + " :: A=" + userAgent.containsAndroid()); - ArrayList<Device> foundDevices = new ArrayList<Device>(); - Iterator it = orderedRules.keySet().iterator(); + final List<Device> foundDevices = new ArrayList<Device>(); + Iterator<String> it = orderedRules.keySet().iterator(); while (it.hasNext()) { - String token = (String) it.next(); + String token = it.next(); // System.out.println(token); Device d = elaborateAndroidDeviceWithToken(userAgent, token); if (d != null) { - System.out.println(token); + System.out.println("Token: " + token); if (d.getConfidence() > confidenceTreshold) { return d; - } else { if (d.getConfidence() > 0) { foundDevices.add(d); @@ -72,6 +71,16 @@ public class AndroidDeviceBuilder extend if (foundDevices.size() > 0) { Collections.sort(foundDevices, Collections.reverseOrder()); return foundDevices.get(0); + } else { + it = orderedRules.keySet().iterator(); // need it again for "Fuzzy" patterns + while (it.hasNext()) { + String token = it.next(); + Device d = fuzzyAndroidDeviceWithToken(userAgent, token); + if (d != null) { + System.out.println("Got: " + token); + return d; + } + } } return null; } @@ -80,13 +89,13 @@ public class AndroidDeviceBuilder extend orderedRules.put(initProperties.get(0), deviceID); } - private Device elaborateAndroidDeviceWithToken(UserAgent userAgent, String token) { + private Device elaborateAndroidDeviceWithToken(final UserAgent userAgent, String token) { if (userAgent.hasMozillaPattern() || userAgent.hasOperaPattern()) { int subtract = 0; String currentToken = token; String looseToken = token.replaceAll("[ _/-]", ".?"); -System.out.println("Loose Token: " + looseToken); +//System.out.println("Loose Token: " + looseToken); Pattern loosePattern = Pattern.compile("(?i).*" + looseToken + ".*"); //System.out.println("Loose Pattern: " + loosePattern); if (!loosePattern.matcher(userAgent.getCompleteUserAgent().replaceAll("Android", "")).matches()) { @@ -107,7 +116,7 @@ System.out.println("Loose Token: " + loo if (patternElementInsideClean != null && currentPattern.matcher(patternElementInsideClean).matches()) {//&& userAgent.getPatternElementsInside().matches(".*" + currentToken + ".?Build/.*")) { String deviceId = (String) orderedRules.get(token); //System.out.println("DID: " + deviceId); - if (devices.containsKey(deviceId)) { + if (devices.containsKey(deviceId)) { Device retDevice = (Device) devices.get(deviceId).clone(); retDevice.setConfidence(100 - subtract); return retDevice; @@ -118,7 +127,7 @@ System.out.println("Loose Token: " + loo if (userAgent.getPatternElementsPre() != null && currentPattern.matcher(userAgent.getPatternElementsPre()).matches()) {//userAgent.getPatternElementsPre().matches(".*" + currentToken)) { String deviceId = (String) orderedRules.get(token); - if (devices.containsKey(deviceId)) { + if (devices.containsKey(deviceId)) { Device retDevice = (Device) devices.get(deviceId).clone(); retDevice.setConfidence(95 - subtract); return retDevice; @@ -128,7 +137,7 @@ System.out.println("Loose Token: " + loo if (patternElementInsideClean != null && currentPattern.matcher(patternElementInsideClean).matches()) {//userAgent.getPatternElementsInside().matches(".*" + currentToken)) { String deviceId = (String) orderedRules.get(token); - if (devices.containsKey(deviceId)) { + if (devices.containsKey(deviceId)) { Device retDevice = (Device) devices.get(deviceId).clone(); retDevice.setConfidence(90 - subtract); return retDevice; @@ -139,7 +148,7 @@ System.out.println("Loose Token: " + loo if (patternElementInsideClean != null && currentPattern.matcher(patternElementInsideClean).matches()) {//userAgent.getPatternElementsInside().matches(".*" + currentToken + ".?;.*")) { String deviceId = (String) orderedRules.get(token); - if (devices.containsKey(deviceId)) { + if (devices.containsKey(deviceId)) { Device retDevice = (Device) devices.get(deviceId).clone(); retDevice.setConfidence(90 - subtract); return retDevice; @@ -148,14 +157,13 @@ System.out.println("Loose Token: " + loo if (i == 1) { currentPattern = loosePattern; - } else { currentPattern = Pattern.compile("(?i).*" + currentToken + ".*"); } if (patternElementInsideClean != null && currentPattern.matcher(patternElementInsideClean).matches()) {//userAgent.getPatternElementsInside().matches(".*" + currentToken + ".*")) { String deviceId = (String) orderedRules.get(token); - if (devices.containsKey(deviceId)) { + if (devices.containsKey(deviceId)) { Device retDevice = (Device) devices.get(deviceId).clone(); retDevice.setConfidence(80 - subtract); return retDevice; @@ -164,7 +172,7 @@ System.out.println("Loose Token: " + loo if (userAgent.getPatternElementsPre() != null && currentPattern.matcher(userAgent.getPatternElementsPre()).matches()) {//userAgent.getPatternElementsPre().matches(".*" + currentToken + ".*")) { String deviceId = (String) orderedRules.get(token); - if (devices.containsKey(deviceId)) { + if (devices.containsKey(deviceId)) { Device retDevice = (Device) devices.get(deviceId).clone(); retDevice.setConfidence(80 - subtract); return retDevice; @@ -173,7 +181,7 @@ System.out.println("Loose Token: " + loo if (userAgent.getPatternElementsPost() != null && currentPattern.matcher(userAgent.getPatternElementsPost()).matches()) {//userAgent.getPatternElementsPost().matches(".*" + currentToken + ".*")) { String deviceId = (String) orderedRules.get(token); - if (devices.containsKey(deviceId)) { + if (devices.containsKey(deviceId)) { Device retDevice = (Device) devices.get(deviceId).clone(); retDevice.setConfidence(60 - subtract); return retDevice; @@ -183,7 +191,7 @@ System.out.println("Loose Token: " + loo if (userAgent.getPatternElementsInside() != null && currentPattern.matcher(userAgent.getPatternElementsInside()).matches()) {//userAgent.getPatternElementsInside().matches(".*" + currentToken + ".*")) { String deviceId = (String) orderedRules.get(token); - if (devices.containsKey(deviceId)) { + if (devices.containsKey(deviceId)) { Device retDevice = (Device) devices.get(deviceId).clone(); retDevice.setConfidence(40); return retDevice; @@ -196,6 +204,24 @@ System.out.println("Loose Token: " + loo return null; } + + private Device fuzzyAndroidDeviceWithToken(final UserAgent userAgent, String token) { + if (userAgent.hasMozillaPattern() || userAgent.hasOperaPattern()) { + int subtract = 1; + for (int j = 0; j<FUZZY_TOKEN_REGEXPS.length; j++) { + if (Pattern.matches(FUZZY_TOKEN_REGEXPS[j], token)) { + String deviceId = (String) orderedRules.get(token); + if (devices.containsKey(deviceId)) { + Device retDevice = (Device) devices.get(deviceId).clone(); + retDevice.setConfidence(40 - subtract); + return retDevice; + } + } + return null; + } + } + return null; + } @Override protected void afterOderingCompleteInit(Map<String, Device> devices) { Modified: devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/OrderedTokenDeviceBuilder.java URL: http://svn.apache.org/viewvc/devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/OrderedTokenDeviceBuilder.java?rev=1648283&r1=1648282&r2=1648283&view=diff ============================================================================== --- devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/OrderedTokenDeviceBuilder.java (original) +++ devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/builder/device/OrderedTokenDeviceBuilder.java Mon Dec 29 02:54:46 2014 @@ -22,12 +22,14 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; + import org.apache.devicemap.simpleddr.model.device.Device; public abstract class OrderedTokenDeviceBuilder implements DeviceBuilder { - protected LinkedHashMap<String, Object> orderedRules; + protected Map<String, Object> orderedRules; public OrderedTokenDeviceBuilder() { orderedRules = new LinkedHashMap<String, Object>(); @@ -36,8 +38,8 @@ public abstract class OrderedTokenDevice abstract protected void afterOderingCompleteInit(Map<String, Device> devices); public final void completeInit(Map<String, Device> devices) { - LinkedHashMap<String, Object> tmp = new LinkedHashMap<String, Object>(); - ArrayList<String> keys = new ArrayList<String>(orderedRules.keySet()); + Map<String, Object> tmp = new LinkedHashMap<String, Object>(); + List<String> keys = new ArrayList<String>(orderedRules.keySet()); Collections.sort(keys, new Comparator<String>() { public int compare(String o1, String o2) { @@ -47,7 +49,7 @@ public abstract class OrderedTokenDevice for (String string : keys) { tmp.put(string, orderedRules.get(string)); } - ArrayList<String> keysOrdered = new ArrayList<String>(); + List<String> keysOrdered = new ArrayList<String>(); orderedRules = new LinkedHashMap(); Modified: devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/identificator/DeviceIdentificator.java URL: http://svn.apache.org/viewvc/devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/identificator/DeviceIdentificator.java?rev=1648283&r1=1648282&r2=1648283&view=diff ============================================================================== --- devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/identificator/DeviceIdentificator.java (original) +++ devicemap/trunk/devicemap/java/simpleddr/src/main/java/org/apache/devicemap/simpleddr/identificator/DeviceIdentificator.java Mon Dec 29 02:54:46 2014 @@ -33,8 +33,8 @@ import org.w3c.ddr.simple.Evidence; public class DeviceIdentificator implements Identificator { - private DeviceBuilder[] builders; - private Map<String, Device> devices; + private final DeviceBuilder[] builders; + private final Map<String, Device> devices; public DeviceIdentificator(DeviceBuilder[] builders, Map<String, Device> devices) { this.builders = builders; @@ -55,7 +55,7 @@ public class DeviceIdentificator impleme } public Device get(UserAgent userAgent, int confidenceTreshold) { - List<Device> foundDevices = new ArrayList<Device>(); + final List<Device> foundDevices = new ArrayList<Device>(); Device foundDevice = null; for (DeviceBuilder deviceBuilder : builders) { if (deviceBuilder.canBuild(userAgent)) {
