отделили реализацию mongodb в отлдельный модуль
This commit is contained in:
@@ -2,5 +2,4 @@ apply plugin: 'java-library'
|
||||
|
||||
dependencies {
|
||||
api("com.squareup.okhttp3:okhttp:${okhttpVersion}")
|
||||
api("org.mongodb:mongodb-driver-sync:${mongoDriver}")
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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";
|
||||
|
||||
public static ArtifactRecord fromHttpPath(String httpPath) {
|
||||
if (httpPath.startsWith("/")) {
|
||||
httpPath = httpPath.substring(1);
|
||||
}
|
||||
log.info("httpPath = {}", httpPath);
|
||||
|
||||
String[] partsPath = httpPath.split("/");
|
||||
String version = partsPath[partsPath.length-2];
|
||||
boolean isSnapshot = version.contains(SNAPSHOT);
|
||||
|
||||
String fileName = partsPath[partsPath.length-1];
|
||||
|
||||
String artifactId;
|
||||
if (isSnapshot) {
|
||||
String partVersion = version.substring(0, version.lastIndexOf(SNAPSHOT));
|
||||
artifactId = fileName.substring(0, fileName.indexOf(partVersion) - 1);
|
||||
} else {
|
||||
artifactId = fileName.substring(0, (fileName.lastIndexOf('.') - version.length() - 1));
|
||||
}
|
||||
|
||||
StringJoiner sj = new StringJoiner(".");
|
||||
for (int i = 0; i < partsPath.length - 3; i++) {
|
||||
sj.add(partsPath[i]);
|
||||
}
|
||||
|
||||
return new ArtifactRecord(sj.toString(), artifactId, version, isSnapshot);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,49 @@
|
||||
package ru.di9.mirror.core.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.bson.types.ObjectId;
|
||||
import ru.di9.mirror.core.domain.ArtifactRecord;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
@EqualsAndHashCode(exclude = {"id"})
|
||||
public class ArtifactEntity {
|
||||
private ObjectId id;
|
||||
private static final String SNAPSHOT = "SNAPSHOT";
|
||||
|
||||
private String group;
|
||||
private String artifactId;
|
||||
private String version;
|
||||
private boolean isSnapshot;
|
||||
|
||||
public static ArtifactEntity fromRecord(ArtifactRecord artifactRecord) {
|
||||
var entity = new ArtifactEntity();
|
||||
public static ArtifactEntity fromHttpPath(String httpPath) {
|
||||
if (httpPath.startsWith("/")) {
|
||||
httpPath = httpPath.substring(1);
|
||||
}
|
||||
log.info("httpPath = {}", httpPath);
|
||||
|
||||
entity.setGroup(artifactRecord.group());
|
||||
entity.setArtifactId(artifactRecord.artifactId());
|
||||
entity.setVersion(artifactRecord.version());
|
||||
entity.setSnapshot(artifactRecord.isSnapshot());
|
||||
String[] partsPath = httpPath.split("/");
|
||||
String version = partsPath[partsPath.length-2];
|
||||
boolean isSnapshot = version.contains(SNAPSHOT);
|
||||
|
||||
return entity;
|
||||
String fileName = partsPath[partsPath.length-1];
|
||||
|
||||
String artifactId;
|
||||
if (isSnapshot) {
|
||||
String partVersion = version.substring(0, version.lastIndexOf(SNAPSHOT));
|
||||
artifactId = fileName.substring(0, fileName.indexOf(partVersion) - 1);
|
||||
} else {
|
||||
artifactId = fileName.substring(0, (fileName.lastIndexOf('.') - version.length() - 1));
|
||||
}
|
||||
|
||||
StringJoiner sj = new StringJoiner(".");
|
||||
for (int i = 0; i < partsPath.length - 3; i++) {
|
||||
sj.add(partsPath[i]);
|
||||
}
|
||||
|
||||
return new ArtifactEntity(sj.toString(), artifactId, version, isSnapshot);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,12 @@ package ru.di9.mirror.core.handler;
|
||||
|
||||
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.repository.FileStorageRepository;
|
||||
import ru.di9.mirror.core.service.ExternalMavenService;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
@@ -25,12 +24,12 @@ public class MavenHandler {
|
||||
public Optional<GetFileResponse> getFile(String path) {
|
||||
var fileInfo = FileInfo.of(path.substring(path.lastIndexOf("/") + 1));
|
||||
|
||||
ArtifactRecord artifactRecord = null;
|
||||
ArtifactEntity artifactEntity = null;
|
||||
if (!fileInfo.fullName().equalsIgnoreCase("maven-metadata.xml")
|
||||
&& !fileInfo.ext().equalsIgnoreCase("sha1")) {
|
||||
|
||||
artifactRecord = ArtifactRecord.fromHttpPath(path);
|
||||
log.info(artifactRecord.toString());
|
||||
artifactEntity = ArtifactEntity.fromHttpPath(path);
|
||||
log.info(artifactEntity.toString());
|
||||
}
|
||||
|
||||
Optional<InputStream> optionalInputStream = fileStorageRepository.findByName("/local/" + path);
|
||||
@@ -38,11 +37,11 @@ public class MavenHandler {
|
||||
return optionalInputStream
|
||||
.map(inputStream -> new GetFileResponse(fileInfo.name(), inputStream));
|
||||
} else {
|
||||
return findInMirrors(path, fileInfo.name(), artifactRecord);
|
||||
return findInMirrors(path, fileInfo.name(), artifactEntity);
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<GetFileResponse> findInMirrors(String path, String fileName, ArtifactRecord artifactRecord) {
|
||||
private Optional<GetFileResponse> findInMirrors(String path, String fileName, ArtifactEntity artifactEntity) {
|
||||
Optional<InputStream> result;
|
||||
for (ExternalMavenService externalMavenService : externalMavenServices) {
|
||||
final String nameForStore = "/" + externalMavenService.getId() + "/" + path;
|
||||
@@ -54,8 +53,8 @@ public class MavenHandler {
|
||||
} else {
|
||||
result = externalMavenService.getFile(path);
|
||||
if (result.isPresent()) {
|
||||
if (artifactRecord != null) {
|
||||
repository.save(ArtifactEntity.fromRecord(artifactRecord));
|
||||
if (artifactEntity != null) {
|
||||
repository.save(artifactEntity);
|
||||
}
|
||||
|
||||
fileStorageRepository.save(nameForStore, result.get());
|
||||
@@ -74,8 +73,8 @@ public class MavenHandler {
|
||||
if (!fileInfo.fullName().equalsIgnoreCase("maven-metadata.xml")
|
||||
&& !fileInfo.ext().equalsIgnoreCase("sha1")) {
|
||||
|
||||
ArtifactRecord artifactRecord = ArtifactRecord.fromHttpPath(path);
|
||||
log.info(artifactRecord.toString());
|
||||
ArtifactEntity artifactEntity = ArtifactEntity.fromHttpPath(path);
|
||||
log.info(artifactEntity.toString());
|
||||
}
|
||||
|
||||
fileStorageRepository.save("/local/" + path, inputStream);
|
||||
|
||||
@@ -1,32 +1,8 @@
|
||||
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 interface ArtifactRepository {
|
||||
|
||||
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());
|
||||
}
|
||||
ArtifactEntity save(ArtifactEntity artifactEntity);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package ru.di9.mirror.core.domain;
|
||||
package ru.di9.mirror.core.entity;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -7,23 +7,23 @@ import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class ArtifactRecordTest {
|
||||
class ArtifactEntityTest {
|
||||
|
||||
@ParameterizedTest(name = "[{index}] {0}")
|
||||
@MethodSource("fromHttpPathSource")
|
||||
void fromHttpPath(String httpPath, ArtifactRecord expected) {
|
||||
ArtifactRecord actual = ArtifactRecord.fromHttpPath(httpPath);
|
||||
void fromHttpPath(String httpPath, ArtifactEntity expected) {
|
||||
ArtifactEntity actual = ArtifactEntity.fromHttpPath(httpPath);
|
||||
Assertions.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
static Stream<Arguments> fromHttpPathSource() {
|
||||
return Stream.of(
|
||||
Arguments.of("com/google/guava/guava/21.0/guava-21.0.jar",
|
||||
new ArtifactRecord("com.google.guava", "guava", "21.0", false)),
|
||||
new ArtifactEntity("com.google.guava", "guava", "21.0", false)),
|
||||
Arguments.of("org/bukkit/bukkit/1.12.2-R0.1-SNAPSHOT/bukkit-1.12.2-R0.1-20180712.012114-155.jar",
|
||||
new ArtifactRecord("org.bukkit", "bukkit", "1.12.2-R0.1-SNAPSHOT", true)),
|
||||
new ArtifactEntity("org.bukkit", "bukkit", "1.12.2-R0.1-SNAPSHOT", true)),
|
||||
Arguments.of("/org/projectlombok/lombok/1.18.22/lombok-1.18.22.jar",
|
||||
new ArtifactRecord("org.projectlombok", "lombok", "1.18.22", false))
|
||||
new ArtifactEntity("org.projectlombok", "lombok", "1.18.22", false))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user