+ public void setKeyring(JcrKeyring keyring) {
+ this.keyring = keyring;
+ }
+
+ class RemoteRepositoryLoginDialog extends TitleAreaDialog {
+ private Text name;
+ private Text uri;
+ 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");
+ uri = 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(uri.getText());
+ String checkedUriStr = checkedUri.toString();
+
+ Hashtable<String, String> params = new Hashtable<String, String>();
+ 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.getText() + "' successful");
+ }
+ } catch (Exception e) {
+ ErrorFeedback.show(
+ "Connection test failed for " + uri.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);
+ 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, 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) {
+ ErrorFeedback.show("Cannot add remote repository", e);
+ }
+ }
+
+ /** 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;
+ }
+ }