]> 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/commands/AddRepository.java
Improve UI dist
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / commands / AddRepository.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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.commands;
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.jcr.SlcNames;
38 import org.argeo.slc.repo.RepoConstants;
39 import org.argeo.util.security.Keyring;
40 import org.eclipse.core.commands.AbstractHandler;
41 import org.eclipse.core.commands.ExecutionEvent;
42 import org.eclipse.core.commands.ExecutionException;
43 import org.eclipse.jface.dialogs.Dialog;
44 import org.eclipse.jface.dialogs.IMessageProvider;
45 import org.eclipse.jface.dialogs.MessageDialog;
46 import org.eclipse.jface.dialogs.TitleAreaDialog;
47 import org.eclipse.swt.SWT;
48 import org.eclipse.swt.events.SelectionAdapter;
49 import org.eclipse.swt.events.SelectionEvent;
50 import org.eclipse.swt.graphics.Point;
51 import org.eclipse.swt.layout.GridData;
52 import org.eclipse.swt.layout.GridLayout;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Composite;
55 import org.eclipse.swt.widgets.Control;
56 import org.eclipse.swt.widgets.Display;
57 import org.eclipse.swt.widgets.Label;
58 import org.eclipse.swt.widgets.Shell;
59 import org.eclipse.swt.widgets.Text;
60
61 /**
62 * Connect to a remote repository.
63 */
64 public class AddRepository extends AbstractHandler implements ArgeoNames,
65 SlcNames {
66
67 private RepositoryFactory repositoryFactory;
68 private Repository nodeRepository;
69 private Keyring keyring;
70
71 public Object execute(ExecutionEvent event) throws ExecutionException {
72 RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
73 Display.getDefault().getActiveShell());
74 if (dlg.open() == Dialog.OK) {
75 }
76 return null;
77 }
78
79 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
80 this.repositoryFactory = repositoryFactory;
81 }
82
83 public void setKeyring(Keyring keyring) {
84 this.keyring = keyring;
85 }
86
87 public void setNodeRepository(Repository nodeRepository) {
88 this.nodeRepository = nodeRepository;
89 }
90
91 class RemoteRepositoryLoginDialog extends TitleAreaDialog {
92 private Text name;
93 private Text uri;
94 private Text username;
95 private Text password;
96 private Button saveInKeyring;
97
98 public RemoteRepositoryLoginDialog(Shell parentShell) {
99 super(parentShell);
100 }
101
102 protected Point getInitialSize() {
103 return new Point(600, 400);
104 }
105
106 protected Control createDialogArea(Composite parent) {
107 Composite dialogarea = (Composite) super.createDialogArea(parent);
108 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
109 true));
110 Composite composite = new Composite(dialogarea, SWT.NONE);
111 composite.setLayout(new GridLayout(2, false));
112 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
113 false));
114 setMessage("Login to remote repository", IMessageProvider.NONE);
115 name = createLT(composite, "Name", "Argeo.org Java Repository");
116 uri = createLT(composite, "URI",
117 "https://repo.argeo.org/org.argeo.jcr.webapp/remoting/java");
118 username = createLT(composite, "User", "");
119 password = createLP(composite, "Password");
120
121 saveInKeyring = createLC(composite, "Remember password", false);
122 parent.pack();
123 return composite;
124 }
125
126 @Override
127 protected void createButtonsForButtonBar(Composite parent) {
128 super.createButtonsForButtonBar(parent);
129 Button test = createButton(parent, 2, "Test", false);
130 test.addSelectionListener(new SelectionAdapter() {
131 public void widgetSelected(SelectionEvent arg0) {
132 testConnection();
133 }
134 });
135 }
136
137 void testConnection() {
138 Session session = null;
139 try {
140 if (uri.getText().startsWith("http")) {// http, https
141 URI checkedUri = new URI(uri.getText());
142 String checkedUriStr = checkedUri.toString();
143 Hashtable<String, String> params = new Hashtable<String, String>();
144 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
145 checkedUriStr);
146 Repository repository = ArgeoJcrUtils.getRepositoryByUri(
147 repositoryFactory, checkedUriStr);
148 if (username.getText().trim().equals("")) {// anonymous
149 session = repository.login();
150 } else {
151 // FIXME use getTextChars() when upgrading to 3.7
152 // see
153 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
154 char[] pwd = password.getText().toCharArray();
155 SimpleCredentials sc = new SimpleCredentials(
156 username.getText(), pwd);
157 session = repository.login(sc);
158 }
159 } else {// alias
160 Repository repository = ArgeoJcrUtils.getRepositoryByAlias(
161 repositoryFactory, uri.getText());
162 session = repository.login();
163 }
164 MessageDialog.openInformation(getParentShell(), "Success",
165 "Connection to '" + uri.getText() + "' successful");
166 } catch (Exception e) {
167 ErrorFeedback.show(
168 "Connection test failed for " + uri.getText(), e);
169 } finally {
170 JcrUtils.logoutQuietly(session);
171 }
172 }
173
174 @Override
175 protected void okPressed() {
176 try {
177 Session nodeSession = nodeRepository.login();
178 String reposPath = UserJcrUtils.getUserHome(nodeSession)
179 .getPath() + RepoConstants.REPOSITORIES_BASE_PATH;
180
181 Node repos = nodeSession.getNode(reposPath);
182 String repoNodeName = JcrUtils.replaceInvalidChars(name
183 .getText());
184 if (repos.hasNode(repoNodeName))
185 throw new ArgeoException(
186 "There is already a remote repository named "
187 + repoNodeName);
188 Node repoNode = repos.addNode(repoNodeName,
189 ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
190 repoNode.setProperty(ARGEO_URI, uri.getText());
191 repoNode.setProperty(ARGEO_USER_ID, username.getText());
192 repoNode.addMixin(NodeType.MIX_TITLE);
193 repoNode.setProperty(Property.JCR_TITLE, name.getText());
194 nodeSession.save();
195 if (saveInKeyring.getSelection()) {
196 String pwdPath = repoNode.getPath() + '/' + ARGEO_PASSWORD;
197 keyring.set(pwdPath, password.getText().toCharArray());
198 nodeSession.save();
199 }
200 MessageDialog.openInformation(getParentShell(),
201 "Repository Added",
202 "Remote repository " + uri.getText() + "' added");
203
204 super.okPressed();
205 } catch (Exception e) {
206 ErrorFeedback.show("Cannot add remote repository", e);
207 }
208 }
209
210 /** Creates label and text. */
211 protected Text createLT(Composite parent, String label, String initial) {
212 new Label(parent, SWT.NONE).setText(label);
213 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
214 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
215 text.setText(initial);
216 return text;
217 }
218
219 /** Creates label and check. */
220 protected Button createLC(Composite parent, String label,
221 Boolean initial) {
222 new Label(parent, SWT.NONE).setText(label);
223 Button check = new Button(parent, SWT.CHECK);
224 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
225 check.setSelection(initial);
226 return check;
227 }
228
229 protected Text createLP(Composite parent, String label) {
230 new Label(parent, SWT.NONE).setText(label);
231 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
232 | SWT.PASSWORD);
233 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
234 return text;
235 }
236 }
237 }