diff --git a/pom.xml b/pom.xml index ad8e604..5bafc5e 100644 --- a/pom.xml +++ b/pom.xml @@ -123,6 +123,15 @@ + + + src/main/resources + true + + version.properties + + + org.apache.maven.plugins diff --git a/src/main/java/com/aliyun/credentials/http/CompatibleUrlConnClient.java b/src/main/java/com/aliyun/credentials/http/CompatibleUrlConnClient.java index 2f1d015..af01fc6 100644 --- a/src/main/java/com/aliyun/credentials/http/CompatibleUrlConnClient.java +++ b/src/main/java/com/aliyun/credentials/http/CompatibleUrlConnClient.java @@ -9,11 +9,28 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; public class CompatibleUrlConnClient implements Closeable { protected static final String ACCEPT_ENCODING = "Accept-Encoding"; protected static final String CONTENT_TYPE = "Content-Type"; + protected static final String USER_AGENT = "User-Agent"; + private static final String DEFAULT_USER_AGENT; + + static { + Properties sysProps = System.getProperties(); + String version = ""; + Properties props = new Properties(); + try { + props.load(CompatibleUrlConnClient.class.getClassLoader().getResourceAsStream("version.properties")); + version = props.getProperty("sdk.credentials.version"); + } catch (IOException e) { + e.printStackTrace(); + } + DEFAULT_USER_AGENT = String.format("AlibabaCloud (%s; %s) Java/%s Credentials/%s TeaDSL/1", sysProps.getProperty("os.name"), sysProps + .getProperty("os.arch"), sysProps.getProperty("java.runtime.version"), version); + } public CompatibleUrlConnClient() { @@ -102,6 +119,7 @@ public HttpURLConnection initHttpConnection(URL url, HttpRequest request) { httpConn.setUseCaches(false); setConnectionTimeout(httpConn, request); httpConn.setRequestProperty(ACCEPT_ENCODING, "identity"); + httpConn.setRequestProperty(USER_AGENT, DEFAULT_USER_AGENT); Map mappedHeaders = request.getSysHeaders(); for (Entry entry : mappedHeaders.entrySet()) { httpConn.setRequestProperty(entry.getKey(), entry.getValue()); diff --git a/src/main/resources/version.properties b/src/main/resources/version.properties new file mode 100644 index 0000000..f9cf48a --- /dev/null +++ b/src/main/resources/version.properties @@ -0,0 +1 @@ +sdk.credentials.version=${project.version} \ No newline at end of file diff --git a/src/test/java/com/aliyun/credentials/http/CompatibleUrlConnClientTest.java b/src/test/java/com/aliyun/credentials/http/CompatibleUrlConnClientTest.java index 613a899..9ffcf0c 100644 --- a/src/test/java/com/aliyun/credentials/http/CompatibleUrlConnClientTest.java +++ b/src/test/java/com/aliyun/credentials/http/CompatibleUrlConnClientTest.java @@ -10,6 +10,8 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class CompatibleUrlConnClientTest { @Test @@ -66,5 +68,8 @@ public void buildHttpConnectionTest() { HttpURLConnection connection = client.buildHttpConnection(request); Assert.assertEquals("value1", connection.getRequestProperty("header1")); Assert.assertEquals("json", connection.getRequestProperty("Content-Type")); + Pattern pattern = Pattern.compile("AlibabaCloud (.+; .+) Java/.+ Credentials/.+ TeaDSL/1"); + Matcher matcher = pattern.matcher(connection.getRequestProperty("User-Agent")); + Assert.assertTrue(matcher.find()); } }