Skip to content

Commit f78b60c

Browse files
JacksonTianyndu13
authored andcommitted
improve for coding style
1 parent bdb4fbc commit f78b60c

File tree

10 files changed

+53
-246
lines changed

10 files changed

+53
-246
lines changed

aliyun-java-sdk-core/src/main/java/com/aliyuncs/DefaultAcsClient.java

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@
3838
@SuppressWarnings("deprecation")
3939
public class DefaultAcsClient implements IAcsClient {
4040

41-
/*
42-
Now maxRetryNumber and autoRetry has no effect.
43-
*/
41+
// Now maxRetryNumber and autoRetry has no effect.
4442
private int maxRetryNumber = 3;
4543
private boolean autoRetry = true;
46-
private IClientProfile clientProfile = null;
44+
private IClientProfile clientProfile;
4745
private AlibabaCloudCredentialsProvider credentialsProvider;
4846

4947
private IHttpClient httpClient;
@@ -118,12 +116,10 @@ public <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request, Stri
118116
request.setSysSignatureAlgorithm(this.signatureAlgorithm);
119117
}
120118
Signer signer = Signer.getSigner(new LegacyCredentials(credential), request.getSysSignatureVersion(), request.getSysSignatureAlgorithm());
121-
FormatType format = null;
122119
if (null == request.getSysRegionId()) {
123120
request.setSysRegionId(regionId);
124121
}
125-
return doAction(request, autoRetry, maxRetryNumber, regionId, new LegacyCredentials(credential), signer,
126-
format);
122+
return doAction(request, regionId, new LegacyCredentials(credential), signer, null);
127123
}
128124

129125
@Override
@@ -191,8 +187,6 @@ public <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request, bool
191187
if (null == profile) {
192188
throw new ClientException("SDK.InvalidProfile", "No active profile found.");
193189
}
194-
boolean retry = autoRetry;
195-
int retryNumber = maxRetryCounts;
196190
String region = profile.getRegionId();
197191
if (null == request.getSysRegionId()) {
198192
request.setSysRegionId(region);
@@ -212,7 +206,7 @@ public <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request, bool
212206
Signer signer = Signer.getSigner(credentials, request.getSysSignatureVersion(), request.getSysSignatureAlgorithm());
213207
FormatType format = profile.getFormat();
214208

215-
return this.doAction(request, retry, retryNumber, request.getSysRegionId(), credentials, signer, format);
209+
return this.doAction(request, request.getSysRegionId(), credentials, signer, format);
216210
}
217211

218212
private <T extends AcsResponse> T parseAcsResponse(AcsRequest<T> request, HttpResponse baseResponse)
@@ -254,13 +248,13 @@ private <T extends AcsResponse> T parseAcsResponse(AcsRequest<T> request, HttpRe
254248
public <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request, boolean autoRetry, int maxRetryNumber,
255249
String regionId, Credential credential, Signer signer, FormatType format)
256250
throws ClientException, ServerException {
257-
return doAction(request, autoRetry, maxRetryNumber, regionId, new LegacyCredentials(credential), signer,
251+
return doAction(request, regionId, new LegacyCredentials(credential), signer,
258252
format);
259253
}
260254

261255
public ProductDomain getDomain(AcsRequest request, String regionId)
262256
throws ClientException {
263-
ProductDomain domain = null;
257+
ProductDomain domain;
264258
if (request.getSysProductDomain() != null) {
265259
domain = request.getSysProductDomain();
266260
} else {
@@ -283,11 +277,11 @@ public ProductDomain getDomain(AcsRequest request, String regionId)
283277
return domain;
284278
}
285279

286-
private <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request, boolean autoRetry, int maxRetryNumber,
280+
private <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request,
287281
String regionId, AlibabaCloudCredentials credentials, Signer signer, FormatType format)
288282
throws ClientException, ServerException {
289283
if (!GlobalTracer.isRegistered() || clientProfile.isCloseTrace()) {
290-
return doRealAction(request, autoRetry, maxRetryNumber, regionId, credentials, signer, format);
284+
return doRealAction(request, regionId, credentials, signer, format);
291285
}
292286

293287
Tracer tracer = GlobalTracer.get();
@@ -302,7 +296,7 @@ private <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request, boo
302296
.start();
303297
tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new HttpHeadersInjectAdapter(request));
304298
try {
305-
HttpResponse response = doRealAction(request, autoRetry, maxRetryNumber, regionId, credentials, signer, format);
299+
HttpResponse response = doRealAction(request, regionId, credentials, signer, format);
306300
span.setTag("status", response.getStatus());
307301
span.setTag("ReasonPhrase", response.getReasonPhrase());
308302
return response;
@@ -315,7 +309,7 @@ private <T extends AcsResponse> HttpResponse doAction(AcsRequest<T> request, boo
315309

316310
}
317311

318-
private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request, boolean autoRetry, int maxRetryNumber,
312+
private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request,
319313
String regionId, AlibabaCloudCredentials credentials, Signer signer, FormatType format)
320314
throws ClientException, ServerException {
321315

@@ -454,17 +448,17 @@ private <T extends AcsResponse> HttpResponse doRealAction(AcsRequest<T> request,
454448
* 2019-01-03 change access control from private to protected, then subClass can
455449
* override it and rewrite httpResponse processing
456450
*/
457-
protected <T extends AcsResponse> T readResponse(Class<T> clasz, HttpResponse httpResponse, FormatType format)
451+
protected <T extends AcsResponse> T readResponse(Class<T> clazz, HttpResponse httpResponse, FormatType format)
458452
throws ClientException {
459453
// new version response contains "@XmlRootElement" annotation
460-
if (clasz.isAnnotationPresent(XmlRootElement.class)
454+
if (clazz.isAnnotationPresent(XmlRootElement.class)
461455
&& !clientProfile.getHttpClientConfig().isCompatibleMode()) {
462456
Unmarshaller unmarshaller = UnmarshallerFactory.getUnmarshaller(format);
463-
return unmarshaller.unmarshal(clasz, httpResponse.getHttpContentString());
457+
return unmarshaller.unmarshal(clazz, httpResponse.getHttpContentString());
464458
} else {
465459
Reader reader = ReaderFactory.createInstance(format);
466460
UnmarshallerContext context = new UnmarshallerContext();
467-
T response = null;
461+
T response;
468462
String stringContent = httpResponse.getHttpContentString();
469463

470464
if (stringContent == null) {
@@ -473,13 +467,13 @@ protected <T extends AcsResponse> T readResponse(Class<T> clasz, HttpResponse ht
473467
}
474468

475469
try {
476-
response = clasz.newInstance();
470+
response = clazz.newInstance();
477471
} catch (Exception e) {
478472
throw new ClientException("SDK.InvalidResponseClass",
479-
"Unable to allocate " + clasz.getName() + " class");
473+
"Unable to allocate " + clazz.getName() + " class");
480474
}
481475

482-
String responseEndpoint = clasz.getName().substring(clasz.getName().lastIndexOf(".") + 1);
476+
String responseEndpoint = clazz.getName().substring(clazz.getName().lastIndexOf(".") + 1);
483477
if (response.checkShowJsonItemName()) {
484478
context.setResponseMap(reader.read(stringContent, responseEndpoint));
485479
} else {
@@ -532,6 +526,7 @@ public void doActionWithProxy(ProtocolType protocolType, String httpsProxy, Stri
532526
config.setHttpsProxy(httpsProxy);
533527
return;
534528
}
529+
return;
535530
}
536531

537532
/**

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/BasicSessionCredentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class BasicSessionCredentials implements AlibabaCloudCredentials {
66
private final String accessKeyId;
77
private final String accessKeySecret;
88
private final String sessionToken;
9-
private long sessionStartedTimeInMilliSeconds = 0;
9+
private long sessionStartedTimeInMilliSeconds;
1010

1111
public BasicSessionCredentials(String accessKeyId, String accessKeySecret, String sessionToken) {
1212
this(accessKeyId, accessKeySecret, sessionToken, 0);

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/ECSMetadataServiceCredentialsFetcher.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class ECSMetadataServiceCredentialsFetcher {
1616
private static final String URL_IN_ECS_METADATA = "/latest/meta-data/ram/security-credentials/";
1717
private static final int DEFAULT_TIMEOUT_IN_MILLISECONDS = 1000;
18-
private static final String ECS_METADAT_FETCH_ERROR_MSG = "Failed to get RAM session credentials from ECS metadata service.";
18+
private static final String ECS_METADATA_FETCH_ERROR_MSG = "Failed to get RAM session credentials from ECS metadata service.";
1919
private static final int DEFAULT_ECS_SESSION_TOKEN_DURATION_SECONDS = 3600 * 6;
2020
private URL credentialUrl;
2121
private String roleName;
@@ -28,7 +28,7 @@ public ECSMetadataServiceCredentialsFetcher() {
2828

2929
public void setRoleName(String roleName) {
3030
if (null == roleName) {
31-
throw new NullPointerException("You must specifiy a valid role name.");
31+
throw new NullPointerException("You must specify a valid role name.");
3232
}
3333
this.roleName = roleName;
3434
setCredentialUrl();
@@ -68,22 +68,22 @@ public String getMetadata() throws ClientException {
6868
}
6969

7070
if (response.getStatus() != HttpURLConnection.HTTP_OK) {
71-
throw new ClientException(ECS_METADAT_FETCH_ERROR_MSG + " HttpCode=" + response.getStatus());
71+
throw new ClientException(ECS_METADATA_FETCH_ERROR_MSG + " HttpCode=" + response.getStatus());
7272
}
7373

7474
return new String(response.getHttpContent());
7575
}
7676

7777
public InstanceProfileCredentials fetch() throws ClientException {
7878
String jsonContent = getMetadata();
79-
JsonObject jsonObject = JsonParser.parseString(jsonContent).getAsJsonObject();;
79+
JsonObject jsonObject = JsonParser.parseString(jsonContent).getAsJsonObject();
8080
if (!jsonObject.has("Code") || !jsonObject.has("AccessKeyId") || !jsonObject.has("AccessKeySecret") || !jsonObject
8181
.has("SecurityToken") || !jsonObject.has("Expiration")) {
8282
throw new ClientException("Invalid json got from ECS Metadata service.");
8383
}
8484

8585
if (!"Success".equals(jsonObject.get("Code").getAsString())) {
86-
throw new ClientException(ECS_METADAT_FETCH_ERROR_MSG);
86+
throw new ClientException(ECS_METADATA_FETCH_ERROR_MSG);
8787
}
8888
return new InstanceProfileCredentials(
8989
jsonObject.get("AccessKeyId").getAsString(),

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/EnvironmentVariableCredentialsProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public AlibabaCloudCredentials getCredentials() throws ClientException {
1515
if (accessKeyId == null || accessKeySecret == null) {
1616
return null;
1717
}
18-
if (accessKeyId.length() == 0) {
18+
if (accessKeyId.isEmpty()) {
1919
throw new ClientException("Environment variable accessKeyId cannot be empty");
2020
}
21-
if (accessKeySecret.length() == 0) {
21+
if (accessKeySecret.isEmpty()) {
2222
throw new ClientException("Environment variable accessKeySecret cannot be empty");
2323
}
2424
return new BasicCredentials(accessKeyId, accessKeySecret);

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/InstanceProfileCredentialsProvider.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class InstanceProfileCredentialsProvider implements AlibabaCloudCredentia
1212

1313
public InstanceProfileCredentialsProvider(String roleName) {
1414
if (null == roleName) {
15-
throw new NullPointerException("You must specifiy a valid role name.");
15+
throw new NullPointerException("You must specify a valid role name.");
1616
}
1717
this.roleName = roleName;
1818
this.fetcher = new ECSMetadataServiceCredentialsFetcher();
@@ -29,11 +29,7 @@ public InstanceProfileCredentialsProvider withFetcher(ECSMetadataServiceCredenti
2929
public AlibabaCloudCredentials getCredentials() throws ClientException {
3030
if (credentials == null || credentials.isExpired()) {
3131
ecsMetadataServiceFetchCount += 1;
32-
int maxRetryTimes = MAX_ECS_METADATA_FETCH_RETRY_TIMES;
33-
credentials = fetcher.fetch(maxRetryTimes);
34-
// } else if (credentials.isExpired()) {
35-
// throw new ClientException("SDK.SessionTokenExpired", "Current session token
36-
// has expired.");
32+
credentials = fetcher.fetch(MAX_ECS_METADATA_FETCH_RETRY_TIMES);
3733
} else if (credentials.willSoonExpire() && credentials.shouldRefresh()) {
3834
try {
3935
ecsMetadataServiceFetchCount += 1;

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/OIDCCredentialsProvider.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Map;
66
import java.util.Date;
77

8-
import java.io.File;
98
import java.io.FileInputStream;
109
import java.io.IOException;
1110
import java.io.UnsupportedEncodingException;
@@ -136,7 +135,7 @@ private String invokeAssumeRoleWithOIDC() throws ClientException {
136135
queries.put("Format", "JSON");
137136
queries.put("Version", "2015-04-01");
138137
queries.put("Timestamp", ParameterHelper.getISO8601Time(new Date()));
139-
String url = null;
138+
String url;
140139
try {
141140
url = this.stsEndpoint + "?" + new String(ParameterHelper.getFormData(queries));
142141
} catch (UnsupportedEncodingException e) {
@@ -148,11 +147,11 @@ private String invokeAssumeRoleWithOIDC() throws ClientException {
148147
httpRequest.setHttpContentType(FormatType.FORM);
149148
httpRequest.setSysConnectTimeout(1000);
150149
httpRequest.setSysReadTimeout(3000);
151-
String oidcToken = null;
150+
String oidcToken;
152151
FileInputStream in = null;
153152
byte[] buffer;
154153
try {
155-
in = new FileInputStream(new File(oidcTokenFilePath));
154+
in = new FileInputStream(oidcTokenFilePath);
156155
buffer = new byte[in.available()];
157156
in.read(buffer);
158157
oidcToken = new String(buffer, "UTF-8");

aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/OssSignatureComposer.java

Lines changed: 0 additions & 70 deletions
This file was deleted.

aliyun-java-sdk-core/src/main/java/com/aliyuncs/profile/DefaultProfile.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public synchronized static DefaultProfile getProfile(String regionId) {
7575
* @Deprecated : Use addEndpoint(String regionId, String product, String endpoint) instead of this
7676
*/
7777
@Deprecated
78-
public synchronized static void addEndpoint(String endpointName, String regionId, String product, String domain)
79-
throws ClientException {
78+
public synchronized static void addEndpoint(String endpointName, String regionId, String product, String domain) {
8079
addEndpoint(endpointName, regionId, product, domain, true);
8180
}
8281

0 commit comments

Comments
 (0)