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");
}
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);
}
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 "[{}]";
}
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();
}
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;
}
Aggregations