Example usage for org.apache.commons.vfs2 UserAuthenticationData getData

List of usage examples for org.apache.commons.vfs2 UserAuthenticationData getData

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 UserAuthenticationData getData.

Prototype

public char[] getData(final Type type) 

Source Link

Document

Gets a data from the collection.

Usage

From source file:maspack.fileutil.vfs.SimpleUserAuthenticator.java

public boolean equals(UserAuthenticator obj) {

    UserAuthenticationData data = obj.requestAuthentication(ALL_AUTH_DATA);

    try {//ww  w. j  ava 2  s  . c o  m
        String str = new String(data.getData(UserAuthenticationData.DOMAIN));
        if (!equals(str, domain)) {
            return false;
        }
    } catch (NullPointerException e) {
        // no domain
        if (domain != null) {
            return false;
        }
    }

    try {
        String str = new String(data.getData(UserAuthenticationData.USERNAME));
        if (!equals(str, username)) {
            return false;
        }
    } catch (NullPointerException e) {
        // no username
        if (username != null) {
            return false;
        }
    }

    try {
        String str = new String(data.getData(UserAuthenticationData.PASSWORD));

        if (!equals(str, password)) {
            return false;
        }
    } catch (Exception e) {
        // no password
        if (password != null) {
            return false;
        }
    }

    return true;
}

From source file:maspack.fileutil.vfs.EncryptedUserAuthenticator.java

public boolean equals(UserAuthenticator obj) {

    UserAuthenticationData data = obj.requestAuthentication(ALL_AUTH_DATA);

    String str = new String(data.getData(UserAuthenticationData.DOMAIN));
    if (!equals(str, domain)) {
        return false;
    }/*from   w ww.  j a v  a2  s.  c o  m*/

    str = new String(data.getData(UserAuthenticationData.USERNAME));
    if (!equals(str, username)) {
        return false;
    }

    str = new String(data.getData(UserAuthenticationData.PASSWORD));
    try {
        // encrypt password
        str = getCryptor().encrypt(str);
        if (!equals(str, encryptedPassword)) {
            return false;
        }
    } catch (Exception e) {
    }

    return true;
}

From source file:org.pentaho.vfs.ui.VfsFileChooserDialog.java

public FileObject open(Shell applicationShell, String[] schemeRestrictions, String initialScheme,
        boolean showFileScheme, String fileName, String[] fileFilters, String[] fileFilterNames,
        boolean returnUserAuthenticatedFile, int fileDialogMode, boolean showLocation, boolean showCustomUI) {

    this.fileDialogMode = fileDialogMode;
    this.fileFilters = fileFilters;
    this.fileFilterNames = fileFilterNames;
    this.applicationShell = applicationShell;
    this.showFileScheme = showFileScheme;
    this.initialScheme = initialScheme;
    this.schemeRestrictions = schemeRestrictions;
    this.showLocation = showLocation;
    this.showCustomUI = showCustomUI;

    FileObject tmpInitialFile = initialFile;

    if (defaultInitialFile != null && rootFile == null) {
        try {//from w  ww.j a  v a2 s.  co m
            rootFile = defaultInitialFile.getFileSystem().getRoot();
            initialFile = defaultInitialFile;
        } catch (FileSystemException ignored) {
            // well we tried
        }
    }

    createDialog(applicationShell);

    if (!showLocation) {
        comboPanel.setParent(fakeShell);
    } else {
        comboPanel.setParent(customUIPanel);
    }

    if (!showCustomUI) {
        customUIPanel.setParent(fakeShell);
    } else {
        customUIPanel.setParent(dialog);
    }

    // create our file chooser tool bar, contains parent folder combo and various controls
    createToolbarPanel(dialog);
    // create our vfs browser component
    createVfsBrowser(dialog);
    populateCustomUIPanel(dialog);
    if (fileDialogMode == VFS_DIALOG_SAVEAS) {
        createFileNamePanel(dialog, fileName);
    } else {
        // create file filter panel
        createFileFilterPanel(dialog);
    }
    // create our ok/cancel buttons
    createButtonPanel(dialog);

    initialFile = tmpInitialFile;
    // set the initial file selection
    try {
        if (initialFile != null || rootFile != null) {
            vfsBrowser.selectTreeItemByFileObject(initialFile != null ? initialFile : rootFile, true);
            updateParentFileCombo(initialFile != null ? initialFile : rootFile);
            setSelectedFile(initialFile != null ? initialFile : rootFile);
            openFileCombo.setText(
                    initialFile != null ? initialFile.getName().getURI() : rootFile.getName().getURI());
        }
    } catch (FileSystemException e) {
        MessageBox box = new MessageBox(dialog.getShell());
        box.setText(Messages.getString("VfsFileChooserDialog.error")); //$NON-NLS-1$
        box.setMessage(e.getMessage());
        box.open();
    }

    // set the size and show the dialog
    int height = 550;
    int width = 800;
    dialog.setSize(width, height);
    Rectangle bounds = dialog.getDisplay().getPrimaryMonitor().getClientArea();
    int x = (bounds.width - width) / 2;
    int y = (bounds.height - height) / 2;
    dialog.setLocation(x, y);
    dialog.open();

    if (rootFile != null && fileDialogMode == VFS_DIALOG_SAVEAS) {
        if (!rootFile.getFileSystem().hasCapability(Capability.WRITE_CONTENT)) {
            MessageBox messageDialog = new MessageBox(dialog.getShell(), SWT.OK);
            messageDialog.setText(Messages.getString("VfsFileChooserDialog.warning")); //$NON-NLS-1$
            messageDialog.setMessage(Messages.getString("VfsFileChooserDialog.noWriteSupport")); //$NON-NLS-1$
            messageDialog.open();
        }
    }

    vfsBrowser.fileSystemTree.forceFocus();
    while (!dialog.isDisposed()) {
        if (!dialog.getDisplay().readAndDispatch())
            dialog.getDisplay().sleep();
    }

    // we just woke up, we are probably disposed already..
    if (!dialog.isDisposed()) {
        hideCustomPanelChildren();
        dialog.dispose();
    }
    if (okPressed) {
        FileObject returnFile = vfsBrowser.getSelectedFileObject();
        if (returnFile != null && fileDialogMode == VFS_DIALOG_SAVEAS) {
            try {
                if (returnFile.getType().equals(FileType.FILE)) {
                    returnFile = returnFile.getParent();
                }
                returnFile = returnFile.resolveFile(enteredFileName);
            } catch (FileSystemException e) {
                e.printStackTrace();
            }
        }

        // put user/pass on the filename so it comes out in the getUri call.
        if (!returnUserAuthenticatedFile) {
            // make sure to put the user/pass on the url if it's not there
            if (returnFile != null && returnFile.getName() instanceof URLFileName) {
                URLFileName urlFileName = (URLFileName) returnFile.getName();
                if (urlFileName.getUserName() == null || urlFileName.getPassword() == null) {
                    // set it
                    String user = "";
                    String pass = "";

                    UserAuthenticator userAuthenticator = DefaultFileSystemConfigBuilder.getInstance()
                            .getUserAuthenticator(returnFile.getFileSystem().getFileSystemOptions());

                    if (userAuthenticator != null) {
                        UserAuthenticationData data = userAuthenticator
                                .requestAuthentication(AUTHENTICATOR_TYPES);
                        user = String.valueOf(data.getData(UserAuthenticationData.USERNAME));
                        pass = String.valueOf(data.getData(UserAuthenticationData.PASSWORD));
                        try {
                            user = URLEncoder.encode(user, "UTF-8");
                            pass = URLEncoder.encode(pass, "UTF-8");
                        } catch (UnsupportedEncodingException e) {
                            // ignore, just use the un encoded values
                        }
                    }

                    // build up the url with user/pass on it
                    int port = urlFileName.getPort();
                    String portString = (port < 1) ? "" : (":" + port);
                    String urlWithUserPass = urlFileName.getScheme() + "://" + user + ":" + pass + "@"
                            + urlFileName.getHostName() + portString + urlFileName.getPath();

                    try {
                        returnFile = currentPanel.resolveFile(urlWithUserPass);
                    } catch (FileSystemException e) {
                        // couldn't resolve with user/pass on url??? interesting
                        e.printStackTrace();
                    }

                }
            }
        }
        return returnFile;
    } else {
        return null;
    }
}

From source file:pl.otros.vfs.browser.auth.AuthStoreUtils.java

public void save(AuthStore authStore, OutputStream out) throws IOException {
    Collection<UserAuthenticationInfo> all = authStore.getAll();
    for (UserAuthenticationInfo userAuthenticationInfo : all) {
        UserAuthenticationData userAuthenticationData = authStore
                .getUserAuthenticationData(userAuthenticationInfo);
    }/*from ww  w. j  a  v a2s . c om*/
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = null;
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element root = document.createElement("root");

        document.appendChild(root);
        for (UserAuthenticationInfo userAuthenticationInfo : all) {
            UserAuthenticationDataWrapper userAuthenticationData = authStore
                    .getUserAuthenticationData(userAuthenticationInfo);
            Element entry = document.createElement(ENTRY);
            entry.setAttribute(PROTOCOL, userAuthenticationInfo.getProtocol());
            entry.setAttribute(HOST, userAuthenticationInfo.getHost());
            entry.setAttribute(USER, userAuthenticationInfo.getUser());
            Map<UserAuthenticationData.Type, char[]> addedTypes = userAuthenticationData.getAddedTypes();

            for (UserAuthenticationData.Type type : addedTypes.keySet()) {
                Element elementUserAuthenticationData = document.createElement(USER_AUTHENTICATION_DATA);
                char[] data = userAuthenticationData.getData(type);
                String value;
                //          if (UserAuthenticationData.PASSWORD.equals(type)) {
                //            if (password == null){
                //              password = passwordProvider.getPassword("Enter password for password store");
                //            }
                //            if (password == null || password.length==0){
                //              throw new IOException("Password for password store not entered");
                //            }
                //            value = saltAndEncrypt(data);
                //          } else {
                //            value = new String(data);
                //          }
                value = new String(data);
                Element elementType = document.createElement(TYPE);
                elementType.setTextContent(type.toString());
                Element elementData = document.createElement(DATA);
                elementData.setTextContent(value);
                elementUserAuthenticationData.appendChild(elementType);
                elementUserAuthenticationData.appendChild(elementData);
                entry.appendChild(elementUserAuthenticationData);
            }
            root.appendChild(entry);
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    } catch (Exception e) {
        throw new IOException(e);
    }

}

From source file:pl.otros.vfs.browser.auth.SftpUserAuthenticator.java

@Override
protected void userSelectedHook(UserAuthenticationData userAuthenticationData) {
    if (userAuthenticationData != null) {
        char[] sshKeyPath = userAuthenticationData.getData(UserAuthenticationDataWrapper.SSH_KEY);
        String path = "";
        if (sshKeyPath != null && sshKeyPath.length > 0) {
            path = new String(sshKeyPath);
        }// w  ww  . ja  v a 2s.c  o  m
        sshKeyFileField.setText(path);
    }
}

From source file:pl.otros.vfs.browser.auth.SmbUserAuthenticator.java

@Override
protected void userSelectedHook(UserAuthenticationData userAuthenticationData) {
    char[] domain = new char[0];
    if (userAuthenticationData != null) {
        domain = userAuthenticationData.getData(UserAuthenticationData.DOMAIN);
    }//from w  ww  .  j a va  2  s. com
    fieldTf.setText(new String(domain));

}

From source file:pl.otros.vfs.browser.auth.UserPassUserAuthenticator.java

@Override
protected JPanel getOptionsPanel() {

    Collection<UserAuthenticationDataWrapper> userAuthenticationDatas = getAuthStore()
            .getUserAuthenticationDatas(getVfsUriParser().getProtocol().getName(),
                    getVfsUriParser().getHostname());
    String[] names = new String[userAuthenticationDatas.size()];
    int i = 0;/* w  w  w. j  av a  2  s .com*/
    for (UserAuthenticationData userAuthenticationData : userAuthenticationDatas) {
        names[i] = new String(userAuthenticationData.getData(UserAuthenticationData.USERNAME));
        i++;
    }

    JPanel panel = new JPanel(new MigLayout());
    String header = Messages.getMessage("authenticator.enterCredentialsForUrl", getUrl());
    panel.add(new JLabel(header), "growx,span");
    panel.add(new JLabel(Messages.getMessage("authenticator.username")));
    nameTf = new JXComboBox(names);
    nameTf.setEditable(true);
    nameTf.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            userSelected(nameTf.getSelectedItem().toString());
        }
    });

    AutoCompleteDecorator.decorate(nameTf);
    nameTf.addAncestorListener(new AncestorListener() {

        @Override
        public void ancestorRemoved(AncestorEvent event) {

        }

        @Override
        public void ancestorMoved(AncestorEvent event) {

        }

        @Override
        public void ancestorAdded(AncestorEvent event) {
            event.getComponent().requestFocusInWindow();
        }
    });
    panel.add(nameTf, "wrap, growx, span");
    panel.add(new JLabel(Messages.getMessage("authenticator.password")));
    passTx = new JPasswordField(15);
    passTx.setText(getVfsUriParser().getPassword());
    panel.add(passTx, "wrap, growx,span");

    if (StringUtils.isNotBlank(getVfsUriParser().getUsername())) {
        nameTf.setSelectedItem(getVfsUriParser().getUsername());
    }
    if (names.length > 0) {
        nameTf.setSelectedIndex(0);
    }
    return panel;
}

From source file:pl.otros.vfs.browser.auth.UserPassUserAuthenticator.java

private void userSelected(String user) {
    UserAuthenticationData userAuthenticationData = getAuthStore()
            .getUserAuthenticationData(new UserAuthenticationInfo(getVfsUriParser().getProtocol().getName(),
                    getVfsUriParser().getHostname(), user));
    char[] passChars = new char[0];

    if (userAuthenticationData != null
            && userAuthenticationData.getData(UserAuthenticationData.PASSWORD) != null) {
        passChars = userAuthenticationData.getData(UserAuthenticationData.PASSWORD);
    }//  w ww  .j  a  v  a2  s  .c om
    passTx.setText(new String(passChars));

    userSelectedHook(userAuthenticationData);
}