Skip to content

Commit edfffa8

Browse files
authored
Merge pull request #201 from Shallwewu/master
remove org json dependency & fix instance profile credential fetcher …
2 parents adeb213 + 75dde8b commit edfffa8

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>com.aliyun.oss</groupId>
1313
<artifactId>aliyun-sdk-oss</artifactId>
14-
<version>3.4.0</version>
14+
<version>3.4.1</version>
1515
<packaging>jar</packaging>
1616
<name>Aliyun OSS SDK for Java</name>
1717
<description>The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service</description>
@@ -37,6 +37,12 @@
3737
<groupId>com.aliyun</groupId>
3838
<artifactId>aliyun-java-sdk-core</artifactId>
3939
<version>3.4.0</version>
40+
<exclusions>
41+
<exclusion>
42+
<groupId>org.json</groupId>
43+
<artifactId>json</artifactId>
44+
</exclusion>
45+
</exclusions>
4046
</dependency>
4147
<dependency>
4248
<groupId>com.aliyun</groupId>

src/main/java/com/aliyun/oss/common/auth/InstanceProfileCredentialsProvider.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
package com.aliyun.oss.common.auth;
2121

22-
import com.aliyun.oss.common.auth.Credentials;
23-
import com.aliyun.oss.common.auth.CredentialsProvider;
2422
import com.aliyun.oss.common.utils.AuthUtils;
2523
import com.aliyun.oss.common.utils.LogUtils;
2624
import com.aliyuncs.exceptions.ClientException;
2725

26+
import java.util.concurrent.locks.ReentrantLock;
27+
2828
/**
2929
* Credentials provider implementation that loads credentials from the Ali Cloud
3030
* ECS Instance Metadata Service.
@@ -54,20 +54,35 @@ public void setCredentials(Credentials creds) {
5454
public InstanceProfileCredentials getCredentials() {
5555
if (credentials == null || credentials.isExpired()) {
5656
try {
57-
credentials = (InstanceProfileCredentials) fetcher.fetch(maxRetryTimes);
58-
} catch (ClientException e) {
59-
LogUtils.logException("EcsInstanceCredentialsFetcher.fetch Exception:", e);
60-
return null;
57+
lock.lock();
58+
if (credentials == null || credentials.isExpired()) {
59+
try {
60+
credentials = (InstanceProfileCredentials) fetcher.fetch(maxRetryTimes);
61+
} catch (ClientException e) {
62+
LogUtils.logException("EcsInstanceCredentialsFetcher.fetch Exception:", e);
63+
return null;
64+
}
65+
}
66+
} finally {
67+
lock.unlock();
6168
}
6269
} else if (credentials.willSoonExpire() && credentials.shouldRefresh()) {
6370
try {
64-
credentials = (InstanceProfileCredentials) fetcher.fetch();
65-
} catch (ClientException e) {
66-
// Use the current expiring session token and wait for next round
67-
credentials.setLastFailedRefreshTime();
68-
LogUtils.logException("EcsInstanceCredentialsFetcher.fetch Exception:", e);
71+
lock.lock();
72+
if (credentials.willSoonExpire() && credentials.shouldRefresh()) {
73+
try {
74+
credentials = (InstanceProfileCredentials) fetcher.fetch();
75+
} catch (ClientException e) {
76+
// Use the current expiring session token and wait for next round
77+
credentials.setLastFailedRefreshTime();
78+
LogUtils.logException("EcsInstanceCredentialsFetcher.fetch Exception:", e);
79+
}
80+
}
81+
} finally {
82+
lock.unlock();
6983
}
7084
}
85+
7186
return credentials;
7287
}
7388

@@ -76,5 +91,6 @@ public InstanceProfileCredentials getCredentials() {
7691
private InstanceProfileCredentialsFetcher fetcher;
7792

7893
private int maxRetryTimes = AuthUtils.MAX_ECS_METADATA_FETCH_RETRY_TIMES;
94+
private ReentrantLock lock = new ReentrantLock();
7995

8096
}

src/main/java/com/aliyun/oss/common/parser/JAXBResponseParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*/
4343
public class JAXBResponseParser implements ResponseParser<Object> {
4444

45-
private static final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
45+
private static final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl",null);
4646

4747
// It allows to specify the class type, if the class type is specified,
4848
// the contextPath will be ignored.

src/main/java/com/aliyun/oss/model/ObjectMetadata.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ public void setLastModified(Date lastModified) {
129129
* The value is not in the Rfc822 format.
130130
*/
131131
public Date getExpirationTime() throws ParseException {
132-
return DateUtil.parseRfc822Date((String) metadata.get(OSSHeaders.EXPIRES));
132+
String expires = (String) metadata.get(OSSHeaders.EXPIRES);
133+
134+
if (expires != null)
135+
return DateUtil.parseRfc822Date((String) metadata.get(OSSHeaders.EXPIRES));
136+
137+
return null;
133138
}
134139

135140
/**
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=3.4.0
1+
version=3.4.1

src/test/java/com/aliyun/oss/common/utils/VersionUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class VersionUtilTest {
2828
@Test
2929
public void testGetDefaultUserAgent() {
3030
String userAgent = VersionInfoUtils.getDefaultUserAgent();
31-
assertTrue(userAgent.startsWith("aliyun-sdk-java/3.4.0("));
31+
assertTrue(userAgent.startsWith("aliyun-sdk-java/3.4.1("));
3232
assertEquals(userAgent.split("/").length, 4);
3333
assertEquals(userAgent.split(";").length, 2);
3434
assertEquals(userAgent.split("\\(").length, 2);

0 commit comments

Comments
 (0)