Skip to content
Snippets Groups Projects
Commit 10ae08e6 authored by Mirco Nasuti's avatar Mirco Nasuti
Browse files

Handle workflow exceptions in a better way (still not that good)

parent 58291f55
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package org.hbp.mip.controllers; package org.hbp.mip.controllers;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -30,22 +31,26 @@ public class WorkflowApi { ...@@ -30,22 +31,26 @@ public class WorkflowApi {
@RequestBody @ApiParam(value = "Model to create", required = true) String query @RequestBody @ApiParam(value = "Model to create", required = true) String query
) throws Exception { ) throws Exception {
String results = ""; StringBuilder results = new StringBuilder();
int code = 0;
if(algo.equals("glr")) if(algo.equals("glr"))
{ {
results = sendPost("https://mip.humanbrainproject.eu/services/request", query); code = sendPost("https://mip.humanbrainproject.eu/services/request", query, results);
if(code != 200){
return new ResponseEntity<>(HttpStatus.valueOf(code));
}
} }
else if(algo.equals("anv")) else if(algo.equals("anv"))
{ {
results = ""; results.append("not implemented");
} }
return ResponseEntity.ok(results); return ResponseEntity.ok(results.toString());
} }
private static String sendPost(String url, String query) throws Exception { private static int sendPost(String url, String query, StringBuilder resp) throws Exception {
URL obj = new URL(url); URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
...@@ -62,18 +67,22 @@ public class WorkflowApi { ...@@ -62,18 +67,22 @@ public class WorkflowApi {
wr.flush(); wr.flush();
wr.close(); wr.close();
con.getResponseCode(); int respCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); if(respCode == 200)
String inputLine; {
StringBuilder response = new StringBuilder(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) { StringBuilder response = new StringBuilder();
response.append(inputLine);
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
resp.append(response.toString());
} }
in.close();
return response.toString(); return respCode;
} }
private static void allowInsecureConnection(HttpsURLConnection con) { private static void allowInsecureConnection(HttpsURLConnection con) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment