logging changes

This commit is contained in:
Efstratios Giannopoulos 2024-05-10 14:28:08 +03:00
parent 5c422fb847
commit 6ed11c2cdd
5 changed files with 35 additions and 20 deletions

View File

@ -1,6 +1,7 @@
package org.opencdmp.service.deposit;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import org.opencdmp.commonmodels.models.dmp.DmpModel;
import org.opencdmp.depositbase.repository.DepositClient;
import org.opencdmp.depositbase.repository.DepositConfiguration;
@ -21,21 +22,25 @@ public class DepositClientImpl implements DepositClient {
@Override
public String deposit(DmpModel dmpDepositModel, String repositoryAccessToken) throws Exception {
logger.debug(new MapLogEntry("deposit").And("dmpDepositModel", dmpDepositModel));
return this.depositClient.post().uri("", uriBuilder -> uriBuilder.queryParam("authToken", repositoryAccessToken).build()).bodyValue(dmpDepositModel).exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(String.class)).block();
}
@Override
public String authenticate(String code) {
logger.debug(new MapLogEntry("code"));
return this.depositClient.get().uri("/authenticate/", uriBuilder -> uriBuilder.queryParam("authToken", code).build()).exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(String.class)).block();
}
@Override
public DepositConfiguration getConfiguration() {
logger.debug(new MapLogEntry("getConfiguration"));
return this.depositClient.get().uri("/configuration").exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(new ParameterizedTypeReference<DepositConfiguration>() {})).block();
}
@Override
public String getLogo() {
logger.debug(new MapLogEntry("getLogo"));
return this.depositClient.get().uri("/logo").exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(String.class)).block();
}
}

View File

@ -10,6 +10,8 @@ import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.ValidatorFactory;
import org.apache.commons.io.FilenameUtils;
import org.opencdmp.authorization.AuthorizationFlags;
@ -55,7 +57,6 @@ import org.opencdmp.service.filetransformer.FileTransformerService;
import org.opencdmp.service.storage.StorageFileProperties;
import org.opencdmp.service.storage.StorageFileService;
import org.opencdmp.service.tenant.TenantProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
@ -84,7 +85,7 @@ import java.util.stream.Collectors;
@Service
public class DepositServiceImpl implements DepositService {
private static final Logger logger = LoggerFactory.getLogger(DepositServiceImpl.class);
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DepositServiceImpl.class));
private final DepositProperties depositProperties;
private final Map<String, DepositClient> clients;
@ -385,7 +386,7 @@ public class DepositServiceImpl implements DepositService {
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
logger.debug("Request: {} {}", clientRequest.method(), clientRequest.url());
logger.debug(new MapLogEntry("Request").And("method", clientRequest.method().toString()).And("url", clientRequest.url()));
return Mono.just(clientRequest);
});
}
@ -395,8 +396,7 @@ public class DepositServiceImpl implements DepositService {
if (response.statusCode().isError()) {
return response.mutate().build().bodyToMono(String.class)
.flatMap(body -> {
logger.error("Request: {} {}", response.request().getMethod(), response.request().getURI());
logger.error("Response: {} {}", response.statusCode(), body);
logger.error(new MapLogEntry("Response").And("method", response.request().getMethod().toString()).And("url", response.request().getURI()).And("status", response.statusCode().toString()).And("body", body));
return Mono.just(response);
});
}

View File

@ -6,6 +6,8 @@ import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import net.minidev.json.JSONArray;
import org.opencdmp.commons.JsonHandlingService;
import org.opencdmp.commons.enums.ExternalFetcherSourceType;
@ -17,7 +19,6 @@ import org.opencdmp.model.reference.Reference;
import org.opencdmp.service.externalfetcher.config.entities.*;
import org.opencdmp.service.externalfetcher.criteria.ExternalReferenceCriteria;
import org.opencdmp.service.externalfetcher.models.ExternalDataResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
@ -40,7 +41,7 @@ import java.util.stream.Collectors;
@Service
public class ExternalFetcherServiceImpl implements ExternalFetcherService {
private static final Logger logger = LoggerFactory.getLogger(ExternalFetcherServiceImpl.class);
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(ExternalFetcherServiceImpl.class));
private WebClient webClient;
private final ConventionService conventionService;
@ -153,6 +154,7 @@ public class ExternalFetcherServiceImpl implements ExternalFetcherService {
default -> throw new MyApplicationException("unrecognized type " + authenticationConfiguration.getAuthMethod());
}
logger.debug(new MapLogEntry("Authentication").And("url", authenticationConfiguration.getAuthUrl()));
Map<String, Object> response = this.getWebClient().method(method).uri(authenticationConfiguration.getAuthUrl())
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(this.parseBodyString(authenticationConfiguration.getAuthRequestBody()))
@ -266,6 +268,9 @@ public class ExternalFetcherServiceImpl implements ExternalFetcherService {
default -> throw new MyApplicationException("unrecognized type " + apiSource.getHttpMethod());
}
logger.debug(new MapLogEntry("Fetch data")
.And("url", urlString)
.And("body", jsonBody));
ResponseEntity<String> response = this.getWebClient().method(method).uri(urlString).headers(httpHeaders -> {
if (this.conventionService.isNullOrEmpty(apiSource.getContentType())) {
httpHeaders.setAccept(Collections.singletonList(MediaType.valueOf(apiSource.getContentType())));
@ -382,7 +387,7 @@ public class ExternalFetcherServiceImpl implements ExternalFetcherService {
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
logger.debug("Request: {} {}", clientRequest.method(), clientRequest.url());
logger.debug(new MapLogEntry("Request").And("method", clientRequest.method().toString()).And("url", clientRequest.url()));
return Mono.just(clientRequest);
});
}
@ -392,8 +397,7 @@ public class ExternalFetcherServiceImpl implements ExternalFetcherService {
if (response.statusCode().isError()) {
return response.mutate().build().bodyToMono(String.class)
.flatMap(body -> {
logger.error("Request: {} {}", response.request().getMethod(), response.request().getURI());
logger.error("Response: {} {}", response.statusCode(), body);
logger.error(new MapLogEntry("Response").And("method", response.request().getMethod().toString()).And("url", response.request().getURI()).And("status", response.statusCode().toString()).And("body", body));
return Mono.just(response);
});
}

View File

@ -1,16 +1,20 @@
package org.opencdmp.service.filetransformer;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import org.opencdmp.commonmodels.models.FileEnvelopeModel;
import org.opencdmp.commonmodels.models.description.DescriptionModel;
import org.opencdmp.commonmodels.models.dmp.DmpModel;
import org.opencdmp.filetransformerbase.interfaces.FileTransformerClient;
import org.opencdmp.filetransformerbase.interfaces.FileTransformerConfiguration;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public class FileTransformerRepository implements FileTransformerClient {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(FileTransformerRepository.class));
private final WebClient transformerClient;
@ -20,30 +24,35 @@ public class FileTransformerRepository implements FileTransformerClient {
@Override
public FileEnvelopeModel exportDmp(DmpModel dmpModel, String format) {
logger.debug(new MapLogEntry("exportDmp").And("format", format).And("dmpModel", dmpModel));
return this.transformerClient.post().uri("/export/dmp", uriBuilder -> uriBuilder.queryParam("format", format).build()).bodyValue(dmpModel)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(FileEnvelopeModel.class)).block();
}
@Override
public DmpModel importDmp(FileEnvelopeModel fileEnvelope) {
logger.debug(new MapLogEntry("importDmp").And("fileEnvelope", fileEnvelope));
return this.transformerClient.post().uri("/import/dmp").bodyValue(fileEnvelope)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(DmpModel.class)).block();
}
@Override
public FileEnvelopeModel exportDescription(DescriptionModel descriptionModel, String format) {
logger.debug(new MapLogEntry("exportDescription").And("format", format).And("descriptionModel", descriptionModel));
return this.transformerClient.post().uri("/export/description", uriBuilder -> uriBuilder.queryParam("format", format).build()).bodyValue(descriptionModel)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(FileEnvelopeModel.class)).block();
}
@Override
public DescriptionModel importDescription(FileEnvelopeModel fileEnvelope) {
logger.debug(new MapLogEntry("importDescription").And("fileEnvelope", fileEnvelope));
return this.transformerClient.post().uri("/import/description").bodyValue(fileEnvelope)
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(DescriptionModel.class)).block();
}
@Override
public FileTransformerConfiguration getConfiguration() {
logger.debug(new MapLogEntry("getConfiguration"));
return this.transformerClient.get().uri("/formats")
.exchangeToMono(mono -> mono.statusCode().isError() ? mono.createException().flatMap(Mono::error) : mono.bodyToMono(new ParameterizedTypeReference<FileTransformerConfiguration>() {})).block();
}

View File

@ -8,6 +8,8 @@ import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.authorization.Permission;
import org.opencdmp.commonmodels.models.FileEnvelopeModel;
@ -36,7 +38,6 @@ import org.opencdmp.query.TenantConfigurationQuery;
import org.opencdmp.service.encryption.EncryptionService;
import org.opencdmp.service.storage.StorageFileService;
import org.opencdmp.service.tenant.TenantProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
@ -58,8 +59,7 @@ import java.util.*;
@Service
public class FileTransformerServiceImpl implements FileTransformerService {
private static final Logger logger = LoggerFactory.getLogger(FileTransformerServiceImpl.class);
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(FileTransformerServiceImpl.class));
private final FileTransformerProperties fileTransformerProperties;
private final Map<String, FileTransformerRepository> clients;
private final TokenExchangeCacheService tokenExchangeCacheService;
@ -260,11 +260,9 @@ public class FileTransformerServiceImpl implements FileTransformerService {
return result;
}
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
logger.debug("Request: {} {}", clientRequest.method(), clientRequest.url());
logger.debug(new MapLogEntry("Request").And("method", clientRequest.method().toString()).And("url", clientRequest.url()));
return Mono.just(clientRequest);
});
}
@ -274,8 +272,7 @@ public class FileTransformerServiceImpl implements FileTransformerService {
if (response.statusCode().isError()) {
return response.mutate().build().bodyToMono(String.class)
.flatMap(body -> {
logger.error("Request: {} {}", response.request().getMethod(), response.request().getURI());
logger.error("Response: {} {}", response.statusCode(), body);
logger.error(new MapLogEntry("Response").And("method", response.request().getMethod().toString()).And("url", response.request().getURI()).And("status", response.statusCode().toString()).And("body", body));
return Mono.just(response);
});
}