Skip to content

Commit ec7c097

Browse files
author
hicooper
committed
method format
1 parent a4e0e60 commit ec7c097

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

src/main/java/com/berry/common/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ private Constants() {
4545

4646
public static final String DEFAULT_MIME = "application/octet-stream";
4747

48-
public static final String API_SUCCESS_CODE = "200";
48+
public static final String API_SUCCESS_CODE = "200";
4949

50-
public static final String API_SUCCESS_MSG = "SUCCESS";
50+
public static final String API_SUCCESS_MSG = "SUCCESS";
5151

5252

5353
/**

src/main/java/com/berry/http/Response.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import javax.annotation.Nullable;
1111
import java.io.IOException;
1212

13+
import static org.apache.commons.lang3.StringUtils.isNoneBlank;
14+
import static org.apache.commons.lang3.StringUtils.isNotBlank;
15+
1316
/**
1417
* Created with IntelliJ IDEA.
1518
*
@@ -39,7 +42,7 @@ private Response(int code, String message, Headers headers, byte[] body, String
3942
}
4043

4144
static Response create(okhttp3.Response response, @Nullable String errorMsg) {
42-
if (response == null || StringUtils.isNoneBlank(errorMsg)) {
45+
if (response == null || isNoneBlank(errorMsg)) {
4346
return new Response(-1, null, null, null, null, null, errorMsg);
4447
}
4548
ResponseBody body = response.body();
@@ -90,7 +93,7 @@ public <T> T jsonToObject(Class<T> clazz) {
9093
}
9194

9295
public boolean isJson() {
93-
return StringUtils.isNotBlank(this.contentType) && this.contentType.startsWith(Constants.JSON_MIME);
96+
return isNotBlank(this.contentType) && this.contentType.startsWith(Constants.JSON_MIME);
9497
}
9598

9699
public int getCode() {

src/main/java/com/berry/storage/BucketManage.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.berry.util.Auth;
1515
import com.berry.util.Json;
1616
import com.berry.util.StringMap;
17-
import com.berry.util.StringUtils;
1817
import com.google.common.collect.Lists;
1918
import org.slf4j.Logger;
2019
import org.slf4j.LoggerFactory;
@@ -23,6 +22,8 @@
2322
import java.util.ArrayList;
2423
import java.util.List;
2524

25+
import static org.apache.commons.lang3.StringUtils.*;
26+
2627
/**
2728
* Created with IntelliJ IDEA.
2829
*
@@ -59,7 +60,8 @@ public List<BucketInfo> queryBucket(@Nullable String bucketName) throws OssExcep
5960
if (response.isSuccessful()) {
6061
Result result = response.jsonToObject(Result.class);
6162
if (result == null || !result.getCode().equals(Constants.API_SUCCESS_CODE) || !result.getMsg().equals(Constants.API_SUCCESS_MSG)) {
62-
logger.error(result == null ? "empty result" : result.getMsg());
63+
String msg = result == null ? "empty result" : result.getMsg();
64+
logger.error(msg);
6365
return Lists.newArrayList();
6466
}
6567
List<BucketInfo> vos = new ArrayList<>();
@@ -85,7 +87,7 @@ public List<BucketInfo> queryBucket(@Nullable String bucketName) throws OssExcep
8587
* @return true or false
8688
*/
8789
public Boolean createBucket(String name, String region, @Nullable String acl) throws OssException {
88-
if (StringUtils.isAnyBlank(name, region)) {
90+
if (isAnyBlank(name, region)) {
8991
throw new IllegalArgumentException("name and region cannot be blank!");
9092
}
9193
if (!name.matches(Constants.BUCKET_NAME_PATTERN)) {
@@ -94,7 +96,7 @@ public Boolean createBucket(String name, String region, @Nullable String acl) th
9496
StringMap params = new StringMap();
9597
params.put("name", name);
9698
params.put("region", region);
97-
if (StringUtils.isNotBlank(acl)) {
99+
if (isNotBlank(acl)) {
98100
// 验证acl 规范
99101
if (!Constants.AclType.ALL_NAME.contains(acl)) {
100102
throw new IllegalArgumentException("illegal acl enum [PRIVATE, PUBLIC_READ, PUBLIC_READ_WRITE]");
@@ -113,7 +115,7 @@ public Boolean createBucket(String name, String region, @Nullable String acl) th
113115
* @return true or false
114116
*/
115117
public Boolean updateAcl(String bucket, String acl) throws OssException {
116-
if (StringUtils.isAnyBlank(bucket, acl)) {
118+
if (isAnyBlank(bucket, acl)) {
117119
throw new IllegalArgumentException("bucket and acl cannot be blank!");
118120
}
119121
String url = String.format("%s%s", config.getAddress(), UrlFactory.BucketUr.set_acl.getUrl());
@@ -130,7 +132,7 @@ public Boolean updateAcl(String bucket, String acl) throws OssException {
130132
* @return true or false
131133
*/
132134
public Boolean delete(String bucket) throws OssException {
133-
if (StringUtils.isBlank(bucket)) {
135+
if (isBlank(bucket)) {
134136
throw new IllegalArgumentException("bucket cannot be blank!");
135137
}
136138
String url = String.format("%s%s", config.getAddress(), UrlFactory.BucketUr.delete_bucket.getUrl());

src/main/java/com/berry/storage/ObjectManage.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
import com.berry.util.Auth;
1515
import com.berry.util.Json;
1616
import com.berry.util.StringMap;
17-
import com.berry.util.StringUtils;
1817
import com.google.gson.Gson;
1918
import org.slf4j.Logger;
2019
import org.slf4j.LoggerFactory;
2120

2221
import javax.annotation.Nullable;
2322
import java.io.File;
2423

24+
import static org.apache.commons.lang3.StringUtils.isNotBlank;
25+
2526
/**
2627
* Created with IntelliJ IDEA.
2728
*
@@ -34,9 +35,12 @@ public final class ObjectManage {
3435

3536
private static final Logger logger = LoggerFactory.getLogger(ObjectManage.class);
3637

38+
private final String errorIMsgTemp = "request fail,stateCode:{}, msg:{}";
39+
40+
private static final String illegalAclMsg = "illegal acl, enum [" + Constants.AclType.ALL_NAME + "]";
41+
3742
private final Auth auth;
3843
private final Config config;
39-
4044
private final HttpClient client;
4145

4246
public ObjectManage(Auth auth, Config config) {
@@ -51,12 +55,12 @@ public ObjectManage(Auth auth, Config config) {
5155
public ObjectInfo upload(String bucket, String acl, @Nullable String filePath, String fileName, byte[] fileData) throws OssException {
5256
// 验证acl 规范
5357
if (!Constants.AclType.ALL_NAME.contains(acl)) {
54-
throw new IllegalArgumentException("illegal acl, enum [" + Constants.AclType.ALL_NAME + "]");
58+
throw new IllegalArgumentException(illegalAclMsg);
5559
}
5660
StringMap params = new StringMap();
5761
params.put("bucket", bucket);
5862
params.put("acl", acl);
59-
if (StringUtils.isNotBlank(filePath)) {
63+
if (isNotBlank(filePath)) {
6064
params.put("filePath", filePath);
6165
}
6266
params.put("fileName", fileName);
@@ -68,7 +72,7 @@ public ObjectInfo upload(String bucket, String acl, @Nullable String filePath, S
6872
if (result.getCode().equals(Constants.API_SUCCESS_CODE) && result.getMsg().equals(Constants.API_SUCCESS_MSG)) {
6973
return Json.decode(Json.encode(result.getData()), ObjectInfo.class);
7074
}
71-
logger.error("request fail,stateCode:{}, msg:{}", result.getCode(), result.getMsg());
75+
logger.error(errorIMsgTemp, result.getCode(), result.getMsg());
7276
throw new OssException(result.getMsg());
7377
}
7478

@@ -96,12 +100,12 @@ public JSONArray upload(String bucket, String acl, @Nullable String filePath, Fi
96100
public JSONArray upload(String bucket, String acl, @Nullable String filePath, File[] files) throws OssException {
97101
// 验证acl 规范
98102
if (!Constants.AclType.ALL_NAME.contains(acl)) {
99-
throw new IllegalArgumentException("illegal acl, enum [" + Constants.AclType.ALL_NAME + "]");
103+
throw new IllegalArgumentException(illegalAclMsg);
100104
}
101105
StringMap fields = new StringMap();
102106
fields.put("bucket", bucket);
103107
fields.put("acl", acl);
104-
if (StringUtils.isNotBlank(filePath)) {
108+
if (isNotBlank(filePath)) {
105109
fields.put("filePath", filePath);
106110
}
107111
String url = String.format("%s%s", config.getAddress(), UrlFactory.ObjectUrl.create.getUrl());
@@ -114,19 +118,19 @@ public JSONArray upload(String bucket, String acl, @Nullable String filePath, Fi
114118
&& result.getData() != null) {
115119
return JSON.parseArray(JSON.toJSONString(result.getData()));
116120
}
117-
logger.error("request fail,stateCode:{}, msg:{}", result.getCode(), result.getMsg());
121+
logger.error(errorIMsgTemp, result.getCode(), result.getMsg());
118122
throw new OssException(result.getMsg());
119123
}
120124

121125
public ObjectInfo upload(String bucket, String acl, @Nullable String filePath, String fileName, String base64Data) throws OssException {
122126
// 验证acl 规范
123127
if (!Constants.AclType.ALL_NAME.contains(acl)) {
124-
throw new IllegalArgumentException("illegal acl, enum [" + Constants.AclType.ALL_NAME + "]");
128+
throw new IllegalArgumentException(illegalAclMsg);
125129
}
126130
StringMap params = new StringMap();
127131
params.put("bucket", bucket);
128132
params.put("acl", acl);
129-
if (StringUtils.isNotBlank(filePath)) {
133+
if (isNotBlank(filePath)) {
130134
params.put("filePath", filePath);
131135
}
132136
params.put("fileName", fileName);
@@ -140,7 +144,7 @@ public ObjectInfo upload(String bucket, String acl, @Nullable String filePath, S
140144
&& result.getData() != null) {
141145
return Json.decode(Json.encode(result.getData()), ObjectInfo.class);
142146
}
143-
logger.error("request fail,stateCode:{}, msg:{}", result.getCode(), result.getMsg());
147+
logger.error(errorIMsgTemp, result.getCode(), result.getMsg());
144148
throw new OssException(result.getMsg());
145149
}
146150

@@ -161,7 +165,7 @@ public byte[] getObject(String bucket, String fullObjectPath) throws OssExceptio
161165
return response.getBody();
162166
}
163167
Result result = response.jsonToObject(Result.class);
164-
logger.error("request fail,stateCode:{}, msg:{}", result.getCode(), result.getMsg());
168+
logger.error(errorIMsgTemp, result.getCode(), result.getMsg());
165169
throw new OssException(result.getMsg());
166170
}
167171

@@ -251,20 +255,20 @@ public String getObjectTempAccessUrlWithExpired(String bucket, String objectPath
251255
GenerateUrlWithSigned vo = new Gson().fromJson(Json.encode(result.getData()), GenerateUrlWithSigned.class);
252256
return vo.getUrl() + "?" + vo.getSignature();
253257
}
254-
logger.error("request fail,stateCode:{}, msg:{}", result.getCode(), result.getMsg());
258+
logger.error(errorIMsgTemp, result.getCode(), result.getMsg());
255259
throw new OssException(result.getMsg());
256260
}
257261

258262
private Response get(String url) throws OssException {
259263
StringMap header = auth.authorization(url);
260-
logger.debug("request url:{}, header:{}", url, Json.encode(header));
264+
logger.debug("request url:{}, header:{}", url, header.map());
261265
String withTokenUrl = url + "?token=" + header.get(Auth.OSS_SDK_AUTH_HEAD_NAME);
262266
return client.get(withTokenUrl, header);
263267
}
264268

265269
private Response post(String url, StringMap params) throws OssException {
266270
StringMap header = auth.authorization(url);
267-
logger.debug("request url:{}, header:{}", url, Json.encode(header));
271+
logger.debug("request url:{}, header:{}", url, header.map());
268272
return client.post(url, params.jsonString(), header);
269273
}
270274
}

src/main/java/com/berry/util/Auth.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import java.net.URI;
66
import java.security.GeneralSecurityException;
77

8+
import static org.apache.commons.lang3.StringUtils.isBlank;
9+
810
/**
911
* Created with IntelliJ IDEA.
1012
*
1113
* @author Berry_Cooper.
1214
* @date 2019-06-29 11:03
1315
* fileName:Auth
14-
* Use:
16+
* Use:提供签名授权
1517
*/
1618
public final class Auth {
1719

@@ -37,7 +39,7 @@ private Auth(String accessKeyId, SecretKeySpec secretKeySpec) {
3739
}
3840

3941
public static Auth create(String accessKeyId, String accessKeySecret) {
40-
if (StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
42+
if (isBlank(accessKeyId) || isBlank(accessKeySecret)) {
4143
throw new IllegalArgumentException("empty key");
4244
}
4345
byte[] sk = StringUtils.utf8Bytes(accessKeySecret);

src/main/java/com/berry/util/StringUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.berry.common.Constants;
55

66
import java.util.ArrayList;
7-
import java.util.Comparator;
87
import java.util.List;
98
import java.util.Map;
109

@@ -21,11 +20,11 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
2120
*/
2221
public static String parseUrlParams(StringMap paramsMap) {
2322
List<Map.Entry<String, Object>> infoIds = new ArrayList<>(paramsMap.entrySet());
24-
infoIds.sort(Comparator.comparing(Map.Entry::getKey));
23+
infoIds.sort(Map.Entry.comparingByKey());
2524
// 构造URL 键值对的格式
2625
StringBuilder buf = new StringBuilder();
2726
for (Map.Entry<String, Object> item : infoIds) {
28-
if (StringUtils.isNotBlank(item.getKey()) && item.getValue() != null) {
27+
if (isNotBlank(item.getKey()) && item.getValue() != null) {
2928
String key = item.getKey();
3029
String val = item.getValue().toString();
3130
buf.append(key).append("=").append(val);

0 commit comments

Comments
 (0)