Зеркалирование Maven Central
This commit is contained in:
@@ -21,6 +21,8 @@ dependencies {
|
|||||||
|
|
||||||
annotationProcessor('org.springframework.boot:spring-boot-configuration-processor')
|
annotationProcessor('org.springframework.boot:spring-boot-configuration-processor')
|
||||||
implementation('org.springframework.boot:spring-boot-starter-web')
|
implementation('org.springframework.boot:spring-boot-starter-web')
|
||||||
|
|
||||||
|
implementation('com.squareup.okhttp3:okhttp:4.9.3')
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('compileJava') {
|
tasks.named('compileJava') {
|
||||||
|
|||||||
14
src/main/java/ru/di9/mirror/config/Config.java
Normal file
14
src/main/java/ru/di9/mirror/config/Config.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package ru.di9.mirror.config;
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public OkHttpClient okHttpClient() {
|
||||||
|
return new OkHttpClient();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,26 +1,46 @@
|
|||||||
package ru.di9.mirror.services;
|
package ru.di9.mirror.services;
|
||||||
|
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.di9.mirror.domain.FileRecord;
|
import ru.di9.mirror.domain.FileRecord;
|
||||||
import ru.di9.mirror.domain.WalkerResult;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.IOException;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MavenCentralStorageService implements StorageService {
|
public class MavenCentralStorageService extends MirrorService {
|
||||||
|
|
||||||
|
private static final String MAVEN_CENTRAL_URL = "https://repo1.maven.org/maven2";
|
||||||
|
private final OkHttpClient okHttpClient;
|
||||||
|
|
||||||
|
public MavenCentralStorageService(OkHttpClient okHttpClient) {
|
||||||
|
this.okHttpClient = okHttpClient;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
public Optional<FileRecord> getFile(String path) {
|
public Optional<FileRecord> getFile(String path) {
|
||||||
return Optional.empty();
|
Request request = new Request.Builder()
|
||||||
}
|
.url(MAVEN_CENTRAL_URL + path)
|
||||||
|
.build();
|
||||||
|
|
||||||
@Override
|
Response response = okHttpClient.newCall(request).execute();
|
||||||
public void putFile(String path, InputStream inputStream) {
|
if (!response.isSuccessful()) {
|
||||||
throw new UnsupportedOperationException();
|
if (response.code() == 404) {
|
||||||
}
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
throw new IOException("Unexpected code " + response);
|
||||||
public Optional<WalkerResult> walker(String path) {
|
}
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
|
return Optional.of(new FileRecord(
|
||||||
|
path,
|
||||||
|
Long.parseLong(Objects.requireNonNull(response.header("Content-Length"))),
|
||||||
|
Objects.requireNonNull(response.body()).byteStream()
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/main/java/ru/di9/mirror/services/MirrorService.java
Normal file
19
src/main/java/ru/di9/mirror/services/MirrorService.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package ru.di9.mirror.services;
|
||||||
|
|
||||||
|
import ru.di9.mirror.domain.WalkerResult;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public abstract class MirrorService implements StorageService{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void putFile(String path, InputStream inputStream) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<WalkerResult> walker(String path) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,18 +5,19 @@ import ru.di9.mirror.domain.FileRecord;
|
|||||||
import ru.di9.mirror.domain.WalkerResult;
|
import ru.di9.mirror.domain.WalkerResult;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MirroredStorageService implements StorageService {
|
public class MirroredStorageService implements StorageService {
|
||||||
|
|
||||||
private final FileStorageService fileStorageService;
|
private final FileStorageService fileStorageService;
|
||||||
private final MavenCentralStorageService mavenCentralStorageService;
|
private final List<MirrorService> mirrors;
|
||||||
|
|
||||||
public MirroredStorageService(FileStorageService fileStorageService,
|
public MirroredStorageService(FileStorageService fileStorageService,
|
||||||
MavenCentralStorageService mavenCentralStorageService) {
|
List<MirrorService> mirrors) {
|
||||||
this.fileStorageService = fileStorageService;
|
this.fileStorageService = fileStorageService;
|
||||||
this.mavenCentralStorageService = mavenCentralStorageService;
|
this.mirrors = mirrors;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -28,9 +29,14 @@ public class MirroredStorageService implements StorageService {
|
|||||||
return optional;
|
return optional;
|
||||||
}
|
}
|
||||||
|
|
||||||
mavenCentralStorageService.getFile(path).ifPresent(fileRecord ->
|
for (StorageService mirror : mirrors) {
|
||||||
fileStorageService.putFile(fileRecord.name(), fileRecord.inputStream()));
|
optional = mirror.getFile(path);
|
||||||
|
if (optional.isPresent()) {
|
||||||
|
FileRecord fileRecord = optional.get();
|
||||||
|
fileStorageService.putFile(fileRecord.name(), fileRecord.inputStream());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return fileStorageService.getFile(path);
|
return fileStorageService.getFile(path);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user