James Moger
2011-10-17 b2fde8f0dfe2d60b08724e92f919c1f68223101f
Consistent rpc result codes.
7 files modified
59 ■■■■ changed files
src/com/gitblit/Constants.java 2 ●●● patch | view | raw | blame | history
src/com/gitblit/GitBlitException.java 13 ●●●●● patch | view | raw | blame | history
src/com/gitblit/JsonServlet.java 6 ●●●●● patch | view | raw | blame | history
src/com/gitblit/RpcFilter.java 4 ●●●● patch | view | raw | blame | history
src/com/gitblit/RpcServlet.java 28 ●●●● patch | view | raw | blame | history
src/com/gitblit/client/GitblitManagerLauncher.java 2 ●●● patch | view | raw | blame | history
src/com/gitblit/utils/JsonUtils.java 4 ●●●● patch | view | raw | blame | history
src/com/gitblit/Constants.java
@@ -212,7 +212,7 @@
                    return type;
                }
            }
            return LIST_REPOSITORIES;
            return null;
        }
        public boolean exceeds(RpcRequest type) {
src/com/gitblit/GitBlitException.java
@@ -56,4 +56,17 @@
            super(message);
        }
    }
    /**
     * Exception to indicate that the requested action can not be executed by
     * the server because it does not recognize the request type.
     */
    public static class UnknownRequestException extends GitBlitException {
        private static final long serialVersionUID = 1L;
        public UnknownRequestException(String message) {
            super(message);
        }
    }
}
src/com/gitblit/JsonServlet.java
@@ -41,6 +41,12 @@
    private static final long serialVersionUID = 1L;
    protected final int forbiddenCode = HttpServletResponse.SC_FORBIDDEN;
    protected final int notAllowedCode = HttpServletResponse.SC_METHOD_NOT_ALLOWED;
    protected final int failureCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    protected final Logger logger;
    public JsonServlet() {
src/com/gitblit/RpcFilter.java
@@ -59,6 +59,10 @@
        String fullUrl = getFullUrl(httpRequest);
        RpcRequest requestType = RpcRequest.fromName(httpRequest.getParameter("req"));
        if (requestType == null) {
            httpResponse.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
            return;
        }
        boolean adminRequest = requestType.exceeds(RpcRequest.LIST_REPOSITORIES);
src/com/gitblit/RpcServlet.java
@@ -95,7 +95,11 @@
        } else if (RpcRequest.CREATE_REPOSITORY.equals(reqType)) {
            // create repository
            RepositoryModel model = deserialize(request, response, RepositoryModel.class);
            try {
            GitBlit.self().updateRepositoryModel(model.name, model, true);
            } catch (GitBlitException e) {
                response.setStatus(failureCode);
            }
        } else if (RpcRequest.EDIT_REPOSITORY.equals(reqType)) {
            // edit repository
            RepositoryModel model = deserialize(request, response, RepositoryModel.class);
@@ -104,7 +108,11 @@
            if (repoName == null) {
                repoName = model.name;
            }
            try {
            GitBlit.self().updateRepositoryModel(repoName, model, false);
            } catch (GitBlitException e) {
                response.setStatus(failureCode);
            }
        } else if (RpcRequest.DELETE_REPOSITORY.equals(reqType)) {
            // delete repository
            RepositoryModel model = deserialize(request, response, RepositoryModel.class);
@@ -112,7 +120,11 @@
        } else if (RpcRequest.CREATE_USER.equals(reqType)) {
            // create user
            UserModel model = deserialize(request, response, UserModel.class);
            try {
            GitBlit.self().updateUserModel(model.username, model, true);
            } catch (GitBlitException e) {
                response.setStatus(failureCode);
            }
        } else if (RpcRequest.EDIT_USER.equals(reqType)) {
            // edit user
            UserModel model = deserialize(request, response, UserModel.class);
@@ -121,11 +133,17 @@
            if (username == null) {
                username = model.username;
            }
            try {
            GitBlit.self().updateUserModel(username, model, false);
            } catch (GitBlitException e) {
                response.setStatus(failureCode);
            }
        } else if (RpcRequest.DELETE_USER.equals(reqType)) {
            // delete user
            UserModel model = deserialize(request, response, UserModel.class);
            GitBlit.self().deleteUser(model.username);
            if (!GitBlit.self().deleteUser(model.username)) {
                response.setStatus(failureCode);
            }
        } else if (RpcRequest.LIST_REPOSITORY_MEMBERS.equals(reqType)) {
            // get repository members
            RepositoryModel model = GitBlit.self().getRepositoryModel(objectName);
@@ -136,7 +154,7 @@
            Collection<String> names = deserialize(request, response, RpcUtils.NAMES_TYPE);
            List<String> users = new ArrayList<String>(names);
            if (!GitBlit.self().setRepositoryUsers(model, users)) {
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.setStatus(failureCode);
            }
        } else if (RpcRequest.LIST_FEDERATION_REGISTRATIONS.equals(reqType)) {
            // return the list of federation registrations
@@ -146,14 +164,14 @@
            if (GitBlit.canFederate()) {
                result = GitBlit.self().getFederationResultRegistrations();
            } else {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                response.sendError(notAllowedCode);
            }
        } else if (RpcRequest.LIST_FEDERATION_PROPOSALS.equals(reqType)) {
            // return the list of federation proposals
            if (GitBlit.canFederate()) {
                result = GitBlit.self().getPendingFederationProposals();
            } else {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                response.sendError(notAllowedCode);
            }
        } else if (RpcRequest.LIST_FEDERATION_SETS.equals(reqType)) {
            // return the list of federation sets
@@ -161,7 +179,7 @@
                String gitblitUrl = HttpUtils.getGitblitURL(request);
                result = GitBlit.self().getFederationSets(gitblitUrl);
            } else {
                response.sendError(HttpServletResponse.SC_FORBIDDEN);
                response.sendError(notAllowedCode);
            }
        } else if (RpcRequest.LIST_SETTINGS.equals(reqType)) {
            // return the server's settings
src/com/gitblit/client/GitblitManagerLauncher.java
@@ -44,7 +44,7 @@
        DownloadListener downloadListener = new DownloadListener() {
            @Override
            public void downloading(String name) {
                updateSplash(splash, Translation.get("gb.downloading") + " " + name + "...");
                updateSplash(splash, Translation.get("gb.downloading") + " " + name);
            }
        };
        
src/com/gitblit/utils/JsonUtils.java
@@ -47,6 +47,7 @@
import com.gitblit.GitBlitException.ForbiddenException;
import com.gitblit.GitBlitException.UnauthorizedException;
import com.gitblit.GitBlitException.UnknownRequestException;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.UserModel;
import com.google.gson.Gson;
@@ -277,6 +278,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("501") > -1) {
                // requested url is not recognized by the server
                throw new UnknownRequestException(url);
            }
            throw e;
        }