Skip to content

Commit

Permalink
Add custom User-Agent for MapFishPrint.
Browse files Browse the repository at this point in the history
This fixes
#1955
  • Loading branch information
Björn Höfling committed Aug 19, 2021
1 parent 2f70aeb commit ebaf680
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.apache.tools.ant.taskdefs.condition.Os
import org.apache.tools.ant.filters.*

apply plugin: 'war'
apply plugin: 'distribution'
Expand Down Expand Up @@ -217,6 +218,13 @@ def gitRevision() {
compileJava.options.compilerArgs = ['-Xlint:deprecation', '-Xlint:unchecked']
compileTestJava.options.compilerArgs = ['-Xlint:deprecation', '-Xlint:unchecked']

// Add version to resources, used for User-Agent string:
processResources {
filter ReplaceTokens, tokens: [
"application.version": version
]
}

def addManifestAttribute(manifest) {
manifest.mainAttributes(
'Mapfish-Print-Version': version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ private static CloseableHttpClient createHttpClient(final int maxConnTotal, fina
setDefaultCredentialsProvider(new MfCredentialsProvider()).
setDefaultRequestConfig(requestConfig).
setMaxConnTotal(maxConnTotal).
setMaxConnPerRoute(maxConnPerRoute);
setMaxConnPerRoute(maxConnPerRoute).
setUserAgent(UserAgentCreator.getUserAgent());
return httpClientBuilder.build();
}

Expand Down
39 changes: 39 additions & 0 deletions core/src/main/java/org/mapfish/print/http/UserAgentCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.mapfish.print.http;

import org.apache.http.util.VersionInfo;

/**
* Utility class for the creation of the User-Agent string.
*
* @author bhoefling
*
*/
public final class UserAgentCreator {

private static final String AGENT_NAME = "MapFishPrint";

/**
* Private constructor.
*/
private UserAgentCreator() {

}
/**
* Builds a User-Agent string.
*
* @return User-Agent
*/
public static String getUserAgent() {

final String httpClientUserAgent = VersionInfo.getUserAgent("Apache-HttpClient",
"org.apache.http.client", UserAgentCreator.class);

// This is based on the code from HttpClient:
final VersionInfo mapFishPrintVersionInfo = VersionInfo.loadVersionInfo("org.mapfish.print",
UserAgentCreator.class.getClassLoader());
final String mfpRelease = mapFishPrintVersionInfo.getRelease();

final String userAgent = String.format("%s/%s %s", AGENT_NAME, mfpRelease, httpClientUserAgent);
return userAgent;
}
}
3 changes: 3 additions & 0 deletions core/src/main/resources/org/mapfish/print/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
info.release=@application.version@
info.module=MapFishPrint
#info.timestamp=...

0 comments on commit ebaf680

Please sign in to comment.