@@ -725,7 +725,16 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
725725 return null ;
726726 }
727727
728- respBody = convertResponseBody (respBody );
728+ StringBuilder builder = new StringBuilder ();
729+
730+ if (!convertResponseBody (respBody ,builder )){
731+ throw new ApiException (
732+ response .code (),
733+ response .headers ().toMultimap (),
734+ respBody );
735+ }else {
736+ respBody = builder .toString ();
737+ }
729738
730739
731740 String contentType = response .headers ().get ("Content-Type" );
@@ -973,26 +982,42 @@ public Call buildCall(String path, String method, List<Pair> queryParams, List<P
973982 return httpClient .newCall (request );
974983 }
975984
976- private boolean isApplicationJsonBody (Map <String , String > headerParams ){
985+ private void getDefaultContentType (Map <String , String > headerParams ) {
977986 String contentType = headerParams .get ("Content-Type" );
978987 if (contentType == null ) {
979- headerParams .put ("Content-Type" ,"application/json" );
988+ headerParams .put ("Content-Type" , "text/plain" );
989+ }
990+ }
991+
992+ private boolean isApplicationJsonBody (Map <String , String > headerParams ) {
993+ String contentType = headerParams .get ("Content-Type" );
994+ if (contentType == null ) {
995+ return false ;
996+ }
997+ if (contentType .equals ("application/json" )) {
980998 return true ;
981999 }
982- if (contentType .equals ("application/json" )){
1000+ return false ;
1001+ }
1002+
1003+ private boolean isPostBody (Map <String , String > headerParams ) {
1004+ String contentType = headerParams .get ("Content-Type" );
1005+ if (contentType == null ) {
1006+ return false ;
1007+ }
1008+ if (contentType .equals ("application/x-www-form-urlencoded" )) {
9831009 return true ;
9841010 }
9851011 return false ;
9861012 }
9871013
988- private ServiceInfo addPairAndGetServiceInfo (String path , List <Pair > queryParams ,Map <String , String > headerParams ) {
1014+ private ServiceInfo addPairAndGetServiceInfo (String path , List <Pair > queryParams , Map <String , String > headerParams ) {
9891015 String [] param = path .split ("/" );
9901016
991- if (!isApplicationJsonBody (headerParams )) {
1017+ if (!isApplicationJsonBody (headerParams ) && ! isPostBody ( headerParams )) {
9921018 queryParams .add (new Pair ("Action" , param [1 ]));
9931019 queryParams .add (new Pair ("Version" , param [2 ]));
9941020 }
995-
9961021 return new ServiceInfo (param [3 ], param [4 ]);
9971022 }
9981023
@@ -1001,41 +1026,50 @@ private void initRequestHeader(Map<String, String> headerParams) {
10011026 SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd'T'HHmmss'Z'" );
10021027 sdf .setTimeZone (TimeZone .getTimeZone ("GMT" ));
10031028 headerParams .put ("X-Date" , sdf .format (new Date ()));
1004- // headerParams.put("X-Date","20220318T082940Z");
10051029 }
10061030 }
10071031
10081032 private String getTruePath (String path , Map <String , String > headerParams ) {
1009- if (isApplicationJsonBody (headerParams )) {
1033+ if (isApplicationJsonBody (headerParams ) || isPostBody ( headerParams )) {
10101034 String [] param = path .split ("/" );
10111035 return "/?Action=" + param [1 ] + "&Version=" + param [2 ];
10121036 } else {
10131037 return "/" ;
10141038 }
10151039 }
10161040
1017- private String convertResponseBody (String source ){
1018- Type t = new TypeToken <Map <String ,?>>(){}.getType ();
1019- Map <String ,?> temp = json .deserialize (source ,t );
1020- if (temp .containsKey ("ResponseMetadata" ) && temp .containsKey ("Result" )){
1021- return json .serialize (temp .get ("Result" ));
1041+ @ SuppressWarnings ("all" )
1042+ private boolean convertResponseBody (String source ,StringBuilder stringBuilder ) {
1043+ Type t = new TypeToken <Map <String , ?>>() {
1044+ }.getType ();
1045+ Map <String , ?> temp = json .deserialize (source , t );
1046+ if (temp .containsKey ("ResponseMetadata" ) && temp .containsKey ("Result" )) {
1047+ stringBuilder .append (json .serialize (temp .get ("Result" )));
1048+ return true ;
10221049 }
1023- return source ;
1050+ return false ;
10241051 }
10251052
1026- private void buildSimpleRequest (Object body , List <Pair > queryParams ,Map <String , String > headerParams ,String chain ) throws Exception {
1027- if (isApplicationJsonBody (headerParams )){
1028- return ;
1053+ private void buildSimpleRequest (Object body , List <Pair > queryParams , Map <String , String > headerParams , StringBuilder builder , String chain ) throws Exception {
1054+ if (isApplicationJsonBody (headerParams )) {
1055+ builder . append ( json . serialize ( body )) ;
10291056 }
10301057 Class <?> clazz = body .getClass ();
10311058
1032- if (!clazz .getName ().startsWith ("com.volcengine.volcstack" )){
1033- Pair pair = new Pair (chain , body .toString ());
1034- queryParams .add (pair );
1059+ if (!clazz .getName ().startsWith ("com.volcengine.volcstack" )) {
1060+ if (isPostBody (headerParams )) {
1061+ builder .append (chain );
1062+ builder .append ("=" );
1063+ builder .append (body );
1064+ builder .append ("&" );
1065+ } else {
1066+ Pair pair = new Pair (chain , body .toString ());
1067+ queryParams .add (pair );
1068+ }
10351069 return ;
10361070 }
10371071
1038- if (!chain .equals ("" )){
1072+ if (!chain .equals ("" )) {
10391073 chain = chain + "." ;
10401074 }
10411075
@@ -1050,21 +1084,40 @@ private void buildSimpleRequest(Object body, List<Pair> queryParams,Map<String,
10501084 int count = 0 ;
10511085 for (Object o : (List <?>) value ) {
10521086 String key = chain + getMethodName (field .getName ()) + "." + (++count );
1053- buildSimpleRequest (o ,queryParams ,headerParams ,key );
1087+ buildSimpleRequest (o , queryParams , headerParams , builder , key );
10541088 }
10551089 } else {
1056- if (!field .getType ().getName ().startsWith ("com.volcengine.volcstack" )){
1057- Pair pair = new Pair (chain + getMethodName (field .getName ()), value .toString ());
1058- queryParams .add (pair );
1059- }else {
1090+ if (!field .getType ().getName ().startsWith ("com.volcengine.volcstack" )) {
1091+ if (isPostBody (headerParams )) {
1092+ builder .append (chain );
1093+ builder .append (getMethodName (field .getName ()));
1094+ builder .append ("=" );
1095+ builder .append (value );
1096+ builder .append ("&" );
1097+ } else {
1098+ Pair pair = new Pair (chain + getMethodName (field .getName ()), value .toString ());
1099+ queryParams .add (pair );
1100+ }
1101+ } else {
10601102 String key = chain + getMethodName (field .getName ());
1061- buildSimpleRequest (value ,queryParams ,headerParams ,key );
1103+ buildSimpleRequest (value , queryParams , headerParams , builder , key );
10621104 }
10631105 }
10641106 }
10651107 }
10661108 }
10671109
1110+ private String getPayload (String contentType , String source ) {
1111+ if (source .equals ("" )) {
1112+ return "" ;
1113+ }
1114+ if (contentType .equals ("application/json" )) {
1115+ return source ;
1116+ } else {
1117+ return source .substring (0 , source .length () - 1 );
1118+ }
1119+ }
1120+
10681121 private String getMethodName (String fieldName ) throws Exception {
10691122 char [] chars = fieldName .toCharArray ();
10701123 chars [0 ] = toUpperCase (chars [0 ]);
@@ -1095,13 +1148,16 @@ private char toUpperCase(char c) {
10951148 */
10961149 public Request buildRequest (String path , String method , List <Pair > queryParams , List <Pair > collectionQueryParams , Object body , Map <String , String > headerParams , Map <String , Object > formParams , String [] authNames , ProgressRequestBody .ProgressRequestListener progressRequestListener ) throws ApiException {
10971150
1098- ServiceInfo serviceInfo = addPairAndGetServiceInfo (path , queryParams ,headerParams );
1151+ getDefaultContentType (headerParams );
1152+
1153+ ServiceInfo serviceInfo = addPairAndGetServiceInfo (path , queryParams , headerParams );
10991154 initRequestHeader (headerParams );
1100- String truePath = getTruePath (path ,headerParams );
1155+ String truePath = getTruePath (path , headerParams );
11011156 String contentType = headerParams .get ("Content-Type" );
1157+ StringBuilder bodyBuilder = new StringBuilder ();
11021158
11031159 try {
1104- buildSimpleRequest (body , queryParams ,headerParams ,"" );
1160+ buildSimpleRequest (body , queryParams , headerParams , bodyBuilder , "" );
11051161 } catch (Exception e ) {
11061162 throw new ApiException (e );
11071163 }
@@ -1115,8 +1171,8 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
11151171 if (!HttpMethod .permitsRequestBody (method )) {
11161172 reqBody = null ;
11171173 } else if ("application/x-www-form-urlencoded" .equals (contentType )) {
1118- for (Pair pair : queryParams ){
1119- formParams .put (pair .getName (),pair .getValue ());
1174+ for (Pair pair : queryParams ) {
1175+ formParams .put (pair .getName (), pair .getValue ());
11201176 }
11211177 reqBody = buildRequestBodyFormEncoding (formParams );
11221178 } else if ("multipart/form-data" .equals (contentType )) {
@@ -1132,8 +1188,9 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
11321188 } else {
11331189 reqBody = serialize (body , contentType );
11341190 }
1135-
1136- updateParamsForAuth (authNames , queryParams , headerParams ,serviceInfo ,reqBody );
1191+ //sign
1192+ updateParamsForAuth (authNames , queryParams , headerParams , serviceInfo , getPayload (contentType ,
1193+ bodyBuilder .toString ()));
11371194
11381195 processHeaderParams (headerParams , reqBuilder );
11391196
@@ -1232,20 +1289,20 @@ public void updateParamsForAuth(String[] authNames, List<Pair> queryParams, Map<
12321289 for (String authName : authNames ) {
12331290 Authentication auth = authentications .get (authName );
12341291 if (auth == null ) throw new RuntimeException ("Authentication undefined: " + authName );
1235- auth .applyToParams (queryParams , headerParams ,null );
1292+ auth .applyToParams (queryParams , headerParams , "" );
12361293 }
12371294 }
12381295
1239- public void updateParamsForAuth (String [] authNames , List <Pair > queryParams , Map <String , String > headerParams ,ServiceInfo serviceInfo ,RequestBody requestBody ) {
1296+ public void updateParamsForAuth (String [] authNames , List <Pair > queryParams , Map <String , String > headerParams , ServiceInfo serviceInfo , String payload ) {
12401297 for (String authName : authNames ) {
12411298 Authentication auth = authentications .get (authName );
12421299 if (auth == null ) throw new RuntimeException ("Authentication undefined: " + authName );
1243- if (auth instanceof VolcstackSign ){
1244- VolcstackSign volcstackSign = (VolcstackSign )auth ;
1300+ if (auth instanceof VolcstackSign ) {
1301+ VolcstackSign volcstackSign = (VolcstackSign ) auth ;
12451302 volcstackSign .setMethod (serviceInfo .getMethod ().toUpperCase ());
12461303 volcstackSign .setService (serviceInfo .getServiceName ());
12471304 }
1248- auth .applyToParams (queryParams , headerParams ,requestBody );
1305+ auth .applyToParams (queryParams , headerParams , payload );
12491306 }
12501307 }
12511308
0 commit comments