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