Example usage for org.eclipse.jgit.transport URIish setPath

List of usage examples for org.eclipse.jgit.transport URIish setPath

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport URIish setPath.

Prototype

public URIish setPath(String n) 

Source Link

Document

Return a new URI matching this one, but with a different path.

Usage

From source file:com.docmd.behavior.GitManager.java

public boolean isValidRemoteRepository(URIish repoUri) {
    boolean result;

    if (repoUri.getScheme().toLowerCase().startsWith("http")) {
        String path = repoUri.getPath();
        String newPath = path.endsWith("/") ? path + INFO_REFS_PATH : path + "/" + INFO_REFS_PATH;
        URIish checkUri = repoUri.setPath(newPath);
        InputStream ins = null;/*from ww  w  .j  a  v  a2 s .com*/
        try {
            URLConnection conn = new URL(checkUri.toString()).openConnection();
            //conn.setReadTimeout(5000);
            ins = conn.getInputStream();
            result = true;
        } catch (Exception e) {
            if (e.getMessage().contains("403"))
                result = true;
            else
                result = false;
        } finally {
            try {
                ins.close();
            } catch (Exception e) {
                /* ignore */ }
        }
    } else {
        if (repoUri.getScheme().toLowerCase().startsWith("ssh")) {
            RemoteSession ssh = null;
            Process exec = null;
            try {
                ssh = SshSessionFactory.getInstance().getSession(repoUri, null, FS.detect(), 5000);
                exec = ssh.exec("cd " + repoUri.getPath() + "; git rev-parse --git-dir", 5000);
                Integer exitValue = null;
                do {
                    try {
                        exitValue = exec.exitValue();
                    } catch (Exception e) {
                        try {
                            Thread.sleep(1000);
                        } catch (Exception ee) {
                        }
                    }
                } while (exitValue == null);
                result = exitValue == 0;
            } catch (Exception e) {
                result = false;
            } finally {
                try {
                    exec.destroy();
                } catch (Exception e) {
                    /* ignore */ }
                try {
                    ssh.disconnect();
                } catch (Exception e) {
                    /* ignore */ }
            }
        } else {
            result = true;
        }
    }
    return result;
}

From source file:com.google.gerrit.server.git.PushReplication.java

License:Apache License

@Override
public void replicateNewProject(Project.NameKey projectName, String head) {
    if (!isEnabled()) {
        return;//from   w  w  w .  j a v  a  2  s.c om
    }

    for (ReplicationConfig config : configs) {
        List<URIish> uriList = config.getURIs(projectName, "*");
        String[] adminUrls = config.getAdminUrls();
        boolean adminURLUsed = false;

        for (String url : adminUrls) {
            URIish adminURI = null;
            try {
                if (url != null && !url.isEmpty()) {
                    adminURI = new URIish(url);
                }
            } catch (URISyntaxException e) {
                log.error("The URL '" + url + "' is invalid");
            }

            if (adminURI != null) {
                final String replacedPath = replace(adminURI.getPath(), "name", projectName.get());
                if (replacedPath != null) {
                    adminURI = adminURI.setPath(replacedPath);
                    if (usingSSH(adminURI)) {
                        replicateProject(adminURI, head);
                        adminURLUsed = true;
                    } else {
                        log.error("The adminURL '" + url + "' is non-SSH which is not allowed");
                    }
                }
            }
        }

        if (!adminURLUsed) {
            for (URIish uri : uriList) {
                replicateProject(uri, head);
            }
        }
    }
}

From source file:com.googlesource.gerrit.plugins.github.replication.Destination.java

License:Apache License

List<URIish> getURIs(Project.NameKey project, String urlMatch) {
    List<URIish> r = Lists.newArrayListWithCapacity(remote.getURIs().size());
    for (URIish uri : remote.getURIs()) {
        if (matches(uri, urlMatch)) {
            String name = project.get();
            if (needsUrlEncoding(uri)) {
                name = encode(name);/*from   w  ww .  java 2 s  .com*/
            }
            if (remoteNameStyle.equals("dash")) {
                name = name.replace("/", "-");
            } else if (remoteNameStyle.equals("underscore")) {
                name = name.replace("/", "_");
            } else if (!remoteNameStyle.equals("slash")) {
                GitHubDestinations.log.debug(
                        String.format("Unknown remoteNameStyle: %s, falling back to slash", remoteNameStyle));
            }
            String replacedPath = GitHubDestinations.replaceName(uri.getPath(), name);
            if (replacedPath != null) {
                uri = uri.setPath(replacedPath);
                r.add(uri);
            }
        }
    }
    return r;
}

From source file:com.googlesource.gerrit.plugins.replication.Destination.java

License:Apache License

List<URIish> getURIs(Project.NameKey project, String urlMatch) {
    List<URIish> r = Lists.newArrayListWithCapacity(config.getRemoteConfig().getURIs().size());
    for (URIish uri : config.getRemoteConfig().getURIs()) {
        if (matches(uri, urlMatch)) {
            String name = project.get();
            if (needsUrlEncoding(uri)) {
                name = encode(name);/*from  w ww  .j av  a2  s.  c  om*/
            }
            String remoteNameStyle = config.getRemoteNameStyle();
            if (remoteNameStyle.equals("dash")) {
                name = name.replace("/", "-");
            } else if (remoteNameStyle.equals("underscore")) {
                name = name.replace("/", "_");
            } else if (remoteNameStyle.equals("basenameOnly")) {
                name = FilenameUtils.getBaseName(name);
            } else if (!remoteNameStyle.equals("slash")) {
                repLog.debug(
                        String.format("Unknown remoteNameStyle: %s, falling back to slash", remoteNameStyle));
            }
            String replacedPath = ReplicationQueue.replaceName(uri.getPath(), name, isSingleProjectMatch());
            if (replacedPath != null) {
                uri = uri.setPath(replacedPath);
                r.add(uri);
            }
        }
    }
    return r;
}

From source file:com.googlesource.gerrit.plugins.replication.ReplicationQueue.java

License:Apache License

private Set<URIish> getURIs(Project.NameKey projectName, FilterType filterType) {
    if (config.getDestinations(filterType).isEmpty()) {
        return Collections.emptySet();
    }/*www  .  j  a v a 2 s  . c  o  m*/
    if (!running) {
        repLog.error("Replication plugin did not finish startup before event");
        return Collections.emptySet();
    }

    Set<URIish> uris = new HashSet<>();
    for (Destination config : this.config.getDestinations(filterType)) {
        if (!config.wouldPushProject(projectName)) {
            continue;
        }

        boolean adminURLUsed = false;

        for (String url : config.getAdminUrls()) {
            if (Strings.isNullOrEmpty(url)) {
                continue;
            }

            URIish uri;
            try {
                uri = new URIish(url);
            } catch (URISyntaxException e) {
                repLog.warn(String.format("adminURL '%s' is invalid: %s", url, e.getMessage()));
                continue;
            }

            String path = replaceName(uri.getPath(), projectName.get(), config.isSingleProjectMatch());
            if (path == null) {
                repLog.warn(String.format("adminURL %s does not contain ${name}", uri));
                continue;
            }

            uri = uri.setPath(path);
            if (!isSSH(uri)) {
                repLog.warn(String.format("adminURL '%s' is invalid: only SSH is supported", uri));
                continue;
            }

            uris.add(uri);
            adminURLUsed = true;
        }

        if (!adminURLUsed) {
            for (URIish uri : config.getURIs(projectName, "*")) {
                uris.add(uri);
            }
        }
    }
    return uris;
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.submodules.SubmoduleResolverImpl.java

License:Apache License

@NotNull
private static String resolveSubmoduleUrl(@NotNull StoredConfig mainRepoConfig, @NotNull String submoduleUrl)
        throws URISyntaxException {
    if (!isRelative(submoduleUrl))
        return submoduleUrl;

    String baseUrl = mainRepoConfig.getString("teamcity", null, "remote");
    URIish u = new URIish(baseUrl);
    String newPath = u.getPath();
    if (newPath.length() == 0) {
        newPath = submoduleUrl;/*w  w  w.ja  va 2 s .c o  m*/
    } else {
        newPath = GitUtils.normalizePath(newPath + '/' + submoduleUrl);
    }
    return u.setPath(newPath).toPrivateString();
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.submodules.TeamCitySubmoduleResolver.java

License:Apache License

private String resolveRelativeUrl(String relativeUrl) throws URISyntaxException {
    String baseUrl = getRepository().getConfig().getString("teamcity", null, "remote");
    URIish u = new URIish(baseUrl);
    String newPath = u.getPath();
    if (newPath.length() == 0) {
        newPath = relativeUrl;//  w w  w . j a  va 2  s .co  m
    } else {
        newPath = GitUtils.normalizePath(newPath + '/' + relativeUrl);
    }
    return u.setPath(newPath).toPrivateString();
}

From source file:org.eclipse.egit.core.op.ConfigureGerritAfterCloneTask.java

License:Open Source License

/**
 * Try to use Gerrit's "Get Version" REST API endpoint [1] to detect if the
 * repository is hosted on a Gerrit server.
 * <p/>/*from   ww w.  jav  a2s .  co  m*/
 * [1] <a href=
 * "https://gerrit-documentation.storage.googleapis.com/Documentation/2.11
 * /rest-api-config.html#get-version">Gerrit 2.11 Get Version REST
 * endpoint</a>
 *
 * @param repo
 *            the repository to be configured
 *
 * @return {@code true} if the repository is hosted on a Gerrit server
 * @throws IOException
 * @throws MalformedURLException
 * @throws URISyntaxException
 */
private boolean isGerrit(Repository repo) throws MalformedURLException, IOException, URISyntaxException {
    URIish u = new URIish(uri);
    final String s = u.getScheme();
    final String host = u.getHost();
    final String path = u.getPath();

    // shortcut for Eclipse Gerrit server
    if (host != null && host.equals(GIT_ECLIPSE_ORG)) {
        if (HTTPS.equals(s) && (u.getPort() == 443 || u.getPort() == -1) && path != null
                && path.startsWith(GERRIT_CONTEXT_ROOT)) {
            return true;
        } else if (SSH.equals(s) && u.getPort() == GERRIT_SSHD_DEFAULT_PORT) {
            return true;
        }
    }

    if (path != null && (HTTP.equals(s) || HTTPS.equals(s))) {
        String baseURL = u.setPath("/").toString(); //$NON-NLS-1$
        baseURL = baseURL.substring(0, baseURL.length() - 1);
        String tmpPath = ""; //$NON-NLS-1$
        int slash = 1;
        while (true) {
            HttpURLConnection httpConnection = null;
            try {
                httpConnection = (HttpURLConnection) new URL(
                        baseURL + tmpPath + GERRIT_CONFIG_SERVER_VERSION_API).openConnection();
                NetUtil.setSslVerification(repo, httpConnection);
                httpConnection.setRequestMethod("GET"); //$NON-NLS-1$
                httpConnection.setReadTimeout(1000 * timeout);
                int responseCode = httpConnection.getResponseCode();
                switch (responseCode) {
                case HttpURLConnection.HTTP_OK:
                    try (InputStream in = httpConnection.getInputStream()) {
                        String response = readFully(in, "UTF-8"); //$NON-NLS-1$
                        return response.startsWith(GERRIT_XSSI_MAGIC_STRING);
                    }
                case HttpURLConnection.HTTP_NOT_FOUND:
                    if (slash > path.length()) {
                        return false;
                    }
                    slash = path.indexOf('/', slash);
                    if (slash == -1) {
                        // try the entire path
                        slash = path.length();
                    }
                    tmpPath = path.substring(0, slash);
                    slash++;
                    break;
                default:
                    return false;
                }
            } catch (IOException e) {
                return false;
            } finally {
                if (httpConnection != null) {
                    httpConnection.disconnect();
                }
            }
        }
    } else if (SSH.equals(s)) {
        if (u.getPort() < 0 || u.getUser() == null || credentialsProvider == null) {
            return false;
        }
        URIish sshUri = u.setPath(""); //$NON-NLS-1$
        try {
            String result = runSshCommand(sshUri, credentialsProvider, repo.getFS(), GERRIT_SSHD_VERSION_API);
            return result != null && GERRIT_SSHD_REPLY.matcher(result).matches();
        } catch (IOException e) {
            // Something went wrong with the connection or with the command
            // execution. Maybe the server didn't recognize the command. Do
            // the safe thing and claim it wasn't a Gerrit. In the worst
            // case, the user may have to do the Gerrit config setup via
            // the ConfigureGerritWizard.
            return false;
        }
    }
    return false;
}

From source file:org.eclipse.egit.ui.internal.clone.GerritConfigurationPage.java

License:Open Source License

/**
 * @param u/*from   w  w  w .  j  a v  a2  s . com*/
 * @return URI with path prefixed for Gerrit smart HTTP support
 */
private URIish prependGerritHttpPathPrefix(URIish u) {
    String path = u.getPath();
    if (!path.startsWith(GERRIT_HTTP_PATH_PREFIX))
        return u.setPath(GERRIT_HTTP_PATH_PREFIX + path);
    return u;
}

From source file:org.eclipse.egit.ui.internal.clone.GerritConfigurationPage.java

License:Open Source License

/**
 * @param u/*from   www . ja  v a2  s  . com*/
 * @return URI without Gerrit smart HTTP path prefix
 */
private URIish removeGerritHttpPathPrefix(URIish u) {
    String path = u.getPath();
    if (path.startsWith(GERRIT_HTTP_PATH_PREFIX))
        return u.setPath(path.substring(4));
    return u;
}