]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/plugins/org.argeo.jcr.ui.explorer/src/main/java/org/argeo/jcr/ui/explorer/commands/AddRemoteRepository.java
Improve secure logging
[lgpl/argeo-commons.git] / server / plugins / org.argeo.jcr.ui.explorer / src / main / java / org / argeo / jcr / ui / explorer / commands / AddRemoteRepository.java
index 4e120ac478a88afab8946ba058c490afe2f5a769..209dcdc6a1ebd68c7709b6573af6292db47a8f22 100644 (file)
@@ -9,6 +9,7 @@ import javax.jcr.RepositoryFactory;
 import javax.jcr.Session;
 import javax.jcr.SimpleCredentials;
 
+import org.argeo.ArgeoException;
 import org.argeo.eclipse.ui.ErrorFeedback;
 import org.argeo.jcr.ArgeoJcrConstants;
 import org.argeo.jcr.ArgeoNames;
@@ -53,29 +54,30 @@ public class AddRemoteRepository extends AbstractHandler implements
        public Object execute(ExecutionEvent event) throws ExecutionException {
                String uri = null;
                if (event.getParameters().containsKey(PARAM_REPOSITORY_URI)) {
+                       // FIXME remove this
                        uri = event.getParameter(PARAM_REPOSITORY_URI);
+                       if (uri == null)
+                               return null;
+
+                       try {
+                               Hashtable<String, String> params = new Hashtable<String, String>();
+                               params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, uri);
+                               // by default we use the URI as alias
+                               params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, uri);
+                               Repository repository = repositoryFactory.getRepository(params);
+                               bundleContext.registerService(Repository.class.getName(),
+                                               repository, params);
+                       } catch (Exception e) {
+                               ErrorFeedback.show("Cannot add remote repository " + uri, e);
+                       }
                } else {
                        RemoteRepositoryLoginDialog dlg = new RemoteRepositoryLoginDialog(
                                        Display.getDefault().getActiveShell());
                        if (dlg.open() == Dialog.OK) {
-                               uri = dlg.getUri();
+                               // uri = dlg.getUri();
                        }
                }
 
-               if (uri == null)
-                       return null;
-
-               try {
-                       Hashtable<String, String> params = new Hashtable<String, String>();
-                       params.put(ArgeoJcrConstants.JCR_REPOSITORY_URI, uri);
-                       // by default we use the URI as alias
-                       params.put(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS, uri);
-                       Repository repository = repositoryFactory.getRepository(params);
-                       bundleContext.registerService(Repository.class.getName(),
-                                       repository, params);
-               } catch (Exception e) {
-                       ErrorFeedback.show("Cannot add remote repository " + uri, e);
-               }
                return null;
        }
 
@@ -92,9 +94,8 @@ public class AddRemoteRepository extends AbstractHandler implements
        }
 
        class RemoteRepositoryLoginDialog extends TitleAreaDialog {
-               private String uri;
                private Text name;
-               private Text uriText;
+               private Text uri;
                private Text username;
                private Text password;
 
@@ -116,7 +117,7 @@ public class AddRemoteRepository extends AbstractHandler implements
                                        false));
                        setMessage("Login to remote repository", IMessageProvider.NONE);
                        name = createLT(composite, "Name", "remoteRepository");
-                       uriText = createLT(composite, "URI",
+                       uri = createLT(composite, "URI",
                                        "http://localhost:7070/org.argeo.jcr.webapp/remoting/node");
                        username = createLT(composite, "User", "");
                        password = createLP(composite, "Password");
@@ -138,7 +139,7 @@ public class AddRemoteRepository extends AbstractHandler implements
                void testConnection() {
                        Session session = null;
                        try {
-                               URI checkedUri = new URI(uriText.getText());
+                               URI checkedUri = new URI(uri.getText());
                                String checkedUriStr = checkedUri.toString();
 
                                Hashtable<String, String> params = new Hashtable<String, String>();
@@ -157,11 +158,11 @@ public class AddRemoteRepository extends AbstractHandler implements
                                                        username.getText(), pwd);
                                        session = repository.login(sc);
                                        MessageDialog.openInformation(getParentShell(), "Success",
-                                                       "Connection to " + uri + "successful");
+                                                       "Connection to '" + uri.getText() + "' successful");
                                }
                        } catch (Exception e) {
                                ErrorFeedback.show(
-                                               "Connection test failed for " + uriText.getText(), e);
+                                               "Connection test failed for " + uri.getText(), e);
                        } finally {
                                JcrUtils.logoutQuietly(session);
                        }
@@ -174,19 +175,26 @@ public class AddRemoteRepository extends AbstractHandler implements
                                Node home = JcrUtils.getUserHome(nodeSession);
                                Node remote = home.hasNode(ARGEO_REMOTE) ? home
                                                .getNode(ARGEO_REMOTE) : home.addNode(ARGEO_REMOTE);
+                               if (remote.hasNode(name.getText()))
+                                       throw new ArgeoException(
+                                                       "There is already a remote repository named "
+                                                                       + name.getText());
                                Node remoteRepository = remote.addNode(name.getText(),
                                                ArgeoTypes.ARGEO_REMOTE_REPOSITORY);
-                               remoteRepository.setProperty(ARGEO_URI, uriText.getText());
+                               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();
+                               MessageDialog.openInformation(
+                                               getParentShell(),
+                                               "Repository Added",
+                                               "Remote repository '" + username.getText() + "@"
+                                                               + uri.getText() + "' added");
+                               super.okPressed();
                        } catch (Exception e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               ErrorFeedback.show("Cannot add remote repository", e);
                        }
-                       uri = uriText.getText();
-                       super.okPressed();
                }
 
                /** Creates label and text. */
@@ -205,9 +213,5 @@ public class AddRemoteRepository extends AbstractHandler implements
                        text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                        return text;
                }
-
-               public String getUri() {
-                       return uri;
-               }
        }
 }