]> source.dussan.org Git - gitblit.git/commitdiff
Consistent rpc result codes.
authorJames Moger <james.moger@gitblit.com>
Mon, 17 Oct 2011 21:50:09 +0000 (17:50 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 17 Oct 2011 21:50:09 +0000 (17:50 -0400)
src/com/gitblit/Constants.java
src/com/gitblit/GitBlitException.java
src/com/gitblit/JsonServlet.java
src/com/gitblit/RpcFilter.java
src/com/gitblit/RpcServlet.java
src/com/gitblit/client/GitblitManagerLauncher.java
src/com/gitblit/utils/JsonUtils.java

index 6300fa2ed8cb9f74bb459f1a5860f27d1b9b8e1b..d6495e6fb35359a6ab6d5de2b93528e10ee1b076 100644 (file)
@@ -212,7 +212,7 @@ public class Constants {
                                        return type;\r
                                }\r
                        }\r
-                       return LIST_REPOSITORIES;\r
+                       return null;\r
                }\r
 \r
                public boolean exceeds(RpcRequest type) {\r
index af32003a45c2f0ff1a26f75cbaa65ff5e6009408..1111463f31333dd98048c41462af34fc23556772 100644 (file)
@@ -56,4 +56,17 @@ public class GitBlitException extends IOException {
                        super(message);\r
                }\r
        }\r
+       \r
+       /**\r
+        * Exception to indicate that the requested action can not be executed by\r
+        * the server because it does not recognize the request type.\r
+        */\r
+       public static class UnknownRequestException extends GitBlitException {\r
+\r
+               private static final long serialVersionUID = 1L;\r
+\r
+               public UnknownRequestException(String message) {\r
+                       super(message);\r
+               }\r
+       }\r
 }\r
index a7958969393b7eb42c56ee653179e29d69dba036..5433a61c0f8b459f685d5b8e03266271db36a2f4 100644 (file)
@@ -41,6 +41,12 @@ public abstract class JsonServlet extends HttpServlet {
 \r
        private static final long serialVersionUID = 1L;\r
 \r
+       protected final int forbiddenCode = HttpServletResponse.SC_FORBIDDEN;\r
+       \r
+       protected final int notAllowedCode = HttpServletResponse.SC_METHOD_NOT_ALLOWED;\r
+\r
+       protected final int failureCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;\r
+       \r
        protected final Logger logger;\r
 \r
        public JsonServlet() {\r
index f92dd962c4645684031510691ee28d1e148936c2..2786f2a4eab9393310c724d49ce789090d9a0d8b 100644 (file)
@@ -59,6 +59,10 @@ public class RpcFilter extends AuthenticationFilter {
 \r
                String fullUrl = getFullUrl(httpRequest);\r
                RpcRequest requestType = RpcRequest.fromName(httpRequest.getParameter("req"));\r
+               if (requestType == null) {\r
+                       httpResponse.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);                 \r
+                       return;\r
+               }\r
 \r
                boolean adminRequest = requestType.exceeds(RpcRequest.LIST_REPOSITORIES);\r
 \r
index c366a187c287eeea985198173081f887363a2752..53426dadb527245bfa137a8bda11e3575537d89c 100644 (file)
@@ -95,7 +95,11 @@ public class RpcServlet extends JsonServlet {
                } else if (RpcRequest.CREATE_REPOSITORY.equals(reqType)) {\r
                        // create repository\r
                        RepositoryModel model = deserialize(request, response, RepositoryModel.class);\r
-                       GitBlit.self().updateRepositoryModel(model.name, model, true);\r
+                       try {\r
+                               GitBlit.self().updateRepositoryModel(model.name, model, true);\r
+                       } catch (GitBlitException e) {\r
+                               response.setStatus(failureCode);\r
+                       }\r
                } else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {\r
                        // edit repository\r
                        RepositoryModel model = deserialize(request, response, RepositoryModel.class);\r
@@ -104,7 +108,11 @@ public class RpcServlet extends JsonServlet {
                        if (repoName == null) {\r
                                repoName = model.name;\r
                        }\r
-                       GitBlit.self().updateRepositoryModel(repoName, model, false);\r
+                       try {\r
+                               GitBlit.self().updateRepositoryModel(repoName, model, false);\r
+                       } catch (GitBlitException e) {\r
+                               response.setStatus(failureCode);\r
+                       }\r
                } else if (RpcRequest.DELETE_REPOSITORY.equals(reqType)) {\r
                        // delete repository\r
                        RepositoryModel model = deserialize(request, response, RepositoryModel.class);\r
@@ -112,7 +120,11 @@ public class RpcServlet extends JsonServlet {
                } else if (RpcRequest.CREATE_USER.equals(reqType)) {\r
                        // create user\r
                        UserModel model = deserialize(request, response, UserModel.class);\r
-                       GitBlit.self().updateUserModel(model.username, model, true);\r
+                       try {\r
+                               GitBlit.self().updateUserModel(model.username, model, true);\r
+                       } catch (GitBlitException e) {\r
+                               response.setStatus(failureCode);\r
+                       }\r
                } else if (RpcRequest.EDIT_USER.equals(reqType)) {\r
                        // edit user\r
                        UserModel model = deserialize(request, response, UserModel.class);\r
@@ -121,11 +133,17 @@ public class RpcServlet extends JsonServlet {
                        if (username == null) {\r
                                username = model.username;\r
                        }\r
-                       GitBlit.self().updateUserModel(username, model, false);\r
+                       try {\r
+                               GitBlit.self().updateUserModel(username, model, false);\r
+                       } catch (GitBlitException e) {\r
+                               response.setStatus(failureCode);\r
+                       }\r
                } else if (RpcRequest.DELETE_USER.equals(reqType)) {\r
                        // delete user\r
                        UserModel model = deserialize(request, response, UserModel.class);\r
-                       GitBlit.self().deleteUser(model.username);\r
+                       if (!GitBlit.self().deleteUser(model.username)) {\r
+                               response.setStatus(failureCode);\r
+                       }\r
                } else if (RpcRequest.LIST_REPOSITORY_MEMBERS.equals(reqType)) {\r
                        // get repository members\r
                        RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);\r
@@ -136,7 +154,7 @@ public class RpcServlet extends JsonServlet {
                        Collection<String> names = deserialize(request, response, RpcUtils.NAMES_TYPE);\r
                        List<String> users = new ArrayList<String>(names);\r
                        if (!GitBlit.self().setRepositoryUsers(model, users)) {\r
-                               response.setStatus(HttpServletResponse.SC_BAD_REQUEST);\r
+                               response.setStatus(failureCode);\r
                        }\r
                } else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {\r
                        // return the list of federation registrations\r
@@ -146,14 +164,14 @@ public class RpcServlet extends JsonServlet {
                        if (GitBlit.canFederate()) {\r
                                result = GitBlit.self().getFederationResultRegistrations();\r
                        } else {\r
-                               response.sendError(HttpServletResponse.SC_FORBIDDEN);\r
+                               response.sendError(notAllowedCode);\r
                        }\r
                } else if (RpcRequest.LIST_FEDERATION_PROPOSALS.equals(reqType)) {\r
                        // return the list of federation proposals\r
                        if (GitBlit.canFederate()) {\r
                                result = GitBlit.self().getPendingFederationProposals();\r
                        } else {\r
-                               response.sendError(HttpServletResponse.SC_FORBIDDEN);\r
+                               response.sendError(notAllowedCode);\r
                        }\r
                } else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {\r
                        // return the list of federation sets\r
@@ -161,13 +179,13 @@ public class RpcServlet extends JsonServlet {
                                String gitblitUrl = HttpUtils.getGitblitURL(request);\r
                                result = GitBlit.self().getFederationSets(gitblitUrl);\r
                        } else {\r
-                               response.sendError(HttpServletResponse.SC_FORBIDDEN);\r
+                               response.sendError(notAllowedCode);\r
                        }\r
                } else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {\r
                        // return the server's settings\r
-                       Properties settings = new Properties();                 \r
+                       Properties settings = new Properties();\r
                        List<String> keys = GitBlit.getAllKeys(null);\r
-                       for (String key:keys) {\r
+                       for (String key : keys) {\r
                                String value = GitBlit.getString(key, null);\r
                                if (value != null) {\r
                                        settings.put(key, value);\r
index 1d4670d67178d20dc1ae0124c2c1435aea23ba0a..9b6ee96b7e6bba64cbd1942361c6a315122fc288 100644 (file)
@@ -44,7 +44,7 @@ public class GitblitManagerLauncher {
                DownloadListener downloadListener = new DownloadListener() {\r
                        @Override\r
                        public void downloading(String name) {\r
-                               updateSplash(splash, Translation.get("gb.downloading") + " " + name + "...");                           \r
+                               updateSplash(splash, Translation.get("gb.downloading") + " " + name);                           \r
                        }\r
                };\r
                \r
index fee799015b463d05d9855e5939708f7ba2f4c405..0c78df95df0076c6afc9c43a5f8dd85576974fa9 100644 (file)
@@ -47,6 +47,7 @@ import org.eclipse.jgit.util.Base64;
 \r
 import com.gitblit.GitBlitException.ForbiddenException;\r
 import com.gitblit.GitBlitException.UnauthorizedException;\r
+import com.gitblit.GitBlitException.UnknownRequestException;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.UserModel;\r
 import com.google.gson.Gson;\r
@@ -277,6 +278,9 @@ public class JsonUtils {
                        } else if (e.getMessage().indexOf("403") > -1) {\r
                                // requested url is forbidden by the requesting user\r
                                throw new ForbiddenException(url);\r
+                       } else if (e.getMessage().indexOf("501") > -1) {\r
+                               // requested url is not recognized by the server\r
+                               throw new UnknownRequestException(url);\r
                        }\r
                        throw e;\r
                }\r