@@ -1,7 +1,10 @@
|
||||
package ru.di9.mirror.core.domain;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
@Slf4j
|
||||
public record ArtifactRecord(String group, String artifactId, String version, boolean isSnapshot) {
|
||||
private static final String SNAPSHOT = "SNAPSHOT";
|
||||
|
||||
@@ -9,6 +12,7 @@ public record ArtifactRecord(String group, String artifactId, String version, bo
|
||||
if (httpPath.startsWith("/")) {
|
||||
httpPath = httpPath.substring(1);
|
||||
}
|
||||
log.info("httpPath = {}", httpPath);
|
||||
|
||||
String[] partsPath = httpPath.split("/");
|
||||
String version = partsPath[partsPath.length-2];
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package ru.di9.mirror.core.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.bson.types.ObjectId;
|
||||
import ru.di9.mirror.core.domain.ArtifactRecord;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(exclude = {"id"})
|
||||
public class ArtifactEntity {
|
||||
private ObjectId id;
|
||||
|
||||
private String group;
|
||||
private String artifactId;
|
||||
private String version;
|
||||
private boolean isSnapshot;
|
||||
|
||||
public static ArtifactEntity fromRecord(ArtifactRecord artifactRecord) {
|
||||
var entity = new ArtifactEntity();
|
||||
|
||||
entity.setGroup(artifactRecord.group());
|
||||
entity.setArtifactId(artifactRecord.artifactId());
|
||||
entity.setVersion(artifactRecord.version());
|
||||
entity.setSnapshot(artifactRecord.isSnapshot());
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.di9.mirror.core.domain.ArtifactRecord;
|
||||
import ru.di9.mirror.core.domain.FileInfo;
|
||||
import ru.di9.mirror.core.entity.ArtifactEntity;
|
||||
import ru.di9.mirror.core.handler.response.GetFileResponse;
|
||||
import ru.di9.mirror.core.repository.ArtifactRepository;
|
||||
import ru.di9.mirror.core.service.ExternalMavenService;
|
||||
import ru.di9.mirror.core.service.MinioService;
|
||||
|
||||
@@ -18,14 +20,16 @@ public class MavenHandler {
|
||||
|
||||
private final MinioService minioService;
|
||||
private final List<ExternalMavenService> externalMavenServices;
|
||||
private final ArtifactRepository repository;
|
||||
|
||||
public Optional<GetFileResponse> getFile(String path) {
|
||||
var fileInfo = FileInfo.of(path.substring(path.lastIndexOf("/") + 1));
|
||||
|
||||
ArtifactRecord artifactRecord = null;
|
||||
if (!fileInfo.fullName().equalsIgnoreCase("maven-metadata.xml")
|
||||
|| !fileInfo.ext().equalsIgnoreCase("sha1")) {
|
||||
&& !fileInfo.ext().equalsIgnoreCase("sha1")) {
|
||||
|
||||
ArtifactRecord artifactRecord = ArtifactRecord.fromHttpPath(path);
|
||||
artifactRecord = ArtifactRecord.fromHttpPath(path);
|
||||
log.info(artifactRecord.toString());
|
||||
}
|
||||
|
||||
@@ -34,11 +38,11 @@ public class MavenHandler {
|
||||
return optionalInputStream
|
||||
.map(inputStream -> new GetFileResponse(fileInfo.name(), inputStream));
|
||||
} else {
|
||||
return findInMirrors(path, fileInfo.name());
|
||||
return findInMirrors(path, fileInfo.name(), artifactRecord);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<GetFileResponse> findInMirrors(String path, String fileName) {
|
||||
private Optional<GetFileResponse> findInMirrors(String path, String fileName, ArtifactRecord artifactRecord) {
|
||||
Optional<InputStream> result;
|
||||
for (ExternalMavenService externalMavenService : externalMavenServices) {
|
||||
final String nameForStore = "/" + externalMavenService.getId() + "/" + path;
|
||||
@@ -50,6 +54,10 @@ public class MavenHandler {
|
||||
} else {
|
||||
result = externalMavenService.getFile(path);
|
||||
if (result.isPresent()) {
|
||||
if (artifactRecord != null) {
|
||||
repository.save(ArtifactEntity.fromRecord(artifactRecord));
|
||||
}
|
||||
|
||||
minioService.put(nameForStore, result.get());
|
||||
return minioService.get(nameForStore)
|
||||
.map(inputStream -> new GetFileResponse(fileName, inputStream));
|
||||
@@ -64,7 +72,7 @@ public class MavenHandler {
|
||||
var fileInfo = FileInfo.of(path.substring(path.lastIndexOf("/") + 1));
|
||||
|
||||
if (!fileInfo.fullName().equalsIgnoreCase("maven-metadata.xml")
|
||||
|| !fileInfo.ext().equalsIgnoreCase("sha1")) {
|
||||
&& !fileInfo.ext().equalsIgnoreCase("sha1")) {
|
||||
|
||||
ArtifactRecord artifactRecord = ArtifactRecord.fromHttpPath(path);
|
||||
log.info(artifactRecord.toString());
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package ru.di9.mirror.core.repository;
|
||||
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.result.InsertOneResult;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import ru.di9.mirror.core.entity.ArtifactEntity;
|
||||
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class ArtifactRepository {
|
||||
private final MongoCollection<Document> collection;
|
||||
|
||||
public void save(ArtifactEntity artifactEntity) {
|
||||
if (artifactEntity.getId() == null) {
|
||||
insert(artifactEntity);
|
||||
}
|
||||
}
|
||||
|
||||
private void insert(ArtifactEntity artifactEntity) {
|
||||
InsertOneResult insertOneResult = collection.insertOne(new Document()
|
||||
.append("_id", new ObjectId())
|
||||
.append("group", artifactEntity.getGroup())
|
||||
.append("artifactId", artifactEntity.getArtifactId())
|
||||
.append("version", artifactEntity.getVersion())
|
||||
.append("is_snapshot", artifactEntity.isSnapshot()));
|
||||
|
||||
log.info("InsertedId: {}", insertOneResult.getInsertedId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user