├── .gitignore
├── LICENSE
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── io
│ │ └── driocc
│ │ └── devicedetector
│ │ ├── Bot.java
│ │ ├── BotDetail.java
│ │ ├── DetectResult.java
│ │ ├── DeviceDetector.java
│ │ ├── OperatingSystem.java
│ │ ├── ParserAbstract.java
│ │ ├── VendorFragment.java
│ │ ├── client
│ │ ├── Browser.java
│ │ ├── ClientParserAbstract.java
│ │ ├── FeedReader.java
│ │ ├── Library.java
│ │ ├── MediaPlayer.java
│ │ ├── MobileApp.java
│ │ ├── PIM.java
│ │ └── engine
│ │ │ ├── Engine.java
│ │ │ └── Version.java
│ │ ├── custom
│ │ ├── CompositeDetectResult.java
│ │ ├── DetectConsultant.java
│ │ └── VersionConsultant.java
│ │ ├── device
│ │ ├── Camera.java
│ │ ├── CarBrowser.java
│ │ ├── Console.java
│ │ ├── DeviceParserAbstract.java
│ │ ├── HbbTv.java
│ │ ├── Mobile.java
│ │ └── PortableMediaPlayer.java
│ │ ├── utils
│ │ └── Utils.java
│ │ └── yaml
│ │ └── YamlParser.java
└── resources
│ └── regexes
│ ├── bots.yml
│ ├── client
│ ├── browser_engine.yml
│ ├── browsers.yml
│ ├── feed_readers.yml
│ ├── libraries.yml
│ ├── mediaplayers.yml
│ ├── mobile_apps.yml
│ └── pim.yml
│ ├── device
│ ├── cameras.yml
│ ├── car_browsers.yml
│ ├── consoles.yml
│ ├── mobiles.yml
│ ├── portable_media_player.yml
│ └── televisions.yml
│ ├── oss.yml
│ └── vendorfragments.yml
└── test
└── java
└── io
└── driocc
└── devicedetector
├── BrowserTest.java
├── DetectorTest.java
├── DeviceTest.java
├── EngineTest.java
├── MatchTest.java
├── OperatingSystemTest.java
├── VendorFragmentTest.java
└── YamlParserTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 | # Log file
4 | *.log
5 | # Package Files #
6 | *.jar
7 | *.war
8 | *.nar
9 | *.ear
10 | *.zip
11 | *.tar.gz
12 | *.rar
13 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
14 | hs_err_pid*
15 | #ide
16 | *.project
17 | *.classpath
18 | /target/
19 | /.settings/
20 | .idea
21 |
22 |
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2018, 八幡
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | * Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | * Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | * Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # java-device-detector
2 | java port of https://github.com/matomo-org/device-detector
3 |
4 | # Get Start
5 | 1. download this project
6 | 2. get the latest regexes from https://github.com/matomo-org/device-detector/tree/master/regexes
7 | 3. run it
8 | ```
9 | DeviceDetector d = new DeviceDetector();
10 | CompositeDetectResult ret = d.parse(ua);
11 | if(ret!=null) {
12 | System.out.println(ret);
13 | }
14 | ```
15 |
16 | # Customize
17 | 1. By default, DeviceDetector will detect bot, os, client and device
18 | 2. You can create your own DeviceDetector to customize the parsers and detect logic
19 |
20 | # Environment
21 | 1. java8
22 |
23 | # Performance
24 | work in progress
25 | 1. Cache regex objects
26 | 2. Pre-match, tests the useragent against a combination of all regexes
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | io.driocc
5 | java-device-detector
6 | 0.0.1
7 |
8 |
9 | UTF-8
10 | 1.9.0
11 |
12 |
13 |
14 |
15 |
16 | org.apache.maven.plugins
17 | maven-compiler-plugin
18 | 3.6.1
19 |
20 | 1.8
21 | 1.8
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 | org.yaml
30 | snakeyaml
31 | 1.21
32 |
33 |
34 | com.google.guava
35 | guava
36 | 23.6.1-jre
37 |
38 |
39 | org.slf4j
40 | slf4j-api
41 | 1.7.25
42 |
43 |
44 |
45 | junit
46 | junit
47 | 4.12
48 | test
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/src/main/java/io/driocc/devicedetector/Bot.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package io.driocc.devicedetector;
5 |
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import io.driocc.devicedetector.yaml.YamlParser;
10 |
11 | /**
12 | * @author kyon
13 | *
14 | */
15 | public class Bot extends ParserAbstract {
16 |
17 | /**
18 | *
19 | */
20 | private static final long serialVersionUID = 1L;
21 |
22 | protected static final String FIXTURE_FILE = "regexes/bots.yml";
23 | protected static final String PARSER = "bot";
24 | protected Boolean discardDetails = false;
25 |
26 | public Bot() {
27 | super(PARSER,FIXTURE_FILE);
28 | }
29 | public Bot(String type, String file, boolean discardDetails) {
30 | super(type, file);
31 | this.discardDetails = discardDetails;
32 | }
33 | /**
34 | * Enables information discarding
35 | */
36 | public void setDiscardDetails(boolean discardDetails){
37 | this.discardDetails = discardDetails;
38 | }
39 | /**
40 | * @return the discardDetails
41 | */
42 | public boolean getDiscardDetails() {
43 | return discardDetails;
44 | }
45 | /**
46 | * Parses the current UA and checks whether it contains bot information
47 | *
48 | * @see bots.yml for list of detected bots
49 | *
50 | * Step 1: Build a big regex containing all regexes and match UA against it
51 | * -> If no matches found: return
52 | * -> Otherwise:
53 | * Step 2: Walk through the list of regexes in bots.yml and try to match every one
54 | * -> Return the matched data
55 | *
56 | * If $discardDetails is set to TRUE, the Step 2 will be skipped
57 | * $bot will be set to TRUE instead
58 | *
59 | * NOTE: Doing the big match before matching every single regex speeds up the detection
60 | */
61 | public DetectResult parse(String userAgent) {
62 | DetectResult ret = null;
63 | if (this.preMatchOverall(userAgent)) {
64 | if (this.discardDetails) {
65 | ret = new DetectResult();
66 | ret.setBot(true);
67 | } else {
68 | List matches = null;
69 | List