add logger
This commit is contained in:
parent
a673e52ff5
commit
29d2655b52
|
@ -1,2 +1,3 @@
|
|||
.idea/
|
||||
target/
|
||||
logs/
|
||||
|
|
12
core/pom.xml
12
core/pom.xml
|
@ -61,6 +61,18 @@
|
|||
<groupId>gr.cite</groupId>
|
||||
<artifactId>cache</artifactId>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>gr.cite</groupId>
|
||||
<artifactId>logging</artifactId>
|
||||
<version>2.2.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>gr.cite</groupId>
|
||||
<artifactId>exceptions</artifactId>
|
||||
<version>2.2.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package org.opencdmp.deposit.zenodorepository.audit;
|
||||
|
||||
|
||||
import gr.cite.tools.logging.EventId;
|
||||
|
||||
public class AuditableAction {
|
||||
|
||||
public static final EventId Deposit_Deposit = new EventId(1000, "Deposit_Deposit");
|
||||
public static final EventId Deposit_Authenticate = new EventId(1001, "Deposit_Authenticate");
|
||||
public static final EventId Deposit_GetConfiguration = new EventId(1002, "Deposit_GetConfiguration");
|
||||
public static final EventId Deposit_GetLogo = new EventId(1003, "Deposit_GetLogo");
|
||||
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package org.opencdmp.deposit.zenodorepository.model.builder;
|
||||
|
||||
import gr.cite.tools.exception.MyApplicationException;
|
||||
import org.opencdmp.commonmodels.enums.DmpAccessType;
|
||||
import org.opencdmp.commonmodels.enums.DmpUserRole;
|
||||
import org.opencdmp.commonmodels.models.DmpUserModel;
|
||||
|
@ -162,8 +163,8 @@ public class ZenodoBuilder {
|
|||
}
|
||||
}
|
||||
}
|
||||
case INTERNAL_ENTRIES_DESCRIPTIONS, INTERNAL_ENTRIES_DMPS, UPLOAD -> throw new RuntimeException("Invalid type " + field.getData().getFieldType());
|
||||
default -> throw new RuntimeException("Invalid type " + field.getData().getFieldType());
|
||||
case INTERNAL_ENTRIES_DESCRIPTIONS, INTERNAL_ENTRIES_DMPS, UPLOAD -> throw new MyApplicationException("Invalid type " + field.getData().getFieldType());
|
||||
default -> throw new MyApplicationException("Invalid type " + field.getData().getFieldType());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -240,8 +241,8 @@ public class ZenodoBuilder {
|
|||
private void applyLicenses(DmpModel dmp, ZenodoDeposit deposit){
|
||||
if (deposit.getMetadata() == null) deposit.setMetadata(new ZenodoDepositMetadata());
|
||||
|
||||
List<ReferenceModel> dmpLicenses = this.getReferenceModelOfType(dmp, zenodoServiceProperties.getResearcherReferenceCode());
|
||||
if (dmpLicenses != null && !dmpLicenses.isEmpty()) {
|
||||
List<ReferenceModel> dmpLicenses = this.getReferenceModelOfType(dmp, zenodoServiceProperties.getLicensesReferenceCode());
|
||||
if (!dmpLicenses.isEmpty()) {
|
||||
for (ReferenceModel dmpLicense : dmpLicenses) {
|
||||
if (dmpLicense != null && dmpLicense.getReference() != null && !dmpLicense.getReference().isBlank()) {
|
||||
deposit.getMetadata().setLicense(dmpLicense.getReference());
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package org.opencdmp.deposit.zenodorepository.service.zenodo;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import gr.cite.tools.exception.MyApplicationException;
|
||||
import gr.cite.tools.logging.LoggerService;
|
||||
import gr.cite.tools.logging.MapLogEntry;
|
||||
import org.opencdmp.commonmodels.models.FileEnvelopeModel;
|
||||
import org.opencdmp.commonmodels.models.dmp.DmpModel;
|
||||
import org.opencdmp.depositbase.repository.DepositConfiguration;
|
||||
|
@ -21,7 +24,9 @@ import org.springframework.util.ResourceUtils;
|
|||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.HttpServerErrorException;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
@ -30,6 +35,7 @@ import java.util.*;
|
|||
|
||||
@Component
|
||||
public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
||||
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(ZenodoDepositServiceImpl.class));
|
||||
private static final String PUBLISH_ID = "conceptdoi";
|
||||
|
||||
private static final String CLIENT_ID = "client_id";
|
||||
|
@ -47,7 +53,6 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
private static final String ZENODO_METADATA = "metadata";
|
||||
private static final String ZENODO_METADATA_VERSION = "version";
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ZenodoDepositServiceImpl.class);
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
private final ZenodoServiceProperties zenodoServiceProperties;
|
||||
|
@ -78,7 +83,7 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
String zenodoUrl = depositConfiguration.getRepositoryUrl();
|
||||
|
||||
// First step, post call to Zenodo, to create the entry.
|
||||
WebClient zenodoClient = WebClient.builder().build();
|
||||
WebClient zenodoClient = this.getWebClient();
|
||||
|
||||
DepositConfiguration zenodoConfig = this.zenodoServiceProperties.getDepositConfiguration();
|
||||
if (zenodoConfig == null) return null;
|
||||
|
@ -93,7 +98,7 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
if (previousDOI == null) {
|
||||
links = deposit(zenodoToken, zenodoUrl, zenodoClient, deposit);
|
||||
} else {
|
||||
unpublishedUrl = this.getUnpublishedDOI(zenodoUrl, previousDOI, zenodoToken, dmpModel.getVersion());
|
||||
unpublishedUrl = this.getUnpublishedDOI(zenodoClient, zenodoUrl, previousDOI, zenodoToken, dmpModel.getVersion());
|
||||
if (unpublishedUrl == null) {
|
||||
//It requires more than one step to create a new version
|
||||
//First, get the deposit related to the concept DOI
|
||||
|
@ -107,7 +112,7 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
// Second step, add the file to the entry.
|
||||
FileEnvelopeModel pdfEnvelope = dmpModel.getPdfFile();
|
||||
|
||||
if (links == null || !links.containsKey(ZENODO_LINKS_BUCKET)) throw new Exception("bucket not found");
|
||||
if (links == null || !links.containsKey(ZENODO_LINKS_BUCKET)) throw new MyApplicationException("bucket not found");
|
||||
|
||||
String addFileUrl = links.get(ZENODO_LINKS_BUCKET) + "/" + pdfEnvelope.getFilename() + "?access_token=" + zenodoToken;
|
||||
|
||||
|
@ -149,9 +154,10 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
publishUrl = unpublishedUrl + "?access_token=" + zenodoToken;
|
||||
}
|
||||
|
||||
return this.publish(publishUrl);
|
||||
return this.publish(zenodoClient, publishUrl);
|
||||
|
||||
} catch (HttpClientErrorException | HttpServerErrorException ex) {
|
||||
logger.error(ex.getMessage(), ex);
|
||||
Map<String, String> parsedException = objectMapper.readValue(ex.getResponseBodyAsString(), Map.class);
|
||||
throw new IOException(parsedException.get("message"), ex);
|
||||
}
|
||||
|
@ -175,7 +181,7 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
links = (LinkedHashMap<String, String>) createResponse.getOrDefault(ZENODO_LINKS, new LinkedHashMap<>());
|
||||
|
||||
//Second, make the new version (not in the links?)
|
||||
if (!links.containsKey(ZENODO_LINKS_LATEST_DRAFT)) throw new Exception("previousDOI not found");
|
||||
if (!links.containsKey(ZENODO_LINKS_LATEST_DRAFT)) throw new MyApplicationException("previousDOI not found");
|
||||
String newVersionUrl = links.get(ZENODO_LINKS_LATEST_DRAFT) + "/actions/newversion" + "?access_token=" + zenodoToken;
|
||||
logger.debug("new version url: " + newVersionUrl);
|
||||
createResponse = zenodoClient.post().uri(newVersionUrl)
|
||||
|
@ -185,7 +191,7 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
links = createResponse == null ? new LinkedHashMap<>() : createResponse.getOrDefault(ZENODO_LINKS, new LinkedHashMap<>());
|
||||
|
||||
//Third, get the new deposit
|
||||
if (!links.containsKey(ZENODO_LINKS_LATEST_DRAFT)) throw new Exception("can not create latest draft");
|
||||
if (!links.containsKey(ZENODO_LINKS_LATEST_DRAFT)) throw new MyApplicationException("can not create latest draft");
|
||||
String latestDraftUrl = links.get(ZENODO_LINKS_LATEST_DRAFT) + "?access_token=" + zenodoToken;
|
||||
createResponse = zenodoClient.get().uri(latestDraftUrl)
|
||||
.exchangeToMono(mono -> mono.bodyToMono(new ParameterizedTypeReference<Map<String, LinkedHashMap<String, String>>>() {})).block();
|
||||
|
@ -213,6 +219,7 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
} catch (Exception e) {
|
||||
//In case the last two steps fail delete the latest Deposit it in order to create a new one (only one at a time is allowed)
|
||||
//restTemplate.delete(latestDraftUrl);
|
||||
logger.error(e.getMessage(), e);
|
||||
zenodoClient.delete().uri(latestDraftUrl).retrieve().toEntity(Map.class).block();
|
||||
throw e;
|
||||
}
|
||||
|
@ -237,20 +244,18 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||
})
|
||||
.bodyValue(deposit).exchangeToMono(mono -> mono.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})).block();
|
||||
.bodyValue(deposit).exchangeToMono(mono ->
|
||||
mono.statusCode().isError() ?
|
||||
mono.createException().flatMap(Mono::error) :
|
||||
mono.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})).block();
|
||||
return (LinkedHashMap<String, String>) createResponse.getOrDefault(ZENODO_LINKS, null);
|
||||
}
|
||||
|
||||
private String publish(String publishUrl){
|
||||
WebClient webClient = WebClient.builder().build();
|
||||
Map<String, Object> publishResponse = webClient.post().uri(publishUrl).bodyValue("").exchangeToMono(mono -> {
|
||||
if (!mono.statusCode().is2xxSuccessful()) {
|
||||
mono.createException();
|
||||
throw new UnsupportedOperationException("Failed to publish to Zenodo");
|
||||
}
|
||||
return mono.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
|
||||
});
|
||||
}).block();
|
||||
private String publish(WebClient webClient, String publishUrl){
|
||||
Map<String, Object> publishResponse = webClient.post().uri(publishUrl).bodyValue("").exchangeToMono(mono ->
|
||||
mono.statusCode().isError() ?
|
||||
mono.createException().flatMap(Mono::error) :
|
||||
mono.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})).block();
|
||||
if (publishResponse == null) throw new UnsupportedOperationException("Failed to publish to Zenodo");
|
||||
return (String) publishResponse.get(PUBLISH_ID);
|
||||
}
|
||||
|
@ -268,7 +273,10 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
|
||||
if(depositConfiguration != null) {
|
||||
|
||||
WebClient client = WebClient.builder().defaultHeaders(httpHeaders -> {
|
||||
WebClient client = WebClient.builder().filters(exchangeFilterFunctions -> {
|
||||
exchangeFilterFunctions.add(logRequest());
|
||||
exchangeFilterFunctions.add(logResponse());
|
||||
}).defaultHeaders(httpHeaders -> {
|
||||
httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
||||
}).build();
|
||||
|
@ -292,7 +300,7 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
|
||||
return values != null ? (String) values.getOrDefault(ACCESS_TOKEN, null) : null;
|
||||
} catch (HttpClientErrorException ex) {
|
||||
logger.error(ex.getResponseBodyAsString(), ex);
|
||||
logger.error(ex.getMessage(), ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -312,6 +320,7 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
this.logo = inputStream.readAllBytes();
|
||||
};
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
@ -320,9 +329,8 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
return null;
|
||||
}
|
||||
|
||||
private String getUnpublishedDOI(String zenodoUrl, String doi, String token, Short version) {
|
||||
private String getUnpublishedDOI(WebClient client, String zenodoUrl, String doi, String token, Short version) {
|
||||
try {
|
||||
WebClient client = WebClient.builder().build();
|
||||
Map<String, LinkedHashMap<String, String>> createResponse = null;
|
||||
LinkedHashMap<String, String> links;
|
||||
LinkedHashMap<String, String> metadata;
|
||||
|
@ -340,8 +348,39 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
|||
return null;
|
||||
}
|
||||
}catch (Exception e) {
|
||||
logger.warn(e.getMessage(), e);
|
||||
logger.error(e.getMessage(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private WebClient getWebClient(){
|
||||
return WebClient.builder().filters(exchangeFilterFunctions -> {
|
||||
exchangeFilterFunctions.add(logRequest());
|
||||
exchangeFilterFunctions.add(logResponse());
|
||||
}).build();
|
||||
}
|
||||
|
||||
private static ExchangeFilterFunction logRequest() {
|
||||
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
|
||||
logger.debug("Request: {} {}", clientRequest.method(), clientRequest.url());
|
||||
clientRequest.headers().forEach((name, values) -> values.forEach(value -> logger.debug("{}={}", name, value)));
|
||||
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("Request: {} {}", response.request().getMethod(), response.request().getURI());
|
||||
response.request().getHeaders().forEach((name, values) -> values.forEach(value -> logger.error("{}={}", name, value)));
|
||||
logger.error("Response: {} {}", response.statusCode(), body);
|
||||
return Mono.just(response);
|
||||
});
|
||||
}
|
||||
return Mono.just(response);
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
7
pom.xml
7
pom.xml
|
@ -18,6 +18,8 @@
|
|||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<maven.compiler.release>21</maven.compiler.release>
|
||||
<java.version>21</java.version>
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
<log4j2.version>2.15.0</log4j2.version>
|
||||
<revision>1.0.0-SNAPSHOT</revision>
|
||||
</properties>
|
||||
|
||||
|
@ -31,6 +33,11 @@
|
|||
<artifactId>repositorydepositbase</artifactId>
|
||||
<version>2.0.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>gr.cite</groupId>
|
||||
<artifactId>logging</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -36,6 +36,11 @@
|
|||
<artifactId>cache</artifactId>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>gr.cite</groupId>
|
||||
<artifactId>exceptions-web</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
|
|
|
@ -1,36 +1,73 @@
|
|||
package org.opencdmp.deposit.controller;
|
||||
|
||||
import gr.cite.tools.auditing.AuditService;
|
||||
import gr.cite.tools.logging.LoggerService;
|
||||
import gr.cite.tools.logging.MapLogEntry;
|
||||
import org.opencdmp.commonmodels.models.dmp.DmpModel;
|
||||
import org.opencdmp.deposit.zenodorepository.audit.AuditableAction;
|
||||
import org.opencdmp.depositbase.repository.DepositConfiguration;
|
||||
import org.opencdmp.deposit.zenodorepository.service.zenodo.ZenodoDepositService;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/deposit")
|
||||
public class DepositController implements org.opencdmp.depositbase.repository.DepositController {
|
||||
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DepositController.class));
|
||||
|
||||
private final ZenodoDepositService depositClient;
|
||||
|
||||
private final AuditService auditService;
|
||||
|
||||
@Autowired
|
||||
public DepositController(ZenodoDepositService depositClient) {
|
||||
public DepositController(ZenodoDepositService depositClient, AuditService auditService) {
|
||||
this.depositClient = depositClient;
|
||||
this.auditService = auditService;
|
||||
}
|
||||
|
||||
public String deposit(@RequestBody DmpModel dmpModel, @RequestParam("authToken")String authToken) throws Exception {
|
||||
return depositClient.deposit(dmpModel, authToken);
|
||||
logger.debug(new MapLogEntry("deposit" + DmpModel.class.getSimpleName()).And("dmpModel", dmpModel));
|
||||
|
||||
String doiId = depositClient.deposit(dmpModel, authToken);
|
||||
|
||||
this.auditService.track(AuditableAction.Deposit_Deposit, Map.ofEntries(
|
||||
new AbstractMap.SimpleEntry<String, Object>("dmpModel", dmpModel)
|
||||
));
|
||||
return doiId;
|
||||
}
|
||||
|
||||
public String authenticate(@RequestParam("authToken") String code) {
|
||||
return depositClient.authenticate(code);
|
||||
logger.debug(new MapLogEntry("authenticate" + DmpModel.class.getSimpleName()));
|
||||
|
||||
String token = depositClient.authenticate(code);
|
||||
|
||||
this.auditService.track(AuditableAction.Deposit_Authenticate);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public DepositConfiguration getConfiguration() {
|
||||
return depositClient.getConfiguration();
|
||||
logger.debug(new MapLogEntry("getConfiguration" + DmpModel.class.getSimpleName()));
|
||||
|
||||
DepositConfiguration configuration = depositClient.getConfiguration();
|
||||
|
||||
this.auditService.track(AuditableAction.Deposit_GetConfiguration);
|
||||
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public String getLogo() {
|
||||
return depositClient.getLogo();
|
||||
logger.debug(new MapLogEntry("getLogo" + DmpModel.class.getSimpleName()));
|
||||
|
||||
String logo = depositClient.getLogo();
|
||||
|
||||
this.auditService.track(AuditableAction.Deposit_GetLogo);
|
||||
|
||||
return logo;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,202 @@
|
|||
package org.opencdmp.deposit.controller.controllerhandler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import gr.cite.tools.exception.*;
|
||||
import gr.cite.tools.logging.LoggerService;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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 org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestControllerAdvice
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(GlobalExceptionHandler.class));
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public GlobalExceptionHandler() {
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper.registerModule(new JavaTimeModule());
|
||||
}
|
||||
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<?> handleUnexpectedErrors(Exception exception, WebRequest request) throws Exception {
|
||||
HandledException handled = this.handleException(exception, request);
|
||||
this.log(handled.getLevel(), exception, MessageFormat.format("returning code {0} and payload {1}", handled.getStatusCode(), handled.getMessage()));
|
||||
return new ResponseEntity<>(handled.getMessage(), handled.getStatusCode());
|
||||
}
|
||||
|
||||
public String toJsonSafe(Object item) {
|
||||
if (item == null) return null;
|
||||
try {
|
||||
return this.objectMapper.writeValueAsString(item);
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void log(System.Logger.Level level, Exception e, String message) {
|
||||
if (level != null) {
|
||||
switch (level) {
|
||||
case TRACE:
|
||||
logger.trace(message, e);
|
||||
break;
|
||||
case DEBUG:
|
||||
logger.debug(message, e);
|
||||
break;
|
||||
case INFO:
|
||||
logger.info(message, e);
|
||||
break;
|
||||
case WARNING:
|
||||
logger.warn(message, e);
|
||||
break;
|
||||
case ERROR:
|
||||
logger.error(message, e);
|
||||
break;
|
||||
default:
|
||||
logger.error(e);
|
||||
}
|
||||
} else {
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public HandledException handleException(Exception exception, WebRequest request) throws Exception {
|
||||
HttpStatus statusCode;
|
||||
Map<String, Object> result;
|
||||
System.Logger.Level logLevel;
|
||||
|
||||
switch (exception){
|
||||
case MyNotFoundException myNotFoundException -> {
|
||||
logLevel = System.Logger.Level.DEBUG;
|
||||
statusCode = HttpStatus.NOT_FOUND;
|
||||
int code = myNotFoundException.getCode();
|
||||
if (code > 0) {
|
||||
result = Map.ofEntries(
|
||||
Map.entry("code", code),
|
||||
Map.entry("error", myNotFoundException.getMessage())
|
||||
);
|
||||
}
|
||||
else {
|
||||
result = Map.ofEntries(
|
||||
Map.entry("error", myNotFoundException.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
case MyUnauthorizedException myUnauthorizedException -> {
|
||||
logLevel = System.Logger.Level.DEBUG;
|
||||
statusCode = HttpStatus.UNAUTHORIZED;
|
||||
int code = myUnauthorizedException.getCode();
|
||||
if (code > 0) {
|
||||
result = Map.ofEntries(
|
||||
Map.entry("code", code),
|
||||
Map.entry("error", myUnauthorizedException.getMessage())
|
||||
);
|
||||
}
|
||||
else {
|
||||
result = Map.ofEntries(
|
||||
Map.entry("error", myUnauthorizedException.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
case MyForbiddenException myForbiddenException -> {
|
||||
logLevel = System.Logger.Level.DEBUG;
|
||||
statusCode = HttpStatus.FORBIDDEN;
|
||||
int code = myForbiddenException.getCode();
|
||||
if (code > 0) {
|
||||
result = Map.ofEntries(
|
||||
Map.entry("code", code),
|
||||
Map.entry("error", myForbiddenException.getMessage())
|
||||
);
|
||||
}
|
||||
else {
|
||||
result = Map.ofEntries(
|
||||
Map.entry("error", myForbiddenException.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
case MyValidationException myValidationException -> {
|
||||
logLevel = System.Logger.Level.DEBUG;
|
||||
statusCode = HttpStatus.BAD_REQUEST;
|
||||
int code = myValidationException.getCode();
|
||||
|
||||
result = new HashMap<>();
|
||||
if (code > 0) result.put("code", code);
|
||||
if (myValidationException.getMessage() != null) result.put("error", myValidationException.getMessage());
|
||||
if (myValidationException.getErrors() != null) result.put("message", myValidationException.getErrors());
|
||||
}
|
||||
case MyApplicationException myApplicationException -> {
|
||||
logLevel = System.Logger.Level.ERROR;
|
||||
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
int code = myApplicationException.getCode();
|
||||
if (code > 0) {
|
||||
result = Map.ofEntries(
|
||||
Map.entry("code", code),
|
||||
Map.entry("error", myApplicationException.getMessage())
|
||||
);
|
||||
}
|
||||
else {
|
||||
result = Map.ofEntries(
|
||||
Map.entry("error", myApplicationException.getMessage())
|
||||
);
|
||||
}
|
||||
}
|
||||
default -> {
|
||||
logLevel = System.Logger.Level.ERROR;
|
||||
statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
result = Map.ofEntries(
|
||||
Map.entry("error", "System error")
|
||||
);
|
||||
}
|
||||
};
|
||||
String serialization = this.toJsonSafe(result);
|
||||
return new HandledException(statusCode, serialization, logLevel);
|
||||
}
|
||||
|
||||
public static class HandledException{
|
||||
public HttpStatus statusCode;
|
||||
public String message;
|
||||
public System.Logger.Level level;
|
||||
|
||||
public HandledException(HttpStatus statusCode, String message, System.Logger.Level level) {
|
||||
this.statusCode = statusCode;
|
||||
this.message = message;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public HttpStatus getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
public void setStatusCode(HttpStatus statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public System.Logger.Level getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(System.Logger.Level level) {
|
||||
this.level = level;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ spring:
|
|||
config:
|
||||
import: optional:classpath:config/app.env[.properties], optional:file:../config/app.env[.properties],
|
||||
optional:classpath:config/server.yml[.yml], optional:classpath:config/server-${spring.profiles.active}.yml[.yml], optional:file:../config/server-${spring.profiles.active}.yml[.yml],
|
||||
optional:classpath:config/logging.yml[.yml], optional:classpath:config/logging-${spring.profiles.active}.yml[.yml], optional:file:../config/logging-${spring.profiles.active}.yml[.yml],
|
||||
optional:classpath:config/zenodo.yml[.yml], optional:classpath:config/zenodo-${spring.profiles.active}.yml[.yml], optional:file:../config/zenodo-${spring.profiles.active}.yml[.yml],
|
||||
optional:classpath:config/pid.yml[.yml], optional:classpath:config/pid-${spring.profiles.active}.yml[.yml], optional:file:../config/pid-${spring.profiles.active}.yml[.yml],
|
||||
optional:classpath:config/funder.yml[.yml], optional:classpath:config/funder-${spring.profiles.active}.yml[.yml], optional:file:../config/funder-${spring.profiles.active}.yml[.yml],
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
logging:
|
||||
config: classpath:logging/logback-${spring.profiles.active}.xml
|
||||
context:
|
||||
request:
|
||||
requestIdKey: req.id
|
||||
requestRemoteHostKey: req.remoteHost
|
||||
requestUriKey: req.requestURI
|
||||
requestQueryStringKey: req.queryString
|
||||
requestUrlKey : req.requestURL
|
||||
requestMethodKey: req.method
|
||||
requestUserAgentKey: req.userAgent
|
||||
requestForwardedForKey: req.xForwardedFor
|
||||
requestSchemeKey: req.scheme
|
||||
requestRemoteAddressKey: req.remoteAddr
|
||||
requestRemotePortKey: req.remotePort
|
||||
requestRemoteUserKey: req.remoteUser
|
||||
principal:
|
||||
subjectKey: usr.subject
|
||||
nameKey: usr.name
|
||||
clientKey: usr.client
|
||||
audit:
|
||||
enable: true
|
||||
requestRemoteHostKey: req.remoteHost
|
||||
requestUriKey: req.requestURI
|
||||
requestQueryStringKey: req.queryString
|
||||
requestUrlKey : req.requestURL
|
||||
requestMethodKey: req.method
|
||||
requestUserAgentKey: req.userAgent
|
||||
requestForwardedForKey: req.xForwardedFor
|
||||
requestSchemeKey: req.scheme
|
||||
requestRemoteAddressKey: req.remoteAddr
|
||||
requestRemotePortKey: req.remotePort
|
||||
requestRemoteUserKey: req.remoteUser
|
||||
principalSubjectKey: usr.subject
|
||||
principalNameKey: usr.name
|
||||
principalClientKey: usr.client
|
|
@ -0,0 +1,35 @@
|
|||
logging:
|
||||
context:
|
||||
request:
|
||||
requestIdKey: req.id
|
||||
requestRemoteHostKey: req.remoteHost
|
||||
requestUriKey: req.requestURI
|
||||
requestQueryStringKey: req.queryString
|
||||
requestUrlKey : req.requestURL
|
||||
requestMethodKey: req.method
|
||||
requestUserAgentKey: req.userAgent
|
||||
requestForwardedForKey: req.xForwardedFor
|
||||
requestSchemeKey: req.scheme
|
||||
requestRemoteAddressKey: req.remoteAddr
|
||||
requestRemotePortKey: req.remotePort
|
||||
requestRemoteUserKey: req.remoteUser
|
||||
principal:
|
||||
subjectKey: usr.subject
|
||||
nameKey: usr.name
|
||||
clientKey: usr.client
|
||||
audit:
|
||||
enable: true
|
||||
requestRemoteHostKey: req.remoteHost
|
||||
requestUriKey: req.requestURI
|
||||
requestQueryStringKey: req.queryString
|
||||
requestUrlKey : req.requestURL
|
||||
requestMethodKey: req.method
|
||||
requestUserAgentKey: req.userAgent
|
||||
requestForwardedForKey: req.xForwardedFor
|
||||
requestSchemeKey: req.scheme
|
||||
requestRemoteAddressKey: req.remoteAddr
|
||||
requestRemotePortKey: req.remotePort
|
||||
requestRemoteUserKey: req.remoteUser
|
||||
principalSubjectKey: usr.subject
|
||||
principalNameKey: usr.name
|
||||
principalClientKey: usr.client
|
|
@ -0,0 +1,62 @@
|
|||
<configuration debug="true">
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<Pattern>%date{ISO8601} [%thread] %-5level %logger{36} [%X{req.id}] - %message%n</Pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="TROUBLESHOOTING" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>logs/logging.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>logs/logging.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<maxFileSize>100MB</maxFileSize>
|
||||
</timeBasedFileNamingAndTriggeringPolicy>
|
||||
<maxHistory>15</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<Pattern>%date{ISO8601} [%thread] %-5level %logger{36} [%X{req.id}] - %message%n</Pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="AUDITING" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>logs/auditing.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>logs/auditing.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<maxFileSize>100MB</maxFileSize>
|
||||
</timeBasedFileNamingAndTriggeringPolicy>
|
||||
<maxHistory>15</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<Pattern>%date{ISO8601} - %X{req.id} - %message%n</Pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
|
||||
<logger name="org.springframework.web" level="INFO" additivity="false">
|
||||
<appender-ref ref="TROUBLESHOOTING"/>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
<logger name="org.hibernate" level="INFO" additivity="false">
|
||||
<appender-ref ref="TROUBLESHOOTING"/>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
<logger name="gr.cite" level="DEBUG" additivity="false">
|
||||
<appender-ref ref="TROUBLESHOOTING"/>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
<logger name="org.opencdmp" level="DEBUG" additivity="false">
|
||||
<appender-ref ref="TROUBLESHOOTING"/>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
<logger name="audit" level="INFO" additivity="false">
|
||||
<appender-ref ref="AUDITING"/>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</logger>
|
||||
<root level="info">
|
||||
<appender-ref ref="TROUBLESHOOTING"/>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
Loading…
Reference in New Issue