]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/commands/AddRemoteRepository.java
Add the ability to remove a JCR privilege from a given node
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / 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.eclipse.ui.workbench.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.dialogs.ErrorFeedback;
29 import org.argeo.eclipse.ui.workbench.CommandUtils;
30 import org.argeo.eclipse.ui.workbench.WorkbenchConstants;
31 import org.argeo.jcr.ArgeoJcrConstants;
32 import org.argeo.jcr.ArgeoNames;
33 import org.argeo.jcr.ArgeoTypes;
34 import org.argeo.jcr.JcrUtils;
35 import org.argeo.jcr.UserJcrUtils;
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 WorkbenchConstants, 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 static final long serialVersionUID = 2234006887750103399L;
92 private Text name;
93 private Text uri;
94 private Text username;
95 private Text password;
96 private Button saveInKeyring;
97
98 public RemoteRepositoryLoginDialog(Shell parentShell) {
99 super(parentShell);
100 }
101
102 protected Point getInitialSize() {
103 return new Point(600, 400);
104 }
105
106 protected Control createDialogArea(Composite parent) {
107 Composite dialogarea = (Composite) super.createDialogArea(parent);
108 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
109 true));
110 Composite composite = new Composite(dialogarea, SWT.NONE);
111 composite.setLayout(new GridLayout(2, false));
112 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
113 false));
114 setMessage("Login to remote repository", IMessageProvider.NONE);
115 name = createLT(composite, "Name", "remoteRepository");
116 uri = createLT(composite, "URI",
117 "http://localhost:7070/data/jcr/node");
118 username = createLT(composite, "User", "");
119 password = createLP(composite, "Password");
120
121 saveInKeyring = createLC(composite, "Remember password", false);
122 parent.pack();
123 return composite;
124 }
125
126 @Override
127 protected void createButtonsForButtonBar(Composite parent) {
128 super.createButtonsForButtonBar(parent);
129 Button test = createButton(parent, 2, "Test", false);
130 test.addSelectionListener(new SelectionAdapter() {
131 private static final long serialVersionUID = -1829962269440419560L;
132
133 public void widgetSelected(SelectionEvent arg0) {
134 testConnection();
135 }
136 });
137 }
138
139 void testConnection() {
140 Session session = null;
141 try {
142 URI checkedUri = new URI(uri.getText());
143 String checkedUriStr = checkedUri.toString();
144
145 Hashtable<String, String> params = new Hashtable<String, String>();
146 params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr);
147 Repository repository = repositoryFactory.getRepository(params);
148 if (username.getText().trim().equals("")) {// anonymous
149 session = repository.login();
150 } else {
151 // FIXME use getTextChars() when upgrading to 3.7
152 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
153 char[] pwd = password.getText().toCharArray();
154 SimpleCredentials sc = new SimpleCredentials(
155 username.getText(), pwd);
156 session = repository.login(sc, "main");
157 MessageDialog.openInformation(getParentShell(), "Success",
158 "Connection to '" + uri.getText() + "' successful");
159 }
160 } catch (Exception e) {
161 ErrorFeedback.show(
162 "Connection test failed for " + uri.getText(), e);
163 } finally {
164 JcrUtils.logoutQuietly(session);
165 }
166 }
167
168 @Override
169 protected void okPressed() {
170 Session nodeSession = null;
171 try {
172 nodeSession = nodeRepository.login();
173 Node home = UserJcrUtils.getUserHome(nodeSession);
174
175 Node remote = home.hasNode(ARGEO_REMOTE) ? home
176 .getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE);
177 if (remote.hasNode(name.getText()))
178 throw new ArgeoException(
179 "There is already a remote repository named "
180 + name.getText());
181 Node remoteRepository = remote.addNode(name.getText(),
182 ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
183 remoteRepository.setProperty(ARGEO_URI, uri.getText());
184 remoteRepository.setProperty(ARGEO_USER_ID, username.getText());
185 nodeSession.save();
186 if (saveInKeyring.getSelection()) {
187 String pwdPath = remoteRepository.getPath() + '/'
188 + ARGEO_PASSWORD;
189 keyring.set(pwdPath, password.getText().toCharArray());
190 }
191 nodeSession.save();
192 MessageDialog.openInformation(
193 getParentShell(),
194 "Repository Added",
195 "Remote repository '" + username.getText() + "@"
196 + uri.getText() + "' added");
197
198 super.okPressed();
199 } catch (Exception e) {
200 ErrorFeedback.show("Cannot add remote repository", e);
201 } finally {
202 JcrUtils.logoutQuietly(nodeSession);
203 }
204 }
205
206 /** Creates label and text. */
207 protected Text createLT(Composite parent, String label, String initial) {
208 new Label(parent, SWT.NONE).setText(label);
209 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
210 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
211 text.setText(initial);
212 return text;
213 }
214
215 /** Creates label and check. */
216 protected Button createLC(Composite parent, String label,
217 Boolean initial) {
218 new Label(parent, SWT.NONE).setText(label);
219 Button check = new Button(parent, SWT.CHECK);
220 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
221 check.setSelection(initial);
222 return check;
223 }
224
225 protected Text createLP(Composite parent, String label) {
226 new Label(parent, SWT.NONE).setText(label);
227 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER
228 | SWT.PASSWORD);
229 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
230 return text;
231 }
232 }
233 }