Skip to content

Commit f6edda9

Browse files
committed
Added encode/decode convenience methods for content (#240).
1 parent 0398df3 commit f6edda9

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed

src/main/java/org/gitlab4j/api/models/RepositoryFile.java

+73
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11

22
package org.gitlab4j.api.models;
33

4+
import java.util.Base64;
5+
46
import javax.xml.bind.annotation.XmlAccessType;
57
import javax.xml.bind.annotation.XmlAccessorType;
68
import javax.xml.bind.annotation.XmlRootElement;
79

10+
import com.fasterxml.jackson.annotation.JsonIgnore;
11+
812
@XmlRootElement
913
@XmlAccessorType(XmlAccessType.FIELD)
1014
public class RepositoryFile {
@@ -90,4 +94,73 @@ public String getLastCommitId() {
9094
public void setLastCommitId(String lastCommitId) {
9195
this.lastCommitId = lastCommitId;
9296
}
97+
98+
/**
99+
* Returns the content as a String, base64 decoding it if necessary.
100+
* For binary files it is recommended to use getDecodedContentAsBytes()
101+
*
102+
* @return the content as a String, base64 decoding it if necessary
103+
*/
104+
@JsonIgnore
105+
public String getDecodedContentAsString() {
106+
107+
if (content == null) {
108+
return (null);
109+
}
110+
111+
if ("base64".equalsIgnoreCase(encoding)) {
112+
return (new String(Base64.getDecoder().decode(content)));
113+
}
114+
115+
return (content);
116+
}
117+
118+
/**
119+
* Returns the content as a byte array, decoding from base64 if necessary.
120+
* For String content it is recommended to use getDecodedContent().
121+
*
122+
* @return the content as a byte array, decoding from base64 if necessary
123+
*/
124+
@JsonIgnore
125+
public byte[] getDecodedContentAsBytes() {
126+
127+
if (content == null) {
128+
return (null);
129+
}
130+
131+
if ("base64".equalsIgnoreCase(encoding)) {
132+
return (Base64.getDecoder().decode(content));
133+
}
134+
135+
return (content.getBytes());
136+
}
137+
138+
/**
139+
* Encodes the provided String using Base64 and sets it as the content. The encoding
140+
* property of this instance will be set to base64.
141+
*
142+
* @param content the String content to encode and set as the base64 encoded String content
143+
*/
144+
@JsonIgnore
145+
public void encodeAndSetContent(String content) {
146+
encodeAndSetContent(content != null ? content.getBytes() : null);
147+
}
148+
149+
/**
150+
* Encodes the provided byte array using Base64 and sets it as the content. The encoding
151+
* property of this instance will be set to base64.
152+
*
153+
* @param content the byte[] content to encode and set as the base64 encoded String content
154+
*/
155+
@JsonIgnore
156+
public void encodeAndSetContent(byte[] byteContent) {
157+
158+
if (byteContent == null) {
159+
byteContent = null;
160+
return;
161+
}
162+
163+
this.content = Base64.getEncoder().encodeToString(byteContent);
164+
encoding = "base64";
165+
}
93166
}

src/test/java/org/gitlab4j/api/TestRepositoryFileApi.java

+39-2
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ public class TestRepositoryFileApi {
4444
private static final String TEST_NAMESPACE;
4545
private static final String TEST_HOST_URL;
4646
private static final String TEST_PRIVATE_TOKEN;
47-
4847
static {
4948
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
5049
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
5150
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
5251
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
5352
}
5453

54+
private static final String TEST_CONTENT = "This is some content to test file content 1234567890 !@#$%^&().";
55+
private static final String TEST_ADDITIONAL_CONTENT = "\n\nThis is an additional line";
56+
5557
private static final String TEST_BRANCH_NAME = "feature/test_branch_for_files";
5658
private static final String TEST_FILEPATH = "test-file.txt";
5759
private static GitLabApi gitLabApi;
@@ -183,7 +185,7 @@ public void testCreateFileAndDeleteFile() throws GitLabApiException {
183185

184186
RepositoryFile file = new RepositoryFile();
185187
file.setFilePath(TEST_FILEPATH);
186-
file.setContent("This is a test file.");
188+
file.setContent(TEST_CONTENT);
187189
RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(file, project.getId(), TEST_BRANCH_NAME, "Testing createFile().");
188190
assertNotNull(createdFile);
189191

@@ -209,4 +211,39 @@ public void testCreateFileWithEmptyContent() throws GitLabApiException {
209211
gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Testing deleteFile().");
210212
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
211213
}
214+
215+
@Test
216+
public void testUpdateFile() throws GitLabApiException {
217+
218+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
219+
assertNotNull(project);
220+
221+
Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master");
222+
assertNotNull(branch);
223+
224+
RepositoryFile file = new RepositoryFile();
225+
file.setFilePath(TEST_FILEPATH);
226+
file.setContent(TEST_CONTENT);
227+
RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(file, project.getId(), TEST_BRANCH_NAME, "Testing createFile().");
228+
assertNotNull(createdFile);
229+
230+
Optional<RepositoryFile> optionalFile = gitLabApi.getRepositoryFileApi().getOptionalFile(project, TEST_FILEPATH, TEST_BRANCH_NAME);
231+
assertTrue(optionalFile.isPresent());
232+
RepositoryFile newFile = optionalFile.get();
233+
assertEquals(TEST_CONTENT, newFile.getDecodedContentAsString());
234+
235+
String newContent = TEST_CONTENT + TEST_ADDITIONAL_CONTENT;
236+
file = new RepositoryFile();
237+
file.setFilePath(TEST_FILEPATH);
238+
file.encodeAndSetContent(newContent);
239+
gitLabApi.getRepositoryFileApi().updateFile(file, project.getId(), TEST_BRANCH_NAME, "Testing updateFile().");
240+
241+
optionalFile = gitLabApi.getRepositoryFileApi().getOptionalFile(project, TEST_FILEPATH, TEST_BRANCH_NAME);
242+
assertTrue(optionalFile.isPresent());
243+
RepositoryFile updatedFile = optionalFile.get();
244+
assertEquals(newContent, updatedFile.getDecodedContentAsString());
245+
246+
gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Testing deleteFile().");
247+
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
248+
}
212249
}

0 commit comments

Comments
 (0)