Skip to content

Commit

Permalink
Add a custom user agent (#314)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Jacomb <[email protected]>
Co-authored-by: Oleg Nenashev <[email protected]>
Co-authored-by: hp77 <[email protected]>
  • Loading branch information
4 people authored Apr 26, 2021
1 parent 467e501 commit 9165fe1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ build

# VSCode workspace file
*.code-workspace
.vscode

# vim
*~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
Expand Down Expand Up @@ -101,6 +102,7 @@ public class PluginManager implements Closeable {
private boolean verbose;
private boolean useLatestSpecified;
private boolean useLatestAll;
private String userAgentInformation;
private boolean skipFailedPlugins;
private CloseableHttpClient httpClient;

Expand All @@ -127,6 +129,20 @@ public PluginManager(Config cfg) {
skipFailedPlugins = cfg.isSkipFailedPlugins();
hashFunction = cfg.getHashFunction();
httpClient = null;
userAgentInformation = this.getUserAgentInformation();
}

private String getUserAgentInformation() {
String userAgentInformation= "Jenkins plugin manager/Dev";
Properties properties = new Properties();
try (InputStream propertiesStream = this.getClass().getClassLoader().getResourceAsStream("version.properties")) {
properties.load(propertiesStream);
userAgentInformation = "Jenkins plugin manager" + "/" + properties.getProperty("project.version");
}
catch (IOException e) {
logVerbose("Not able to load/detect version.properties file");
}
return userAgentInformation;
}

private HttpClient getHttpClient() {
Expand All @@ -136,6 +152,7 @@ private HttpClient getHttpClient() {
// this affects only the single request
.setRetryHandler(new DefaultHttpRequestRetryHandler(DEFAULT_MAX_RETRIES, true))
.setConnectionManager(new PoolingHttpClientConnectionManager())
.setUserAgent(userAgentInformation)
.build();
}
return httpClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
project.version = ${project.version}
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>

</project>

0 comments on commit 9165fe1

Please sign in to comment.