|
45 | 45 | import org.slf4j.Logger;
|
46 | 46 | import org.slf4j.LoggerFactory;
|
47 | 47 |
|
| 48 | +import com.gitblit.Constants.FederationProposalResult; |
48 | 49 | import com.gitblit.Constants.FederationRequest;
|
49 | 50 | import com.gitblit.FederationServlet;
|
50 | 51 | import com.gitblit.IStoredSettings;
|
@@ -82,7 +83,7 @@ public class FederationUtils {
|
82 | 83 | private static final SSLContext SSL_CONTEXT;
|
83 | 84 |
|
84 | 85 | private static final DummyHostnameVerifier HOSTNAME_VERIFIER;
|
85 |
| - |
| 86 | + |
86 | 87 | private static final Logger LOGGER = LoggerFactory.getLogger(FederationUtils.class);
|
87 | 88 |
|
88 | 89 | static {
|
@@ -181,22 +182,61 @@ public static List<FederationModel> getFederationRegistrations(IStoredSettings s
|
181 | 182 | return federationRegistrations;
|
182 | 183 | }
|
183 | 184 |
|
| 185 | + /** |
| 186 | + * Sends a federation poke to the Gitblit instance at remoteUrl. Pokes are |
| 187 | + * sent by an pulling Gitblit instance to an origin Gitblit instance as part |
| 188 | + * of the proposal process. This is to ensure that the pulling Gitblit |
| 189 | + * instance has an IP route to the origin instance. |
| 190 | + * |
| 191 | + * @param remoteUrl |
| 192 | + * the remote Gitblit instance to send a federation proposal to |
| 193 | + * @param proposal |
| 194 | + * a complete federation proposal |
| 195 | + * @return true if there is a route to the remoteUrl |
| 196 | + */ |
| 197 | + public static boolean poke(String remoteUrl) throws Exception { |
| 198 | + String url = FederationServlet.asFederationLink(remoteUrl, null, FederationRequest.POKE); |
| 199 | + Gson gson = new Gson(); |
| 200 | + String json = gson.toJson("POKE"); |
| 201 | + int status = writeJson(url, json); |
| 202 | + return status == HttpServletResponse.SC_OK; |
| 203 | + } |
| 204 | + |
184 | 205 | /**
|
185 | 206 | * Sends a federation proposal to the Gitblit instance at remoteUrl
|
186 | 207 | *
|
187 | 208 | * @param remoteUrl
|
188 | 209 | * the remote Gitblit instance to send a federation proposal to
|
189 | 210 | * @param proposal
|
190 | 211 | * a complete federation proposal
|
191 |
| - * @return true if the proposal was received |
| 212 | + * @return the federation proposal result code |
192 | 213 | */
|
193 |
| - public static boolean propose(String remoteUrl, FederationProposal proposal) throws Exception { |
| 214 | + public static FederationProposalResult propose(String remoteUrl, FederationProposal proposal) |
| 215 | + throws Exception { |
194 | 216 | String url = FederationServlet
|
195 | 217 | .asFederationLink(remoteUrl, null, FederationRequest.PROPOSAL);
|
196 | 218 | Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
197 | 219 | String json = gson.toJson(proposal);
|
198 | 220 | int status = writeJson(url, json);
|
199 |
| - return status == HttpServletResponse.SC_OK; |
| 221 | + switch (status) { |
| 222 | + case HttpServletResponse.SC_FORBIDDEN: |
| 223 | + // remote Gitblit Federation disabled |
| 224 | + return FederationProposalResult.FEDERATION_DISABLED; |
| 225 | + case HttpServletResponse.SC_BAD_REQUEST: |
| 226 | + // remote Gitblit did not receive any JSON data |
| 227 | + return FederationProposalResult.MISSING_DATA; |
| 228 | + case HttpServletResponse.SC_METHOD_NOT_ALLOWED: |
| 229 | + // remote Gitblit not accepting proposals |
| 230 | + return FederationProposalResult.NO_PROPOSALS; |
| 231 | + case HttpServletResponse.SC_NOT_ACCEPTABLE: |
| 232 | + // remote Gitblit failed to poke this Gitblit instance |
| 233 | + return FederationProposalResult.NO_POKE; |
| 234 | + case HttpServletResponse.SC_OK: |
| 235 | + // received |
| 236 | + return FederationProposalResult.ACCEPTED; |
| 237 | + default: |
| 238 | + return FederationProposalResult.ERROR; |
| 239 | + } |
200 | 240 | }
|
201 | 241 |
|
202 | 242 | /**
|
|
0 commit comments