Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support of Windows OS for desktops #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@
import java.util.Collections;
import java.util.List;
import org.openddr.simpleapi.oddr.builder.Builder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.AndroidMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.BadaMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.BlackBerryMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.BrewMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.IOSMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.LinuxMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.MacOSXMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.SymbianMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.WebOSMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.WinCEMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.WinPhoneMozillaSubBuilder;
import org.openddr.simpleapi.oddr.builder.os.mozilla.*;
import org.openddr.simpleapi.oddr.model.UserAgent;
import org.openddr.simpleapi.oddr.model.os.OperatingSystem;

Expand All @@ -51,7 +41,8 @@ public class MozillaOSModelBuilder implements Builder {
new BrewMozillaSubBuilder(),
new WebOSMozillaSubBuilder(),
new LinuxMozillaSubBuilder(),
new MacOSXMozillaSubBuilder()
new MacOSXMozillaSubBuilder(),
new WinNTMozillaSubBuilder()
};

public boolean canBuild(UserAgent userAgent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2011 OpenDDR LLC
* This software is distributed under the terms of the GNU Lesser General Public License.
*
*
* This file is part of OpenDDR Simple APIs.
* OpenDDR Simple APIs is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* OpenDDR Simple APIs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Simple APIs. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.openddr.simpleapi.oddr.builder.os.mozilla;

import org.openddr.simpleapi.oddr.builder.Builder;
import org.openddr.simpleapi.oddr.model.UserAgent;
import org.openddr.simpleapi.oddr.model.os.OperatingSystem;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WinNTMozillaSubBuilder implements Builder {

private static final String VERSION_REGEXP = ".*Windows NT ((\\d+)\\.(\\d+)).*";
private static final String TRIDENT_REGEXP = ".*Trident/[0-9.].*";
private static final String NET_CLR_REGEXP = ".*\\.NET CLR[ /][0-9.]+.*";
private Pattern versionPattern = Pattern.compile(VERSION_REGEXP);
private Pattern tridentPattern = Pattern.compile(TRIDENT_REGEXP);
private Pattern clrPattern = Pattern.compile(NET_CLR_REGEXP);

public boolean canBuild(UserAgent userAgent) {
return userAgent.containsWindowsPhone();
}

public OperatingSystem build(UserAgent userAgent, int confidenceTreshold) {
final OperatingSystem os = new OperatingSystem();
os.setVendor("Microsoft");
os.setModel("Windows");
os.setConfidence(40);

final String[] splittedTokens = userAgent.getPatternElementsInside().split(";");
for (String tokenElement : splittedTokens) {
final Matcher versionMatcher = versionPattern.matcher(tokenElement);
if (versionMatcher.find()) {
os.setConfidence(85);
if (versionMatcher.group(1) != null) {
os.setVersion(versionMatcher.group(1));
}
if (versionMatcher.group(2) != null) {
os.setMajorRevision(versionMatcher.group(2));
}
if (versionMatcher.group(3) != null) {
os.setMinorRevision(versionMatcher.group(3));
}
} else if (tridentPattern.matcher(tokenElement).find() || clrPattern.matcher(tokenElement).find()) {
if (os.getConfidence() > 80) {
os.setConfidence(100);
} else {
os.setConfidence(85);
}
}
}

if (os.getVersion() != null) {
os.setDescription("Windows " + os.getVersion());
} else {
os.setDescription("Windows");
}

return os;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
package org.openddr.simpleapi.oddr.builder.os.mozilla;

import org.junit.Test;
import org.openddr.simpleapi.oddr.model.UserAgent;
import org.openddr.simpleapi.oddr.model.UserAgentFactory;
import org.openddr.simpleapi.oddr.model.os.OperatingSystem;
import org.w3c.ddr.simple.exception.InitializationException;
import org.w3c.ddr.simple.exception.NameException;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class WinNTMozillaSubBuilderTest {

private final WinNTMozillaSubBuilder winNTMozillaSubBuilder = new WinNTMozillaSubBuilder();

@Test
public void testCanBuild() throws InitializationException, IOException, NameException {

final UserAgent wrongUserAgent = UserAgentFactory.newUserAgent("wrong user agent");
assertEquals(false, winNTMozillaSubBuilder.canBuild(wrongUserAgent));

final UserAgent realUserAgent = UserAgentFactory.newUserAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");
assertEquals(true, winNTMozillaSubBuilder.canBuild(realUserAgent));
}

@Test
public void testBuild() {

UserAgent userAgent = UserAgentFactory.newUserAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");
OperatingSystem os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(85, os.getConfidence());
assertEquals("Windows 6.1", os.getDescription());
assertEquals("6.1", os.getVersion());
assertEquals("6", os.getMajorRevision());
assertEquals("1", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent("Mozilla/5.0 (Windows NT)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(40, os.getConfidence());
assertEquals("Windows", os.getDescription());


userAgent = UserAgentFactory.newUserAgent("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(100, os.getConfidence());
assertEquals("Windows 6.1", os.getDescription());
assertEquals("6.1", os.getVersion());
assertEquals("6", os.getMajorRevision());
assertEquals("1", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent("Mozilla/5.0 (compatible; MSIE 10.0; WOW64; Trident/6.0)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(85, os.getConfidence());
assertEquals("Windows", os.getDescription());
assertNull(os.getVersion());
assertEquals("0", os.getMajorRevision());
assertEquals("0", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent
("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT; WOW64; GTB7.5; SLCC1; .NET CLR 2.0.50727)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(85, os.getConfidence());
assertEquals("Windows", os.getDescription());
assertNull(os.getVersion());
assertEquals("0", os.getMajorRevision());
assertEquals("0", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent(
"Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(100, os.getConfidence());
assertEquals("Windows 6.3", os.getDescription());
assertEquals("6.3", os.getVersion());
assertEquals("6", os.getMajorRevision());
assertEquals("3", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(100, os.getConfidence());
assertEquals("Windows 6.2", os.getDescription());
assertEquals("6.2", os.getVersion());
assertEquals("6", os.getMajorRevision());
assertEquals("2", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent(
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/6.0; SLCC2; .NET CLR 3.0.30729; OfficeLiveConnector.1.5; OfficeLivePatch.1.3; BRI/2; .NET4.0E; yie9)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(100, os.getConfidence());
assertEquals("Windows 6.1", os.getDescription());
assertEquals("6.1", os.getVersion());
assertEquals("6", os.getMajorRevision());
assertEquals("1", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent(
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; chromeframe/11.0.696.57)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(100, os.getConfidence());
assertEquals("Windows 6.0", os.getDescription());
assertEquals("6.0", os.getVersion());
assertEquals("6", os.getMajorRevision());
assertEquals("0", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent(
"Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(100, os.getConfidence());
assertEquals("Windows 5.2", os.getDescription());
assertEquals("5.2", os.getVersion());
assertEquals("5", os.getMajorRevision());
assertEquals("2", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent("Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(100, os.getConfidence());
assertEquals("Windows 5.1", os.getDescription());
assertEquals("5.1", os.getVersion());
assertEquals("5", os.getMajorRevision());
assertEquals("1", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent("Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.01; YComp 5.0.2.6; Hotbar 3.0)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(85, os.getConfidence());
assertEquals("Windows 5.01", os.getDescription());
assertEquals("5.01", os.getVersion());
assertEquals("5", os.getMajorRevision());
assertEquals("01", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent("Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.0; YComp 5.0.0.0)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(85, os.getConfidence());
assertEquals("Windows 5.0", os.getDescription());
assertEquals("5.0", os.getVersion());
assertEquals("5", os.getMajorRevision());
assertEquals("0", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());


userAgent = UserAgentFactory.newUserAgent("Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 4.0)");
os = winNTMozillaSubBuilder.build(userAgent, 0);

assertEquals("Microsoft", os.getVendor());
assertEquals("Windows", os.getModel());
assertEquals(85, os.getConfidence());
assertEquals("Windows 4.0", os.getDescription());
assertEquals("4.0", os.getVersion());
assertEquals("4", os.getMajorRevision());
assertEquals("0", os.getMinorRevision());
assertEquals("0", os.getMicroRevision());
assertEquals("0", os.getNanoRevision());
}
}