You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SendProposalPage.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import java.text.MessageFormat;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.apache.wicket.PageParameters;
  21. import org.apache.wicket.markup.html.basic.Label;
  22. import org.apache.wicket.markup.html.form.Button;
  23. import org.apache.wicket.markup.html.form.Form;
  24. import org.apache.wicket.markup.html.form.TextField;
  25. import org.apache.wicket.model.CompoundPropertyModel;
  26. import com.gitblit.Constants.FederationProposalResult;
  27. import com.gitblit.models.FederationProposal;
  28. import com.gitblit.models.RepositoryModel;
  29. import com.gitblit.utils.FederationUtils;
  30. import com.gitblit.utils.StringUtils;
  31. import com.gitblit.wicket.RequiresAdminRole;
  32. import com.gitblit.wicket.WicketUtils;
  33. import com.gitblit.wicket.panels.RepositoriesPanel;
  34. @RequiresAdminRole
  35. public class SendProposalPage extends RootSubPage {
  36. public String myUrl;
  37. public String destinationUrl;
  38. public String message;
  39. public SendProposalPage(PageParameters params) {
  40. super(params);
  41. setupPage(getString("gb.sendProposal"), "");
  42. setStatelessHint(true);
  43. final String token = WicketUtils.getToken(params);
  44. myUrl = WicketUtils.getGitblitURL(getRequest());
  45. destinationUrl = "https://";
  46. // temporary proposal
  47. FederationProposal proposal = app().federation().createFederationProposal(myUrl, token);
  48. if (proposal == null) {
  49. error(getString("gb.couldNotCreateFederationProposal"), true);
  50. }
  51. CompoundPropertyModel<SendProposalPage> model = new CompoundPropertyModel<SendProposalPage>(
  52. this);
  53. Form<SendProposalPage> form = new Form<SendProposalPage>("editForm", model) {
  54. private static final long serialVersionUID = 1L;
  55. @Override
  56. protected void onSubmit() {
  57. // confirm a repository name was entered
  58. if (StringUtils.isEmpty(myUrl)) {
  59. error(getString("gb.pleaseSetGitblitUrl"));
  60. return;
  61. }
  62. if (StringUtils.isEmpty(destinationUrl)) {
  63. error(getString("gb.pleaseSetDestinationUrl"));
  64. return;
  65. }
  66. // build new proposal
  67. FederationProposal proposal = app().federation().createFederationProposal(myUrl, token);
  68. proposal.url = myUrl;
  69. proposal.message = message;
  70. try {
  71. FederationProposalResult res = FederationUtils
  72. .propose(destinationUrl, proposal);
  73. switch (res) {
  74. case ACCEPTED:
  75. info(MessageFormat.format(getString("gb.proposalReceived"),
  76. destinationUrl));
  77. setResponsePage(RepositoriesPage.class);
  78. break;
  79. case NO_POKE:
  80. error(MessageFormat.format(getString("noGitblitFound"),
  81. destinationUrl, myUrl));
  82. break;
  83. case NO_PROPOSALS:
  84. error(MessageFormat.format(getString("gb.noProposals"),
  85. destinationUrl));
  86. break;
  87. case FEDERATION_DISABLED:
  88. error(MessageFormat
  89. .format(getString("gb.noFederation"),
  90. destinationUrl));
  91. break;
  92. case MISSING_DATA:
  93. error(MessageFormat.format(getString("gb.proposalFailed"),
  94. destinationUrl));
  95. break;
  96. case ERROR:
  97. error(MessageFormat.format(getString("gb.proposalError"),
  98. destinationUrl));
  99. break;
  100. }
  101. } catch (Exception e) {
  102. if (!StringUtils.isEmpty(e.getMessage())) {
  103. error(e.getMessage());
  104. } else {
  105. error(getString("gb.failedToSendProposal"));
  106. }
  107. }
  108. }
  109. };
  110. form.add(new TextField<String>("myUrl"));
  111. form.add(new TextField<String>("destinationUrl"));
  112. form.add(new TextField<String>("message"));
  113. form.add(new Label("tokenType", proposal.tokenType.name()));
  114. form.add(new Label("token", proposal.token));
  115. form.add(new Button("save"));
  116. Button cancel = new Button("cancel") {
  117. private static final long serialVersionUID = 1L;
  118. @Override
  119. public void onSubmit() {
  120. setResponsePage(FederationPage.class);
  121. }
  122. };
  123. cancel.setDefaultFormProcessing(false);
  124. form.add(cancel);
  125. add(form);
  126. List<RepositoryModel> repositories = new ArrayList<RepositoryModel>(
  127. proposal.repositories.values());
  128. RepositoriesPanel repositoriesPanel = new RepositoriesPanel("repositoriesPanel", false,
  129. false, repositories, false, getAccessRestrictions());
  130. add(repositoriesPanel);
  131. }
  132. @Override
  133. protected Class<? extends BasePage> getRootNavPageClass() {
  134. return FederationPage.class;
  135. }
  136. }