]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.e4/src/org/argeo/cms/e4/jcr/handlers/AddRemoteRepository.java
Make Eclipse 4 UI auth more robust
[lgpl/argeo-commons.git] / org.argeo.cms.e4 / src / org / argeo / cms / e4 / jcr / handlers / 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.cms.e4.jcr.handlers;
17
18 import java.net.URI;
19 import java.util.Hashtable;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.jcr.Node;
24 import javax.jcr.Repository;
25 import javax.jcr.RepositoryFactory;
26 import javax.jcr.Session;
27 import javax.jcr.SimpleCredentials;
28
29 import org.argeo.cms.ArgeoNames;
30 import org.argeo.cms.ArgeoTypes;
31 import org.argeo.cms.e4.jcr.JcrBrowserView;
32 import org.argeo.eclipse.ui.EclipseUiException;
33 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
34 import org.argeo.jcr.JcrUtils;
35 import org.argeo.node.NodeConstants;
36 import org.argeo.node.NodeUtils;
37 import org.argeo.node.security.Keyring;
38 import org.eclipse.e4.core.di.annotations.Execute;
39 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
40 import org.eclipse.e4.ui.services.IServiceConstants;
41 import org.eclipse.jface.dialogs.Dialog;
42 import org.eclipse.jface.dialogs.IMessageProvider;
43 import org.eclipse.jface.dialogs.MessageDialog;
44 import org.eclipse.jface.dialogs.TitleAreaDialog;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.events.SelectionAdapter;
47 import org.eclipse.swt.events.SelectionEvent;
48 import org.eclipse.swt.graphics.Point;
49 import org.eclipse.swt.layout.GridData;
50 import org.eclipse.swt.layout.GridLayout;
51 import org.eclipse.swt.widgets.Button;
52 import org.eclipse.swt.widgets.Composite;
53 import org.eclipse.swt.widgets.Control;
54 import org.eclipse.swt.widgets.Display;
55 import org.eclipse.swt.widgets.Label;
56 import org.eclipse.swt.widgets.Shell;
57 import org.eclipse.swt.widgets.Text;
58
59 /**
60 * Connect to a remote repository and, if successful publish it as an OSGi
61 * service.
62 */
63 public class AddRemoteRepository implements ArgeoNames {
64
65 @Inject
66 private RepositoryFactory repositoryFactory;
67 @Inject
68 private Repository nodeRepository;
69 @Inject
70 private Keyring keyring;
71
72 @Execute
73 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part) {
74 JcrBrowserView view = (JcrBrowserView) part.getObject();
75 RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(Display.getDefault().getActiveShell());
76 if (dlg.open() == Dialog.OK) {
77 view.refresh(null);
78 }
79 }
80
81 // public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
82 // this.repositoryFactory = repositoryFactory;
83 // }
84 //
85 // public void setKeyring(Keyring keyring) {
86 // this.keyring = keyring;
87 // }
88 //
89 // public void setNodeRepository(Repository nodeRepository) {
90 // this.nodeRepository = nodeRepository;
91 // }
92
93 class RemoteRepositoryLoginDialog extends TitleAreaDialog {
94 private static final long serialVersionUID = 2234006887750103399L;
95 private Text name;
96 private Text uri;
97 private Text username;
98 private Text password;
99 private Button saveInKeyring;
100
101 public RemoteRepositoryLoginDialog(Shell parentShell) {
102 super(parentShell);
103 }
104
105 protected Point getInitialSize() {
106 return new Point(600, 400);
107 }
108
109 protected Control createDialogArea(Composite parent) {
110 Composite dialogarea = (Composite) super.createDialogArea(parent);
111 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
112 Composite composite = new Composite(dialogarea, SWT.NONE);
113 composite.setLayout(new GridLayout(2, false));
114 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
115 setMessage("Login to remote repository", IMessageProvider.NONE);
116 name = createLT(composite, "Name", "remoteRepository");
117 uri = createLT(composite, "URI", "http://localhost:7070/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(NodeConstants.LABELED_URI, checkedUriStr);
147 Repository repository = repositoryFactory.getRepository(params);
148 if (username.getText().trim().equals("")) {// anonymous
149 // FIXME make it more generic
150 session = repository.login("main");
151 } else {
152 // FIXME use getTextChars() when upgrading to 3.7
153 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412
154 char[] pwd = password.getText().toCharArray();
155 SimpleCredentials sc = new SimpleCredentials(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("Connection test failed for " + uri.getText(), e);
162 } finally {
163 JcrUtils.logoutQuietly(session);
164 }
165 }
166
167 @Override
168 protected void okPressed() {
169 Session nodeSession = null;
170 try {
171 nodeSession = nodeRepository.login();
172 Node home = NodeUtils.getUserHome(nodeSession);
173
174 Node remote = home.hasNode(ARGEO_REMOTE) ? home.getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE);
175 if (remote.hasNode(name.getText()))
176 throw new EclipseUiException("There is already a remote repository named " + name.getText());
177 Node remoteRepository = remote.addNode(name.getText(), 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() + '/' + ARGEO_PASSWORD;
183 keyring.set(pwdPath, password.getText().toCharArray());
184 }
185 nodeSession.save();
186 MessageDialog.openInformation(getParentShell(), "Repository Added",
187 "Remote repository '" + username.getText() + "@" + uri.getText() + "' added");
188
189 super.okPressed();
190 } catch (Exception e) {
191 ErrorFeedback.show("Cannot add remote repository", e);
192 } finally {
193 JcrUtils.logoutQuietly(nodeSession);
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, Boolean initial) {
208 new Label(parent, SWT.NONE).setText(label);
209 Button check = new Button(parent, SWT.CHECK);
210 check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
211 check.setSelection(initial);
212 return check;
213 }
214
215 protected Text createLP(Composite parent, String label) {
216 new Label(parent, SWT.NONE).setText(label);
217 Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER | SWT.PASSWORD);
218 text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
219 return text;
220 }
221 }
222 }