Bugfix, handling of empty types
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / policyagentapi / PolicyAgentApiImpl.java
index c084443..30dcdc5 100644 (file)
@@ -50,6 +50,8 @@ import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.HttpServerErrorException;
 import org.springframework.web.client.RestTemplate;
 
 @Component("PolicyAgentApi")
@@ -103,14 +105,18 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             JsonArray schemas = JsonParser.parseString(rsp.getBody()).getAsJsonArray();
             for (JsonElement schema : schemas) {
                 JsonObject schemaObj = schema.getAsJsonObject();
-                String title = schemaObj.get("title").getAsString();
-                String schemaAsStr = schemaObj.toString();
-                PolicyType pt = new PolicyType(title, schemaAsStr);
-                result.add(pt);
+                if (schemaObj.get("title") != null) {
+                    String title = schemaObj.get("title").getAsString();
+                    String schemaAsStr = schemaObj.toString();
+                    PolicyType pt = new PolicyType(title, schemaAsStr);
+                    result.add(pt);
+                } else {
+                    logger.warn("Ignoring schema: {}", schemaObj);
+                }
             }
             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            return handleException(e);
         }
     }
 
@@ -132,7 +138,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             }
             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            return handleException(e);
         }
     }
 
@@ -158,7 +164,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             this.restTemplate.put(url, createJsonHttpEntity(json), uriVariables);
             return new ResponseEntity<>(HttpStatus.OK);
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            return handleException(e);
         }
     }
 
@@ -170,7 +176,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             this.restTemplate.delete(url, uriVariables);
             return new ResponseEntity<>(HttpStatus.OK);
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
+            return handleException(e);
         }
 
     }
@@ -200,7 +206,7 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
             }
             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
         } catch (Exception e) {
-            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            return handleException(e);
         }
     }
 
@@ -210,4 +216,15 @@ public class PolicyAgentApiImpl implements PolicyAgentApi {
         return new HttpEntity<>(content, headers);
     }
 
+    private ResponseEntity<String> handleException(Exception throwable) {
+        if (throwable instanceof HttpClientErrorException) {
+            HttpClientErrorException e = (HttpClientErrorException) throwable;
+            return new ResponseEntity<>(e.getMessage(), e.getStatusCode());
+        } else if (throwable instanceof HttpServerErrorException) {
+            HttpServerErrorException e = (HttpServerErrorException) throwable;
+            return new ResponseEntity<>(e.getResponseBodyAsString(), e.getStatusCode());
+        }
+        return new ResponseEntity<>(throwable.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+
 }