Search in sources :

Example 6 with EncryptedCredentialsStorage

use of org.archicontribs.modelrepository.authentication.EncryptedCredentialsStorage in project archi-modelrepository-plugin by archi-contribs.

the class CloneModelAction method run.

@Override
public void run() {
    // Check primary key set
    try {
        if (!EncryptedCredentialsStorage.checkPrimaryKeySet()) {
            return;
        }
    } catch (GeneralSecurityException ex) {
        displayCredentialsErrorDialog(ex);
        return;
    } catch (Exception ex) {
        displayErrorDialog(Messages.CloneModelAction_0, ex);
        return;
    }
    CloneInputDialog dialog = new CloneInputDialog(fWindow.getShell());
    if (dialog.open() != Window.OK) {
        return;
    }
    final String repoURL = dialog.getURL();
    final boolean storeCredentials = dialog.doStoreCredentials();
    final UsernamePassword npw = dialog.getUsernamePassword();
    if (!StringUtils.isSet(repoURL)) {
        return;
    }
    if (GraficoUtils.isHTTP(repoURL) && !StringUtils.isSet(npw.getUsername()) && npw.getPassword().length == 0) {
        MessageDialog.openError(fWindow.getShell(), Messages.CloneModelAction_0, Messages.CloneModelAction_1);
        return;
    }
    // Create a new local folder
    File localRepoFolder = GraficoUtils.getUniqueLocalFolder(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), repoURL);
    setRepository(new ArchiRepository(localRepoFolder));
    try {
        // Clone
        Exception[] exception = new Exception[1];
        IProgressService ps = PlatformUI.getWorkbench().getProgressService();
        ps.busyCursorWhile(new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor pm) {
                try {
                    // Update Proxy
                    ProxyAuthenticator.update();
                    pm.beginTask(Messages.CloneModelAction_4, -1);
                    getRepository().cloneModel(repoURL, npw, new ProgressMonitorWrapper(pm));
                } catch (Exception ex) {
                    exception[0] = ex;
                } finally {
                    // Clear Proxy
                    ProxyAuthenticator.clear();
                }
            }
        });
        if (exception[0] != null) {
            throw exception[0];
        }
        // Load it from the Grafico files if we can
        IArchimateModel graficoModel = new GraficoModelLoader(getRepository()).loadModel();
        // We couldn't load it from Grafico so create a new blank model
        if (graficoModel == null) {
            // New one. This will open in the tree
            IArchimateModel model = IEditorModelManager.INSTANCE.createNewModel();
            model.setFile(getRepository().getTempModelFile());
            // And Save it
            IEditorModelManager.INSTANCE.saveModel(model);
            // Export to Grafico
            getRepository().exportModelToGraficoFiles();
            // And do a first commit
            getRepository().commitChanges(Messages.CloneModelAction_3, false);
            // Save the checksum
            getRepository().saveChecksum();
        }
        // Store repo credentials if HTTP and option is set
        if (GraficoUtils.isHTTP(repoURL) && storeCredentials) {
            EncryptedCredentialsStorage cs = EncryptedCredentialsStorage.forRepository(getRepository());
            cs.store(npw);
        }
        // Notify listeners
        notifyChangeListeners(IRepositoryListener.REPOSITORY_ADDED);
    } catch (Exception ex) {
        displayErrorDialog(Messages.CloneModelAction_0, ex);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) CloneInputDialog(org.archicontribs.modelrepository.dialogs.CloneInputDialog) ArchiRepository(org.archicontribs.modelrepository.grafico.ArchiRepository) GeneralSecurityException(java.security.GeneralSecurityException) UsernamePassword(org.archicontribs.modelrepository.authentication.UsernamePassword) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GraficoModelLoader(org.archicontribs.modelrepository.grafico.GraficoModelLoader) EncryptedCredentialsStorage(org.archicontribs.modelrepository.authentication.EncryptedCredentialsStorage) IProgressService(org.eclipse.ui.progress.IProgressService) File(java.io.File) IArchimateModel(com.archimatetool.model.IArchimateModel)

Example 7 with EncryptedCredentialsStorage

use of org.archicontribs.modelrepository.authentication.EncryptedCredentialsStorage in project archi-modelrepository-plugin by archi-contribs.

the class CreateRepoFromModelAction method run.

@Override
public void run() {
    try {
        if (!EncryptedCredentialsStorage.checkPrimaryKeySet()) {
            return;
        }
    } catch (GeneralSecurityException ex) {
        displayCredentialsErrorDialog(ex);
        return;
    } catch (IOException ex) {
        displayErrorDialog(Messages.CreateRepoFromModelAction_7, ex);
        return;
    }
    NewModelRepoDialog dialog = new NewModelRepoDialog(fWindow.getShell());
    if (dialog.open() != Window.OK) {
        return;
    }
    final String repoURL = dialog.getURL();
    final boolean storeCredentials = dialog.doStoreCredentials();
    final UsernamePassword npw = dialog.getUsernamePassword();
    if (!StringUtils.isSet(repoURL)) {
        return;
    }
    if (GraficoUtils.isHTTP(repoURL) && !StringUtils.isSet(npw.getUsername()) && npw.getPassword().length == 0) {
        MessageDialog.openError(fWindow.getShell(), Messages.CreateRepoFromModelAction_0, Messages.CreateRepoFromModelAction_3);
        return;
    }
    // Create a new local folder
    File localRepoFolder = GraficoUtils.getUniqueLocalFolder(ModelRepositoryPlugin.INSTANCE.getUserModelRepositoryFolder(), repoURL);
    setRepository(new ArchiRepository(localRepoFolder));
    try {
        // Create a new repo
        try (Git git = getRepository().createNewLocalGitRepository(repoURL)) {
        }
        // TODO: If the model has not been saved yet this is fine but if the model already exists
        // We should tell the user this is the case
        // Set new file location
        fModel.setFile(getRepository().getTempModelFile());
        // And Save it
        IEditorModelManager.INSTANCE.saveModel(fModel);
        // Export to Grafico
        getRepository().exportModelToGraficoFiles();
        // Commit changes
        getRepository().commitChanges(Messages.CreateRepoFromModelAction_5, false);
        // Push
        Exception[] exception = new Exception[1];
        IProgressService ps = PlatformUI.getWorkbench().getProgressService();
        ps.busyCursorWhile(new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor pm) {
                try {
                    // Update Proxy
                    ProxyAuthenticator.update();
                    pm.beginTask(Messages.CreateRepoFromModelAction_4, -1);
                    getRepository().pushToRemote(npw, new ProgressMonitorWrapper(pm));
                } catch (Exception ex) {
                    exception[0] = ex;
                } finally {
                    // Clear Proxy
                    ProxyAuthenticator.clear();
                }
            }
        });
        if (exception[0] != null) {
            throw exception[0];
        }
        // Store repo credentials if HTTP and option is set
        if (GraficoUtils.isHTTP(repoURL) && storeCredentials) {
            EncryptedCredentialsStorage cs = EncryptedCredentialsStorage.forRepository(getRepository());
            cs.store(npw);
        }
        // Save the checksum
        getRepository().saveChecksum();
    } catch (Exception ex) {
        displayErrorDialog(Messages.CreateRepoFromModelAction_7, ex);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) NewModelRepoDialog(org.archicontribs.modelrepository.dialogs.NewModelRepoDialog) IOException(java.io.IOException) ArchiRepository(org.archicontribs.modelrepository.grafico.ArchiRepository) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) UsernamePassword(org.archicontribs.modelrepository.authentication.UsernamePassword) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) EncryptedCredentialsStorage(org.archicontribs.modelrepository.authentication.EncryptedCredentialsStorage) IProgressService(org.eclipse.ui.progress.IProgressService) File(java.io.File)

Aggregations

EncryptedCredentialsStorage (org.archicontribs.modelrepository.authentication.EncryptedCredentialsStorage)7 IOException (java.io.IOException)5 GeneralSecurityException (java.security.GeneralSecurityException)4 UsernamePassword (org.archicontribs.modelrepository.authentication.UsernamePassword)4 File (java.io.File)2 ArchiRepository (org.archicontribs.modelrepository.grafico.ArchiRepository)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)2 IProgressService (org.eclipse.ui.progress.IProgressService)2 IArchimateModel (com.archimatetool.model.IArchimateModel)1 CloneInputDialog (org.archicontribs.modelrepository.dialogs.CloneInputDialog)1 NewModelRepoDialog (org.archicontribs.modelrepository.dialogs.NewModelRepoDialog)1 UserNamePasswordDialog (org.archicontribs.modelrepository.dialogs.UserNamePasswordDialog)1 GraficoModelLoader (org.archicontribs.modelrepository.grafico.GraficoModelLoader)1 IArchiRepository (org.archicontribs.modelrepository.grafico.IArchiRepository)1 Git (org.eclipse.jgit.api.Git)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 TransportException (org.eclipse.jgit.api.errors.TransportException)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1