Return xml Resource for setupMonitoring method

This commit is contained in:
Mikhail Trofimov 2025-06-04 16:53:57 +03:00
parent 590baeba97
commit 97cdefd166
2 changed files with 24 additions and 13 deletions

View file

@ -1,5 +1,7 @@
package ru.nbch.credit_tracker.controller;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -18,8 +20,8 @@ public class SignalController {
this.signalFacade = signalFacade;
}
@PostMapping("/set")
public ResponseEntity<String> setupMonitoring(@RequestParam("file") MultipartFile file) {
@PostMapping(value = "/set", produces = MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<Resource> setupMonitoring(@RequestParam("file") MultipartFile file) {
return signalFacade.setupMonitoring(file);
}

View file

@ -4,6 +4,8 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@ -29,13 +31,13 @@ public class SignalFacade {
this.failureAnswerGenerator = failureAnswerGenerator;
}
public ResponseEntity<String> setupMonitoring(MultipartFile file) {
public ResponseEntity<Resource> setupMonitoring(MultipartFile file) {
if (file.isEmpty()) {
return ResponseEntity.badRequest().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_001));
return getBadResponse(SignalError.ERROR_001);
}
if (!isZipFile(file)) {
return ResponseEntity.badRequest().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_001));
return getBadResponse(SignalError.ERROR_001);
}
try (ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream())) {
@ -47,43 +49,44 @@ public class SignalFacade {
if (!entry.isDirectory()) {
fileCount++;
if (fileCount > 1) { // Архив должен содержать ровно один XML-файл
return ResponseEntity.badRequest().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_001));
return getBadResponse(SignalError.ERROR_001);
}
if (!entry.getName().toLowerCase().endsWith(".xml")) { // Файл должен быть с расширением xml
return ResponseEntity.badRequest().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_001));
return getBadResponse(SignalError.ERROR_001);
}
xmlEntry = entry;
} else {
return ResponseEntity.badRequest().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_001)); // Архив должен содержать ровно один XML-файл. Никаких доп директорий
return getBadResponse(SignalError.ERROR_001); // Архив должен содержать ровно один XML-файл. Никаких доп директорий
}
zipInputStream.closeEntry();
entry = zipInputStream.getNextEntry();
}
if (fileCount == 0) { // Архив должен содержать ровно один XML-файл
return ResponseEntity.badRequest().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_001));
return getBadResponse(SignalError.ERROR_001);
}
if (xmlEntry.getSize() > MAX_XML_FILE_SIZE) { // максимально 2 Гб до запаковки
return ResponseEntity.badRequest().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_009));
return getBadResponse(SignalError.ERROR_009);
}
AuthInfoData authInfoData = parseAuthInfo(zipInputStream);
if (!authorizationService.hasAuthorization(authInfoData.getUserId(), authInfoData.getPassword())) {
return ResponseEntity.badRequest().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_005));
return getBadResponse(SignalError.ERROR_005);
}
// TODO process xml file
} catch (Exception e) {
log.error(e.getMessage(), e);
return ResponseEntity.internalServerError().body(failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_001));
String errorPath = failureAnswerGenerator.generateFailureAnswer(SignalError.ERROR_001);
return ResponseEntity.internalServerError().body(new PathResource(errorPath));
}
return ResponseEntity.ok().body("OK"); // TODO generate success response
return ResponseEntity.ok().body(new PathResource("D:\\folder_name\\legal.xml")); // TODO generate success response
}
@ -97,6 +100,12 @@ public class SignalFacade {
return new AuthInfoData("0001XX000001", "test"); // TODO parse
}
private ResponseEntity<Resource> getBadResponse(SignalError error) {
String errorPath = failureAnswerGenerator.generateFailureAnswer(error);
return ResponseEntity.badRequest().body(new PathResource(errorPath));
}
@Getter
@Setter
@AllArgsConstructor