]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/wizards/RegisterRepoWizard.java
Work around issue with remote Jackrabbit default workspace.
[gpl/argeo-slc.git] / org.argeo.slc.client.ui.dist / src / 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.dialogs.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, RepositoryFactory repositoryFactory, Repository nodeRepository) {
84 super();
85 this.keyring = keyring;
86 this.repositoryFactory = repositoryFactory;
87 this.nodeRepository = nodeRepository;
88 }
89
90 @Override
91 public void addPages() {
92 try {
93 page = new DefineModelPage();
94 addPage(page);
95 setWindowTitle("Register a new remote repository");
96 } catch (Exception e) {
97 throw new SlcException("Cannot add page to wizard ", e);
98 }
99 }
100
101 @Override
102 public boolean performFinish() {
103 if (!canFinish())
104 return false;
105
106 Session nodeSession = null;
107 try {
108 nodeSession = nodeRepository.login();
109 String reposPath = UserJcrUtils.getUserHome(nodeSession).getPath() + RepoConstants.REPOSITORIES_BASE_PATH;
110
111 Node repos = nodeSession.getNode(reposPath);
112 String repoNodeName = JcrUtils.replaceInvalidChars(name.getText());
113 if (repos.hasNode(repoNodeName))
114 throw new SlcException("There is already a remote repository named " + repoNodeName);
115
116 // check if the same URI has already been registered
117 NodeIterator ni = repos.getNodes();
118 while (ni.hasNext()) {
119 Node node = ni.nextNode();
120 if (node.isNodeType(ArgeoTypes.ARGEO_REMOTE_REPOSITORY) && node.hasProperty(ArgeoNames.ARGEO_URI)
121 && node.getProperty(ArgeoNames.ARGEO_URI).getString().equals(uri.getText()))
122 throw new SlcException("This URI " + uri.getText() + " is already registered, "
123 + "for the time being, only one instance of a single "
124 + "repository at a time is implemented.");
125 }
126
127 Node repoNode = repos.addNode(repoNodeName, ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
128 repoNode.setProperty(ArgeoNames.ARGEO_URI, uri.getText());
129 repoNode.setProperty(ArgeoNames.ARGEO_USER_ID, username.getText());
130 repoNode.addMixin(NodeType.MIX_TITLE);
131 repoNode.setProperty(Property.JCR_TITLE, name.getText());
132 nodeSession.save();
133 if (saveInKeyring.getSelection()) {
134 String pwdPath = repoNode.getPath() + '/' + ArgeoNames.ARGEO_PASSWORD;
135 keyring.set(pwdPath, password.getText().toCharArray());
136 nodeSession.save();
137 }
138 MessageDialog.openInformation(getShell(), "Repository Added",
139 "Remote repository " + uri.getText() + "' added");
140 } catch (Exception e) {
141 ErrorFeedback.show("Cannot add remote repository", e);
142 } finally {
143 JcrUtils.logoutQuietly(nodeSession);
144 }
145 return true;
146 }
147
148 private class DefineModelPage extends WizardPage {
149 private static final long serialVersionUID = 874386824101995060L;
150
151 public DefineModelPage() {
152 super("Main");
153 setTitle("Fill information to register a repository");
154 }
155
156 public void createControl(Composite parent) {
157
158 // main layout
159 Composite composite = new Composite(parent, SWT.NONE);
160 composite.setLayout(new GridLayout(2, false));
161 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
162
163 // Create various fields
164 // setMessage("Login to remote repository", IMessageProvider.NONE);
165 name = createLT(composite, "Name", DEFAULT_NAME);
166 uri = createLT(composite, "URI", DEFAULT_URI);
167
168 final Button anonymousLogin = createLC(composite, "Log as anonymous", true);
169 anonymousLogin.addSelectionListener(new SelectionListener() {
170 private static final long serialVersionUID = 4874716406036981039L;
171
172 public void widgetSelected(SelectionEvent e) {
173 if (anonymousLogin.getSelection()) {
174 username.setText(DEFAULT_USER_NAME);
175 password.setText("");
176 username.setEnabled(false);
177 password.setEnabled(false);
178 } else {
179 username.setText("");
180 password.setText("");
181 username.setEnabled(true);
182 password.setEnabled(true);
183 }
184 }
185
186 public void widgetDefaultSelected(SelectionEvent e) {
187 }
188 });
189
190 username = createLT(composite, "User", DEFAULT_USER_NAME);
191 password = createLP(composite, "Password");
192 saveInKeyring = createLC(composite, "Remember password", false);
193
194 if (DEFAULT_ANONYMOUS) {
195 username.setEnabled(false);
196 password.setEnabled(false);
197 }
198
199 Button test = createButton(composite, "Test");
200 GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false, 2, 1);
201 gd.widthHint = 140;
202 test.setLayoutData(gd);
203
204 test.addSelectionListener(new SelectionAdapter() {
205 private static final long serialVersionUID = -4034851916548656293L;
206
207 public void widgetSelected(SelectionEvent arg0) {
208 testConnection();
209 }
210 });
211
212 // Compulsory
213 setControl(composite);
214 }
215
216 /** Creates label and text. */
217 protected Text createLT(Composite parent, String label, String initial) {
218 new Label(parent, SWT.RIGHT).setText(label);
219 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
220 text.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
221 text.setText(initial);
222 return text;
223 }
224
225 /** Creates label and check. */
226 protected Button createLC(Composite parent, String label, Boolean initial) {
227 new Label(parent, SWT.RIGHT).setText(label);
228 Button check = new Button(parent, SWT.CHECK);
229 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
230 check.setSelection(initial);
231 return check;
232 }
233
234 /** Creates a button with a label. */
235 protected Button createButton(Composite parent, String label) {
236 Button button = new Button(parent, SWT.PUSH);
237 button.setText(label);
238 button.setFont(JFaceResources.getDialogFont());
239 setButtonLayoutData(button);
240 return button;
241 }
242
243 /** Creates label and password field */
244 protected Text createLP(Composite parent, String label) {
245 new Label(parent, SWT.NONE).setText(label);
246 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER | SWT.PASSWORD);
247 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
248 return text;
249 }
250
251 }
252
253 void testConnection() {
254 Session session = null;
255 try {
256 if (uri.getText().startsWith("http")) {// http, https
257 URI checkedUri = new URI(uri.getText());
258 String checkedUriStr = checkedUri.toString();
259 Hashtable<String, String> params = new Hashtable<String, String>();
260 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
261 Repository repository = ArgeoJcrUtils.getRepositoryByUri(repositoryFactory, checkedUriStr);
262 // FIXME make it more generic
263 String defaultWorkspace = "main";
264 if (username.getText().trim().equals("")) {// anonymous
265 session = repository.login(defaultWorkspace);
266 } else {
267 char[] pwd = password.getTextChars();
268 SimpleCredentials sc = new SimpleCredentials(username.getText(), pwd);
269 session = repository.login(sc, defaultWorkspace);
270 }
271 } else {// alias
272 Repository repository = ArgeoJcrUtils.getRepositoryByAlias(repositoryFactory, uri.getText());
273 session = repository.login();
274 }
275 MessageDialog.openInformation(getShell(), "Success", "Connection to '" + uri.getText() + "' successful");
276 } catch (Exception e) {
277 ErrorFeedback.show("Connection test failed for " + uri.getText(), e);
278 } finally {
279 JcrUtils.logoutQuietly(session);
280 }
281 }
282 }