From 40358c80665f70e634416fc02716a01bb534439e Mon Sep 17 00:00:00 2001 From: Popov Aleksander Date: Sun, 4 Feb 2024 04:07:22 +0600 Subject: [PATCH] RELL-1 add controller and handler --- pom.xml | 35 +++++++++++++- .../rell/controller/RecognizeController.java | 46 +++++++++++++++++++ .../rell/handler/GlobalExceptionHandler.java | 31 +++++++++++++ .../handler/exception/CustomException.java | 10 ++++ 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ru/rell/controller/RecognizeController.java create mode 100644 src/main/java/ru/rell/handler/GlobalExceptionHandler.java create mode 100644 src/main/java/ru/rell/handler/exception/CustomException.java diff --git a/pom.xml b/pom.xml index 89921a8..fcb03bd 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,11 @@ 3.2.2 0.8.11 5.10.1 + 2.2.2 + 0.2.2 + 2.0.1.Final + 1.3.2 + 3.0.0 @@ -53,6 +58,7 @@ spring-boot-starter-actuator ${spring.boot.version} + org.springframework.boot spring-boot-starter-hateoas @@ -91,6 +97,33 @@ ${version.mapstruct} + + io.swagger.core.v3 + swagger-annotations + ${swagger-annotations.version} + + + + org.openapitools + jackson-databind-nullable + ${jackson-databind-nullable.version} + + + javax.validation + validation-api + ${validation-api.version} + + + javax.annotation + javax.annotation-api + ${javax-annotation-api.version} + + + io.springfox + springfox-swagger2 + ${springfox-swagger2.version} + + org.springframework.boot @@ -105,7 +138,6 @@ ${junit.version} test - @@ -162,7 +194,6 @@ - \ No newline at end of file diff --git a/src/main/java/ru/rell/controller/RecognizeController.java b/src/main/java/ru/rell/controller/RecognizeController.java new file mode 100644 index 0000000..c3b727f --- /dev/null +++ b/src/main/java/ru/rell/controller/RecognizeController.java @@ -0,0 +1,46 @@ +package ru.rell.controller; + +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; +import ru.rell.handler.exception.CustomException; + +import java.io.IOException; + +@RestController +@RequestMapping("api/v1/recognize") +@RequiredArgsConstructor +public class RecognizeController { + + @Operation( + summary = "Распознование текста из фаила вернет txt; attachment; filename= имя фаила", + description = "Распознование текста из фаила вернет txt; attachment; filename= имя фаила" + ) + @PostMapping(produces = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity recognize(@RequestPart("file") MultipartFile file){ + if (file.isEmpty()) { + return ResponseEntity.badRequest().build(); + } + + ByteArrayResource resource = null; + try { + resource = new ByteArrayResource(file.getBytes()); + } catch (IOException e) { + throw new CustomException(e.getMessage(),e); + } + resource.getFilename(); + + return ResponseEntity.ok() + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .header("Content-Disposition", "attachment; filename=\"" + file.getOriginalFilename() + "\"") + .body(resource); + } +} diff --git a/src/main/java/ru/rell/handler/GlobalExceptionHandler.java b/src/main/java/ru/rell/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..8d4f2d3 --- /dev/null +++ b/src/main/java/ru/rell/handler/GlobalExceptionHandler.java @@ -0,0 +1,31 @@ +package ru.rell.handler; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.springframework.boot.actuate.cache.NonUniqueCacheException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import ru.rell.handler.exception.CustomException; + +@ControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + + @ExceptionHandler(Exception.class) + public ResponseEntity handleGlobalException(Exception ex) { + log.error(ex.getMessage(), ExceptionUtils.getStackTrace(ex)); + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Сервис временно не доступен"); + } + + @ExceptionHandler(CustomException.class) + public ResponseEntity handleCustomException(CustomException ex) { + log.error(ex.getMessage(), ExceptionUtils.getStackTrace(ex)); + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Возникла ошибка при работе с сервисом"); + } +} diff --git a/src/main/java/ru/rell/handler/exception/CustomException.java b/src/main/java/ru/rell/handler/exception/CustomException.java new file mode 100644 index 0000000..2c34354 --- /dev/null +++ b/src/main/java/ru/rell/handler/exception/CustomException.java @@ -0,0 +1,10 @@ +package ru.rell.handler.exception; + +import lombok.Getter; + +@Getter +public class CustomException extends RuntimeException { + public CustomException(String message, Throwable cause) { + super(message, cause); + } +}