дробим проект на субмодули

This commit is contained in:
2022-04-28 13:41:57 +03:00
parent e2fc75877d
commit a868b66e0c
24 changed files with 161 additions and 133 deletions

25
mirror-web/build.gradle Normal file
View File

@@ -0,0 +1,25 @@
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation(project(':mirror-app'))
annotationProcessor('org.springframework.boot:spring-boot-configuration-processor')
implementation('org.springframework.boot:spring-boot-starter-web')
}
tasks.named('compileJava') {
it.dependsOn('moveSpringConfigurationMetadata')
}
tasks.register('moveSpringConfigurationMetadata').configure {
it.dependsOn('processResources')
doLast {
def metafile = file("${sourceSets.main.output.resourcesDir}/META-INF/additional-spring-configuration-metadata.json")
if (metafile.exists()) {
def metafileTo = file("${sourceSets.main.output.classesDirs.asPath}/META-INF/spring-configuration-metadata.json")
metafileTo.parentFile.mkdirs()
metafile.renameTo(metafileTo)
}
}
}

View File

@@ -0,0 +1,12 @@
package ru.di9.mirror.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,31 @@
package ru.di9.mirror.web.config;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.di9.mirror.app.service.*;
import java.util.List;
@Configuration
public class WebConfig {
@Bean
public StorageService mirroredStorageService(FileStorageService fileStorageService,
List<MirrorService> mirrors) {
return new MirroredStorageService(fileStorageService, mirrors);
}
@Bean
public FileStorageService fileStorageService(@Value("${maven.storage}") String storagePath) {
return new FileStorageService(storagePath);
}
@Bean
public List<MirrorService> mirrors() {
return List.of(
new MavenCentralStorageService(new OkHttpClient())
);
}
}

View File

@@ -0,0 +1,86 @@
package ru.di9.mirror.web.controller;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.di9.mirror.app.Utils;
import ru.di9.mirror.app.domain.FileRecord;
import ru.di9.mirror.app.domain.PathRecord;
import ru.di9.mirror.app.domain.PathType;
import ru.di9.mirror.app.domain.WalkerResult;
import ru.di9.mirror.app.service.StorageService;
import java.util.List;
import java.util.Optional;
@Controller
@RequestMapping(path = "/maven")
public class IndexOfMavenController {
private final StorageService storageService;
public IndexOfMavenController(@Qualifier("mirroredStorageService") StorageService storageService) {
this.storageService = storageService;
}
@GetMapping(path = "/{*path}", headers = "accept=text/html")
public ResponseEntity<Object> walker(@PathVariable("path") String httpPath) {
ResponseEntity<Object> responseEntity;
Optional<WalkerResult> walkerResultOptional = storageService.walker(httpPath);
if (walkerResultOptional.isEmpty()) {
responseEntity = ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("<html><body><h1>404 - Not Found</h1><hr><p style=\"text-align:right\">Project-Mirror</p></body></html>");
} else {
WalkerResult walkerResult = walkerResultOptional.get();
if (walkerResult.pathType() == PathType.DIRECTORY) {
responseEntity = generateIndexOf(httpPath, walkerResult.filesList());
} else {
responseEntity = downloadFile(walkerResult.fileRecord());
}
}
return responseEntity;
}
private ResponseEntity<Object> downloadFile(FileRecord fileRecord) {
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + fileRecord.name() + "\"")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(fileRecord.size())
.body(new InputStreamResource(fileRecord.inputStream()));
}
private ResponseEntity<Object> generateIndexOf(String httpPath, List<PathRecord> filesList) {
ResponseEntity<Object> responseEntity;
StringBuilder sb = new StringBuilder("<html><body><h1>Index of</h1><hr><ul>");
if (!Utils.isEmptyString(httpPath)) {
String levelUpPath = httpPath.substring(0, httpPath.lastIndexOf("/"));
sb.append("<li><a href=\"/maven").append(levelUpPath).append("\">..</a></li>");
}
filesList.forEach(pathRecord -> {
sb.append("<li><a href=\"/maven")
.append(httpPath)
.append("/")
.append(pathRecord.name())
.append("\">")
.append(pathRecord.name());
if (pathRecord.type() == PathType.DIRECTORY) {
sb.append("/");
}
sb.append("</a></li>");
});
responseEntity = ResponseEntity.ok(sb.append("</ul><hr><p style=\"text-align:right\">Project-Mirror</p></body></html>").toString());
return responseEntity;
}
}

View File

@@ -0,0 +1,65 @@
package ru.di9.mirror.web.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.di9.mirror.app.domain.FileRecord;
import ru.di9.mirror.app.service.StorageService;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
@Slf4j
@Controller
@RequestMapping(path = "/maven")
public class MavenController {
private final StorageService storageService;
public MavenController(@Qualifier("mirroredStorageService") StorageService storageService) {
this.storageService = storageService;
}
@GetMapping(path = "/{*path}")
public ResponseEntity<Resource> mavenGetFile(@PathVariable("path") String httpPath) {
ResponseEntity<Resource> responseEntity;
Optional<FileRecord> fileRecordOptional = storageService.getFile(httpPath);
if (fileRecordOptional.isEmpty()) {
responseEntity = ResponseEntity.notFound().build();
} else {
FileRecord fileRecord = fileRecordOptional.get();
responseEntity = ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + fileRecord.name() + "\"")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.contentLength(fileRecord.size())
.body(new InputStreamResource(fileRecord.inputStream()));
}
return responseEntity;
}
@SuppressWarnings("java:S1452")
@PutMapping(path = "/{*path}", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity<?> mavenPutFile(@PathVariable("path") String httpPath,
HttpServletRequest httpRequest) throws IOException {
try (InputStream inputStream = httpRequest.getInputStream()) {
storageService.putFile(httpPath, inputStream);
}
return ResponseEntity.noContent().build();
}
}

View File

@@ -0,0 +1,8 @@
{
"properties": [
{
"name": "maven.storage",
"type": "java.lang.String"
}
]
}

View File

@@ -0,0 +1,8 @@
server:
address: 127.0.0.1
port: 8080
debug: false
maven:
storage: './storage'