Search in sources :

Example 26 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class TestDemo method testParser.

@Test
void testParser() {
    Demo demo = new Demo();
    String userAgent = "Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.124 Mobile Safari/537.36";
    UserAgent result = demo.parse(userAgent);
    assertTrue(result.toXML().contains("<DeviceName>Google Nexus 6</DeviceName>"), "The parser must extract the correct DeviceName");
}
Also used : UserAgent(nl.basjes.parse.useragent.UserAgent) Test(org.junit.jupiter.api.Test)

Example 27 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class ParseUserAgentFunction method parseUserAgent.

@ScalarFunction("parse_user_agent")
@Description("Tries to parse and analyze the provided useragent string and extract as many attributes " + "as possible. Uses Yauaa (Yet Another UserAgent Analyzer) version " + Version.PROJECT_VERSION + ". " + "See https://yauaa.basjes.nl/udf/trino/ for documentation.")
@SqlType("map(varchar, varchar)")
public static Block parseUserAgent(@SqlType(StandardTypes.VARCHAR) Slice userAgentSlice) throws IllegalArgumentException {
    String userAgentStringToParse = null;
    if (userAgentSlice != null) {
        userAgentStringToParse = userAgentSlice.toStringUtf8();
    }
    UserAgentAnalyzer userAgentAnalyzer = getInstance();
    UserAgent userAgent = userAgentAnalyzer.parse(userAgentStringToParse);
    Map<String, String> resultMap = userAgent.toMap(userAgent.getAvailableFieldNamesSorted());
    MapType mapType = new MapType(VARCHAR, VARCHAR, new TypeOperators());
    BlockBuilder blockBuilder = mapType.createBlockBuilder(null, resultMap.size());
    BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
    for (Map.Entry<String, String> entry : resultMap.entrySet()) {
        VARCHAR.writeString(singleMapBlockBuilder, entry.getKey());
        VARCHAR.writeString(singleMapBlockBuilder, entry.getValue());
    }
    blockBuilder.closeEntry();
    return mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) UserAgent(nl.basjes.parse.useragent.UserAgent) Map(java.util.Map) MapType(io.trino.spi.type.MapType) TypeOperators(io.trino.spi.type.TypeOperators) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 28 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class ApiJsonOutput method createOutput.

// -------------------------------------------------
private String createOutput(String userAgentString) {
    if (userAgentString == null) {
        throw new MissingUserAgentException();
    }
    ensureStartedForApis(OutputType.JSON);
    if (userAgentAnalyzerIsAvailable()) {
        UserAgentAnalyzer userAgentAnalyzer = ParseService.getUserAgentAnalyzer();
        List<String> result = new ArrayList<>(2048);
        for (String input : splitPerFilledLine(userAgentString)) {
            UserAgent userAgent = userAgentAnalyzer.parse(input);
            result.add(userAgent.toJson(userAgent.getCleanedAvailableFieldNamesSorted()));
        }
        return "[" + String.join(",\n", result) + "]";
    }
    return "[{}]";
}
Also used : UserAgentAnalyzer(nl.basjes.parse.useragent.UserAgentAnalyzer) ArrayList(java.util.ArrayList) UserAgent(nl.basjes.parse.useragent.UserAgent) MissingUserAgentException(nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)

Example 29 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project yauaa by nielsbasjes.

the class ParseUserAgent method evaluate.

@Override
public Object evaluate(DeferredObject[] args) throws HiveException {
    String userAgentString = useragentOI.getPrimitiveJavaObject(args[0].get());
    if (userAgentString == null) {
        return null;
    }
    UserAgent userAgent = userAgentAnalyzer.parse(userAgentString);
    List<Object> result = new ArrayList<>(fieldNames.size());
    for (String fieldName : fieldNames) {
        String value = userAgent.getValue(fieldName);
        if (value == null) {
            result.add(null);
        } else {
            result.add(new Text(value));
        }
    }
    return result.toArray();
}
Also used : UserAgent(nl.basjes.parse.useragent.UserAgent) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text)

Example 30 with UserAgent

use of nl.basjes.parse.useragent.UserAgent in project teamapps by teamapps-org.

the class UserAgentParser method parseUserAgent.

public ClientUserAgent parseUserAgent(String userAgent) {
    try {
        if (agentAnalyzer == null) {
            return null;
        }
        UserAgent agent = agentAnalyzer.parse(userAgent);
        ClientUserAgent agentInfo = new ClientUserAgent();
        agentInfo.setDeviceClass(agent.getValue("DeviceClass"));
        agentInfo.setDeviceName(agent.getValue("DeviceName"));
        agentInfo.setDeviceBrand(agent.getValue("DeviceBrand"));
        agentInfo.setDeviceCpu(agent.getValue("DeviceCpu"));
        agentInfo.setOperatingSystemClass(agent.getValue("OperatingSystemClass"));
        agentInfo.setOperatingSystemName(agent.getValue("OperatingSystemName"));
        agentInfo.setOperatingSystemNameVersion(agent.getValue("OperatingSystemNameVersion"));
        agentInfo.setLayoutEngineNameVersion(agent.getValue("LayoutEngineNameVersion"));
        agentInfo.setAgentClass(agent.getValue("AgentClass"));
        agentInfo.setAgentName(agent.getValue("AgentName"));
        agentInfo.setAgentVersion(agent.getValue("AgentVersion"));
        agentInfo.setAgentNameVersion(agent.getValue("AgentNameVersion"));
        return agentInfo;
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ClientUserAgent(org.teamapps.ux.session.ClientUserAgent) UserAgent(nl.basjes.parse.useragent.UserAgent) ClientUserAgent(org.teamapps.ux.session.ClientUserAgent)

Aggregations

UserAgent (nl.basjes.parse.useragent.UserAgent)30 UserAgentAnalyzer (nl.basjes.parse.useragent.UserAgentAnalyzer)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 MissingUserAgentException (nl.basjes.parse.useragent.servlet.exceptions.MissingUserAgentException)4 MutableUserAgent (nl.basjes.parse.useragent.UserAgent.MutableUserAgent)3 AgentField (nl.basjes.parse.useragent.AgentField)2 Matcher (nl.basjes.parse.useragent.analyze.Matcher)2 Event (co.elastic.logstash.api.Event)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 Description (io.trino.spi.function.Description)1 ScalarFunction (io.trino.spi.function.ScalarFunction)1 SqlType (io.trino.spi.function.SqlType)1 MapType (io.trino.spi.type.MapType)1 TypeOperators (io.trino.spi.type.TypeOperators)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 ParsedField (nl.basjes.parse.core.ParsedField)1