Validate request bodies in controller methods
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / CustomResponseEntityExceptionHandler.java
index a328e47..f4a873d 100644 (file)
@@ -21,6 +21,7 @@ package org.oransc.ric.portal.dashboard.controller;
 
 import java.lang.invoke.MethodHandles;
 
+import org.oransc.ric.portal.dashboard.exception.InvalidArgumentException;
 import org.oransc.ric.portal.dashboard.exception.StatsManagerException;
 import org.oransc.ric.portal.dashboard.exception.UnknownInstanceException;
 import org.slf4j.Logger;
@@ -81,10 +82,10 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio
         * @return A response entity with status code 502 and an unstructured message.
         */
        @ExceptionHandler({ RestClientResponseException.class })
-       public final ResponseEntity<String> handleProxyMethodException(Exception ex, WebRequest request) {
+       public final ResponseEntity<String> handleRestClientResponse(Exception ex, WebRequest request) {
                // Capture the full stack trace in the log.
                if (log.isErrorEnabled())
-                       log.error("handleProxyMethodException: request " + request.getDescription(false), ex);
+                       log.error("handleRestClientResponse: request " + request.getDescription(false), ex);
                if (ex instanceof HttpStatusCodeException) {
                        HttpStatusCodeException hsce = (HttpStatusCodeException) ex;
                        return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(hsce.getResponseBodyAsString());
@@ -104,9 +105,24 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio
         * @return A response entity with status code 400 and an unstructured message.
         */
        @ExceptionHandler({ UnknownInstanceException.class })
-       public final ResponseEntity<String> handleUnknownInstanceException(Exception ex, WebRequest request) {
-               log.warn("handleUnknownInstanceException: request {}, exception {}", request.getDescription(false),
-                               ex.toString());
+       public final ResponseEntity<String> handleUnknownInstance(Exception ex, WebRequest request) {
+               log.warn("handleUnknownInstance: request {}, exception {}", request.getDescription(false), ex.toString());
+               return ResponseEntity.badRequest().body(getShortExceptionMessage(ex));
+       }
+
+       /**
+        * Logs the error and generates a response when a REST controller method takes
+        * an InvalidArgumentException, an invalid JSON was sent.
+        * 
+        * @param ex
+        *                    The exception
+        * @param request
+        *                    The original request
+        * @return A response entity with status code 400 and an unstructured message.
+        */
+       @ExceptionHandler({ InvalidArgumentException.class })
+       public final ResponseEntity<String> handleInvalidArgument(Exception ex, WebRequest request) {
+               log.warn("handleInvalidArgument: request {}, exception {}", request.getDescription(false), ex.toString());
                return ResponseEntity.badRequest().body(getShortExceptionMessage(ex));
        }
 
@@ -121,8 +137,8 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio
         * @return A response entity with status code 400 and an unstructured message.
         */
        @ExceptionHandler({ StatsManagerException.class })
-       public final ResponseEntity<String> handleStatsManagerException(Exception ex, WebRequest request) {
-               log.warn("handleStatsManagerException: request {}, exception {}", request.getDescription(false), ex.toString());
+       public final ResponseEntity<String> handleStatsManager(Exception ex, WebRequest request) {
+               log.warn("handleStatsManager: request {}, exception {}", request.getDescription(false), ex.toString());
                return ResponseEntity.badRequest().body(getShortExceptionMessage(ex));
        }