1919package com .netflix .conductor .contribs .http ;
2020
2121
22+ import java .io .IOException ;
2223import java .util .HashMap ;
2324import java .util .List ;
2425import java .util .Map ;
2526
2627import javax .inject .Inject ;
2728import javax .inject .Singleton ;
2829import javax .ws .rs .core .MediaType ;
30+ import javax .ws .rs .core .MultivaluedMap ;
2931
3032import org .slf4j .Logger ;
3133import org .slf4j .LoggerFactory ;
3234
3335import com .fasterxml .jackson .annotation .JsonInclude .Include ;
34- import com .fasterxml .jackson .core .JsonParseException ;
3536import com .fasterxml .jackson .core .type .TypeReference ;
3637import com .fasterxml .jackson .databind .DeserializationFeature ;
3738import com .fasterxml .jackson .databind .JsonNode ;
@@ -87,7 +88,6 @@ public HttpTask(String name, RestClientManager rcm, Configuration config) {
8788 logger .info ("HttpTask initialized..." );
8889 }
8990
90-
9191 @ Override
9292 public void start (Workflow workflow , Task task , WorkflowExecutor executor ) throws Exception {
9393 Object request = task .getInputData ().get (requestParameter );
@@ -116,8 +116,13 @@ public void start(Workflow workflow, Task task, WorkflowExecutor executor) throw
116116
117117 try {
118118
119- Object response = httpCall (input );
120- task .setStatus (Status .COMPLETED );
119+ HttpResponse response = httpCall (input );
120+ if (response .statusCode > 199 && response .statusCode < 300 ) {
121+ task .setStatus (Status .COMPLETED );
122+ } else {
123+ task .setReasonForIncompletion (response .body .toString ());
124+ task .setStatus (Status .FAILED );
125+ }
121126 if (response != null ) {
122127 task .getOutputData ().put ("response" , response );
123128 }
@@ -127,16 +132,15 @@ public void start(Workflow workflow, Task task, WorkflowExecutor executor) throw
127132 task .setReasonForIncompletion (e .getMessage ());
128133 task .getOutputData ().put ("response" , e .getMessage ());
129134 }
130-
131135 }
132136
133137 /**
134138 *
135139 * @param input HTTP Request
136- * @return Response if any, null otherwise
140+ * @return Response of the http call
137141 * @throws Exception If there was an error making http call
138142 */
139- protected Object httpCall (Input input ) throws Exception {
143+ protected HttpResponse httpCall (Input input ) throws Exception {
140144 Client client = rcm .getClient (input );
141145 Builder builder = client .resource (input .uri ).type (MediaType .APPLICATION_JSON );
142146 if (input .body != null ) {
@@ -146,35 +150,30 @@ protected Object httpCall(Input input) throws Exception {
146150 builder .header (e .getKey (), e .getValue ());
147151 });
148152
149- Object response = null ;
153+ HttpResponse response = new HttpResponse () ;
150154 try {
151-
152- String json = builder .accept (input .accept ).method (input .method , String .class );
153- logger .debug (json );
154- try {
155- JsonNode node = om .readTree (json );
156- if (node .isArray ()) {
157- response = om .convertValue (node , listOfObj );
158- } else if (node .isObject ()) {
159- response = om .convertValue (node , mapOfObj );
160- } else if (node .isNumber ()) {
161- response = om .convertValue (node , Double .class );
162- } else {
163- response = node .asText ();
164- }
165-
166- }catch (JsonParseException jpe ) {
167- logger .error (jpe .getMessage (), jpe );
168- response = json ;
155+
156+ ClientResponse cr = builder .accept (input .accept ).method (input .method , ClientResponse .class );
157+ if (cr .hasEntity ()) {
158+ response .body = extractBody (cr );
169159 }
160+ response .statusCode = cr .getStatus ();
161+ response .headers = cr .getHeaders ();
170162 return response ;
171-
163+
172164 } catch (UniformInterfaceException ex ) {
173165
174166 ClientResponse cr = ex .getResponse ();
175167
176168 if (cr .getStatus () > 199 && cr .getStatus () < 300 ) {
177- return cr .getEntity (String .class );
169+
170+ if (cr .hasEntity ()) {
171+ response .body = extractBody (cr );
172+ }
173+ response .headers = cr .getHeaders ();
174+ response .statusCode = cr .getStatus ();
175+ return response ;
176+
178177 }else {
179178 String reason = cr .getEntity (String .class );
180179 logger .error (reason , ex );
@@ -183,6 +182,30 @@ protected Object httpCall(Input input) throws Exception {
183182 }
184183 }
185184
185+ private Object extractBody (ClientResponse cr ) {
186+
187+ String json = cr .getEntity (String .class );
188+ logger .debug (json );
189+
190+ try {
191+
192+ JsonNode node = om .readTree (json );
193+ if (node .isArray ()) {
194+ return om .convertValue (node , listOfObj );
195+ } else if (node .isObject ()) {
196+ return om .convertValue (node , mapOfObj );
197+ } else if (node .isNumber ()) {
198+ return om .convertValue (node , Double .class );
199+ } else {
200+ return node .asText ();
201+ }
202+
203+ } catch (IOException jpe ) {
204+ logger .error (jpe .getMessage (), jpe );
205+ return json ;
206+ }
207+ }
208+
186209 @ Override
187210 public boolean execute (Workflow workflow , Task task , WorkflowExecutor executor ) throws Exception {
188211 if (task .getStatus ().equals (Status .SCHEDULED )) {
@@ -212,6 +235,20 @@ private static ObjectMapper objectMapper() {
212235 return om ;
213236 }
214237
238+ public static class HttpResponse {
239+
240+ public Object body ;
241+
242+ public MultivaluedMap <String , String > headers ;
243+
244+ public int statusCode ;
245+
246+ @ Override
247+ public String toString () {
248+ return "HttpResponse [body=" + body + ", headers=" + headers + ", statusCode=" + statusCode + "]" ;
249+ }
250+ }
251+
215252 public static class Input {
216253
217254 private String method ; //PUT, POST, GET, DELETE, OPTIONS, HEAD
0 commit comments