]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AddRemoteRepository.java
7f101b8bc36e246634b4226af35270449ce95cf2
[lgpl/argeo-commons.git] / 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.ArgeoJcrUtils;
31 import org.argeo.jcr.ArgeoNames;
32 import org.argeo.jcr.ArgeoTypes;
33 import org.argeo.jcr.JcrUtils;
34 import org.argeo.jcr.security.JcrKeyring;
35 import org.argeo.jcr.security.SecurityJcrUtils;
36 import org.argeo.jcr.ui.explorer.JcrExplorerConstants;
37 import org.eclipse.core.commands.AbstractHandler;
38 import org.eclipse.core.commands.ExecutionEvent;
39 import org.eclipse.core.commands.ExecutionException;
40 import org.eclipse.jface.dialogs.Dialog;
41 import org.eclipse.jface.dialogs.IMessageProvider;
42 import org.eclipse.jface.dialogs.MessageDialog;
43 import org.eclipse.jface.dialogs.TitleAreaDialog;
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.graphics.Point;
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.Control;
53 import org.eclipse.swt.widgets.Display;
54 import org.eclipse.swt.widgets.Label;
55 import org.eclipse.swt.widgets.Shell;
56 import org.eclipse.swt.widgets.Text;
57
58 /**
59 * Connect to a remote repository and, if successful publish it as an OSGi
60 * service.
61 */
62 public class AddRemoteRepository extends AbstractHandler implements
63 JcrExplorerConstants, ArgeoNames {
64
65 private RepositoryFactory repositoryFactory;
66 private JcrKeyring 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(JcrKeyring keyring) {
81 this.keyring = keyring;
82 }
83
84 class RemoteRepositoryLoginDialog extends TitleAreaDialog {
85 private Text name;
86 private Text uri;
87 private Text username;
88 private Text password;
89 private Button saveInKeyring;
90
91 public RemoteRepositoryLoginDialog(Shell parentShell) {
92 super(parentShell);
93 }
94
95 protected Point getInitialSize() {
96 return new Point(600, 400);
97 }
98
99 protected Control createDialogArea(Composite parent) {
100 Composite dialogarea = (Composite) super.createDialogArea(parent);
101 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
102 true));
103 Composite composite = new Composite(dialogarea, SWT.NONE);
104 composite.setLayout(new GridLayout(2, false));
105 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
106 false));
107 setMessage("Login to remote repository", IMessageProvider.NONE);
108 name = createLT(composite, "Name", "remoteRepository");
109 uri = createLT(composite, "URI",
110 "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
111 username = createLT(composite, "User", "");
112 password = createLP(composite, "Password");
113
114 saveInKeyring = createLC(composite, "Remember password", false);
115 parent.pack();
116 return composite;
117 }
118
119 @Override
120 protected void createButtonsForButtonBar(Composite parent) {
121 super.createButtonsForButtonBar(parent);
122 Button test = createButton(parent, 2, "Test", false);
123 test.addSelectionListener(new SelectionAdapter() {
124 public void widgetSelected(SelectionEvent arg0) {
125 testConnection();
126 }
127 });
128 }
129
130 void testConnection() {
131 Session session = null;
132 try {
133 URI checkedUri = new URI(uri.getText());
134 String checkedUriStr = checkedUri.toString();
135
136 Hashtable<String, String> params = new Hashtable<String, String>();
137 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
138 Repository repository = repositoryFactory.getRepository(params);
139 if (username.getText().trim().equals("")) {// anonymous
140 session = repository.login();
141 } else {
142 // FIXME use getTextChars() when upgrading to 3.7
143 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
144 char[] pwd = password.getText().toCharArray();
145 SimpleCredentials sc = new SimpleCredentials(
146 username.getText(), pwd);
147 session = repository.login(sc);
148 MessageDialog.openInformation(getParentShell(), "Success",
149 "Connection to '" + uri.getText() + "' successful");
150 }
151 } catch (Exception e) {
152 ErrorFeedback.show(
153 "Connection test failed for " + uri.getText(), e);
154 } finally {
155 JcrUtils.logoutQuietly(session);
156 }
157 }
158
159 @Override
160 protected void okPressed() {
161 try {
162 Session nodeSession = keyring.getSession();
163 Node home = ArgeoJcrUtils.getUserHome(nodeSession);
164
165 // FIXME better deal with non existing home dir
166 if (home == null)
167 home = SecurityJcrUtils.createUserHomeIfNeeded(nodeSession,
168 nodeSession.getUserID());
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 Node pwd = remoteRepository.addNode(ARGEO_PASSWORD);
181 pwd.getSession().save();
182 if (saveInKeyring.getSelection())
183 keyring.set(pwd.getPath(), password.getText().toCharArray());
184 keyring.getSession().save();
185 MessageDialog.openInformation(
186 getParentShell(),
187 "Repository Added",
188 "Remote repository '" + username.getText() + "@"
189 + uri.getText() + "' added");
190
191 super.okPressed();
192 } catch (Exception e) {
193 ErrorFeedback.show("Cannot add remote repository", e);
194 }
195 }
196
197 /** Creates label and text. */
198 protected Text createLT(Composite parent, String label, String initial) {
199 new Label(parent, SWT.NONE).setText(label);
200 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
201 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
202 text.setText(initial);
203 return text;
204 }
205
206 /** Creates label and check. */
207 protected Button createLC(Composite parent, String label,
208 Boolean initial) {
209 new Label(parent, SWT.NONE).setText(label);
210 Button check = new Button(parent, SWT.CHECK);
211 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
212 check.setSelection(initial);
213 return check;
214 }
215
216 protected Text createLP(Composite parent, String label) {
217 new Label(parent, SWT.NONE).setText(label);
218 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
219 | SWT.PASSWORD);
220 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
221 return text;
222 }
223 }
224 }