From 4e9499a1c39498f8646799e947e38f5f491c1428 Mon Sep 17 00:00:00 2001 From: lalber Date: Mon, 8 Mar 2021 18:27:55 +0100 Subject: First version of feature --- .../impl/idp/auth/services/ErrorTicketService.java | 212 +++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ErrorTicketService.java (limited to 'eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ErrorTicketService.java') diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ErrorTicketService.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ErrorTicketService.java new file mode 100644 index 00000000..c5bac225 --- /dev/null +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ErrorTicketService.java @@ -0,0 +1,212 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.services; + +import at.gv.egiz.eaaf.core.api.IStatusMessenger; +import at.gv.egiz.eaaf.core.api.data.EaafConstants; +import at.gv.egiz.eaaf.core.api.idp.IConfiguration; +import at.gv.egiz.eaaf.core.exceptions.EaafException; +import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException; +import at.gv.egiz.eaaf.core.impl.idp.controller.ProtocolFinalizationController; +import at.gv.egiz.eaaf.core.impl.utils.FileUtils; +import at.gv.egiz.eaaf.core.impl.utils.ServletUtils; +import lombok.Getter; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.text.StringEscapeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import javax.servlet.http.HttpServletRequest; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +@Service() +public class ErrorTicketService { + private static final Logger log = LoggerFactory.getLogger(ErrorTicketService.class); + + private static final String CONFIG_PROP_ERRORHANDLING_ACTION_PATH = "core.errorhandling.action"; + private static final String TECH_LOG_MSG = "errorCode={} Message={}"; + private static final String TICKET_LOG_MSG = "Ticket={} errorCode={} Message={}"; + + private final HashMap propertyMap = new HashMap(); + + + public enum ActionType { + TICKET_REDIRECT("ticket_redirect"), TICKET_NOREDIRECT("ticket_noredirect"), NOTICKET_REDIRECT( + "noticket_redirect"), NOTICKET_NOREDIRECT("noticket_noredirect"); + + private final String name; + + ActionType(final String text) { + this.name = text; + } + + @Override + public String toString() { + return name; + } + } + + @Autowired(required = true) + IConfiguration basicConfig; + @Autowired(required = true) + ResourceLoader resourceLoader; + + @PostConstruct + private void initialize() throws EaafException { + log.info("initErrorTicketService"); + + final String ticketConfPath = basicConfig.getBasicConfiguration(CONFIG_PROP_ERRORHANDLING_ACTION_PATH); + log.info("ticketConfPath" + ticketConfPath); + + + if (StringUtils.isEmpty(ticketConfPath)) { + log.error("Error: Path to errorhandling action configuration not known"); + throw new EaafException("Error: Path to errorhandling action configuration not known"); + } else { + + Properties getProperties = new Properties(); + try { + + final String fullFilePath = FileUtils + .makeAbsoluteUrl(ticketConfPath, basicConfig.getConfigurationRootDirectory()); + final Resource ressource = resourceLoader.getResource(fullFilePath); + final InputStream is = ressource.getInputStream(); + getProperties.load(is); + is.close(); + propertyMap.putAll((Map) getProperties); + + // log.error(propertyMap.toString()); + // log.error("working: " + propertyMap.get("auth.00")); + + } catch (Exception e) { + log.error("Error: something went wrong"); + throw new EaafException("Error: Parsing errorhandling actions failed"); + } + } + } + + public HandleData createHandleData(Throwable throwable, HttpServletRequest req) { + HandleData data = new HandleData(throwable, req); + extractErrorCode(data); + setUpErrorData(data); + + return data; + } + + private void extractErrorCode(HandleData data) { + Throwable originalException; + if (data.throwable instanceof TaskExecutionException + && ((TaskExecutionException) data.throwable).getOriginalException() != null) { + originalException = ((TaskExecutionException) data.throwable).getOriginalException(); + + } else { + originalException = data.throwable; + + } + + if (!(originalException instanceof EaafException)) { + data.errorCode = IStatusMessenger.CODES_INTERNAL_ERROR_GENERIC; + + } else { + data.errorCode = ((EaafException) originalException).getErrorId(); + + } + } + + private void setUpErrorData(HandleData data) { + + if (propertyMap.containsKey(data.errorCode)) { + String action = propertyMap.get(data.errorCode); + + if (action.equals(ActionType.TICKET_REDIRECT.toString())) { + data.actionType = ActionType.TICKET_REDIRECT; + data.generateSupportTicket(); + data.generateRedirect(); + + } else if (action.equals(ActionType.TICKET_NOREDIRECT.toString())) { + data.actionType = ActionType.TICKET_NOREDIRECT; + data.generateSupportTicket(); + + } else if (action.equals(ActionType.NOTICKET_REDIRECT.toString())) { + data.actionType = ActionType.NOTICKET_REDIRECT; + data.generateRedirect(); + + } else {// ActionType.NOTICKET_NOREDIRECT -> nothing to be done + data.actionType = ActionType.NOTICKET_NOREDIRECT; + + } + + } else { + data.generateSupportTicket(); + // TODO log with ticket gernal internal error + } + } + + public class HandleData { + private final HttpServletRequest req; + @Getter private String supportTicket; + @Getter private String redirectUrl; + @Getter private final Throwable throwable; + @Getter private String errorCode; + @Getter private ActionType actionType; + + + private HandleData(Throwable throwable, HttpServletRequest req) { + this.throwable = throwable; + this.req = req; + } + + private void generateRedirect() { + redirectUrl = ServletUtils.getBaseUrl(req); + redirectUrl += "/" + ProtocolFinalizationController.ENDPOINT_ERROR_REDIRECT + + "?" + EaafConstants.PARAM_HTTP_ERROR_CODE + "=" + + StringEscapeUtils.escapeHtml4(req.getParameter(EaafConstants.PARAM_HTTP_ERROR_CODE));; + + } + + private void generateSupportTicket() { + + String randomCode = RandomStringUtils.randomAlphanumeric(4).toUpperCase() + '-' + + RandomStringUtils.randomAlphanumeric(4).toUpperCase() + '-' + + RandomStringUtils.randomAlphanumeric(4).toUpperCase(); + supportTicket = randomCode; + } + + public void log_error() { + + if (supportTicket != null) { + log.error(TICKET_LOG_MSG, supportTicket, errorCode, throwable.getMessage(), + throwable); + } else { + log.error(TECH_LOG_MSG, errorCode, throwable.getMessage(), throwable); + } + } + + public void log_info() { + + if (supportTicket != null) { + log.info(TICKET_LOG_MSG, supportTicket, errorCode, throwable.getMessage(), throwable); + + } else { + log.info(TECH_LOG_MSG, errorCode, throwable.getMessage(), throwable); + } + } + + public void log_warn() { + + if (supportTicket != null) { + log.warn(TICKET_LOG_MSG, supportTicket, errorCode, throwable.getMessage(), throwable); + + } else { + log.warn(TECH_LOG_MSG, errorCode, throwable.getMessage(), throwable); + } + } + } +} -- cgit v1.2.3