23
23
import java .net .http .HttpClient ;
24
24
import java .net .http .HttpRequest ;
25
25
import java .net .http .HttpResponse ;
26
+ import java .net .http .HttpHeaders ;
26
27
import java .time .Duration ;
27
28
import java .util .Map ;
28
29
import java .util .Optional ;
@@ -51,6 +52,8 @@ public class HttpJobHandler implements JobHandler {
51
52
private static final String PARAMETER_METHOD = "method" ;
52
53
private static final String PARAMETER_BODY = "body" ;
53
54
private static final String PARAMETER_AUTHORIZATION = "authorization" ;
55
+ private static final String PARAMETER_CONTENT_TYPE = "contentType" ;
56
+ private static final String PARAMETER_ACCEPT = "accept" ;
54
57
private static final String PARAMETER_HTTP_STATUS_CODE_FAILURE = "statusCodeFailure" ;
55
58
private static final String PARAMETER_HTTP_STATUS_CODE_COMPLETION = "statusCodeCompletion" ;
56
59
private static final String PARAMETER_HTTP_ERROR_CODE_PATH = "errorCodePath" ;
@@ -75,7 +78,7 @@ public void handle(JobClient jobClient, ActivatedJob job) throws IOException, In
75
78
if (hasFailingStatusCode (response , configurationMaps )) {
76
79
processFailure (configurationMaps , jobClient , job , response );
77
80
} else if (hasCompletingStatusCode (response , configurationMaps )) {
78
- final Map <String , Object > result = processResponse (job , response );
81
+ final Map <String , Object > result = processResponse (job , response , request );
79
82
jobClient .newCompleteCommand (job .getKey ()).variables (result ).send ().join ();
80
83
} else {
81
84
// do nothing
@@ -128,11 +131,17 @@ private HttpRequest buildRequest(ConfigurationMaps configurationMaps) {
128
131
HttpRequest .newBuilder ()
129
132
.uri (URI .create (url ))
130
133
.timeout (CONNECTION_TIMEOUT )
131
- .header ("Content-Type" , "application/json" )
132
- .header ("Accept" , "application/json" )
133
134
.method (method , bodyPublisher );
134
135
135
- getAuthentication (configurationMaps ).ifPresent (auth -> builder .header ("Authorization" , auth ));
136
+ getAuthorization (configurationMaps ).ifPresent (auth -> builder .header ("Authorization" , auth ));
137
+
138
+ // if no accept or content type header then default to json
139
+ getContentType (configurationMaps )
140
+ .ifPresentOrElse (contentType -> builder .header ("Content-Type" , contentType ),
141
+ () -> builder .header ("Content-Type" , "application/json" ));
142
+ getAccept (configurationMaps )
143
+ .ifPresentOrElse (accept -> builder .header ("Accept" , accept ),
144
+ () -> builder .header ("Accept" , "application/json" ));
136
145
137
146
return builder .build ();
138
147
}
@@ -144,12 +153,24 @@ private String getUrl(ConfigurationMaps configMaps) {
144
153
.orElseThrow (() -> new RuntimeException ("Missing required parameter: " + PARAMETER_URL ));
145
154
}
146
155
147
- private Optional <String > getAuthentication (ConfigurationMaps configMaps ) {
156
+ private Optional <String > getAuthorization (ConfigurationMaps configMaps ) {
148
157
return configMaps
149
158
.getString (PARAMETER_AUTHORIZATION )
150
159
.map (auth -> placeholderProcessor .process (auth , configMaps .getConfig ()));
151
160
}
152
161
162
+ private Optional <String > getContentType (ConfigurationMaps configMaps ) {
163
+ return configMaps
164
+ .getString (PARAMETER_CONTENT_TYPE )
165
+ .map (contentType -> placeholderProcessor .process (contentType , configMaps .getConfig ()));
166
+ }
167
+
168
+ private Optional <String > getAccept (ConfigurationMaps configMaps ) {
169
+ return configMaps
170
+ .getString (PARAMETER_ACCEPT )
171
+ .map (accept -> placeholderProcessor .process (accept , configMaps .getConfig ()));
172
+ }
173
+
153
174
private String getMethod (ConfigurationMaps configMaps ) {
154
175
return configMaps
155
176
.getString (PARAMETER_METHOD )
@@ -201,20 +222,38 @@ private boolean checkIfCodeMatches(String statusCode, String matchCodePattern) {
201
222
|| (statusCode .startsWith ("5" ) && matchCodePattern .contains ("5xx" ));
202
223
}
203
224
204
- private Map <String , Object > processResponse (ActivatedJob job , HttpResponse <String > response ) {
225
+ private Map <String , Object > processResponse (ActivatedJob job ,
226
+ HttpResponse <String > response , HttpRequest request ) {
205
227
final Map <String , Object > result = new java .util .HashMap <>();
206
-
207
228
int statusCode = response .statusCode ();
208
229
result .put ("statusCode" , statusCode );
209
-
210
- Optional .ofNullable (response .body ())
211
- .filter (body -> !body .isEmpty ())
212
- .map (this ::bodyToObject )
230
+ Optional <String > respBody = Optional .ofNullable (response .body ())
231
+ .filter (body -> !body .isEmpty ());
232
+ String acceptValue = request .headers ().firstValue ("Accept" ).orElse (null );
233
+ // If accepting plain text
234
+ if (hasContentTypeHeader (response .headers (), "text/plain" ) &&
235
+ ("text/plain" .equals (acceptValue ))) {
236
+ respBody .ifPresent (body -> result .put ("body" , body ));
237
+ } else {
238
+ // Assuming json by default
239
+ respBody .map (this ::bodyToObject )
213
240
.ifPresent (body -> result .put ("body" , body ));
214
-
241
+ }
215
242
return result ;
216
243
}
217
244
245
+ private boolean hasContentTypeHeader (HttpHeaders headers , String contentTypeHeader ) {
246
+ boolean hasContentTypeHeader = false ;
247
+ try {
248
+ hasContentTypeHeader = Optional .ofNullable (headers .firstValue ("Content-Type" ))
249
+ .filter (contentType -> contentType .get ().contains (contentTypeHeader ))
250
+ .isPresent ();
251
+ }catch (Exception e ) {
252
+ System .out .println (e .toString ());
253
+ }
254
+ return hasContentTypeHeader ;
255
+ }
256
+
218
257
private Object bodyToObject (String body ) {
219
258
try {
220
259
return objectMapper .readValue (body , Object .class );
0 commit comments