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