com.damnhandy.uri.template.UriTemplateBuilder Java Examples

The following examples show how to use com.damnhandy.uri.template.UriTemplateBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public GiteaPullRequest fetchPullRequest(String username, String name, long id)
        throws IOException, InterruptedException {
    return getObject(
            api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("name"))
                    .literal("/pulls")
                    .path(UriTemplateBuilder.var("id"))
                    .build()
                    .set("username", username)
                    .set("name", name)
                    .set("id", Long.toString(id)),
            GiteaPullRequest.class
    );
}
 
Example #2
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public void deleteHook(GiteaOrganization organization, long id) throws IOException, InterruptedException {
    int status = delete(api()
            .literal("/orgs")
            .path(UriTemplateBuilder.var("name"))
            .literal("/hooks")
            .path(UriTemplateBuilder.var("id"))
            .build()
            .set("name", organization.getUsername())
            .set("id", id)
    );
    if (status / 100 != 2) {
        throw new IOException(
                "Could not delete organization hook " + id + " for " + organization.getUsername() + " HTTP/"
                        + status);
    }
}
 
Example #3
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public void deleteHook(GiteaRepository repository, long id) throws IOException, InterruptedException {
    int status = delete(api()
            .literal("/repos")
            .path(UriTemplateBuilder.var("username"))
            .path(UriTemplateBuilder.var("name"))
            .literal("/hooks")
            .path(UriTemplateBuilder.var("id"))
            .build()
            .set("username", repository.getOwner().getUsername())
            .set("name", repository.getName())
            .set("id", id)
    );
    if (status / 100 != 2) {
        throw new IOException(
                "Could not delete hook " + id + " for " + repository.getOwner().getUsername() + "/" + repository
                        .getName() + " HTTP/" + status);
    }
}
 
Example #4
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public void updateHook(GiteaOrganization organization, GiteaHook hook) throws IOException, InterruptedException {
    GiteaHook diff = new GiteaHook();
    diff.setConfig(hook.getConfig());
    diff.setActive(hook.isActive());
    diff.setEvents(hook.getEvents());
    patch(api()
                    .literal("/orgs")
                    .path(UriTemplateBuilder.var("name"))
                    .literal("/hooks")
                    .path(UriTemplateBuilder.var("id"))
                    .build()
                    .set("name", organization.getUsername())
                    .set("id", hook.getId()),
            diff, Void.class);
}
 
Example #5
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public boolean checkCollaborator(String username, String name, String collaboratorName)
        throws IOException, InterruptedException {
    return status(
            api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("name"))
                    .literal("/collaborators")
                    .path(UriTemplateBuilder.var("collaboratorName"))
                    .build()
                    .set("username", username)
                    .set("name", name)
                    .set("collaboratorName", collaboratorName)
    ) / 100 == 2;
}
 
Example #6
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public void updateHook(GiteaRepository repository, GiteaHook hook) throws IOException, InterruptedException {
    GiteaHook diff = new GiteaHook();
    diff.setConfig(hook.getConfig());
    diff.setActive(hook.isActive());
    diff.setEvents(hook.getEvents());
    patch(api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("name"))
                    .literal("/hooks")
                    .path(UriTemplateBuilder.var("id"))
                    .build()
                    .set("username", repository.getOwner().getUsername())
                    .set("name", repository.getName())
                    .set("id", hook.getId()),
            diff, Void.class);
}
 
Example #7
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public GiteaCommitDetail fetchCommit(String username, String repository, String sha1)
        throws IOException, InterruptedException {
    return getObject(
            api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("repository"))
                    .literal("/git/commits")
                    .path(UriTemplateBuilder.var("sha1"))
                    .build()
                    .set("username", username)
                    .set("repository", repository)
                    .set("sha1", sha1),
            GiteaCommitDetail.class
    );
}
 
Example #8
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public List<GiteaCommitStatus> fetchCommitStatuses(GiteaRepository repository, String sha)
        throws IOException, InterruptedException {
    return getList(
            api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("name"))
                    .literal("/statuses")
                    .path(UriTemplateBuilder.var("sha"))
                    .build()
                    .set("username", repository.getOwner().getUsername())
                    .set("name", repository.getName())
                    .set("sha", sha),
            GiteaCommitStatus.class
    );
}
 
Example #9
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public GiteaAnnotatedTag fetchAnnotatedTag(String username, String repository, String sha1)
        throws IOException, InterruptedException {
    return getObject(
            api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("repository"))
                    .literal("/git/tags")
                    .path(UriTemplateBuilder.var("sha1"))
                    .build()
                    .set("username", username)
                    .set("repository", repository)
                    .set("sha1", sha1),
            GiteaAnnotatedTag.class
    );
}
 
Example #10
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 6 votes vote down vote up
@Override
public GiteaBranch fetchBranch(String username, String repository, String name)
        throws IOException, InterruptedException {
    if (name.indexOf('/') != -1) {
        // TODO remove hack once https://github.com/go-gitea/gitea/issues/2088 is fixed
        for (GiteaBranch b : fetchBranches(username, repository)) {
            if (name.equals(b.getName())) {
                return b;
            }
        }
    }
    return getObject(
            api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("repository"))
                    .literal("/branches")
                    .path(UriTemplateBuilder.var("name", true))
                    .build()
                    .set("username", username)
                    .set("repository", repository)
                    .set("name", StringUtils.split(name, '/')),
            GiteaBranch.class
    );
}
 
Example #11
Source File: GiteaBrowser.java    From gitea-plugin with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public URL getFileLink(Path path) throws IOException {
    if (path.getEditType().equals(EditType.DELETE)) {
        return diffLink(path);
    } else {
        return new URL(
                UriTemplate.buildFromTemplate(getRepoUrl())
                        .literal("/src")
                        .path(UriTemplateBuilder.var("changeSet"))
                        .path(UriTemplateBuilder.var("path", true))
                        .build()
                        .set("changeSet", path.getChangeSet().getId())
                        .set("path", StringUtils.split(path.getPath(), '/'))
                        .expand()
        );
    }
}
 
Example #12
Source File: GiteaSCMSource.java    From gitea-plugin with MIT License 6 votes vote down vote up
@NonNull
@Override
protected List<Action> retrieveActions(SCMSourceEvent event, @NonNull TaskListener listener)
        throws IOException, InterruptedException {
    if (giteaRepository == null) {
        try (GiteaConnection c = gitea().open()) {
            listener.getLogger().format("Looking up repository %s/%s%n", repoOwner, repository);
            giteaRepository = c.fetchRepository(repoOwner, repository);
        }
    }
    List<Action> result = new ArrayList<>();
    result.add(new ObjectMetadataAction(null, giteaRepository.getDescription(), giteaRepository.getWebsite()));
    result.add(new GiteaLink("icon-gitea-repo", UriTemplate.buildFromTemplate(serverUrl)
            .path(UriTemplateBuilder.var("owner"))
            .path(UriTemplateBuilder.var("repository"))
            .build()
            .set("owner", repoOwner)
            .set("repository", repository)
            .expand()
    ));
    return result;
}
 
Example #13
Source File: GitLabHookCreator.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
/**
 * @param server the {@code GitLabServer} for which the hooks URL would be created. If not {@code null} and it
 *        has a {@link GitLabServer#getHooksRootUrl()}, then the hook URL will be based on this root URL.
 *        Otherwise, the hook URL will be based on {@link Jenkins#getRootUrl()}.
 * @param isWebHook {@code true} to get the webhook URL, {@code false} for the systemhook URL
 * @return a webhook or systemhook URL
 */
public static String getHookUrl(GitLabServer server, boolean isWebHook) {
    String rootUrl = (server == null || server.getHooksRootUrl() == null)
            ? Jenkins.get().getRootUrl()
            : server.getHooksRootUrl();
    if (StringUtils.isBlank(rootUrl)) {
        return "";
    }
    checkURL(rootUrl);
    UriTemplateBuilder templateBuilder = UriTemplate.buildFromTemplate(rootUrl);
    if (isWebHook) {
        templateBuilder.literal("gitlab-webhook");
    } else {
        templateBuilder.literal("gitlab-systemhook");
    }
    return templateBuilder.literal("/post").build().expand();
}
 
Example #14
Source File: ResourceScanner.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
private void buildHalLink() {
    for (Map.Entry<String, List<Method>> entry : resourceByRel.entrySet()) {
        String rel = entry.getKey();
        List<Method> methodsByRel = entry.getValue();

        String path = RESTReflect.findPath(methodsByRel.get(0));
        if (path == null) {
            throw new IllegalStateException("Path not found for rel: " + rel);
        }
        UriTemplateBuilder uriTemplateBuilder = UriTemplate.buildFromTemplate(path);

        Set<String> queryParams = findAllQueryParamsForRel(methodsByRel);
        if (!queryParams.isEmpty()) {
            uriTemplateBuilder.query(queryParams.toArray(new String[queryParams.size()]));
        }

        String absolutePath = UriBuilder.uri(servletContextPath, restConfig.getPath(),
                uriTemplateBuilder.build().getTemplate());

        halLinks.put(rel, new Link(absolutePath));
    }
}
 
Example #15
Source File: GitLabBrowser.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
@Override
public URL getFileLink(GitChangeSet.Path path) throws IOException {
    if (path.getEditType().equals(EditType.DELETE)) {
        return diffLink(path);
    } else {
        return new URL(
            getUriTemplateFromServer(getProjectUrl())
                .literal("/blob")
                .path(UriTemplateBuilder.var("changeSet"))
                .path(UriTemplateBuilder.var("path", true))
                .build()
                .set("changeSet", path.getChangeSet().getId())
                .set("path", splitPath(path.getPath()))
                .expand()
        );
    }
}
 
Example #16
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public List<GiteaPullRequest> fetchPullRequests(String username, String name, Set<GiteaIssueState> states)
        throws IOException, InterruptedException {
    String state = null;
    if (states != null && states.size() == 1) {
        // state query only works if there is one state
        for (GiteaIssueState s : GiteaIssueState.values()) {
            if (states.contains(s)) {
                state = s.getKey();
            }
        }
    }
    try {
        return getList(
                api()
                        .literal("/repos")
                        .path(UriTemplateBuilder.var("username"))
                        .path(UriTemplateBuilder.var("name"))
                        .literal("/pulls")
                        .query(UriTemplateBuilder.var("state"))
                        .build()
                        .set("username", username)
                        .set("name", name)
                        .set("state", state),
                GiteaPullRequest.class
        );
    } catch (GiteaHttpStatusException e) {
        // Gitea REST API returns HTTP Code 404 when pull requests or issues are disabled
        // Therefore we need to handle this case and return a empty List
        if (e.getStatusCode() == 404) {
            return Collections.emptyList();
        } else {
            // Else other cause... throw exception again
            throw e;
        }
    }
}
 
Example #17
Source File: Resource.java    From seed with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Return the href template. It's empty unless the path is templated.
 *
 * @return hrefTemplate
 */
public String hrefTemplate() {
    UriTemplateBuilder uriTemplateBuilder = UriTemplate.buildFromTemplate(hrefTemplate);
    if (!queryParams.isEmpty()) {
        uriTemplateBuilder = uriTemplateBuilder.query(
                queryParams.keySet().toArray(new String[queryParams.keySet().size()]));
    }
    return uriTemplateBuilder.build().getTemplate();
}
 
Example #18
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public List<GiteaIssue> fetchIssues(String username, String name, Set<GiteaIssueState> states)
        throws IOException, InterruptedException {
    String state = null;
    if (states != null && states.size() == 1) {
        // state query only works if there is one state
        for (GiteaIssueState s : GiteaIssueState.values()) {
            if (states.contains(s)) {
                state = s.getKey();
            }
        }
    }

    try {
        return getList(
                api()
                        .literal("/repos")
                        .path(UriTemplateBuilder.var("username"))
                        .path(UriTemplateBuilder.var("name"))
                        .literal("/issues")
                        .query(UriTemplateBuilder.var("state"))
                        .build()
                        .set("username", username)
                        .set("name", name)
                        .set("state", state),
                GiteaIssue.class
        );
    } catch (GiteaHttpStatusException e) {
        // Gitea REST API returns HTTP Code 404 when pull requests or issues are disabled
        // Therefore we need to handle this case and return a empty List
        if (e.getStatusCode() == 404) {
            return Collections.emptyList();
        } else {
            // Else other cause... throw exception again
            throw e;
        }
    }
}
 
Example #19
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public GiteaCommitStatus createCommitStatus(String username, String repository, String sha,
                                            GiteaCommitStatus status) throws IOException, InterruptedException {
    return post(api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("name"))
                    .literal("/statuses")
                    .path(UriTemplateBuilder.var("sha"))
                    .build()
                    .set("username", username)
                    .set("name", repository)
                    .set("sha", sha),
            status, GiteaCommitStatus.class);
}
 
Example #20
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public byte[] fetchFile(GiteaRepository repository, String ref, String path)
        throws IOException, InterruptedException {
    HttpURLConnection connection = openConnection(api()
            .literal("/repos")
            .path(UriTemplateBuilder.var("username"))
            .path(UriTemplateBuilder.var("name"))
            .literal("/raw")
            .path(UriTemplateBuilder.var("ref", true))
            .path(UriTemplateBuilder.var("path", true))
            .build()
            .set("username", repository.getOwner().getUsername())
            .set("name", repository.getName())
            .set("ref", StringUtils.split(ref, '/'))
            .set("path", StringUtils.split(path, "/")));
    withAuthentication(connection);
    try {
        connection.connect();
        int status = connection.getResponseCode();
        if (status == 404) {
            throw new FileNotFoundException(path);
        }
        if (status / 100 == 2) {
            try (InputStream is = connection.getInputStream()) {
                return IOUtils.toByteArray(is);
            }
        }
        throw new IOException("HTTP " + status + "/" + connection.getResponseMessage());
    } finally {
        connection.disconnect();
    }
}
 
Example #21
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public boolean checkFile(GiteaRepository repository, String ref, String path)
        throws IOException, InterruptedException {
    HttpURLConnection connection = openConnection(api()
            .literal("/repos")
            .path(UriTemplateBuilder.var("username"))
            .path(UriTemplateBuilder.var("name"))
            .literal("/raw")
            .path(UriTemplateBuilder.var("ref", true))
            .path(UriTemplateBuilder.var("path", true))
            .build()
            .set("username", repository.getOwner().getUsername())
            .set("name", repository.getName())
            .set("ref", StringUtils.split(ref, '/'))
            .set("path", StringUtils.split(path, "/")));
    withAuthentication(connection);
    try {
        connection.connect();
        int status = connection.getResponseCode();
        if (status == 404) {
            return false;
        }
        if (status / 100 == 2) {
            return true;
        }
        throw new IOException("HTTP " + status + "/" + connection.getResponseMessage());
    } finally {
        connection.disconnect();
    }
}
 
Example #22
Source File: GiteaSCMBuilder.java    From gitea-plugin with MIT License 5 votes vote down vote up
public final String repositoryUrl(String owner, String repository) {
    return UriTemplate.buildFromTemplate(serverUrl)
            .path(UriTemplateBuilder.var("owner"))
            .path(UriTemplateBuilder.var("repository"))
            .build()
            .set("owner", owner)
            .set("repository", repository)
            .expand();
}
 
Example #23
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public GiteaHook createHook(GiteaRepository repository, GiteaHook hook) throws IOException, InterruptedException {
    return post(api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("name"))
                    .literal("/hooks")
                    .build()
                    .set("username", repository.getOwner().getUsername())
                    .set("name", repository.getName()),
            hook, GiteaHook.class);
}
 
Example #24
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public GiteaHook createHook(GiteaOrganization organization, GiteaHook hook)
        throws IOException, InterruptedException {
    return post(api()
                    .literal("/orgs")
                    .path(UriTemplateBuilder.var("name"))
                    .literal("/hooks")
                    .build()
                    .set("name", organization.getUsername()),
            hook, GiteaHook.class);
}
 
Example #25
Source File: GitLabBrowser.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
private URL diffLink(GitChangeSet.Path path) throws IOException {
    return new URL(
        getUriTemplateFromServer(getProjectUrl())
            .literal("/commit")
            .path(UriTemplateBuilder.var("changeSet"))
            .fragment(UriTemplateBuilder.var("diff"))
            .build()
            .set("changeSet", path.getChangeSet().getId())
            .set("diff", "#diff-" + getIndexOfPath(path))
            .expand()
    );
}
 
Example #26
Source File: GiteaBrowser.java    From gitea-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
    return new URL(
            UriTemplate.buildFromTemplate(getRepoUrl())
                    .literal("/commit")
                    .path(UriTemplateBuilder.var("changeSet"))
                    .build()
                    .set("changeSet", changeSet.getId())
                    .expand()
    );
}
 
Example #27
Source File: GiteaBrowser.java    From gitea-plugin with MIT License 5 votes vote down vote up
/**
 * Generates a diff link for the supplied path.
 * @param path the path.
 * @return the diff link.
 * @throws IOException if there was an error parsing the index of the path from the changeset.
 */
private URL diffLink(Path path) throws IOException {
    return new URL(
            UriTemplate.buildFromTemplate(getRepoUrl())
                    .literal("/commit")
                    .path(UriTemplateBuilder.var("changeSet"))
                    .fragment(UriTemplateBuilder.var("diff"))
                    .build()
                    .set("changeSet", path.getChangeSet().getId())
                    .set("diff", "diff-" + (getIndexOfPath(path) + 1))
                    .expand()
    );
}
 
Example #28
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public GiteaUser fetchUser(String name) throws IOException, InterruptedException {
    return getObject(
            api()
                    .literal("/users")
                    .path(UriTemplateBuilder.var("name"))
                    .build()
                    .set("name", name),
            GiteaUser.class
    );
}
 
Example #29
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public GiteaOrganization fetchOrganization(String name) throws IOException, InterruptedException {
    return getObject(
            api()
                    .literal("/orgs")
                    .path(UriTemplateBuilder.var("name"))
                    .build()
                    .set("name", name),
            GiteaOrganization.class
    );
}
 
Example #30
Source File: DefaultGiteaConnection.java    From gitea-plugin with MIT License 5 votes vote down vote up
@Override
public GiteaRepository fetchRepository(String username, String name) throws IOException, InterruptedException {
    return getObject(
            api()
                    .literal("/repos")
                    .path(UriTemplateBuilder.var("username"))
                    .path(UriTemplateBuilder.var("name"))
                    .build()
                    .set("username", username)
                    .set("name", name),
            GiteaRepository.class
    );
}