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

Added encode/decode convenience methods for content (#240).

parent 0398df3c
package org.gitlab4j.api.models;
import java.util.Base64;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnore;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class RepositoryFile {
......@@ -90,4 +94,73 @@ public class RepositoryFile {
public void setLastCommitId(String lastCommitId) {
this.lastCommitId = lastCommitId;
}
/**
* Returns the content as a String, base64 decoding it if necessary.
* For binary files it is recommended to use getDecodedContentAsBytes()
*
* @return the content as a String, base64 decoding it if necessary
*/
@JsonIgnore
public String getDecodedContentAsString() {
if (content == null) {
return (null);
}
if ("base64".equalsIgnoreCase(encoding)) {
return (new String(Base64.getDecoder().decode(content)));
}
return (content);
}
/**
* Returns the content as a byte array, decoding from base64 if necessary.
* For String content it is recommended to use getDecodedContent().
*
* @return the content as a byte array, decoding from base64 if necessary
*/
@JsonIgnore
public byte[] getDecodedContentAsBytes() {
if (content == null) {
return (null);
}
if ("base64".equalsIgnoreCase(encoding)) {
return (Base64.getDecoder().decode(content));
}
return (content.getBytes());
}
/**
* Encodes the provided String using Base64 and sets it as the content. The encoding
* property of this instance will be set to base64.
*
* @param content the String content to encode and set as the base64 encoded String content
*/
@JsonIgnore
public void encodeAndSetContent(String content) {
encodeAndSetContent(content != null ? content.getBytes() : null);
}
/**
* Encodes the provided byte array using Base64 and sets it as the content. The encoding
* property of this instance will be set to base64.
*
* @param content the byte[] content to encode and set as the base64 encoded String content
*/
@JsonIgnore
public void encodeAndSetContent(byte[] byteContent) {
if (byteContent == null) {
byteContent = null;
return;
}
this.content = Base64.getEncoder().encodeToString(byteContent);
encoding = "base64";
}
}
......@@ -44,7 +44,6 @@ public class TestRepositoryFileApi {
private static final String TEST_NAMESPACE;
private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
static {
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
......@@ -52,6 +51,9 @@ public class TestRepositoryFileApi {
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
}
private static final String TEST_CONTENT = "This is some content to test file content 1234567890 !@#$%^&().";
private static final String TEST_ADDITIONAL_CONTENT = "\n\nThis is an additional line";
private static final String TEST_BRANCH_NAME = "feature/test_branch_for_files";
private static final String TEST_FILEPATH = "test-file.txt";
private static GitLabApi gitLabApi;
......@@ -183,7 +185,7 @@ public class TestRepositoryFileApi {
RepositoryFile file = new RepositoryFile();
file.setFilePath(TEST_FILEPATH);
file.setContent("This is a test file.");
file.setContent(TEST_CONTENT);
RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(file, project.getId(), TEST_BRANCH_NAME, "Testing createFile().");
assertNotNull(createdFile);
......@@ -209,4 +211,39 @@ public class TestRepositoryFileApi {
gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Testing deleteFile().");
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
}
@Test
public void testUpdateFile() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master");
assertNotNull(branch);
RepositoryFile file = new RepositoryFile();
file.setFilePath(TEST_FILEPATH);
file.setContent(TEST_CONTENT);
RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(file, project.getId(), TEST_BRANCH_NAME, "Testing createFile().");
assertNotNull(createdFile);
Optional<RepositoryFile> optionalFile = gitLabApi.getRepositoryFileApi().getOptionalFile(project, TEST_FILEPATH, TEST_BRANCH_NAME);
assertTrue(optionalFile.isPresent());
RepositoryFile newFile = optionalFile.get();
assertEquals(TEST_CONTENT, newFile.getDecodedContentAsString());
String newContent = TEST_CONTENT + TEST_ADDITIONAL_CONTENT;
file = new RepositoryFile();
file.setFilePath(TEST_FILEPATH);
file.encodeAndSetContent(newContent);
gitLabApi.getRepositoryFileApi().updateFile(file, project.getId(), TEST_BRANCH_NAME, "Testing updateFile().");
optionalFile = gitLabApi.getRepositoryFileApi().getOptionalFile(project, TEST_FILEPATH, TEST_BRANCH_NAME);
assertTrue(optionalFile.isPresent());
RepositoryFile updatedFile = optionalFile.get();
assertEquals(newContent, updatedFile.getDecodedContentAsString());
gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Testing deleteFile().");
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
}
}
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