1414import  com .berry .util .Auth ;
1515import  com .berry .util .Json ;
1616import  com .berry .util .StringMap ;
17- import  com .berry .util .StringUtils ;
1817import  com .google .gson .Gson ;
1918import  org .slf4j .Logger ;
2019import  org .slf4j .LoggerFactory ;
2120
2221import  javax .annotation .Nullable ;
2322import  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}
0 commit comments