James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
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) {
6caa93 62             error(getString("gb.couldNotCreateFederationProposal"), true);
dd9ae7 63         }
JM 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)) {
6caa93 75                     error(getString("gb.pleaseSetGitblitUrl"));
dd9ae7 76                     return;
JM 77                 }
78                 if (StringUtils.isEmpty(destinationUrl)) {
6caa93 79                     error(getString("gb.pleaseSetDestinationUrl"));
dd9ae7 80                     return;
JM 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:
6caa93 92                         info(MessageFormat.format(getString("gb.proposalReceived"),
4aafd4 93                                 destinationUrl));
dd9ae7 94                         setResponsePage(RepositoriesPage.class);
4aafd4 95                         break;
JM 96                     case NO_POKE:
6caa93 97                         error(MessageFormat.format(getString("noGitblitFound"),
4aafd4 98                                 destinationUrl, myUrl));
JM 99                         break;
100                     case NO_PROPOSALS:
6caa93 101                         error(MessageFormat.format(getString("gb.noProposals"),
4aafd4 102                                 destinationUrl));
JM 103                         break;
104                     case FEDERATION_DISABLED:
105                         error(MessageFormat
6caa93 106                                 .format(getString("gb.noFederation"),
4aafd4 107                                         destinationUrl));
JM 108                         break;
109                     case MISSING_DATA:
6caa93 110                         error(MessageFormat.format(getString("gb.proposalFailed"),
4aafd4 111                                 destinationUrl));
JM 112                         break;
113                     case ERROR:
6caa93 114                         error(MessageFormat.format(getString("gb.proposalError"),
4aafd4 115                                 destinationUrl));
JM 116                         break;
dd9ae7 117                     }
JM 118                 } catch (Exception e) {
119                     if (!StringUtils.isEmpty(e.getMessage())) {
120                         error(e.getMessage());
121                     } else {
6caa93 122                         error(getString("gb.failedToSendProposal"));
dd9ae7 123                     }
JM 124                 }
125             }
126         };
127         form.add(new TextField<String>("myUrl"));
128         form.add(new TextField<String>("destinationUrl"));
129         form.add(new TextField<String>("message"));
130         form.add(new Label("tokenType", proposal.tokenType.name()));
131         form.add(new Label("token", proposal.token));
132
719798 133         form.add(new Button("save"));
JM 134         Button cancel = new Button("cancel") {
dd9ae7 135             private static final long serialVersionUID = 1L;
JM 136
137             @Override
138             public void onSubmit() {
d376ab 139                 setResponsePage(FederationPage.class);
dd9ae7 140             }
JM 141         };
142         cancel.setDefaultFormProcessing(false);
143         form.add(cancel);
144         add(form);
145
146         List<RepositoryModel> repositories = new ArrayList<RepositoryModel>(
147                 proposal.repositories.values());
d376ab 148         RepositoriesPanel repositoriesPanel = new RepositoriesPanel("repositoriesPanel", false,
cb57ec 149                 repositories, false, getAccessRestrictions());
dd9ae7 150         add(repositoriesPanel);
JM 151     }
152 }