]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/wizards/RegisterRepoWizard.java
+ introduce advanced submenu for distribution view
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / wizards / RegisterRepoWizard.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 org.argeo.slc.client.ui.dist.wizards;
17
18 import java.net.URI;
19 import java.util.Hashtable;
20
21 import javax.jcr.Node;
22 import javax.jcr.NodeIterator;
23 import javax.jcr.Property;
24 import javax.jcr.Repository;
25 import javax.jcr.RepositoryFactory;
26 import javax.jcr.Session;
27 import javax.jcr.SimpleCredentials;
28 import javax.jcr.nodetype.NodeType;
29
30 import org.argeo.eclipse.ui.ErrorFeedback;
31 import org.argeo.jcr.ArgeoJcrConstants;
32 import org.argeo.jcr.ArgeoJcrUtils;
33 import org.argeo.jcr.ArgeoNames;
34 import org.argeo.jcr.ArgeoTypes;
35 import org.argeo.jcr.JcrUtils;
36 import org.argeo.jcr.UserJcrUtils;
37 import org.argeo.slc.SlcException;
38 import org.argeo.slc.repo.RepoConstants;
39 import org.argeo.util.security.Keyring;
40 import org.eclipse.jface.dialogs.MessageDialog;
41 import org.eclipse.jface.resource.JFaceResources;
42 import org.eclipse.jface.wizard.Wizard;
43 import org.eclipse.jface.wizard.WizardPage;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.events.SelectionAdapter;
46 import org.eclipse.swt.events.SelectionEvent;
47 import org.eclipse.swt.events.SelectionListener;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Label;
53 import org.eclipse.swt.widgets.Text;
54
55 /**
56 *
57 * Registers a new remote repository in the current Node.
58 *
59 */
60 public class RegisterRepoWizard extends Wizard {
61
62 // Business objects
63 private Keyring keyring;
64 private RepositoryFactory repositoryFactory;
65 private Repository nodeRepository;
66
67 // Pages
68 private DefineModelPage page;
69
70 // Widgets of model page
71 private Text name;
72 private Text uri;
73 private Text username;
74 private Text password;
75 private Button saveInKeyring;
76
77 // Default values
78 private final static String DEFAULT_NAME = "Argeo Public Repository";
79 private final static String DEFAULT_URI = "http://repo.argeo.org/data/pub/java";
80 private final static String DEFAULT_USER_NAME = "anonymous";
81 private final static boolean DEFAULT_ANONYMOUS = true;
82
83 public RegisterRepoWizard(Keyring keyring,
84 RepositoryFactory repositoryFactory, Repository nodeRepository) {
85 super();
86 this.keyring = keyring;
87 this.repositoryFactory = repositoryFactory;
88 this.nodeRepository = nodeRepository;
89 }
90
91 @Override
92 public void addPages() {
93 try {
94 page = new DefineModelPage();
95 addPage(page);
96 setWindowTitle("Register a new remote repository");
97 } catch (Exception e) {
98 throw new SlcException("Cannot add page to wizard ", e);
99 }
100 }
101
102 @Override
103 public boolean performFinish() {
104 if (!canFinish())
105 return false;
106
107 Session nodeSession = null;
108 try {
109 nodeSession = nodeRepository.login();
110 String reposPath = UserJcrUtils.getUserHome(nodeSession).getPath()
111 + RepoConstants.REPOSITORIES_BASE_PATH;
112
113 Node repos = nodeSession.getNode(reposPath);
114 String repoNodeName = JcrUtils.replaceInvalidChars(name.getText());
115 if (repos.hasNode(repoNodeName))
116 throw new SlcException(
117 "There is already a remote repository named "
118 + repoNodeName);
119
120 // check if the same URI has already been registered
121 NodeIterator ni = repos.getNodes();
122 while (ni.hasNext()) {
123 Node node = ni.nextNode();
124 if (node.isNodeType(ArgeoTypes.ARGEO_REMOTE_REPOSITORY)
125 && node.hasProperty(ArgeoNames.ARGEO_URI)
126 && node.getProperty(ArgeoNames.ARGEO_URI).getString()
127 .equals(uri.getText()))
128 throw new SlcException(
129 "This URI "
130 + uri.getText()
131 + " is already registered, "
132 + "for the time being, only one instance of a single "
133 + "repository at a time is implemented.");
134 }
135
136 Node repoNode = repos.addNode(repoNodeName,
137 ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
138 repoNode.setProperty(ArgeoNames.ARGEO_URI, uri.getText());
139 repoNode.setProperty(ArgeoNames.ARGEO_USER_ID, username.getText());
140 repoNode.addMixin(NodeType.MIX_TITLE);
141 repoNode.setProperty(Property.JCR_TITLE, name.getText());
142 nodeSession.save();
143 if (saveInKeyring.getSelection()) {
144 String pwdPath = repoNode.getPath() + '/'
145 + ArgeoNames.ARGEO_PASSWORD;
146 keyring.set(pwdPath, password.getText().toCharArray());
147 nodeSession.save();
148 }
149 MessageDialog.openInformation(getShell(), "Repository Added",
150 "Remote repository " + uri.getText() + "' added");
151 } catch (Exception e) {
152 ErrorFeedback.show("Cannot add remote repository", e);
153 } finally {
154 JcrUtils.logoutQuietly(nodeSession);
155 }
156 return true;
157 }
158
159 private class DefineModelPage extends WizardPage {
160
161 public DefineModelPage() {
162 super("Main");
163 setTitle("Fill information to register a repository");
164 }
165
166 public void createControl(Composite parent) {
167
168 // main layout
169 Composite composite = new Composite(parent, SWT.NONE);
170 composite.setLayout(new GridLayout(2, false));
171 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
172 false));
173
174 // Create various fields
175 // setMessage("Login to remote repository", IMessageProvider.NONE);
176 name = createLT(composite, "Name", DEFAULT_NAME);
177 uri = createLT(composite, "URI", DEFAULT_URI);
178
179 final Button anonymousLogin = createLC(composite,
180 "Log as anonymous", true);
181 anonymousLogin.addSelectionListener(new SelectionListener() {
182 public void widgetSelected(SelectionEvent e) {
183 if (anonymousLogin.getSelection()) {
184 username.setText(DEFAULT_USER_NAME);
185 password.setText("");
186 username.setEnabled(false);
187 password.setEnabled(false);
188 } else {
189 username.setText("");
190 password.setText("");
191 username.setEnabled(true);
192 password.setEnabled(true);
193 }
194 }
195
196 public void widgetDefaultSelected(SelectionEvent e) {
197 }
198 });
199
200 username = createLT(composite, "User", DEFAULT_USER_NAME);
201 password = createLP(composite, "Password");
202 saveInKeyring = createLC(composite, "Remember password", false);
203
204 if (DEFAULT_ANONYMOUS) {
205 username.setEnabled(false);
206 password.setEnabled(false);
207 }
208
209 Button test = createButton(composite, "Test");
210 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false, 2,
211 1);
212 gd.widthHint = 140;
213 test.setLayoutData(gd);
214
215 test.addSelectionListener(new SelectionAdapter() {
216 public void widgetSelected(SelectionEvent arg0) {
217 testConnection();
218 }
219 });
220
221 // Compulsory
222 setControl(composite);
223 }
224
225 /** Creates label and text. */
226 protected Text createLT(Composite parent, String label, String initial) {
227 new Label(parent, SWT.RIGHT).setText(label);
228 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
229 text.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
230 text.setText(initial);
231 return text;
232 }
233
234 /** Creates label and check. */
235 protected Button createLC(Composite parent, String label,
236 Boolean initial) {
237 new Label(parent, SWT.RIGHT).setText(label);
238 Button check = new Button(parent, SWT.CHECK);
239 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
240 check.setSelection(initial);
241 return check;
242 }
243
244 /** Creates a button with a label. */
245 protected Button createButton(Composite parent, String label) {
246 Button button = new Button(parent, SWT.PUSH);
247 button.setText(label);
248 button.setFont(JFaceResources.getDialogFont());
249 setButtonLayoutData(button);
250 return button;
251 }
252
253 /** Creates label and password field */
254 protected Text createLP(Composite parent, String label) {
255 new Label(parent, SWT.NONE).setText(label);
256 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
257 | SWT.PASSWORD);
258 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
259 return text;
260 }
261
262 }
263
264 void testConnection() {
265 Session session = null;
266 try {
267 if (uri.getText().startsWith("http")) {// http, https
268 URI checkedUri = new URI(uri.getText());
269 String checkedUriStr = checkedUri.toString();
270 Hashtable<String, String> params = new Hashtable<String, String>();
271 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
272 Repository repository = ArgeoJcrUtils.getRepositoryByUri(
273 repositoryFactory, checkedUriStr);
274 if (username.getText().trim().equals("")) {// anonymous
275 session = repository.login();
276 } else {
277 // FIXME use getTextChars() when upgrading to 3.7
278 // see
279 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
280 char[] pwd = password.getText().toCharArray();
281 SimpleCredentials sc = new SimpleCredentials(
282 username.getText(), pwd);
283 session = repository.login(sc);
284 }
285 } else {// alias
286 Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
287 repositoryFactory, uri.getText());
288 session = repository.login();
289 }
290 MessageDialog.openInformation(getShell(), "Success",
291 "Connection to '" + uri.getText() + "' successful");
292 } catch (Exception e) {
293 ErrorFeedback
294 .show("Connection test failed for " + uri.getText(), e);
295 } finally {
296 JcrUtils.logoutQuietly(session);
297 }
298 }
299 }