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