fix HttpClient execute, bump libs

This commit is contained in:
Anatoly Karlov 2024-08-27 18:22:34 +07:00
parent 4e64264772
commit 226be289e5
4 changed files with 13 additions and 25 deletions

View File

@ -42,7 +42,7 @@
<dependency>
<groupId>dev.vality</groupId>
<artifactId>swag-disputes</artifactId>
<version>1.16-063c381-server</version>
<version>1.17-8e552ef-server</version>
</dependency>
<dependency>
<groupId>dev.vality</groupId>
@ -253,7 +253,7 @@
<dependency>
<groupId>dev.vality</groupId>
<artifactId>testcontainers-annotations</artifactId>
<version>2.0.3</version>
<version>2.0.4</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -3,7 +3,7 @@ package dev.vality.disputes.api.controller;
import dev.vality.disputes.exception.AuthorizationException;
import dev.vality.disputes.exception.NotFoundException;
import dev.vality.disputes.exception.TokenKeeperException;
import dev.vality.swag.disputes.model.DefaultLogicError;
import dev.vality.swag.disputes.model.GeneralError;
import jakarta.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -38,8 +38,7 @@ public class ErrorControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Object handleInvalidMimeTypeException(InvalidMimeTypeException e) {
log.warn("<- Res [400]: MimeType not valid", e);
return new DefaultLogicError()
.code(DefaultLogicError.CodeEnum.INVALIDREQUEST)
return new GeneralError()
.message(e.getMessage());
}
@ -50,17 +49,15 @@ public class ErrorControllerAdvice {
var errorMessage = e.getConstraintViolations().stream()
.map(violation -> violation.getPropertyPath() + ": " + violation.getMessage())
.collect(Collectors.joining(", "));
return new DefaultLogicError()
.code(DefaultLogicError.CodeEnum.INVALIDREQUEST)
.message(errorMessage);
return new GeneralError()
.message(e.getMessage());
}
@ExceptionHandler({MethodArgumentNotValidException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.warn("<- Res [400]: MethodArgument not valid", e);
return new DefaultLogicError()
.code(DefaultLogicError.CodeEnum.INVALIDREQUEST)
return new GeneralError()
.message(e.getMessage());
}
@ -68,8 +65,7 @@ public class ErrorControllerAdvice {
@ResponseStatus(HttpStatus.BAD_REQUEST)
public Object handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
log.warn("<- Res [400]: Missing ServletRequestParameter", e);
return new DefaultLogicError()
.code(DefaultLogicError.CodeEnum.INVALIDREQUEST)
return new GeneralError()
.message(e.getMessage());
}

View File

@ -8,7 +8,7 @@ import dev.vality.file.storage.FileStorageSrv;
import dev.vality.token.keeper.TokenAuthenticatorSrv;
import dev.vality.woody.thrift.impl.http.THSpawnClientBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -73,7 +73,7 @@ public class ApplicationConfig {
@Bean
public CloseableHttpClient httpClient() {
return HttpClientBuilder.create().build();
return HttpClients.createDefault();
}
@Bean

View File

@ -11,9 +11,8 @@ import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.impl.classic.BasicHttpClientResponseHandler;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.HttpEntities;
import org.apache.thrift.TException;
import org.springframework.stereotype.Service;
@ -41,8 +40,8 @@ public class FileStorageServiceImpl implements FileStorageService {
log.debug("Trying to upload data to file-storage with id: {}", fileDataId);
var requestPut = new HttpPut(result.getUploadUrl());
requestPut.setEntity(HttpEntities.create(data, null));
var response = httpClient.execute(requestPut);
checkResponse(fileDataId, response);
// execute() делает внутри try-with-resources + закрывает InputStream в EntityUtils.consume(entity)
httpClient.execute(requestPut, new BasicHttpClientResponseHandler());
log.debug("File has been successfully uploaded with id: {}", fileDataId);
return fileDataId;
}
@ -64,13 +63,6 @@ public class FileStorageServiceImpl implements FileStorageService {
}
}
private void checkResponse(String fileDataId, HttpResponse response) {
if (response.getCode() != HttpStatus.SC_OK) {
throw new FileStorageException(String.format(
"Failed to upload file, fileDataId='%s', response='%s'", fileDataId, response));
}
}
private Instant getTime() {
return LocalDateTime.now(fileStorageProperties.getTimeZone())
.plusMinutes(fileStorageProperties.getUrlLifeTimeDuration())