lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
commit | author | age
dd9ae7 1 /*
JM 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
18 import java.text.MessageFormat;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.wicket.PageParameters;
23 import org.apache.wicket.markup.html.basic.Label;
24 import org.apache.wicket.markup.html.form.Button;
25 import org.apache.wicket.markup.html.form.Form;
26 import org.apache.wicket.markup.html.form.TextField;
27 import org.apache.wicket.model.CompoundPropertyModel;
28
4aafd4 29 import com.gitblit.Constants.FederationProposalResult;
dd9ae7 30 import com.gitblit.GitBlit;
JM 31 import com.gitblit.models.FederationProposal;
32 import com.gitblit.models.RepositoryModel;
33 import com.gitblit.utils.FederationUtils;
34 import com.gitblit.utils.StringUtils;
35 import com.gitblit.wicket.RequiresAdminRole;
36 import com.gitblit.wicket.WicketUtils;
37 import com.gitblit.wicket.panels.RepositoriesPanel;
38
39 @RequiresAdminRole
d376ab 40 public class SendProposalPage extends RootSubPage {
dd9ae7 41
JM 42     public String myUrl;
43
44     public String destinationUrl;
45
46     public String message;
47
48     public SendProposalPage(PageParameters params) {
49         super(params);
50
a7571b 51         setupPage(getString("gb.sendProposal"), "");
dd9ae7 52         setStatelessHint(true);
JM 53
54         final String token = WicketUtils.getToken(params);
55
2179fb 56         myUrl = WicketUtils.getGitblitURL(getRequest());
dd9ae7 57         destinationUrl = "https://";
JM 58
59         // temporary proposal
60         FederationProposal proposal = GitBlit.self().createFederationProposal(myUrl, token);
61         if (proposal == null) {
62             error("Could not create federation proposal!", true);
63         }
64
65         CompoundPropertyModel<SendProposalPage> model = new CompoundPropertyModel<SendProposalPage>(
66                 this);
67
68         Form<SendProposalPage> form = new Form<SendProposalPage>("editForm", model) {
69             private static final long serialVersionUID = 1L;
70
71             @Override
72             protected void onSubmit() {
73                 // confirm a repository name was entered
74                 if (StringUtils.isEmpty(myUrl)) {
75                     error("Please enter your Gitblit url!");
76                     return;
77                 }
78                 if (StringUtils.isEmpty(destinationUrl)) {
79                     error("Please enter a destination url for your proposal!");
80                     return;
81                 }
4aafd4 82
dd9ae7 83                 // build new proposal
JM 84                 FederationProposal proposal = GitBlit.self().createFederationProposal(myUrl, token);
85                 proposal.url = myUrl;
86                 proposal.message = message;
87                 try {
4aafd4 88                     FederationProposalResult res = FederationUtils
JM 89                             .propose(destinationUrl, proposal);
90                     switch (res) {
91                     case ACCEPTED:
92                         info(MessageFormat.format("Proposal successfully received by {0}.",
93                                 destinationUrl));
dd9ae7 94                         setResponsePage(RepositoriesPage.class);
4aafd4 95                         break;
JM 96                     case NO_POKE:
97                         error(MessageFormat.format(
98                                 "Sorry, {0} could not find a Gitblit instance at {1}.",
99                                 destinationUrl, myUrl));
100                         break;
101                     case NO_PROPOSALS:
102                         error(MessageFormat.format(
103                                 "Sorry, {0} is not accepting proposals at this time.",
104                                 destinationUrl));
105                         break;
106                     case FEDERATION_DISABLED:
107                         error(MessageFormat
108                                 .format("Sorry, {0} is not configured to federate with any Gitblit instances.",
109                                         destinationUrl));
110                         break;
111                     case MISSING_DATA:
112                         error(MessageFormat.format("Sorry, {0} did not receive any proposal data!",
113                                 destinationUrl));
114                         break;
115                     case ERROR:
116                         error(MessageFormat.format(
117                                 "Sorry, {0} reports that an unexpected error occurred!",
118                                 destinationUrl));
119                         break;
dd9ae7 120                     }
JM 121                 } catch (Exception e) {
122                     if (!StringUtils.isEmpty(e.getMessage())) {
123                         error(e.getMessage());
124                     } else {
125                         error("Failed to send proposal!");
126                     }
127                 }
128             }
129         };
130         form.add(new TextField<String>("myUrl"));
131         form.add(new TextField<String>("destinationUrl"));
132         form.add(new TextField<String>("message"));
133         form.add(new Label("tokenType", proposal.tokenType.name()));
134         form.add(new Label("token", proposal.token));
135
136         form.add(new Button("save"));
137         Button cancel = new Button("cancel") {
138             private static final long serialVersionUID = 1L;
139
140             @Override
141             public void onSubmit() {
d376ab 142                 setResponsePage(FederationPage.class);
dd9ae7 143             }
JM 144         };
145         cancel.setDefaultFormProcessing(false);
146         form.add(cancel);
147         add(form);
148
149         List<RepositoryModel> repositories = new ArrayList<RepositoryModel>(
150                 proposal.repositories.values());
d376ab 151         RepositoriesPanel repositoriesPanel = new RepositoriesPanel("repositoriesPanel", false,
cb57ec 152                 repositories, false, getAccessRestrictions());
dd9ae7 153         add(repositoriesPanel);
JM 154     }
155 }