Skip to content

Commit

Permalink
feat: add user-agent for all credential requests
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 authored and JacksonTian committed Jun 20, 2024
1 parent 064a487 commit 06abc5d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>version.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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<String, String> mappedHeaders = request.getSysHeaders();
for (Entry<String, String> entry : mappedHeaders.entrySet()) {
httpConn.setRequestProperty(entry.getKey(), entry.getValue());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sdk.credentials.version=${project.version}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 06abc5d

Please sign in to comment.