From 36b87c62016e559e0aa670ac510f95796e4b5212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9cile=20Chemin?= Date: Mon, 11 Dec 2023 12:43:25 +0100 Subject: [PATCH] [LOG] :loud_sound: Exceptions are logged both side when using JMS store MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Cécile Chemin --- CHANGELOG.md | 2 +- .../sugoi/core/model/ProviderResponse.java | 10 +++++++ .../tomcat-properties/tomcat2.properties | 1 + .../fr/insee/sugoi/jms/JmsWriterStore.java | 20 +++++++++---- .../sugoi/jms/listener/JmsRequestRouter.java | 14 +++++++++- .../insee/sugoi/jms/utils/JmsAtttributes.java | 2 ++ .../exceptions/JmsResponseException.java | 28 +++++++++++++++++++ .../technics/SugoiAdviceController.java | 7 ++--- 8 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 sugoi-api-model/src/main/java/fr/insee/sugoi/model/exceptions/JmsResponseException.java diff --git a/CHANGELOG.md b/CHANGELOG.md index a23cdabe..1eb2816a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ - [BUG] :goal_net: Catch all LdapException when using mono connection - [REL] :rocket: release version 2.1.2 - + # 2.1.2 - [BUG] :bug: Fix max time before a connection is dropped from ldap connection pool diff --git a/sugoi-api-core/src/main/java/fr/insee/sugoi/core/model/ProviderResponse.java b/sugoi-api-core/src/main/java/fr/insee/sugoi/core/model/ProviderResponse.java index d616cf75..545ff1ff 100644 --- a/sugoi-api-core/src/main/java/fr/insee/sugoi/core/model/ProviderResponse.java +++ b/sugoi-api-core/src/main/java/fr/insee/sugoi/core/model/ProviderResponse.java @@ -24,6 +24,8 @@ public class ProviderResponse implements Serializable { String exceptionType; Object entity; + String hostname; + public ProviderResponse( String entityId, String requestId, @@ -42,6 +44,10 @@ public ProviderResponse( public ProviderResponse() {} + public ProviderResponse(String hostname) { + this.hostname = hostname; + } + public enum ProviderResponseStatus { // Request is executed and result is effective at once OK, @@ -105,4 +111,8 @@ public String getExceptionType() { public void setExceptionType(String exceptionType) { this.exceptionType = exceptionType; } + + public String getHostname() { + return hostname; + } } diff --git a/sugoi-api-distribution/sugoi-api-distribution-full-env/src/main/resources/tomcat-properties/tomcat2.properties b/sugoi-api-distribution/sugoi-api-distribution-full-env/src/main/resources/tomcat-properties/tomcat2.properties index c9ad92a2..efbc56a3 100644 --- a/sugoi-api-distribution/sugoi-api-distribution-full-env/src/main/resources/tomcat-properties/tomcat2.properties +++ b/sugoi-api-distribution/sugoi-api-distribution-full-env/src/main/resources/tomcat-properties/tomcat2.properties @@ -25,6 +25,7 @@ fr.insee.sugoi.springdoc.contact.email=tomcat2 fr.insee.sugoi.config.ldap.profils.url=localhost fr.insee.sugoi.config.ldap.profils.port=10389 fr.insee.sugoi.config.ldap.profils.branche=cn=profil-contact-WebServicesLdap,ou=WebServicesLdap_Objets,ou=WebServicesLdap,ou=applications,o=insee,c=fr +fr.insee.sugoi.config.ldap.profils.pattern=cn=Profil_{realm}_WebServiceLdap ## LDAP PROPERTIES (use by default writer if set to LdapReaderStore or LdapWriterStore) diff --git a/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/JmsWriterStore.java b/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/JmsWriterStore.java index 9ce6b5dc..ac94ea10 100644 --- a/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/JmsWriterStore.java +++ b/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/JmsWriterStore.java @@ -27,6 +27,7 @@ import fr.insee.sugoi.model.Realm; import fr.insee.sugoi.model.User; import fr.insee.sugoi.model.UserStorage; +import fr.insee.sugoi.model.exceptions.JmsResponseException; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; @@ -44,6 +45,8 @@ public class JmsWriterStore implements WriterStore { private UserStorage userStorage; + private static final String hostName = System.getenv("HOSTNAME"); + public JmsWriterStore( JmsWriter jmsWriter, String queueRequestName, @@ -381,6 +384,7 @@ private ProviderResponse checkAndSend( // Send Request params.put(JmsAtttributes.PROVIDER_REQUEST, providerRequest); + params.put(JmsAtttributes.PROVIDER_HOSTNAME, hostName); String correlationId = providerRequest.isAsynchronousAllowed() ? jmsWriter.writeRequestInQueueAsynchronous(queueAsyncRequestName, method, params) @@ -407,7 +411,7 @@ private ProviderResponse checkAndSend( response = br.getProviderResponse(); if (response.getStatus() == ProviderResponseStatus.KO) { - throw createExceptionFromResponse(response); + throw createExceptionFromResponse(response, correlationId); } } catch (JmsException e) { response.setStatus(ProviderResponseStatus.REQUESTED); @@ -418,13 +422,17 @@ private ProviderResponse checkAndSend( return response; } - private RuntimeException createExceptionFromResponse(ProviderResponse providerResponse) { + private RuntimeException createExceptionFromResponse( + ProviderResponse providerResponse, String correlationID) { try { Class c = Class.forName(providerResponse.getExceptionType()); - return (RuntimeException) - c.getDeclaredConstructor(String.class, Throwable.class) - .newInstance( - providerResponse.getException().getMessage(), providerResponse.getException()); + RuntimeException e = + (RuntimeException) + c.getDeclaredConstructor(String.class, Throwable.class) + .newInstance( + providerResponse.getException().getMessage(), + providerResponse.getException()); + return new JmsResponseException(e, correlationID, providerResponse.getHostname()); } catch (Exception e) { return new RuntimeException( providerResponse.getException().getMessage(), providerResponse.getException()); diff --git a/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/listener/JmsRequestRouter.java b/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/listener/JmsRequestRouter.java index 69e088e7..c332d104 100644 --- a/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/listener/JmsRequestRouter.java +++ b/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/listener/JmsRequestRouter.java @@ -56,8 +56,10 @@ public class JmsRequestRouter { @Autowired private OrganizationService orgService; + private static final String hostName = System.getenv("HOSTNAME"); + public ProviderResponse exec(BrokerRequest request) throws Exception { - ProviderResponse response = new ProviderResponse(); + ProviderResponse response = new ProviderResponse(hostName); try { String realm = (String) request.getmethodParams().get("realm"); String userStorage = (String) request.getmethodParams().get("userStorage"); @@ -256,6 +258,16 @@ public ProviderResponse exec(BrokerRequest request) throws Exception { } response.setStatus(ProviderResponseStatus.OK); } catch (RuntimeException e) { + if (!e.getClass().getPackage().getName().startsWith("fr.insee.sugoi") + || e.getClass().getSuperclass().getCanonicalName().equals("java.lang.RuntimeException")) { + logger.error( + "An error occurred while processing request " + + request.getCorrelationId() + + " for " + + request.getmethodParams().get(JmsAtttributes.PROVIDER_HOSTNAME) + + " : ", + e); + } response.setStatus(ProviderResponseStatus.KO); response.setException(e); response.setExceptionType(e.getClass().getCanonicalName()); diff --git a/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/utils/JmsAtttributes.java b/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/utils/JmsAtttributes.java index f2f56cca..ec421060 100644 --- a/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/utils/JmsAtttributes.java +++ b/sugoi-api-jms-store-provider/src/main/java/fr/insee/sugoi/jms/utils/JmsAtttributes.java @@ -60,4 +60,6 @@ public class JmsAtttributes { public static final String WEBHOOK_TAG = "webhookTag"; public static final String SHOULD_RESET_PASSWORD = "should-reset-password"; + + public static final String PROVIDER_HOSTNAME = "hostname"; } diff --git a/sugoi-api-model/src/main/java/fr/insee/sugoi/model/exceptions/JmsResponseException.java b/sugoi-api-model/src/main/java/fr/insee/sugoi/model/exceptions/JmsResponseException.java new file mode 100644 index 00000000..af47c120 --- /dev/null +++ b/sugoi-api-model/src/main/java/fr/insee/sugoi/model/exceptions/JmsResponseException.java @@ -0,0 +1,28 @@ +/* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +package fr.insee.sugoi.model.exceptions; + +public class JmsResponseException extends RuntimeException { + + public JmsResponseException(Throwable e, String correlationID, String hostname) { + super( + "Error message from " + + hostname + + " when processing " + + correlationID + + " : " + + e.getMessage(), + e); + } +} diff --git a/sugoi-api-rest-commons-services/src/main/java/fr/insee/sugoi/commons/services/controller/technics/SugoiAdviceController.java b/sugoi-api-rest-commons-services/src/main/java/fr/insee/sugoi/commons/services/controller/technics/SugoiAdviceController.java index 0b0b33a8..2f97e00e 100644 --- a/sugoi-api-rest-commons-services/src/main/java/fr/insee/sugoi/commons/services/controller/technics/SugoiAdviceController.java +++ b/sugoi-api-rest-commons-services/src/main/java/fr/insee/sugoi/commons/services/controller/technics/SugoiAdviceController.java @@ -14,10 +14,7 @@ package fr.insee.sugoi.commons.services.controller.technics; import fr.insee.sugoi.commons.services.view.ErrorView; -import fr.insee.sugoi.model.exceptions.BadRequestException; -import fr.insee.sugoi.model.exceptions.ConflictException; -import fr.insee.sugoi.model.exceptions.ForbiddenException; -import fr.insee.sugoi.model.exceptions.NotFoundException; +import fr.insee.sugoi.model.exceptions.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -63,6 +60,8 @@ private HttpStatus computeStatusFromException(Exception e) { return HttpStatus.NOT_IMPLEMENTED; } else if (e instanceof HttpRequestMethodNotSupportedException) { return HttpStatus.METHOD_NOT_ALLOWED; + } else if (e instanceof JmsResponseException) { + return computeStatusFromException((Exception) e.getCause()); } else { return HttpStatus.INTERNAL_SERVER_ERROR; }