From 8b76369fb44bfd863b27bcede453d676905f52e5 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 26 Oct 2011 17:12:50 -0400
Subject: [PATCH] Properly catch Not Allowed (405) and Unknown Request (501) errors

---
 src/com/gitblit/client/GitblitWorker.java  |    6 +++
 src/com/gitblit/client/GitblitClient.java  |   14 +++++--
 src/com/gitblit/client/GitblitPanel.java   |    1 
 src/com/gitblit/GitBlitException.java      |   15 +++++++
 src/com/gitblit/utils/JsonUtils.java       |   12 +++++
 src/com/gitblit/client/GitblitManager.java |    7 +++
 src/com/gitblit/client/Utils.java          |   15 +++++++
 7 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/src/com/gitblit/GitBlitException.java b/src/com/gitblit/GitBlitException.java
index 1111463..360f9f0 100644
--- a/src/com/gitblit/GitBlitException.java
+++ b/src/com/gitblit/GitBlitException.java
@@ -56,7 +56,20 @@
 			super(message);
 		}
 	}
-	
+
+	/**
+	 * Exception to indicate that the requested action has been disabled on the
+	 * Gitblit server.
+	 */
+	public static class NotAllowedException extends GitBlitException {
+
+		private static final long serialVersionUID = 1L;
+
+		public NotAllowedException(String message) {
+			super(message);
+		}
+	}
+
 	/**
 	 * Exception to indicate that the requested action can not be executed by
 	 * the server because it does not recognize the request type.
diff --git a/src/com/gitblit/client/GitblitClient.java b/src/com/gitblit/client/GitblitClient.java
index 761283e..9f4dd3e 100644
--- a/src/com/gitblit/client/GitblitClient.java
+++ b/src/com/gitblit/client/GitblitClient.java
@@ -23,7 +23,9 @@
 import java.util.Map;
 
 import com.gitblit.GitBlitException.ForbiddenException;
+import com.gitblit.GitBlitException.NotAllowedException;
 import com.gitblit.GitBlitException.UnauthorizedException;
+import com.gitblit.GitBlitException.UnknownRequestException;
 import com.gitblit.Keys;
 import com.gitblit.models.FederationModel;
 import com.gitblit.models.RepositoryModel;
@@ -78,21 +80,25 @@
 
 		try {
 			refreshUsers();
+			refreshSettings();
 			allowManagement = true;
 		} catch (UnauthorizedException e) {
 		} catch (ForbiddenException e) {
+		} catch (NotAllowedException e) {
+		} catch (UnknownRequestException e) {
 		} catch (IOException e) {
-			System.err.println(e.getMessage());
+			e.printStackTrace();
 		}
 
 		try {
-			refreshSettings();
 			refreshStatus();
 			allowAdministration = true;
 		} catch (UnauthorizedException e) {
 		} catch (ForbiddenException e) {
+		} catch (NotAllowedException e) {
+		} catch (UnknownRequestException e) {
 		} catch (IOException e) {
-			System.err.println(e.getMessage());
+			e.printStackTrace();
 		}
 
 	}
@@ -141,7 +147,7 @@
 		settings = RpcUtils.getSettings(url, account, password);
 		return settings;
 	}
-	
+
 	public ServerStatus refreshStatus() throws IOException {
 		status = RpcUtils.getStatus(url, account, password);
 		return status;
diff --git a/src/com/gitblit/client/GitblitManager.java b/src/com/gitblit/client/GitblitManager.java
index d902c59..524c213 100644
--- a/src/com/gitblit/client/GitblitManager.java
+++ b/src/com/gitblit/client/GitblitManager.java
@@ -65,6 +65,7 @@
 import org.eclipse.jgit.util.FS;
 
 import com.gitblit.Constants;
+import com.gitblit.GitBlitException.ForbiddenException;
 import com.gitblit.utils.StringUtils;
 
 /**
@@ -277,6 +278,12 @@
 					if (cause instanceof ConnectException) {
 						JOptionPane.showMessageDialog(GitblitManager.this, cause.getMessage(),
 								Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
+					} else if (cause instanceof ForbiddenException) {
+						JOptionPane
+								.showMessageDialog(
+										GitblitManager.this,
+										"This Gitblit server does not allow RPC Management or Administration",
+										Translation.get("gb.error"), JOptionPane.ERROR_MESSAGE);
 					} else {
 						Utils.showException(GitblitManager.this, t);
 					}
diff --git a/src/com/gitblit/client/GitblitPanel.java b/src/com/gitblit/client/GitblitPanel.java
index 1a24f71..9eb896b 100644
--- a/src/com/gitblit/client/GitblitPanel.java
+++ b/src/com/gitblit/client/GitblitPanel.java
@@ -683,6 +683,7 @@
 		dialog.setLocationRelativeTo(GitblitPanel.this);
 		dialog.setUsers(null, gitblit.getUsernames(), null);
 		dialog.setRepositories(gitblit.getRepositories());
+		dialog.setFederationSets(gitblit.getFederationSets(), null);
 		dialog.setVisible(true);
 		final RepositoryModel newRepository = dialog.getRepository();
 		final List<String> permittedUsers = dialog.getPermittedUsers();
diff --git a/src/com/gitblit/client/GitblitWorker.java b/src/com/gitblit/client/GitblitWorker.java
index 45baf03..5926a77 100644
--- a/src/com/gitblit/client/GitblitWorker.java
+++ b/src/com/gitblit/client/GitblitWorker.java
@@ -24,7 +24,9 @@
 
 import com.gitblit.Constants.RpcRequest;
 import com.gitblit.GitBlitException.ForbiddenException;
+import com.gitblit.GitBlitException.NotAllowedException;
 import com.gitblit.GitBlitException.UnauthorizedException;
+import com.gitblit.GitBlitException.UnknownRequestException;
 
 public abstract class GitblitWorker extends SwingWorker<Boolean, Void> {
 
@@ -59,6 +61,10 @@
 				Utils.explainForbidden(parent, request);
 			} else if (t instanceof UnauthorizedException) {
 				Utils.explainUnauthorized(parent, request);
+			} else if (t instanceof NotAllowedException) {
+				Utils.explainNotAllowed(parent, request);
+			} else if (t instanceof UnknownRequestException) {
+				Utils.explainNotAllowed(parent, request);
 			} else {
 				Utils.showException(parent, t);
 			}
diff --git a/src/com/gitblit/client/Utils.java b/src/com/gitblit/client/Utils.java
index ae81e7f..786eb9f 100644
--- a/src/com/gitblit/client/Utils.java
+++ b/src/com/gitblit/client/Utils.java
@@ -48,9 +48,16 @@
 		return table;
 	}
 
+	public static void explainNotAllowed(Component c, RpcRequest request) {
+		String msg = MessageFormat.format("The Gitblit server does not allow the request \"{0}\".",
+				request.name());
+		JOptionPane.showMessageDialog(c, msg, "Not Allowed", JOptionPane.ERROR_MESSAGE);
+	}
+
 	public static void explainForbidden(Component c, RpcRequest request) {
 		String msg = MessageFormat.format(
-				"The request \"{0}\" has been forbidden by the Gitblit server.", request.name());
+				"The request \"{0}\" has been forbidden for the account by the Gitblit server.",
+				request.name());
 		JOptionPane.showMessageDialog(c, msg, "Forbidden", JOptionPane.ERROR_MESSAGE);
 	}
 
@@ -60,6 +67,12 @@
 		JOptionPane.showMessageDialog(c, msg, "Unauthorized", JOptionPane.ERROR_MESSAGE);
 	}
 
+	public static void explainUnknown(Component c, RpcRequest request) {
+		String msg = MessageFormat.format(
+				"The request \"{0}\" is not recognized by the Gitblit server.", request.name());
+		JOptionPane.showMessageDialog(c, msg, "Unknown Request", JOptionPane.ERROR_MESSAGE);
+	}
+
 	public static void showException(Component c, Throwable t) {
 		StringWriter writer = new StringWriter();
 		t.printStackTrace(new PrintWriter(writer));
diff --git a/src/com/gitblit/utils/JsonUtils.java b/src/com/gitblit/utils/JsonUtils.java
index 0c78df9..5b53bf4 100644
--- a/src/com/gitblit/utils/JsonUtils.java
+++ b/src/com/gitblit/utils/JsonUtils.java
@@ -46,6 +46,7 @@
 import org.eclipse.jgit.util.Base64;
 
 import com.gitblit.GitBlitException.ForbiddenException;
+import com.gitblit.GitBlitException.NotAllowedException;
 import com.gitblit.GitBlitException.UnauthorizedException;
 import com.gitblit.GitBlitException.UnknownRequestException;
 import com.gitblit.models.RepositoryModel;
@@ -158,7 +159,7 @@
 		}
 		return gson().fromJson(json, type);
 	}
-	
+
 	/**
 	 * Reads a gson object from the specified url.
 	 * 
@@ -216,6 +217,12 @@
 			} else if (e.getMessage().indexOf("403") > -1) {
 				// requested url is forbidden by the requesting user
 				throw new ForbiddenException(url);
+			} else if (e.getMessage().indexOf("405") > -1) {
+				// requested url is not allowed by the server
+				throw new NotAllowedException(url);
+			} else if (e.getMessage().indexOf("501") > -1) {
+				// requested url is not recognized by the server
+				throw new UnknownRequestException(url);
 			}
 			throw e;
 		}
@@ -278,6 +285,9 @@
 			} else if (e.getMessage().indexOf("403") > -1) {
 				// requested url is forbidden by the requesting user
 				throw new ForbiddenException(url);
+			} else if (e.getMessage().indexOf("405") > -1) {
+				// requested url is not allowed by the server
+				throw new NotAllowedException(url);
 			} else if (e.getMessage().indexOf("501") > -1) {
 				// requested url is not recognized by the server
 				throw new UnknownRequestException(url);

--
Gitblit v1.9.1