From ac74aa4ed8f0a3ff2c56ea98533ab1d0a5e6ca33 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 29 Sep 2011 16:56:34 +0000 Subject: [PATCH] Add repository with keyring git-svn-id: https://svn.argeo.org/commons/trunk@4766 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../META-INF/MANIFEST.MF | 4 + .../META-INF/spring/commands.xml | 1 + .../META-INF/spring/jcr.xml | 10 + .../META-INF/spring/osgi.xml | 5 + .../commands/AddRemoteRepository.java | 171 +++++++++++++++++- 5 files changed, 181 insertions(+), 10 deletions(-) diff --git a/server/plugins/org.argeo.jcr.ui.explorer/META-INF/MANIFEST.MF b/server/plugins/org.argeo.jcr.ui.explorer/META-INF/MANIFEST.MF index bea753bab..b903e0763 100644 --- a/server/plugins/org.argeo.jcr.ui.explorer/META-INF/MANIFEST.MF +++ b/server/plugins/org.argeo.jcr.ui.explorer/META-INF/MANIFEST.MF @@ -30,8 +30,12 @@ Import-Package: javax.jcr, org.argeo.eclipse.ui.specific, org.argeo.eclipse.ui.utils, org.argeo.jcr, + org.argeo.jcr.security, org.argeo.jcr.spring, + org.argeo.security.jcr, + org.argeo.security.ui, org.argeo.util, + org.argeo.util.crypto, org.eclipse.ui.forms, org.eclipse.ui.forms.editor, org.eclipse.ui.forms.events, diff --git a/server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/commands.xml b/server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/commands.xml index 938451929..8a2f071eb 100644 --- a/server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/commands.xml +++ b/server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/commands.xml @@ -23,6 +23,7 @@ + + + + + + + + + + + diff --git a/server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/osgi.xml b/server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/osgi.xml index 0aa6ff3a3..6771ecc56 100644 --- a/server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/osgi.xml +++ b/server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/osgi.xml @@ -16,4 +16,9 @@ + + + + \ No newline at end of file diff --git a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java index 9f12281d4..4e120ac47 100644 --- a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java +++ b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java @@ -1,38 +1,66 @@ package org.argeo.jcr.ui.explorer.commands; +import java.net.URI; import java.util.Hashtable; +import javax.jcr.Node; import javax.jcr.Repository; import javax.jcr.RepositoryFactory; +import javax.jcr.Session; +import javax.jcr.SimpleCredentials; import org.argeo.eclipse.ui.ErrorFeedback; -import org.argeo.eclipse.ui.dialogs.SingleValue; import org.argeo.jcr.ArgeoJcrConstants; +import org.argeo.jcr.ArgeoNames; +import org.argeo.jcr.ArgeoTypes; +import org.argeo.jcr.JcrUtils; +import org.argeo.jcr.security.JcrKeyring; import org.argeo.jcr.ui.explorer.JcrExplorerConstants; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IMessageProvider; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.dialogs.TitleAreaDialog; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; import org.osgi.framework.BundleContext; /** - * Connect to a remote repository and, if usccessful publish it as an OSGi + * Connect to a remote repository and, if successful publish it as an OSGi * service. */ public class AddRemoteRepository extends AbstractHandler implements - JcrExplorerConstants { + JcrExplorerConstants, ArgeoNames { private RepositoryFactory repositoryFactory; private BundleContext bundleContext; + private JcrKeyring keyring; + public Object execute(ExecutionEvent event) throws ExecutionException { - String uri; - if (event.getParameters().containsKey(PARAM_REPOSITORY_URI)) + String uri = null; + if (event.getParameters().containsKey(PARAM_REPOSITORY_URI)) { uri = event.getParameter(PARAM_REPOSITORY_URI); - else - uri = SingleValue - .ask("URI", - "Remote repository URI" - + " (e.g. http://localhost:7070/org.argeo.jcr.webapp/remoting/node)"); + } else { + RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog( + Display.getDefault().getActiveShell()); + if (dlg.open() == Dialog.OK) { + uri = dlg.getUri(); + } + } if (uri == null) return null; @@ -59,4 +87,127 @@ public class AddRemoteRepository extends AbstractHandler implements this.bundleContext = bundleContext; } + public void setKeyring(JcrKeyring keyring) { + this.keyring = keyring; + } + + class RemoteRepositoryLoginDialog extends TitleAreaDialog { + private String uri; + private Text name; + private Text uriText; + private Text username; + private Text password; + + public RemoteRepositoryLoginDialog(Shell parentShell) { + super(parentShell); + } + + protected Point getInitialSize() { + return new Point(600, 400); + } + + protected Control createDialogArea(Composite parent) { + Composite dialogarea = (Composite) super.createDialogArea(parent); + dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, + true)); + Composite composite = new Composite(dialogarea, SWT.NONE); + composite.setLayout(new GridLayout(2, false)); + composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, + false)); + setMessage("Login to remote repository", IMessageProvider.NONE); + name = createLT(composite, "Name", "remoteRepository"); + uriText = createLT(composite, "URI", + "http://localhost:7070/org.argeo.jcr.webapp/remoting/node"); + username = createLT(composite, "User", ""); + password = createLP(composite, "Password"); + parent.pack(); + return composite; + } + + @Override + protected void createButtonsForButtonBar(Composite parent) { + super.createButtonsForButtonBar(parent); + Button test = createButton(parent, 2, "Test", false); + test.addSelectionListener(new SelectionAdapter() { + public void widgetSelected(SelectionEvent arg0) { + testConnection(); + } + }); + } + + void testConnection() { + Session session = null; + try { + URI checkedUri = new URI(uriText.getText()); + String checkedUriStr = checkedUri.toString(); + + Hashtable params = new Hashtable(); + params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, checkedUriStr); + // by default we use the URI as alias + params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, + checkedUriStr); + Repository repository = repositoryFactory.getRepository(params); + if (username.getText().trim().equals("")) {// anonymous + session = repository.login(); + } else { + // FIXME use getTextChars() when upgrading to 3.7 + // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297412 + char[] pwd = password.getText().toCharArray(); + SimpleCredentials sc = new SimpleCredentials( + username.getText(), pwd); + session = repository.login(sc); + MessageDialog.openInformation(getParentShell(), "Success", + "Connection to " + uri + "successful"); + } + } catch (Exception e) { + ErrorFeedback.show( + "Connection test failed for " + uriText.getText(), e); + } finally { + JcrUtils.logoutQuietly(session); + } + } + + @Override + protected void okPressed() { + try { + Session nodeSession = keyring.getSession(); + Node home = JcrUtils.getUserHome(nodeSession); + Node remote = home.hasNode(ARGEO_REMOTE) ? home + .getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE); + Node remoteRepository = remote.addNode(name.getText(), + ArgeoTypes.ARGEO_REMOTE_REPOSITORY); + remoteRepository.setProperty(ARGEO_URI, uriText.getText()); + remoteRepository.setProperty(ARGEO_USER_ID, username.getText()); + Node pwd = remoteRepository.addNode(ARGEO_PASSWORD); + keyring.set(pwd.getPath(), password.getText().toCharArray()); + nodeSession.save(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + uri = uriText.getText(); + super.okPressed(); + } + + /** Creates label and text. */ + protected Text createLT(Composite parent, String label, String initial) { + new Label(parent, SWT.NONE).setText(label); + Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER); + text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + text.setText(initial); + return text; + } + + protected Text createLP(Composite parent, String label) { + new Label(parent, SWT.NONE).setText(label); + Text text = new Text(parent, SWT.SINGLE | SWT.LEAD | SWT.BORDER + | SWT.PASSWORD); + text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); + return text; + } + + public String getUri() { + return uri; + } + } } -- 2.39.2