Skip to content

Commit

Permalink
Java example using SLF4J (so no Log4j2)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Dec 23, 2021
1 parent d7324c0 commit 5dba99a
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ v6.6-SNAPSHOT
- Updated log4j dependency to 2.17.0
- Updated UDF dependencies
- Apache Nifi 1.15.2
- Examples
- Added a Java example using SLF4J (so no Log4j2).

v6.5
===
Expand Down
68 changes: 68 additions & 0 deletions examples/java-slf4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Yet Another UserAgent Analyzer
~ Copyright (C) 2013-2021 Niels Basjes
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>nl.basjes.parse.useragent</groupId>
<artifactId>yauaa-example-parent</artifactId>
<version>6.6-SNAPSHOT</version>
</parent>

<packaging>jar</packaging>

<artifactId>yauaa-example-java-slf4j</artifactId>
<name>Yauaa : Examples : Java (SLF4J)</name>

<dependencies>
<dependency>
<groupId>nl.basjes.parse.useragent</groupId>
<artifactId>yauaa</artifactId>
<version>${yauaa.version}</version>
</dependency>

<!-- The default logging implementation for Yauaa -->
<!-- Send all Log4j2 call to SLF4J -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>${log4j2.version}</version>
</dependency>

<!-- The included classes from org.apache.httpcomponents.client5:httpclient5 already expect SLF4J -->

<!-- Needed for the included classes from org.springframework:spring-core -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>

<!-- Just the simplest of logger for this demo. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Yet Another UserAgent Analyzer
* Copyright (C) 2013-2021 Niels Basjes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nl.basjes.parse.useragent.example;

import nl.basjes.parse.useragent.UserAgent;
import nl.basjes.parse.useragent.UserAgentAnalyzer;

public class Demo {

private final UserAgentAnalyzer uaa;

public Demo() {
uaa = UserAgentAnalyzer
.newBuilder()
.withCache(1234)
.withField("DeviceClass")
.withAllFields()
.build();
}

public UserAgent parse(String userAgent) {
return uaa.parse(userAgent);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Yet Another UserAgent Analyzer
* Copyright (C) 2013-2021 Niels Basjes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nl.basjes.parse.useragent.example;

import nl.basjes.parse.useragent.UserAgent;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

class TestDemo {
@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");
}
}
1 change: 1 addition & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

<modules>
<module>java</module>
<module>java-slf4j</module>
<module>kotlin</module>
<module>scala</module>
<!-- <module>quarkus</module>-->
Expand Down

0 comments on commit 5dba99a

Please sign in to comment.