Imporve JCR keyring and remote repository exploring
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 31 May 2012 16:28:37 +0000 (16:28 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 31 May 2012 16:28:37 +0000 (16:28 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@5319 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

server/plugins/org.argeo.jcr.ui.explorer/META-INF/spring/commands.xml
server/plugins/org.argeo.jcr.ui.explorer/plugin.xml
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/RemoveRemoteRepository.java [new file with mode: 0644]
server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/model/RemoteRepositoryNode.java
server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/security/JcrKeyring.java

index f5f8e9d3fe7f658def7fd0242be49ce59b52e3fd..d57c98974270f086a7e52828e7e956c2fa46e58f 100644 (file)
@@ -26,6 +26,9 @@
                <property name="keyring" ref="jcrKeyring" />
        </bean>
 
+       <bean id="removeRemoteRepository" class="org.argeo.jcr.ui.explorer.commands.RemoveRemoteRepository">
+       </bean>
+
        <bean id="addFolderNode" class="org.argeo.jcr.ui.explorer.commands.AddFolderNode"
                scope="prototype" />
 
index 9976ad27c14dc3dd5c39089c49456925339b08d4..d466a92c4bab2a740547b3136cb1a0467d506c40 100644 (file)
                                name="Repository URI">
                        </commandParameter>
                </command>    
+       <command
+                       defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
+                       id="org.argeo.jcr.ui.explorer.removeRemoteRepository"
+                       name="Remove remote JCR repository">
+               </command>    
          <command
                defaultHandler="org.argeo.eclipse.spring.SpringCommandHandler"
                id="org.argeo.jcr.ui.explorer.addFolderNode"
            </iterate>
         </visibleWhen>
      </command>
+     <command
+           commandId="org.argeo.jcr.ui.explorer.removeRemoteRepository"
+           icon="icons/remove.gif"
+           style="push">
+        <visibleWhen>
+           <iterate> 
+                               <or>
+                                       <instanceof
+                                               value="org.argeo.jcr.ui.explorer.model.RemoteRepositoryNode">
+                                       </instanceof>
+                               </or> 
+           </iterate>
+        </visibleWhen>
+     </command>
      <command
            commandId="org.argeo.jcr.ui.explorer.getNodeSize"
            icon="icons/getSize.gif"
index 818b6b6fee6ff7e205054d6d475fe5ce718cf416..6b5dfa8b0c490bafaa37059d6b38098b7e9e962b 100644 (file)
@@ -113,6 +113,7 @@ public class AddRemoteRepository extends AbstractHandler implements
                private Text uri;
                private Text username;
                private Text password;
+               private Button saveInKeyring;
 
                public RemoteRepositoryLoginDialog(Shell parentShell) {
                        super(parentShell);
@@ -136,6 +137,8 @@ public class AddRemoteRepository extends AbstractHandler implements
                                        "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
                        username = createLT(composite, "User", "");
                        password = createLP(composite, "Password");
+
+                       saveInKeyring = createLC(composite, "Remember password", false);
                        parent.pack();
                        return composite;
                }
@@ -188,6 +191,12 @@ public class AddRemoteRepository extends AbstractHandler implements
                        try {
                                Session nodeSession = keyring.getSession();
                                Node home = JcrUtils.getUserHome(nodeSession);
+
+                               // FIXME better deal with non existing home dir
+                               if (home == null)
+                                       home = JcrUtils.createUserHomeIfNeeded(nodeSession,
+                                                       nodeSession.getUserID());
+
                                Node remote = home.hasNode(ARGEO_REMOTE) ? home
                                                .getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE);
                                if (remote.hasNode(name.getText()))
@@ -199,13 +208,16 @@ public class AddRemoteRepository extends AbstractHandler implements
                                remoteRepository.setProperty(ARGEO_URI, uri.getText());
                                remoteRepository.setProperty(ARGEO_USER_ID, username.getText());
                                Node pwd = remoteRepository.addNode(ARGEO_PASSWORD);
-                               keyring.set(pwd.getPath(), password.getText().toCharArray());
-                               nodeSession.save();
+                               pwd.getSession().save();
+                               if (saveInKeyring.getSelection())
+                                       keyring.set(pwd.getPath(), password.getText().toCharArray());
+                               keyring.getSession().save();
                                MessageDialog.openInformation(
                                                getParentShell(),
                                                "Repository Added",
                                                "Remote repository '" + username.getText() + "@"
                                                                + uri.getText() + "' added");
+
                                super.okPressed();
                        } catch (Exception e) {
                                ErrorFeedback.show("Cannot add remote repository", e);
@@ -221,6 +233,16 @@ public class AddRemoteRepository extends AbstractHandler implements
                        return text;
                }
 
+               /** Creates label and check. */
+               protected Button createLC(Composite parent, String label,
+                               Boolean initial) {
+                       new Label(parent, SWT.NONE).setText(label);
+                       Button check = new Button(parent, SWT.CHECK);
+                       check.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+                       check.setSelection(initial);
+                       return check;
+               }
+
                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
diff --git a/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/RemoveRemoteRepository.java b/server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/RemoveRemoteRepository.java
new file mode 100644 (file)
index 0000000..cb3a629
--- /dev/null
@@ -0,0 +1,36 @@
+package org.argeo.jcr.ui.explorer.commands;
+
+import org.argeo.jcr.ui.explorer.model.RemoteRepositoryNode;
+import org.argeo.jcr.ui.explorer.views.GenericJcrBrowser;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.handlers.HandlerUtil;
+
+/** Remove a registered remote repository */
+public class RemoveRemoteRepository extends AbstractHandler {
+
+       public Object execute(ExecutionEvent event) throws ExecutionException {
+
+               ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
+                               .getActivePage().getSelection();
+
+               GenericJcrBrowser view = (GenericJcrBrowser) HandlerUtil
+                               .getActiveWorkbenchWindow(event).getActivePage()
+                               .findView(HandlerUtil.getActivePartId(event));
+
+               if (selection != null && !selection.isEmpty()
+                               && selection instanceof IStructuredSelection) {
+                       Object obj = ((IStructuredSelection) selection).getFirstElement();
+
+                       if (obj instanceof RemoteRepositoryNode) {
+                               ((RemoteRepositoryNode) obj).remove();
+                               view.refresh(null);
+                       }
+               }
+               return null;
+       }
+
+}
index b92540610894317bf1ba7c4357684e81a27c66b9..8f71ace0a51b6f002b057c7c82552b3a84425200 100644 (file)
@@ -23,6 +23,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.SimpleCredentials;
 
+import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.TreeParent;
 import org.argeo.jcr.ArgeoNames;
 import org.argeo.jcr.security.JcrKeyring;
@@ -56,4 +57,14 @@ public class RemoteRepositoryNode extends RepositoryNode {
                }
        }
 
+       public void remove() {
+               try {
+                       Node remoteNode = jcrKeyring.getSession().getNode(remoteNodePath);
+                       remoteNode.remove();
+                       remoteNode.getSession().save();
+               } catch (RepositoryException e) {
+                       throw new ArgeoException("Cannot remove " + remoteNodePath, e);
+               }
+       }
+
 }
index c09209a7cd22b45a148bfb287c9d22dd395de602..e10cccf60fc48c45dac74801654ba8fee0993ea4 100644 (file)
@@ -149,9 +149,9 @@ public class JcrKeyring extends AbstractKeyring implements ArgeoNames {
                }
        }
 
-       /** The node must already exist at this path */
+       /** The node must already exist at this path. Session is saved. */
        @Override
-       protected void encrypt(String path, InputStream unencrypted) {
+       protected synchronized void encrypt(String path, InputStream unencrypted) {
                // should be called first for lazy initialization
                SecretKey secretKey = getSecretKey();
 
@@ -164,6 +164,8 @@ public class JcrKeyring extends AbstractKeyring implements ArgeoNames {
                        Cipher cipher = createCipher();
                        if (!session.nodeExists(path))
                                throw new ArgeoException("No node at " + path);
+                       if (session.hasPendingChanges())
+                               session.save();
                        Node node = session.getNode(path);
                        node.addMixin(ArgeoTypes.ARGEO_ENCRYPTED);
                        SecureRandom random = new SecureRandom();
@@ -194,6 +196,7 @@ public class JcrKeyring extends AbstractKeyring implements ArgeoNames {
                        in = new CipherInputStream(unencrypted, cipher);
                        binary = session.getValueFactory().createBinary(in);
                        node.setProperty(Property.JCR_DATA, binary);
+                       session.save();
                } catch (Exception e) {
                        throw new ArgeoException("Cannot encrypt", e);
                } finally {
@@ -206,7 +209,7 @@ public class JcrKeyring extends AbstractKeyring implements ArgeoNames {
        }
 
        @Override
-       protected InputStream decrypt(String path) {
+       protected synchronized InputStream decrypt(String path) {
                // should be called first for lazy initialization
                SecretKey secretKey = getSecretKey();
 
@@ -258,16 +261,16 @@ public class JcrKeyring extends AbstractKeyring implements ArgeoNames {
                }
        }
 
-       public void changePassword(char[] oldPassword, char[] newPassword) {
-               // TODO Auto-generated method stub
-
+       public synchronized void changePassword(char[] oldPassword,
+                       char[] newPassword) {
+               // TODO decrypt with old pw / encrypt with new pw all argeo:encrypted
        }
 
-       public Session getSession() {
+       public synchronized Session getSession() {
                return session;
        }
 
-       public void setSession(Session session) {
+       public synchronized void setSession(Session session) {
                this.session = session;
        }