]> 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
fb80ac3cf5d7076821fd39178fb55eb6fff22962
[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.Property;
23 import javax.jcr.Repository;
24 import javax.jcr.RepositoryFactory;
25 import javax.jcr.Session;
26 import javax.jcr.SimpleCredentials;
27 import javax.jcr.nodetype.NodeType;
28
29 import org.argeo.ArgeoException;
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 ArgeoException(
117 "There is already a remote repository named "
118 + repoNodeName);
119 Node repoNode = repos.addNode(repoNodeName,
120 ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
121 repoNode.setProperty(ArgeoNames.ARGEO_URI, uri.getText());
122 repoNode.setProperty(ArgeoNames.ARGEO_USER_ID, username.getText());
123 repoNode.addMixin(NodeType.MIX_TITLE);
124 repoNode.setProperty(Property.JCR_TITLE, name.getText());
125 nodeSession.save();
126 if (saveInKeyring.getSelection()) {
127 String pwdPath = repoNode.getPath() + '/'
128 + ArgeoNames.ARGEO_PASSWORD;
129 keyring.set(pwdPath, password.getText().toCharArray());
130 nodeSession.save();
131 }
132 MessageDialog.openInformation(getShell(), "Repository Added",
133 "Remote repository " + uri.getText() + "' added");
134 } catch (Exception e) {
135 ErrorFeedback.show("Cannot add remote repository", e);
136 } finally {
137 JcrUtils.logoutQuietly(nodeSession);
138 }
139 return true;
140 }
141
142 private class DefineModelPage extends WizardPage {
143
144 public DefineModelPage() {
145 super("Main");
146 setTitle("Fill information to register a repository");
147 }
148
149 public void createControl(Composite parent) {
150
151 // main layout
152 Composite composite = new Composite(parent, SWT.NONE);
153 composite.setLayout(new GridLayout(2, false));
154 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
155 false));
156
157 // Create various fields
158 // setMessage("Login to remote repository", IMessageProvider.NONE);
159 name = createLT(composite, "Name", DEFAULT_NAME);
160 uri = createLT(composite, "URI", DEFAULT_URI);
161
162 final Button anonymousLogin = createLC(composite,
163 "log as anonymous", true);
164 anonymousLogin.addSelectionListener(new SelectionListener() {
165 public void widgetSelected(SelectionEvent e) {
166 if (anonymousLogin.getSelection()) {
167 username.setText(DEFAULT_USER_NAME);
168 password.setText("");
169 username.setEnabled(false);
170 password.setEnabled(false);
171 } else {
172 username.setText("");
173 password.setText("");
174 username.setEnabled(true);
175 password.setEnabled(true);
176 }
177 }
178
179 public void widgetDefaultSelected(SelectionEvent e) {
180 }
181 });
182
183 username = createLT(composite, "User", DEFAULT_USER_NAME);
184 password = createLP(composite, "Password");
185 saveInKeyring = createLC(composite, "Remember password", false);
186
187 if (DEFAULT_ANONYMOUS) {
188 username.setEnabled(false);
189 password.setEnabled(false);
190 }
191
192 Button test = createButton(composite, "Test");
193 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false, 2,
194 1);
195 gd.widthHint = 140;
196 test.setLayoutData(gd);
197
198 test.addSelectionListener(new SelectionAdapter() {
199 public void widgetSelected(SelectionEvent arg0) {
200 testConnection();
201 }
202 });
203
204 // Compulsory
205 setControl(composite);
206 }
207
208 /** Creates label and text. */
209 protected Text createLT(Composite parent, String label, String initial) {
210 new Label(parent, SWT.NONE).setText(label);
211 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
212 text.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
213 text.setText(initial);
214 return text;
215 }
216
217 /** Creates label and check. */
218 protected Button createLC(Composite parent, String label,
219 Boolean initial) {
220 new Label(parent, SWT.NONE).setText(label);
221 Button check = new Button(parent, SWT.CHECK);
222 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
223 check.setSelection(initial);
224 return check;
225 }
226
227 /** Creates a button with a label. */
228 protected Button createButton(Composite parent, String label) {
229 Button button = new Button(parent, SWT.PUSH);
230 button.setText(label);
231 button.setFont(JFaceResources.getDialogFont());
232 setButtonLayoutData(button);
233 return button;
234 }
235
236 /** Creates label and password field */
237 protected Text createLP(Composite parent, String label) {
238 new Label(parent, SWT.NONE).setText(label);
239 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
240 | SWT.PASSWORD);
241 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
242 return text;
243 }
244
245 }
246
247 void testConnection() {
248 Session session = null;
249 try {
250 if (uri.getText().startsWith("http")) {// http, https
251 URI checkedUri = new URI(uri.getText());
252 String checkedUriStr = checkedUri.toString();
253 Hashtable<String, String> params = new Hashtable<String, String>();
254 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
255 Repository repository = ArgeoJcrUtils.getRepositoryByUri(
256 repositoryFactory, checkedUriStr);
257 if (username.getText().trim().equals("")) {// anonymous
258 session = repository.login();
259 } else {
260 // FIXME use getTextChars() when upgrading to 3.7
261 // see
262 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
263 char[] pwd = password.getText().toCharArray();
264 SimpleCredentials sc = new SimpleCredentials(
265 username.getText(), pwd);
266 session = repository.login(sc);
267 }
268 } else {// alias
269 Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
270 repositoryFactory, uri.getText());
271 session = repository.login();
272 }
273 MessageDialog.openInformation(getShell(), "Success",
274 "Connection to '" + uri.getText() + "' successful");
275 } catch (Exception e) {
276 ErrorFeedback
277 .show("Connection test failed for " + uri.getText(), e);
278 } finally {
279 JcrUtils.logoutQuietly(session);
280 }
281 }
282 }