From 666573cd3467eff9637acf52d1034f91a8aab3ee Mon Sep 17 00:00:00 2001
From: Amanda Anganes <aanganes@mitre.org>
Date: Thu, 28 Mar 2013 12:37:18 -0400
Subject: [PATCH] Updated blacklist and client api for ui error handling

---
 .../openid/connect/web/BlacklistAPI.java      | 21 +++++++----
 .../mitre/openid/connect/web/ClientAPI.java   | 17 ++++++---
 .../src/main/webapp/resources/js/admin.js     | 33 +++++++++++++++--
 .../src/main/webapp/resources/js/client.js    | 35 ++++++++++++++++---
 4 files changed, 87 insertions(+), 19 deletions(-)

diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/BlacklistAPI.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/BlacklistAPI.java
index 527054915..c46901f4d 100644
--- a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/BlacklistAPI.java
+++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/BlacklistAPI.java
@@ -82,11 +82,13 @@ public class BlacklistAPI {
 		catch (JsonSyntaxException e) {
 			logger.error("addNewBlacklistedSite failed due to JsonSyntaxException: " + e.getStackTrace().toString());
 			m.put("code", HttpStatus.BAD_REQUEST);
-			return "httpCodeView";
+			m.put("entity", "An error occurred while processing your request. Contact a system administrator for assistance.");
+			return "jsonEntityView";
 		} catch (IllegalStateException e) {
 			logger.error("addNewBlacklistedSite failed due to IllegalStateException: " + e.getStackTrace().toString());
 			m.put("code", HttpStatus.BAD_REQUEST);
-			return "httpCodeView";
+			m.put("entity", "An error occurred while processing your request. Refresh and try again - if the problem persists, contact a system administrator for assistance.");
+			return "jsonEntityView";
 		}
 		
 		return "jsonEntityView";
@@ -112,11 +114,13 @@ public class BlacklistAPI {
 		catch (JsonSyntaxException e) {
 			logger.error("updateBlacklistedSite failed due to JsonSyntaxException: " + e.getStackTrace().toString());
 			m.put("code", HttpStatus.BAD_REQUEST);
-			return "httpCodeView";
+			m.put("entity", "An error occurred while processing your request. Contact a system administrator for assistance.");
+			return "jsonEntityView";
 		} catch (IllegalStateException e) {
 			logger.error("updateBlacklistedSite failed due to IllegalStateException: " + e.getStackTrace().toString());
 			m.put("code", HttpStatus.BAD_REQUEST);
-			return "httpCodeView";
+			m.put("entity", "An error occurred while processing your request. Refresh and try again - if the problem persists, contact a system administrator for assistance.");
+			return "jsonEntityView";
 		}
 		
 		
@@ -125,7 +129,8 @@ public class BlacklistAPI {
 		if (oldBlacklist == null) {
 			logger.error("updateBlacklistedSite failed; blacklist with id " + id + " could not be found");
 			m.put("code", HttpStatus.NOT_FOUND);
-			return "httpCodeView";
+			m.put("entity", "An error occurred while processing your request - the requested blacklisted site could not be found.");
+			return "jsonEntityView";
 		} else {
 			
 			BlacklistedSite newBlacklist = blacklistService.update(oldBlacklist, blacklist);
@@ -146,7 +151,8 @@ public class BlacklistAPI {
 		
 		if (blacklist == null) {
 			logger.error("deleteBlacklistedSite failed; blacklist with id " + id + " could not be found");
-			m.put("code", HttpStatus.NOT_FOUND);
+			m.put("entity", "An error occurred while processing your request - the requested blacklisted site could not be found.");
+			return "jsonEntityView";
 		} else {
 			m.put("code", HttpStatus.OK);
 			blacklistService.remove(blacklist);
@@ -164,7 +170,8 @@ public class BlacklistAPI {
 		if (blacklist == null) {
 			logger.error("getBlacklistedSite failed; blacklist with id " + id + " could not be found");
 			m.put("code", HttpStatus.NOT_FOUND);
-			return "httpCodeView";
+			m.put("entity", "An error occurred while processing your request - the requested blacklisted site could not be found.");
+			return "jsonEntityView";
 		} else {
 		
 			m.put("entity", blacklist);
diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java
index 1fd0bec06..3682ce41f 100644
--- a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java
+++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/ClientAPI.java
@@ -138,11 +138,13 @@ public class ClientAPI {
     	catch (JsonSyntaxException e) {
     		logger.error("apiAddClient failed due to JsonSyntaxException: " + e.getStackTrace().toString());
     		m.addAttribute("code", HttpStatus.BAD_REQUEST);
-			return "httpCodeView";
+    		m.addAttribute("entity", "An error occurred while processing your request. Contact a system administrator for assistance.");
+			return "jsonEntityView";
     	} catch (IllegalStateException e) {
     		logger.error("apiAddClient failed due to IllegalStateException: " + e.getStackTrace().toString());
     		m.addAttribute("code", HttpStatus.BAD_REQUEST);
-			return "httpCodeView";
+    		m.addAttribute("entity", "An error occurred while processing your request. Contact a system administrator for assistance.");
+			return "jsonEntityView";
 		}
     	
         // if they leave the client secret empty, force it to be generated
@@ -193,11 +195,13 @@ public class ClientAPI {
     	catch (JsonSyntaxException e) {
     		logger.error("apiUpdateClient failed due to JsonSyntaxException: " + e.getStackTrace().toString());
     		m.addAttribute("code", HttpStatus.BAD_REQUEST);
-			return "httpCodeView";
+    		m.addAttribute("entity", "An error occurred while processing your request. Contact a system administrator for assistance.");
+			return "jsonEntityView";
     	} catch (IllegalStateException e) {
     		logger.error("apiUpdateClient failed due to IllegalStateException: " + e.getStackTrace().toString());
     		m.addAttribute("code", HttpStatus.BAD_REQUEST);
-			return "httpCodeView";
+    		m.addAttribute("entity", "An error occurred while processing your request. Contact a system administrator for assistance.");
+			return "jsonEntityView";
 		}
 
         ClientDetailsEntity oldClient = clientService.getClientById(id);
@@ -205,7 +209,8 @@ public class ClientAPI {
         if (oldClient == null) {
         	logger.error("apiUpdateClient failed; client with id " + id + " could not be found.");
         	m.addAttribute("code", HttpStatus.NOT_FOUND);
-        	return "httpCodeView";
+        	m.addAttribute("entity", "An error occurred while processing your request. The requested client could not be found.");
+			return "jsonEntityView";
         }
         
         // if they leave the client secret empty, force it to be generated
@@ -247,6 +252,8 @@ public class ClientAPI {
 		if (client == null) {
 			logger.error("apiDeleteClient failed; client with id " + id + " could not be found.");
 			modelAndView.getModelMap().put("code", HttpStatus.NOT_FOUND);
+			modelAndView.getModelMap().put("entity", "An error occurred while processing your request. The requested client could not be found.");
+			return "jsonEntityView";
 		} else {
 			modelAndView.getModelMap().put("code", HttpStatus.OK);
 			clientService.deleteClient(client);
diff --git a/openid-connect-server/src/main/webapp/resources/js/admin.js b/openid-connect-server/src/main/webapp/resources/js/admin.js
index 725426136..8ad3d3f13 100644
--- a/openid-connect-server/src/main/webapp/resources/js/admin.js
+++ b/openid-connect-server/src/main/webapp/resources/js/admin.js
@@ -32,7 +32,25 @@ var ListWidgetChildView = Backbone.View.extend({
         "click .btn-delete":function (e) {
         	e.preventDefault();
             //this.$el.tooltip('delete');
-            this.model.destroy();
+            
+            this.model.destroy({         
+            	error:function (error, response) {
+            		console.log("An error occurred when deleting from a list widget");
+    
+					//Pull out the response text.
+					var responseText = JSON.parse(response.responseText);
+            		
+            		//Display an alert with an error message
+            		$('#modalAlert div.modal-body').html("<div class='alert alert-error'><strong>Warning!</strong>" + responseText + "</div>");
+            		
+        			 $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
+        				 "backdrop" : "static",
+        				 "keyboard" : true,
+        				 "show" : true // ensure the modal is shown immediately
+        			 });
+            	}
+            });
+            
         }
     },
 
@@ -252,8 +270,17 @@ var BlackListWidgetView = ListWidgetView.extend({
     			_self.collection.add(item);
     		},
     		error:function(error, response) {
-    			console.log("error: " + error);
-    			console.log("response: " + response);
+    			//Pull out the response text.
+				var responseText = JSON.parse(response.responseText);
+        		
+        		//Display an alert with an error message
+        		$('#modalAlert div.modal-body').html("<div class='alert alert-error'><strong>Warning!</strong>" + responseText + "</div>");
+        		
+    			 $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
+    				 "backdrop" : "static",
+    				 "keyboard" : true,
+    				 "show" : true // ensure the modal is shown immediately
+    			 });
     		}
     	});
 
diff --git a/openid-connect-server/src/main/webapp/resources/js/client.js b/openid-connect-server/src/main/webapp/resources/js/client.js
index e18b8fdf3..a8d3d5c0b 100644
--- a/openid-connect-server/src/main/webapp/resources/js/client.js
+++ b/openid-connect-server/src/main/webapp/resources/js/client.js
@@ -160,7 +160,22 @@ var ClientView = Backbone.View.extend({
                             app.clientListView.togglePlaceholder();
                         });
                     });
-                }
+                },
+                error:function (error, response) {
+            		console.log("An error occurred when deleting a client");
+    
+					//Pull out the response text.
+					var responseText = JSON.parse(response.responseText);
+            		
+            		//Display an alert with an error message
+            		$('#modalAlert div.modal-body').html("<div class='alert alert-error'><strong>Warning!</strong>" + responseText + "</div>");
+            		
+        			 $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
+        				 "backdrop" : "static",
+        				 "keyboard" : true,
+        				 "show" : true // ensure the modal is shown immediately
+        			 });
+            	}
             });
 
             app.clientListView.delegateEvents();
@@ -483,9 +498,21 @@ var ClientFormView = Backbone.View.extend({
                 app.clientList.add(_self.model);
                 app.navigate('admin/clients', {trigger:true});
             },
-            error:function (model,resp) {
-                console.error("Oops! The object didn't save correctly.",resp);
-            }
+            error:function (error, response) {
+        		console.log("An error occurred when deleting from a list widget");
+
+				//Pull out the response text.
+				var responseText = JSON.parse(response.responseText);
+        		
+        		//Display an alert with an error message
+        		$('#modalAlert div.modal-body').html("<div class='alert alert-error'><strong>Warning!</strong>" + responseText + "</div>");
+        		
+    			 $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
+    				 "backdrop" : "static",
+    				 "keyboard" : true,
+    				 "show" : true // ensure the modal is shown immediately
+    			 });
+        	}
         });
 
         return false;