Commit ae9200f0 authored by Greg Messner's avatar Greg Messner
Browse files

Mods to use enums for archive format.

parent 1f358da7
package org.gitlab4j.api;
import java.util.HashMap;
import java.util.Map;
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
import com.fasterxml.jackson.annotation.JsonCreator;
......@@ -438,4 +441,41 @@ public interface Constants {
return (enumHelper.toString(this));
}
}
/** Enum to specify the format of a downloaded archive. */
public enum ArchiveFormat {
BZ2, TAR, TAR_BZ2, TAR_GZ, TB2, TBZ, TBZ2, ZIP;
private final String value;
ArchiveFormat() {
this.value = name().toLowerCase().replace('_', '.');
}
private static Map<String, ArchiveFormat> valuesMap = new HashMap<String, ArchiveFormat>(8);
static {
for (ArchiveFormat archiveFormat : ArchiveFormat.values())
valuesMap.put(archiveFormat.value, archiveFormat);
}
public static ArchiveFormat forValue(String value) throws GitLabApiException {
if (value == null || value.trim().isEmpty()) {
return (null);
}
ArchiveFormat archiveFormat = valuesMap.get(value);
if (archiveFormat != null) {
return (archiveFormat);
}
throw new GitLabApiException("Invalid format! Options are tar.gz, tar.bz2, tbz, tbz2, tb2, bz2, tar, and zip.");
}
@Override
public String toString() {
return (value);
}
}
}
......@@ -7,10 +7,8 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
......@@ -29,19 +27,6 @@ import org.gitlab4j.api.utils.FileUtils;
*/
public class RepositoryApi extends AbstractApi {
private static final Set<String> validFormat = new HashSet<String>(){
{
add("tar.bz2");
add("tbz");
add("tbz2");
add("tb2");
add("bz2");
add("tar");
add("zip");
add("tar.gz");
}
};
public RepositoryApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
......@@ -451,29 +436,44 @@ public class RepositoryApi extends AbstractApi {
*
* GET /projects/:id/repository/archive
*
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
* So we must check format if it is valid.
* @param projectId the ID of the project
* @param sha the SHA of the archive to get
* @param format The archive format, defaults to "tar.gz" if null
* @return an input stream that can be used to save as a file or to read the content of the archive
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
*/
public InputStream getRepositoryArchive(Integer projectId, String sha, String format) throws GitLabApiException {
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
return (getRepositoryArchive(projectId, sha, archiveFormat));
}
/**
* Get an archive of the complete repository by SHA (optional).
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
* GET /projects/:id/repository/archive
*
* @param projectId the ID of the project
* @param sha the SHA of the archive to get
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
* tb2, bz2, tar, zip
* @return an input stream that can be used to save as a file
* or to read the content of the archive
* @param format The archive format, defaults to TAR_GZ if null
* @return an input stream that can be used to save as a file or to read the content of the archive
* @throws GitLabApiException if any exception occurs
*/
public InputStream getRepositoryArchive(Integer projectId, String sha, String format) throws GitLabApiException {
public InputStream getRepositoryArchive(Integer projectId, String sha, ArchiveFormat format) throws GitLabApiException {
// Throws a GitLabApiException if format is invalid
format = checkFormat(format);
if (format == null) {
format = ArchiveFormat.TAR_GZ;
}
/*
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
*/
Form formData = new GitLabApiForm().withParam("sha", sha);
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
"projects", projectId, "repository", "archive", ".", format);
"projects", projectId, "repository", "archive", ".", format.toString());
return (response.readEntity(InputStream.class));
}
......@@ -518,29 +518,47 @@ public class RepositoryApi extends AbstractApi {
*
* GET /projects/:id/repository/archive
*
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
* So we must check format if it is valid.
* @param projectId the ID of the project
* @param sha the SHA of the archive to get
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
* @param format The archive format, defaults to "tar.gz" if null
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if format is not a valid archive format or any exception occurs
*/
public File getRepositoryArchive(Integer projectId, String sha, File directory, String format) throws GitLabApiException {
ArchiveFormat archiveFormat = ArchiveFormat.forValue(format);
return (getRepositoryArchive(projectId, sha, directory, archiveFormat));
}
/**
* Get an archive of the complete repository by SHA (optional) and saves to the specified directory.
* If the archive already exists in the directory it will be overwritten.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
* GET /projects/:id/repository/archive
*
* @param projectId the ID of the project
* @param sha the SHA of the archive to get
* @param directory the File instance of the directory to save the archive to, if null will use "java.io.tmpdir"
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
* tb2, bz2, tar, zip
* @param format The archive format, defaults to TAR_GZ if null
* @return a File instance pointing to the downloaded instance
* @throws GitLabApiException if any exception occurs
*/
public File getRepositoryArchive(Integer projectId, String sha, File directory, String format) throws GitLabApiException {
public File getRepositoryArchive(Integer projectId, String sha, File directory, ArchiveFormat format) throws GitLabApiException {
// Throws a GitLabApiException if format is invalid
format = checkFormat(format);
if (format == null) {
format = ArchiveFormat.TAR_GZ;
}
/*
* Gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
*/
Form formData = new GitLabApiForm().withParam("sha", sha);
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
"projects", projectId, "repository", "archive", ".", format);
"projects", projectId, "repository", "archive", ".", format.toString());
try {
......@@ -642,42 +660,4 @@ public class RepositoryApi extends AbstractApi {
public Pager<Contributor> getContributors(Integer projectId, int itemsPerPage) throws GitLabApiException {
return new Pager<Contributor>(this, Contributor.class, itemsPerPage, null, "projects", projectId, "repository", "contributors");
}
/* gitlab-ce/lib/gitlab/git/repository.rb #386 */
/*
extension =
case format
when "tar.bz2", "tbz", "tbz2", "tb2", "bz2"
"tar.bz2"
when "tar"
"tar"
when "zip"
"zip"
else
# everything else should fall back to tar.gz
"tar.gz"
end
*/
/**
* While gitlab-ce has a bug when you try to download file archives with format by using "&format=zip(or tar... etc.)",
* there is a solution to request .../archive.:format instead of .../archive?format=:format.
* We must check format if it is valid.
*
* Issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/45992
* https://gitlab.com/gitlab-com/support-forum/issues/3067
*
* @param format The archive format. Default is tar.gz. Options are tar.gz, tar.bz2, tbz, tbz2,
* tb2, bz2, tar, zip
* @return A valid format. Default is tar.gz.
*/
private String checkFormat(String format) throws GitLabApiException {
if(format == null || format.isEmpty())
return "tar.gz";
if(!validFormat.contains(format))
throw new GitLabApiException("Invalid format! Options are tar.gz, tar.bz2, tbz, tbz2, tb2, bz2, tar, zip.");
return format;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment