James Moger
2012-01-05 cb285cbfddfc0b633d6b8cdb4dc0d2bd2b8b51ef
src/com/gitblit/FederationServlet.java
@@ -15,20 +15,25 @@
 */
package com.gitblit;
import java.io.File;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import com.gitblit.Constants.FederationRequest;
import com.gitblit.models.FederationModel;
import com.gitblit.models.FederationProposal;
import com.gitblit.models.TeamModel;
import com.gitblit.models.UserModel;
import com.gitblit.utils.FederationUtils;
import com.gitblit.utils.FileUtils;
import com.gitblit.utils.HttpUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.utils.TimeUtils;
@@ -90,7 +95,7 @@
         if (proposal == null) {
            return;
         }
         // reject proposal, if not receipt prohibited
         if (!GitBlit.getBoolean(Keys.federation.allowProposals, false)) {
            logger.error(MessageFormat.format("Rejected {0} federation proposal from {1}",
@@ -198,6 +203,58 @@
               }
            }
            result = users;
         } else if (FederationRequest.PULL_TEAMS.equals(reqType)) {
            // pull teams
            if (!GitBlit.self().validateFederationRequest(reqType, token)) {
               // invalid token to pull teams
               logger.warn(MessageFormat.format(
                     "Federation token from {0} not authorized to pull TEAMS",
                     request.getRemoteAddr()));
               response.sendError(HttpServletResponse.SC_FORBIDDEN);
               return;
            }
            List<String> teamnames = GitBlit.self().getAllTeamnames();
            List<TeamModel> teams = new ArrayList<TeamModel>();
            for (String teamname : teamnames) {
               TeamModel user = GitBlit.self().getTeamModel(teamname);
               teams.add(user);
            }
            result = teams;
         } else if (FederationRequest.PULL_SCRIPTS.equals(reqType)) {
            // pull scripts
            if (!GitBlit.self().validateFederationRequest(reqType, token)) {
               // invalid token to pull script
               logger.warn(MessageFormat.format(
                     "Federation token from {0} not authorized to pull SCRIPTS",
                     request.getRemoteAddr()));
               response.sendError(HttpServletResponse.SC_FORBIDDEN);
               return;
            }
            Map<String, String> scripts = new HashMap<String, String>();
            Set<String> names = new HashSet<String>();
            names.addAll(GitBlit.getStrings(Keys.groovy.preReceiveScripts));
            names.addAll(GitBlit.getStrings(Keys.groovy.postReceiveScripts));
            for (TeamModel team :  GitBlit.self().getAllTeams()) {
               names.addAll(team.preReceiveScripts);
               names.addAll(team.postReceiveScripts);
            }
            File scriptsFolder = GitBlit.getFileOrFolder(Keys.groovy.scriptsFolder, "groovy");
            for (String name : names) {
               File file = new File(scriptsFolder, name);
               if (!file.exists() && !file.getName().endsWith(".groovy")) {
                  file = new File(scriptsFolder, name + ".groovy");
               }
               if (file.exists()) {
                  // read the script
                  String content = FileUtils.readContent(file, "\n");
                  scripts.put(name, content);
               } else {
                  // missing script?!
                  logger.warn(MessageFormat.format("Failed to find push script \"{0}\"", name));
               }
            }
            result = scripts;
         }
      }