Search in sources :

Example 16 with URISyntaxException

use of java.net.URISyntaxException in project che by eclipse.

the class GitProjectImporter method importSources.

@Override
public void importSources(FolderEntry baseFolder, SourceStorage storage, LineConsumerFactory consumerFactory) throws ForbiddenException, ConflictException, UnauthorizedException, IOException, ServerException {
    GitConnection git = null;
    boolean credentialsHaveBeenSet = false;
    try {
        // For factory: checkout particular commit after clone
        String commitId = null;
        // For factory: github pull request feature
        String fetch = null;
        String branch = null;
        String startPoint = null;
        // For factory or probably for our projects templates:
        // If git repository contains more than one project need clone all repository but after cloning keep just
        // sub-project that is specified in parameter "keepDir".
        String keepDir = null;
        // For factory and for our projects templates:
        // Keep all info related to the vcs. In case of Git: ".git" directory and ".gitignore" file.
        // Delete vcs info if false.
        String branchMerge = null;
        boolean keepVcs = true;
        boolean recursiveEnabled = false;
        boolean convertToTopLevelProject = false;
        Map<String, String> parameters = storage.getParameters();
        if (parameters != null) {
            commitId = parameters.get("commitId");
            branch = parameters.get("branch");
            startPoint = parameters.get("startPoint");
            fetch = parameters.get("fetch");
            keepDir = parameters.get("keepDir");
            if (parameters.containsKey("keepVcs")) {
                keepVcs = Boolean.parseBoolean(parameters.get("keepVcs"));
            }
            if (parameters.containsKey("recursive")) {
                recursiveEnabled = true;
            }
            //and when we are working in git sparse checkout mode.
            if (!keepVcs && !isNullOrEmpty(keepDir) && parameters.containsKey("convertToTopLevelProject")) {
                convertToTopLevelProject = Boolean.parseBoolean(parameters.get("convertToTopLevelProject"));
            }
            branchMerge = parameters.get("branchMerge");
            final String user = storage.getParameters().remove("username");
            final String pass = storage.getParameters().remove("password");
            if (user != null && pass != null) {
                credentialsHaveBeenSet = true;
                setCurrentCredentials(user, pass);
            }
        }
        // Get path to local file. Git works with local filesystem only.
        final String localPath = baseFolder.getVirtualFile().toIoFile().getAbsolutePath();
        final String location = storage.getLocation();
        final String projectName = baseFolder.getName();
        // otherwise we will have to replace atomic move with copy-delete operation.
        if (convertToTopLevelProject) {
            File tempDir = new File(new File(localPath).getParent(), NameGenerator.generate(".che", 6));
            git = gitConnectionFactory.getConnection(tempDir, consumerFactory);
        } else {
            git = gitConnectionFactory.getConnection(localPath, consumerFactory);
        }
        if (keepDir != null) {
            git.cloneWithSparseCheckout(keepDir, location);
            if (branch != null) {
                git.checkout(CheckoutParams.create(branch));
            }
        } else {
            if (baseFolder.getChildren().size() == 0) {
                cloneRepository(git, "origin", location, recursiveEnabled);
                if (commitId != null) {
                    checkoutCommit(git, commitId);
                } else if (fetch != null) {
                    git.getConfig().add("remote.origin.fetch", fetch);
                    fetch(git, "origin");
                    if (branch != null) {
                        checkoutBranch(git, projectName, branch, startPoint);
                    }
                } else if (branch != null) {
                    checkoutBranch(git, projectName, branch, startPoint);
                }
            } else {
                git.init(false);
                addRemote(git, "origin", location);
                if (commitId != null) {
                    fetchBranch(git, "origin", branch == null ? "*" : branch);
                    checkoutCommit(git, commitId);
                } else if (fetch != null) {
                    git.getConfig().add("remote.origin.fetch", fetch);
                    fetch(git, "origin");
                    if (branch != null) {
                        checkoutBranch(git, projectName, branch, startPoint);
                    }
                } else {
                    fetchBranch(git, "origin", branch == null ? "*" : branch);
                    List<Branch> branchList = git.branchList(LIST_REMOTE);
                    if (!branchList.isEmpty()) {
                        checkoutBranch(git, projectName, branch == null ? "master" : branch, startPoint);
                    }
                }
            }
            if (branchMerge != null) {
                git.getConfig().set("branch." + (branch == null ? "master" : branch) + ".merge", branchMerge);
            }
        }
        if (!keepVcs) {
            cleanGit(git.getWorkingDir());
        }
        if (convertToTopLevelProject) {
            Files.move(new File(git.getWorkingDir(), keepDir).toPath(), new File(localPath).toPath(), StandardCopyOption.ATOMIC_MOVE);
            IoUtil.deleteRecursive(git.getWorkingDir());
        }
    } catch (URISyntaxException e) {
        throw new ServerException("Your project cannot be imported. The issue is either from git configuration, a malformed URL, " + "or file system corruption. Please contact support for assistance.", e);
    } finally {
        if (git != null) {
            git.close();
        }
        if (credentialsHaveBeenSet) {
            clearCredentials();
        }
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) List(java.util.List) URISyntaxException(java.net.URISyntaxException) File(java.io.File)

Example 17 with URISyntaxException

use of java.net.URISyntaxException in project che by eclipse.

the class JGitConnection method remoteList.

@Override
public List<Remote> remoteList(String remoteName, boolean verbose) throws GitException {
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = new HashSet<>(config.getSubsections(ConfigConstants.CONFIG_KEY_REMOTE));
    if (remoteName != null && remoteNames.contains(remoteName)) {
        remoteNames.clear();
        remoteNames.add(remoteName);
    }
    List<Remote> result = new ArrayList<>(remoteNames.size());
    for (String remote : remoteNames) {
        try {
            List<URIish> uris = new RemoteConfig(config, remote).getURIs();
            result.add(newDto(Remote.class).withName(remote).withUrl(uris.isEmpty() ? null : uris.get(0).toString()));
        } catch (URISyntaxException exception) {
            throw new GitException(exception.getMessage(), exception);
        }
    }
    return result;
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) URIish(org.eclipse.jgit.transport.URIish) GitException(org.eclipse.che.api.git.exception.GitException) ArrayList(java.util.ArrayList) Remote(org.eclipse.che.api.git.shared.Remote) URISyntaxException(java.net.URISyntaxException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) HashSet(java.util.HashSet)

Example 18 with URISyntaxException

use of java.net.URISyntaxException in project che by eclipse.

the class JGitConnection method remoteAdd.

@Override
public void remoteAdd(RemoteAddParams params) throws GitException {
    String remoteName = params.getName();
    if (isNullOrEmpty(remoteName)) {
        throw new GitException(ERROR_ADD_REMOTE_NAME_MISSING);
    }
    StoredConfig config = repository.getConfig();
    Set<String> remoteNames = config.getSubsections("remote");
    if (remoteNames.contains(remoteName)) {
        throw new GitException(format(ERROR_ADD_REMOTE_NAME_ALREADY_EXISTS, remoteName));
    }
    String url = params.getUrl();
    if (isNullOrEmpty(url)) {
        throw new GitException(ERROR_ADD_REMOTE_URL_MISSING);
    }
    RemoteConfig remoteConfig;
    try {
        remoteConfig = new RemoteConfig(config, remoteName);
    } catch (URISyntaxException exception) {
        // Not happen since it is newly created remote.
        throw new GitException(exception.getMessage(), exception);
    }
    try {
        remoteConfig.addURI(new URIish(url));
    } catch (URISyntaxException exception) {
        throw new GitException("Remote url " + url + " is invalid. ");
    }
    List<String> branches = params.getBranches();
    if (branches.isEmpty()) {
        remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + "*" + ":" + Constants.R_REMOTES + remoteName + "/*").setForceUpdate(true));
    } else {
        for (String branch : branches) {
            remoteConfig.addFetchRefSpec(new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_REMOTES + remoteName + "/" + branch).setForceUpdate(true));
        }
    }
    remoteConfig.update(config);
    try {
        config.save();
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) URIish(org.eclipse.jgit.transport.URIish) RefSpec(org.eclipse.jgit.transport.RefSpec) GitException(org.eclipse.che.api.git.exception.GitException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Example 19 with URISyntaxException

use of java.net.URISyntaxException in project jetty.project by eclipse.

the class OverlayedAppProviderTest method touch.

private void touch(File base, String path) {
    try {
        File target = new File(new URI(base.toURI().toString() + path));
        target.getParentFile().mkdirs();
        touch(target);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI)

Example 20 with URISyntaxException

use of java.net.URISyntaxException in project elasticsearch by elastic.

the class RestClient method buildUri.

private static URI buildUri(String pathPrefix, String path, Map<String, String> params) {
    Objects.requireNonNull(path, "path must not be null");
    try {
        String fullPath;
        if (pathPrefix != null) {
            if (path.startsWith("/")) {
                fullPath = pathPrefix + path;
            } else {
                fullPath = pathPrefix + "/" + path;
            }
        } else {
            fullPath = path;
        }
        URIBuilder uriBuilder = new URIBuilder(fullPath);
        for (Map.Entry<String, String> param : params.entrySet()) {
            uriBuilder.addParameter(param.getKey(), param.getValue());
        }
        return uriBuilder.build();
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

URISyntaxException (java.net.URISyntaxException)4043 URI (java.net.URI)2496 IOException (java.io.IOException)1273 File (java.io.File)716 URL (java.net.URL)702 ArrayList (java.util.ArrayList)407 Test (org.junit.Test)274 MalformedURLException (java.net.MalformedURLException)270 InputStream (java.io.InputStream)224 HashMap (java.util.HashMap)212 Response (javax.ws.rs.core.Response)194 Test (org.testng.annotations.Test)175 Parameters (org.testng.annotations.Parameters)166 Builder (javax.ws.rs.client.Invocation.Builder)165 ResteasyClientBuilder (org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder)165 Map (java.util.Map)162 StorageException (com.microsoft.azure.storage.StorageException)142 Path (java.nio.file.Path)141 URIBuilder (org.apache.http.client.utils.URIBuilder)140 List (java.util.List)125