40
40
* Request decorator implementatioon that uses AWS SigV4 signing.
41
41
*/
42
42
public class IamRequestDecorator implements RequestDecorator {
43
- private static final String CONTENT_TYPE = "application/json" ;
44
- private static final MediaType JSON_MEDIA_TYPE = MediaType .parse (CONTENT_TYPE );
43
+ private static final String DEFAULT_CONTENT_TYPE = "application/json" ;
45
44
private final CredentialsProvider credentialsProvider ;
46
45
private final AWS4Signer v4Signer ;
47
46
private final String serviceName ;
@@ -89,8 +88,25 @@ public final okhttp3.Request decorate(okhttp3.Request req) throws ApiAuthExcepti
89
88
//Copy the signed/credentialed request back into an OKHTTP Request object.
90
89
okhttp3 .Request .Builder okReqBuilder = new okhttp3 .Request .Builder ();
91
90
91
+ // By default, we send application/json content-type
92
+ String contentType = DEFAULT_CONTENT_TYPE ;
93
+
92
94
for (Map .Entry <String , List <String >> e : request .getHeaders ().entries ()) {
93
- okReqBuilder .addHeader (e .getKey (), e .getValue ().get (0 ));
95
+ String key = e .getKey ();
96
+ String value = e .getValue ().get (0 );
97
+ okReqBuilder .addHeader (key , value );
98
+
99
+ // If content-type detected in headers, capture to use later
100
+ if ("content-type" .equalsIgnoreCase (key )) {
101
+ contentType = value ;
102
+ }
103
+ }
104
+
105
+ // Set MediaType for OkHttp request body, because it will overwrite content-type headers.
106
+ MediaType mediaType = MediaType .parse (contentType );
107
+ // If the provided content-type is not valid, reset to default MediaType
108
+ if (mediaType == null ) {
109
+ mediaType = MediaType .parse (DEFAULT_CONTENT_TYPE );
94
110
}
95
111
96
112
//Set the URL and Method
@@ -99,7 +115,7 @@ public final okhttp3.Request decorate(okhttp3.Request req) throws ApiAuthExcepti
99
115
if (req .body () == null ) {
100
116
requestBody = null ;
101
117
} else {
102
- requestBody = RequestBody .create (bodyBytes , JSON_MEDIA_TYPE );
118
+ requestBody = RequestBody .create (bodyBytes , mediaType );
103
119
}
104
120
105
121
okReqBuilder .method (req .method (), requestBody );
0 commit comments