]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java
Add repository with keyring
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / AddRemoteRepository.java
1 package org.argeo.jcr.ui.explorer.commands;
2
3 import java.net.URI;
4 import java.util.Hashtable;
5
6 import javax.jcr.Node;
7 import javax.jcr.Repository;
8 import javax.jcr.RepositoryFactory;
9 import javax.jcr.Session;
10 import javax.jcr.SimpleCredentials;
11
12 import org.argeo.eclipse.ui.ErrorFeedback;
13 import org.argeo.jcr.ArgeoJcrConstants;
14 import org.argeo.jcr.ArgeoNames;
15 import org.argeo.jcr.ArgeoTypes;
16 import org.argeo.jcr.JcrUtils;
17 import org.argeo.jcr.security.JcrKeyring;
18 import org.argeo.jcr.ui.explorer.JcrExplorerConstants;
19 import org.eclipse.core.commands.AbstractHandler;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ExecutionException;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.dialogs.IMessageProvider;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.dialogs.TitleAreaDialog;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.Text;
39 import org.osgi.framework.BundleContext;
40
41 /**
42 * Connect to a remote repository and, if successful publish it as an OSGi
43 * service.
44 */
45 public class AddRemoteRepository extends AbstractHandler implements
46 JcrExplorerConstants, ArgeoNames {
47
48 private RepositoryFactory repositoryFactory;
49 private BundleContext bundleContext;
50
51 private JcrKeyring keyring;
52
53 public Object execute(ExecutionEvent event) throws ExecutionException {
54 String uri = null;
55 if (event.getParameters().containsKey(PARAM_REPOSITORY_URI)) {
56 uri = event.getParameter(PARAM_REPOSITORY_URI);
57 } else {
58 RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
59 Display.getDefault().getActiveShell());
60 if (dlg.open() == Dialog.OK) {
61 uri = dlg.getUri();
62 }
63 }
64
65 if (uri == null)
66 return null;
67
68 try {
69 Hashtable<String, String> params = new Hashtable<String, String>();
70 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, uri);
71 // by default we use the URI as alias
72 params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, uri);
73 Repository repository = repositoryFactory.getRepository(params);
74 bundleContext.registerService(Repository.class.getName(),
75 repository, params);
76 } catch (Exception e) {
77 ErrorFeedback.show("Cannot add remote repository " + uri, e);
78 }
79 return null;
80 }
81
82 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
83 this.repositoryFactory = repositoryFactory;
84 }
85
86 public void setBundleContext(BundleContext bundleContext) {
87 this.bundleContext = bundleContext;
88 }
89
90 public void setKeyring(JcrKeyring keyring) {
91 this.keyring = keyring;
92 }
93
94 class RemoteRepositoryLoginDialog extends TitleAreaDialog {
95 private String uri;
96 private Text name;
97 private Text uriText;
98 private Text username;
99 private Text password;
100
101 public RemoteRepositoryLoginDialog(Shell parentShell) {
102 super(parentShell);
103 }
104
105 protected Point getInitialSize() {
106 return new Point(600, 400);
107 }
108
109 protected Control createDialogArea(Composite parent) {
110 Composite dialogarea = (Composite) super.createDialogArea(parent);
111 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
112 true));
113 Composite composite = new Composite(dialogarea, SWT.NONE);
114 composite.setLayout(new GridLayout(2, false));
115 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
116 false));
117 setMessage("Login to remote repository", IMessageProvider.NONE);
118 name = createLT(composite, "Name", "remoteRepository");
119 uriText = createLT(composite, "URI",
120 "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
121 username = createLT(composite, "User", "");
122 password = createLP(composite, "Password");
123 parent.pack();
124 return composite;
125 }
126
127 @Override
128 protected void createButtonsForButtonBar(Composite parent) {
129 super.createButtonsForButtonBar(parent);
130 Button test = createButton(parent, 2, "Test", false);
131 test.addSelectionListener(new SelectionAdapter() {
132 public void widgetSelected(SelectionEvent arg0) {
133 testConnection();
134 }
135 });
136 }
137
138 void testConnection() {
139 Session session = null;
140 try {
141 URI checkedUri = new URI(uriText.getText());
142 String checkedUriStr = checkedUri.toString();
143
144 Hashtable<String, String> params = new Hashtable<String, String>();
145 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
146 // by default we use the URI as alias
147 params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS,
148 checkedUriStr);
149 Repository repository = repositoryFactory.getRepository(params);
150 if (username.getText().trim().equals("")) {// anonymous
151 session = repository.login();
152 } else {
153 // FIXME use getTextChars() when upgrading to 3.7
154 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
155 char[] pwd = password.getText().toCharArray();
156 SimpleCredentials sc = new SimpleCredentials(
157 username.getText(), pwd);
158 session = repository.login(sc);
159 MessageDialog.openInformation(getParentShell(), "Success",
160 "Connection to " + uri + "successful");
161 }
162 } catch (Exception e) {
163 ErrorFeedback.show(
164 "Connection test failed for " + uriText.getText(), e);
165 } finally {
166 JcrUtils.logoutQuietly(session);
167 }
168 }
169
170 @Override
171 protected void okPressed() {
172 try {
173 Session nodeSession = keyring.getSession();
174 Node home = JcrUtils.getUserHome(nodeSession);
175 Node remote = home.hasNode(ARGEO_REMOTE) ? home
176 .getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE);
177 Node remoteRepository = remote.addNode(name.getText(),
178 ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
179 remoteRepository.setProperty(ARGEO_URI, uriText.getText());
180 remoteRepository.setProperty(ARGEO_USER_ID, username.getText());
181 Node pwd = remoteRepository.addNode(ARGEO_PASSWORD);
182 keyring.set(pwd.getPath(), password.getText().toCharArray());
183 nodeSession.save();
184 } catch (Exception e) {
185 // TODO Auto-generated catch block
186 e.printStackTrace();
187 }
188 uri = uriText.getText();
189 super.okPressed();
190 }
191
192 /** Creates label and text. */
193 protected Text createLT(Composite parent, String label, String initial) {
194 new Label(parent, SWT.NONE).setText(label);
195 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
196 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
197 text.setText(initial);
198 return text;
199 }
200
201 protected Text createLP(Composite parent, String label) {
202 new Label(parent, SWT.NONE).setText(label);
203 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
204 | SWT.PASSWORD);
205 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
206 return text;
207 }
208
209 public String getUri() {
210 return uri;
211 }
212 }
213 }