Skip to content

Commit

Permalink
Development: Modernize Java server code (#10221)
Browse files Browse the repository at this point in the history
  • Loading branch information
krusche authored Feb 11, 2025
1 parent 3c8453c commit 9549046
Show file tree
Hide file tree
Showing 54 changed files with 218 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_ATHENA;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -79,11 +78,11 @@ private void checkFeedbackSuggestionsOrAutomaticFeedbackEnabledElseThrow(Exercis
* @param exerciseId the id of the exercise to export the repository for
* @param submissionId the id of the submission to export the repository for (only for student repository, otherwise pass null)
* @param repositoryType the type of repository to export. Pass null to export the student repository.
* @return the zip file containing the exported repository
* @return the path to the zip file containing the exported repository
* @throws IOException if the export fails
* @throws AccessForbiddenException if the feedback suggestions are not enabled for the given exercise
*/
public File exportRepository(long exerciseId, Long submissionId, RepositoryType repositoryType) throws IOException {
public Path exportRepository(long exerciseId, Long submissionId, RepositoryType repositoryType) throws IOException {
log.debug("Exporting repository for exercise {}, submission {}", exerciseId, submissionId);

var programmingExercise = programmingExerciseRepository.findByIdElseThrow(exerciseId);
Expand All @@ -97,27 +96,27 @@ public File exportRepository(long exerciseId, Long submissionId, RepositoryType
}

Path exportDir = fileService.getTemporaryUniqueSubfolderPath(repoDownloadClonePath, 15);
Path zipFile = null;
Path zipFilePath = null;

if (repositoryType == null) { // Export student repository
var submission = programmingSubmissionRepository.findById(submissionId).orElseThrow();
// Load participation with eager submissions
var participation = programmingExerciseStudentParticipationRepository.findWithSubmissionsById(submission.getParticipation().getId()).getFirst();
zipFile = programmingExerciseExportService.getRepositoryWithParticipation(programmingExercise, participation, exportOptions, exportDir, exportDir, true);
zipFilePath = programmingExerciseExportService.getRepositoryWithParticipation(programmingExercise, participation, exportOptions, exportDir, exportDir, true);
}
else {
List<String> exportErrors = List.of();
var exportFile = programmingExerciseExportService.exportInstructorRepositoryForExercise(programmingExercise.getId(), repositoryType, exportDir, exportDir,
exportErrors);
if (exportFile.isPresent()) {
zipFile = exportFile.get().toPath();
zipFilePath = exportFile.get().toPath();
}
}

if (zipFile == null) {
if (zipFilePath == null) {
throw new IOException("Failed to export repository");
}

return zipFile.toFile();
return zipFilePath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_BUILDAGENT;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -353,8 +354,8 @@ public void checkUsableDiskSpaceThenCleanUp() {

try {
// Get the Docker root directory to check disk space.
File dockerRootDirectory = new File(Objects.requireNonNullElse(dockerClient.infoCmd().exec().getDockerRootDir(), "/"));
long usableSpace = dockerRootDirectory.getUsableSpace();
Path dockerRootDirectory = Path.of(Objects.requireNonNullElse(dockerClient.infoCmd().exec().getDockerRootDir(), "/"));
long usableSpace = Files.getFileStore(dockerRootDirectory).getUsableSpace();

long threshold = convertMegabytesToBytes(imageCleanupDiskSpaceThresholdMb);

Expand Down Expand Up @@ -385,7 +386,7 @@ public void checkUsableDiskSpaceThenCleanUp() {
log.info("Remove oldest docker image {} to cleanup disk space to avoid filling up the hard disk", oldestImage.getKey());
try {
dockerClient.removeImageCmd(oldestImage.getKey()).exec();
usableSpace = dockerRootDirectory.getUsableSpace();
usableSpace = Files.getFileStore(dockerRootDirectory).getUsableSpace();
deleteAttempts--;
}
catch (NotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -18,6 +17,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
Expand Down Expand Up @@ -349,7 +349,7 @@ private void createScriptFile(String buildJobContainerId) {
}

private void addAndPrepareDirectoryAndReplaceContent(String containerId, Path repositoryPath, String newDirectoryName) {
copyToContainer(repositoryPath.toString(), containerId);
copyToContainer(repositoryPath, containerId);
addDirectory(containerId, newDirectoryName, true);
insertRepositoryFiles(containerId, LOCALCI_WORKING_DIRECTORY + "/" + repositoryPath.getFileName().toString(), newDirectoryName);
}
Expand All @@ -363,7 +363,7 @@ private void addDirectory(String containerId, String directoryName, boolean crea
executeDockerCommand(containerId, null, false, false, true, command);
}

private void copyToContainer(String sourcePath, String containerId) {
private void copyToContainer(Path sourcePath, String containerId) {
try (final var uploadStream = new ByteArrayInputStream(createTarArchive(sourcePath).toByteArray());
final var copyToContainerCommand = buildAgentConfiguration.getDockerClient().copyArchiveToContainerCmd(containerId).withRemotePath(LOCALCI_WORKING_DIRECTORY)
.withTarInputStream(uploadStream)) {
Expand All @@ -374,8 +374,7 @@ private void copyToContainer(String sourcePath, String containerId) {
}
}

private ByteArrayOutputStream createTarArchive(String sourcePath) {
Path path = Paths.get(sourcePath);
private ByteArrayOutputStream createTarArchive(Path sourcePath) {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

Expand All @@ -385,37 +384,37 @@ private ByteArrayOutputStream createTarArchive(String sourcePath) {
tarArchiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

try {
addFileToTar(tarArchiveOutputStream, path.toFile(), "");
addFileToTar(tarArchiveOutputStream, sourcePath, "");
}
catch (IOException e) {
throw new LocalCIException("Could not create tar archive", e);
}
return byteArrayOutputStream;
}

private void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, File file, String parent) throws IOException {
TarArchiveEntry tarEntry = new TarArchiveEntry(file, parent + file.getName());
private void addFileToTar(TarArchiveOutputStream tarArchiveOutputStream, Path path, String parent) throws IOException {
TarArchiveEntry tarEntry = new TarArchiveEntry(path, parent + path.getFileName());
tarArchiveOutputStream.putArchiveEntry(tarEntry);

if (file.isFile()) {
try (FileInputStream fis = new FileInputStream(file)) {
if (Files.isRegularFile(path)) {
try (InputStream is = Files.newInputStream(path)) {
byte[] buffer = new byte[1024];
int count;
while ((count = fis.read(buffer)) != -1) {
while ((count = is.read(buffer)) != -1) {
tarArchiveOutputStream.write(buffer, 0, count);
}
}
tarArchiveOutputStream.closeArchiveEntry();
}
else {
tarArchiveOutputStream.closeArchiveEntry();
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
addFileToTar(tarArchiveOutputStream, child, parent + file.getName() + "/");
try (Stream<Path> children = Files.list(path)) {
for (Path child : children.toList()) {
addFileToTar(tarArchiveOutputStream, child, parent + path.getFileName() + "/");
}
}
}

}

private void executeDockerCommandWithoutAwaitingResponse(String containerId, String... command) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -578,7 +577,7 @@ private Path cloneRepository(VcsRepositoryUri repositoryUri, @Nullable String co
private void deleteCloneRepo(VcsRepositoryUri repositoryUri, @Nullable String commitHash, String buildJobId, Path repositoryPath) {
String msg;
try {
Path repositoryPathForDeletion = commitHash != null ? Paths.get(CHECKED_OUT_REPOS_TEMP_DIR, commitHash, repositoryUri.folderNameForRepositoryUri()) : repositoryPath;
Path repositoryPathForDeletion = commitHash != null ? Path.of(CHECKED_OUT_REPOS_TEMP_DIR, commitHash, repositoryUri.folderNameForRepositoryUri()) : repositoryPath;
Repository repository = buildJobGitService.getExistingCheckedOutRepositoryByLocalPath(repositoryPathForDeletion, repositoryUri, defaultBranch);
if (repository == null) {
msg = "Repository with commit hash " + commitHash + " not found";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package de.tum.cit.aet.artemis.core.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
Expand Down Expand Up @@ -117,27 +116,27 @@ private boolean checkFiles(SAML2Properties.RelyingPartyProperties config) {
return false;
}

File keyFile = new File(config.getKeyFile());
File certFile = new File(config.getCertFile());
Path keyFile = Path.of(config.getKeyFile());
Path certFile = Path.of(config.getCertFile());

if (!keyFile.exists() || !certFile.exists()) {
if (!Files.exists(keyFile) || !Files.exists(certFile)) {
log.error("Keyfile or Certfile for SAML[{}] does not exist.", config.getRegistrationId());
return false;
}

return true;
}

private static X509Certificate readPublicCert(String file) throws IOException, CertificateException {
try (InputStream inStream = new FileInputStream(file)) {
private static X509Certificate readPublicCert(String filepath) throws IOException, CertificateException {
try (InputStream inStream = Files.newInputStream(Path.of(filepath))) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return (X509Certificate) cf.generateCertificate(inStream);
}
}

private RSAPrivateKey readPrivateKey(String file) throws IOException {
private RSAPrivateKey readPrivateKey(String filepath) throws IOException {
// Read PKCS#8 File!
try (var keyReader = new FileReader(file, StandardCharsets.UTF_8); var pemParser = new PEMParser(keyReader)) {
try (var keyReader = Files.newBufferedReader(Path.of(filepath), StandardCharsets.UTF_8); var pemParser = new PEMParser(keyReader)) {
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(pemParser.readObject());
return (RSAPrivateKey) converter.getPrivateKey(privateKeyInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_CORE;
import static java.net.URLDecoder.decode;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EnumSet;

Expand Down Expand Up @@ -96,9 +96,9 @@ private void setMimeMappings(WebServerFactory server) {
private void setLocationForStaticAssets(WebServerFactory server) {
if (server instanceof ConfigurableServletWebServerFactory servletWebServer) {
String prefixPath = resolvePathPrefix();
File root = new File(prefixPath + "build/resources/main/static/");
if (root.exists() && root.isDirectory()) {
servletWebServer.setDocumentRoot(root);
Path root = Path.of(prefixPath + "build/resources/main/static/");
if (Files.exists(root) && Files.isDirectory(root)) {
servletWebServer.setDocumentRoot(root.toFile());
}
}
}
Expand Down
33 changes: 14 additions & 19 deletions src/main/java/de/tum/cit/aet/artemis/core/service/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
import static de.tum.cit.aet.artemis.core.config.Constants.PROFILE_CORE;
import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -501,12 +496,11 @@ private boolean isIgnoredDirectory(final Path filePath) {
* @throws IOException if the directory could not be renamed.
*/
public void renameDirectory(Path oldDirectoryPath, Path targetDirectoryPath) throws IOException {
File oldDirectory = oldDirectoryPath.toFile();
if (!oldDirectory.exists()) {
log.error("Directory {} should be renamed but does not exist.", oldDirectoryPath);
throw new RuntimeException("Directory " + oldDirectoryPath + " should be renamed but does not exist.");
}

if (!Files.exists(oldDirectoryPath)) {
throw new FilePathParsingException("Directory " + oldDirectoryPath + " should be renamed but does not exist.");
}
File oldDirectory = oldDirectoryPath.toFile();
File targetDirectory = targetDirectoryPath.toFile();

FileUtils.moveDirectory(oldDirectory, targetDirectory);
Expand All @@ -521,12 +515,12 @@ public void renameDirectory(Path oldDirectoryPath, Path targetDirectoryPath) thr
public void replacePlaceholderSections(Path filePath, Map<String, Boolean> sections) {
Map<Pattern, Boolean> patternBooleanMap = sections.entrySet().stream().collect(Collectors.toMap(e -> Pattern.compile(".*%" + e.getKey() + ".*%.*"), Map.Entry::getValue));
File file = filePath.toFile();
File tempFile = new File(filePath + "_temp");
File tempFile = Path.of(filePath + "_temp").toFile();
if (!file.exists()) {
throw new FilePathParsingException("File " + filePath + " should be updated but does not exist.");
}

try (var reader = new BufferedReader(new FileReader(file, UTF_8)); var writer = new BufferedWriter(new FileWriter(tempFile, UTF_8))) {
try (var reader = Files.newBufferedReader(file.toPath(), UTF_8); var writer = Files.newBufferedWriter(tempFile.toPath(), UTF_8)) {
Map.Entry<Pattern, Boolean> matchingStartPattern = null;
String line = reader.readLine();
while (line != null) {
Expand Down Expand Up @@ -599,12 +593,13 @@ public void replaceVariablesInDirectoryName(Path startPath, String targetString,
if (pathString.contains(targetString)) {
log.debug("Target String found, replacing..");
String targetPath = pathString.replace(targetString, replacementString);
renameDirectory(startPath, Path.of(targetPath));
directory = new File(targetPath);
final var path = Path.of(targetPath);
renameDirectory(startPath, path);
directory = path.toFile();
}

// Get all subdirectories
final var subDirectories = directory.list((current, name) -> new File(current, name).isDirectory());
final var subDirectories = directory.list((current, name) -> current.toPath().resolve(name).toFile().isDirectory());

if (subDirectories != null) {
for (String subDirectory : subDirectories) {
Expand Down Expand Up @@ -634,7 +629,7 @@ public void replaceVariablesInFilename(Path startPath, String targetString, Stri
try {
// We expect the strings to be clean already, so the filename shouldn't change. If it does, we are on the safe side with the sanitation.
String cleanFileName = sanitizeFilename(filePath.toString().replace(targetString, replacementString));
FileUtils.moveFile(filePath.toFile(), new File(cleanFileName));
FileUtils.moveFile(filePath.toFile(), Path.of(cleanFileName).toFile());
}
catch (IOException e) {
throw new RuntimeException("File " + filePath + " should be replaced but does not exist.");
Expand Down Expand Up @@ -672,7 +667,7 @@ public void replaceVariablesInFileRecursive(Path startPath, Map<String, String>
}

// Get all files in directory
String[] files = directory.list((current, name) -> new File(current, name).isFile());
String[] files = directory.list((current, name) -> current.toPath().resolve(name).toFile().isFile());
if (files != null) {
// filter out files that should be ignored
files = Arrays.stream(files).filter(Predicate.not(filesToIgnore::contains)).toArray(String[]::new);
Expand All @@ -682,7 +677,7 @@ public void replaceVariablesInFileRecursive(Path startPath, Map<String, String>
}

// Recursive call: get all subdirectories
String[] subDirectories = directory.list((current, name) -> new File(current, name).isDirectory());
String[] subDirectories = directory.list((current, name) -> current.toPath().resolve(name).toFile().isDirectory());
if (subDirectories != null) {
for (String subDirectory : subDirectories) {
if (subDirectory.equalsIgnoreCase(".git")) {
Expand Down Expand Up @@ -1040,7 +1035,7 @@ public MultipartFile convertByteArrayToMultipart(String filename, String extensi
FileItem fileItem = new DiskFileItem(cleanFilename, Files.probeContentType(tempPath), false, outputFile.getName(), (int) outputFile.length(),
outputFile.getParentFile());

try (InputStream input = new FileInputStream(outputFile); OutputStream fileItemOutputStream = fileItem.getOutputStream()) {
try (InputStream input = Files.newInputStream(outputFile.toPath()); OutputStream fileItemOutputStream = fileItem.getOutputStream()) {
IOUtils.copy(input, fileItemOutputStream);
}
return new CommonsMultipartFile(fileItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -216,7 +215,7 @@ public Path getResourceFilePath(Path path) throws IOException, URISyntaxExceptio

if ("file".equals(resourceUrl.getProtocol())) {
// Resource is in the file system.
return Paths.get(resourceUrl.toURI());
return Path.of(resourceUrl.toURI());
}
else if ("jar".equals(resourceUrl.getProtocol())) {
// Resource is in a jar file.
Expand Down
Loading

0 comments on commit 9549046

Please sign in to comment.