Skip to content

Commit 6fd80d6

Browse files
committed
add user agent adhereing to RFC format
1 parent c1eb8a5 commit 6fd80d6

File tree

6 files changed

+48
-10
lines changed

6 files changed

+48
-10
lines changed

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,6 @@ public class ApiConstants {
13641364

13651365
public static final String RECURSIVE_DOMAINS = "recursivedomains";
13661366

1367-
public static final String CLOUDSTACK_USER_AGENT = "CloudStack-Agent";
1368-
13691367
/**
13701368
* This enum specifies IO Drivers, each option controls specific policies on I/O.
13711369
* Qemu guests support "threads" and "native" options Since 0.8.8 ; "io_uring" is supported Since 6.3.0 (QEMU 5.0).

core/src/main/java/com/cloud/storage/template/HttpTemplateDownloader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package com.cloud.storage.template;
2121

2222
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
23-
import static org.apache.cloudstack.api.ApiConstants.CLOUDSTACK_USER_AGENT;
2423

2524
import java.io.File;
2625
import java.io.IOException;
@@ -53,6 +52,7 @@
5352
import com.cloud.utils.Pair;
5453
import com.cloud.utils.UriUtils;
5554
import com.cloud.utils.exception.CloudRuntimeException;
55+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
5656
import com.cloud.utils.net.Proxy;
5757

5858
/**
@@ -126,7 +126,7 @@ private GetMethod createRequest(String downloadUrl) {
126126
GetMethod request = new GetMethod(downloadUrl);
127127
request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
128128
request.setFollowRedirects(followRedirects);
129-
request.getParams().setParameter(HttpMethodParams.USER_AGENT, CLOUDSTACK_USER_AGENT);
129+
request.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
130130
return request;
131131
}
132132

core/src/main/java/com/cloud/storage/template/MetalinkTemplateDownloader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
//
1919
package com.cloud.storage.template;
2020

21-
import static org.apache.cloudstack.api.ApiConstants.CLOUDSTACK_USER_AGENT;
2221

2322
import com.cloud.storage.StorageLayer;
2423
import com.cloud.utils.UriUtils;
24+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
25+
2526
import org.apache.commons.httpclient.HttpClient;
2627
import org.apache.commons.httpclient.HttpMethod;
2728
import org.apache.commons.httpclient.HttpMethodRetryHandler;
@@ -61,7 +62,7 @@ protected GetMethod createRequest(String downloadUrl) {
6162
GetMethod request = new GetMethod(downloadUrl);
6263
request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
6364
request.setFollowRedirects(followRedirects);
64-
request.getParams().setParameter(HttpMethodParams.USER_AGENT, CLOUDSTACK_USER_AGENT);
65+
request.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
6566
if (!toFileSet) {
6667
String[] parts = downloadUrl.split("/");
6768
String filename = parts[parts.length - 1];

core/src/main/java/com/cloud/storage/template/SimpleHttpMultiFileDownloader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package com.cloud.storage.template;
1919

2020
import static com.cloud.utils.NumbersUtil.toHumanReadableSize;
21-
import static org.apache.cloudstack.api.ApiConstants.CLOUDSTACK_USER_AGENT;
2221

2322
import java.io.File;
2423
import java.io.IOException;
@@ -45,6 +44,7 @@
4544
import org.apache.commons.lang3.StringUtils;
4645

4746
import com.cloud.storage.StorageLayer;
47+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
4848

4949
public class SimpleHttpMultiFileDownloader extends ManagedContextRunnable implements TemplateDownloader {
5050
private static final MultiThreadedHttpConnectionManager s_httpClientManager = new MultiThreadedHttpConnectionManager();
@@ -96,7 +96,7 @@ private GetMethod createRequest(String downloadUrl) {
9696
GetMethod request = new GetMethod(downloadUrl);
9797
request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
9898
request.setFollowRedirects(followRedirects);
99-
request.getParams().setParameter(HttpMethodParams.USER_AGENT, CLOUDSTACK_USER_AGENT);
99+
request.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
100100
return request;
101101
}
102102

core/src/main/java/org/apache/cloudstack/direct/download/HttpDirectTemplateDownloader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.apache.cloudstack.direct.download;
2121

22-
import static org.apache.cloudstack.api.ApiConstants.CLOUDSTACK_USER_AGENT;
2322

2423
import java.io.File;
2524
import java.io.FileOutputStream;
@@ -34,6 +33,7 @@
3433
import com.cloud.utils.Pair;
3534
import com.cloud.utils.UriUtils;
3635
import com.cloud.utils.exception.CloudRuntimeException;
36+
import com.cloud.utils.net.HttpClientCloudStackUserAgent;
3737
import com.cloud.utils.storage.QCOW2Utils;
3838
import org.apache.commons.collections.MapUtils;
3939
import org.apache.commons.httpclient.HttpClient;
@@ -71,7 +71,7 @@ public HttpDirectTemplateDownloader(String url, Long templateId, String destPool
7171
protected GetMethod createRequest(String downloadUrl, Map<String, String> headers) {
7272
GetMethod request = new GetMethod(downloadUrl);
7373
request.setFollowRedirects(this.isFollowRedirects());
74-
request.getParams().setParameter(HttpMethodParams.USER_AGENT, CLOUDSTACK_USER_AGENT);
74+
request.getParams().setParameter(HttpMethodParams.USER_AGENT, HttpClientCloudStackUserAgent.CLOUDSTACK_USER_AGENT);
7575
if (MapUtils.isNotEmpty(headers)) {
7676
for (String key : headers.keySet()) {
7777
request.setRequestHeader(key, headers.get(key));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.utils.net;
21+
22+
import org.apache.logging.log4j.util.Strings;
23+
24+
public class HttpClientCloudStackUserAgent {
25+
public static final String CLOUDSTACK_USER_AGENT = buildUserAgent();
26+
27+
private static String buildUserAgent() {
28+
String version = HttpClientCloudStackUserAgent.class
29+
.getPackage()
30+
.getImplementationVersion();
31+
32+
if (Strings.isBlank(version)) {
33+
version = "unknown";
34+
}
35+
return "CloudStack-Agent/" + version + " (Apache CloudStack)";
36+
}
37+
38+
private HttpClientCloudStackUserAgent() {}
39+
}

0 commit comments

Comments
 (0)