more logging

This commit is contained in:
Efstratios Giannopoulos 2024-05-10 14:02:38 +03:00
parent efe03306f5
commit 169f0a71ff
1 changed files with 33 additions and 1 deletions

View File

@ -1,16 +1,22 @@
package org.opencdmp.filetransformer.docx.service.pdf; package org.opencdmp.filetransformer.docx.service.pdf;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.client.MultipartBodyBuilder; import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.util.UUID; import java.util.UUID;
@Component @Component
public class PdfServiceImpl implements PdfService { public class PdfServiceImpl implements PdfService {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(PdfServiceImpl.class));
private final PdfServiceProperties pdfServiceProperties; private final PdfServiceProperties pdfServiceProperties;
@ -20,7 +26,10 @@ public class PdfServiceImpl implements PdfService {
@Override @Override
public byte[] convertToPDF(byte[] file) { public byte[] convertToPDF(byte[] file) {
WebClient webClient = WebClient.builder().baseUrl(pdfServiceProperties.getUrl()).build(); WebClient webClient = WebClient.builder().filters(exchangeFilterFunctions -> {
exchangeFilterFunctions.add(logRequest());
exchangeFilterFunctions.add(logResponse());
}).baseUrl(pdfServiceProperties.getUrl()).build();
MultipartBodyBuilder builder = new MultipartBodyBuilder(); MultipartBodyBuilder builder = new MultipartBodyBuilder();
builder.part("files", new ByteArrayResource(file)).filename(UUID.randomUUID() + ".docx"); builder.part("files", new ByteArrayResource(file)).filename(UUID.randomUUID() + ".docx");
@ -33,4 +42,27 @@ public class PdfServiceImpl implements PdfService {
.body(BodyInserters.fromMultipartData(builder.build())) .body(BodyInserters.fromMultipartData(builder.build()))
.retrieve().bodyToMono(byte[].class).block(); .retrieve().bodyToMono(byte[].class).block();
} }
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
logger.debug(new MapLogEntry("Request").And("method", clientRequest.method()).And("url", clientRequest.url()));
return Mono.just(clientRequest);
});
}
private static ExchangeFilterFunction logResponse() {
return ExchangeFilterFunction.ofResponseProcessor(response -> {
if (response.statusCode().isError()) {
return response.mutate().build().bodyToMono(String.class)
.flatMap(body -> {
logger.error(new MapLogEntry("Response").And("method", response.request().getMethod()).And("url", response.request().getURI()).And("status", response.statusCode()).And("body", body));
return Mono.just(response);
});
}
return Mono.just(response);
});
}
} }