This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Fahrschein User-Agent to Nakadi requests (#350)
* Add User-Agent to Nakadi requests We would like to have an easy way to get some statistics around Fahrschein usage in general, and which versions, which JDK version, and via which HTTP client implementation specificially, in order to a) justify the time we're spending on the library, b) get more insights about builder behaviour. The header should be set by default (not overridable initially). Format "Fahrschein/" [FahrscheinVersion] " (" [HttpClient] "; " [Java Version] ("; " [Additional Property])* ")" Examples User-Agent: Fahrschein/0.22.0 (Simple; Java11) User-Agent: Fahrschein/0.22.0 (ApacheHttpClient; Java8) User-Agent: Fahrschein/0.22.0 (JavaNet; Java17; experimental-features=a,b,c) User-Agent: Fahrschein/0.22.0 (Spring; Java8; Kotlin) * Fix github workflow references in README * Update fahrschein-http-api/src/main/java/org/zalando/fahrschein/http/api/UserAgent.java Co-authored-by: Malte <[email protected]> * Update fahrschein-http-api/src/main/java/org/zalando/fahrschein/http/api/UserAgent.java Co-authored-by: Malte <[email protected]> * Update fahrschein-http-api/src/main/java/org/zalando/fahrschein/http/api/UserAgent.java Co-authored-by: Malte <[email protected]> * Update fahrschein-http-api/src/main/java/org/zalando/fahrschein/http/api/UserAgent.java Co-authored-by: Malte <[email protected]> * Reduce logging from error to warn * mark project.version as task input Co-authored-by: Malte <[email protected]>
- Loading branch information
Showing
12 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
fahrschein-http-api/src/main/java/org/zalando/fahrschein/http/api/UserAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.zalando.fahrschein.http.api; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Properties; | ||
|
||
public final class UserAgent { | ||
private static final String PROPERTIES_FILE = "fahrschein.properties"; | ||
private static final Logger logger = LoggerFactory.getLogger(UserAgent.class); | ||
|
||
private static final Properties fahrscheinProperties = new Properties(); | ||
|
||
static { | ||
try (final InputStream stream = | ||
UserAgent.class.getResourceAsStream("/" + PROPERTIES_FILE)) { | ||
if (stream != null) { | ||
fahrscheinProperties.load(stream); | ||
} else { | ||
logger.warn("Properties file not found: {}", PROPERTIES_FILE); | ||
} | ||
} catch (IOException e) { | ||
logger.warn("Cannot read file: " + PROPERTIES_FILE, e); | ||
} | ||
} | ||
|
||
private static final String AGENT_STR_TEMPLATE = "Fahrschein/%s (%s; Java%d)"; | ||
|
||
private final String userAgent; | ||
|
||
public UserAgent(Class implementation) { | ||
this.userAgent = String.format(AGENT_STR_TEMPLATE, fahrscheinVersion(), implementation.getSimpleName().replace("RequestFactory", ""), javaVersion()); | ||
} | ||
|
||
public String userAgent() { | ||
return userAgent; | ||
} | ||
|
||
static String fahrscheinVersion() { | ||
return String.valueOf(fahrscheinProperties.get("fahrschein-version")); | ||
} | ||
|
||
static int javaVersion() { | ||
String version = System.getProperty("java.version"); | ||
if (version.startsWith("1.")) { | ||
version = version.substring(2, 3); | ||
} else { | ||
int dot = version.indexOf("."); | ||
if (dot != -1) { | ||
version = version.substring(0, dot); | ||
} | ||
} | ||
return Integer.parseInt(version); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
fahrschein-http-api/src/test/java/org/zalando/fahrschein/http/api/UserAgentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.zalando.fahrschein.http.api; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class UserAgentTest { | ||
|
||
abstract class DummyRequestFactory implements RequestFactory {} | ||
|
||
@Test | ||
public void javaVersion() { | ||
String version = System.getProperty("java.version"); | ||
int javaVersion = UserAgent.javaVersion(); | ||
if (version.startsWith("1.8")) { | ||
assertEquals(8, javaVersion, "Java 8"); | ||
} else { | ||
assertTrue(version.startsWith(String.valueOf(javaVersion))); | ||
assertTrue(javaVersion > 8, "Java " + javaVersion); | ||
} | ||
} | ||
|
||
@Test | ||
public void userAgentString() { | ||
UserAgent ua = new UserAgent(DummyRequestFactory.class); | ||
String userAgent = ua.userAgent(); | ||
assertEquals("Fahrschein/0.1.0-SNAPSHOT (Dummy; Java" + UserAgent.javaVersion() + ")", userAgent); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# for testing | ||
fahrschein-version=0.1.0-SNAPSHOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
fahrschein/src/main/java/org/zalando/fahrschein/UserAgentRequestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.zalando.fahrschein; | ||
|
||
import org.zalando.fahrschein.http.api.Headers; | ||
import org.zalando.fahrschein.http.api.Request; | ||
import org.zalando.fahrschein.http.api.RequestFactory; | ||
import org.zalando.fahrschein.http.api.UserAgent; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
public class UserAgentRequestFactory implements RequestFactory { | ||
private final RequestFactory delegate; | ||
private final String userAgent; | ||
|
||
UserAgentRequestFactory(final RequestFactory delegate) { | ||
this.userAgent = new UserAgent(delegate.getClass()).userAgent(); | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public Request createRequest(URI uri, String method) throws IOException { | ||
final Request request = delegate.createRequest(uri, method); | ||
request.getHeaders().put(Headers.USER_AGENT, userAgent); | ||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
fahrschein/src/test/java/org/zalando/fahrschein/UserAgentRequestFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.zalando.fahrschein; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.zalando.fahrschein.http.api.Headers; | ||
import org.zalando.fahrschein.http.api.HeadersImpl; | ||
import org.zalando.fahrschein.http.api.Request; | ||
import org.zalando.fahrschein.http.api.RequestFactory; | ||
import org.zalando.fahrschein.http.api.Response; | ||
import org.zalando.fahrschein.http.api.UserAgent; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.URI; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class UserAgentRequestFactoryTest { | ||
static class DummyRequestFactory implements RequestFactory { | ||
|
||
@Override | ||
public Request createRequest(URI uri, String method) throws IOException { | ||
return new Request() { | ||
private final Headers headers = new HeadersImpl(); | ||
@Override | ||
public String getMethod() { | ||
return method; | ||
} | ||
|
||
@Override | ||
public URI getURI() { | ||
return uri; | ||
} | ||
|
||
@Override | ||
public Headers getHeaders() { | ||
return headers; | ||
} | ||
|
||
@Override | ||
public OutputStream getBody() throws IOException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Response execute() throws IOException { | ||
return null; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
@Test | ||
public void appendUserAgentToRequest() throws IOException { | ||
UserAgent ua = new UserAgent(DummyRequestFactory.class); | ||
UserAgentRequestFactory rf = new UserAgentRequestFactory(new DummyRequestFactory()); | ||
Request r = rf.createRequest(URI.create("dummy://req"), "POST"); | ||
assertEquals(ua.userAgent(), r.getHeaders().getFirst("User-Agent")); | ||
} | ||
} |