From f18e44490057ba6e5fa719fefc47c8fd2e039b04 Mon Sep 17 00:00:00 2001 From: lalber Date: Wed, 7 Apr 2021 16:56:26 +0200 Subject: Interface extraction --- .../impl/idp/auth/services/ErrorTicketService.java | 241 --------------------- .../idp/auth/services/IErrorTicketService.java | 92 ++++++++ .../services/ProtocolAuthenticationService.java | 41 ++-- 3 files changed, 111 insertions(+), 263 deletions(-) delete mode 100644 eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ErrorTicketService.java create mode 100644 eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/IErrorTicketService.java (limited to 'eaaf_core/src') 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 deleted file mode 100644 index 08fb04c6..00000000 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ErrorTicketService.java +++ /dev/null @@ -1,241 +0,0 @@ -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"), NOTICKET_AUTOREDIRECT( - "noticket_autoredirect"); - - 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 mapping not known"); - throw new EaafException("internal.configuration.00", - new Object[]{CONFIG_PROP_ERRORHANDLING_ACTION_PATH}); - } else { - - Properties getProperties = new Properties(); - String fullFilePath = null; - try { - - 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: could not found file.", e); - throw new EaafException("internal.configuration.01", - new Object[]{CONFIG_PROP_ERRORHANDLING_ACTION_PATH, "File for errorhandling-action mapping cloud " - + "not be found."}); - } - } - } - - /** - * creates error handling data. - * @param throwable error - * @param req http request - * @return eror handle Data - * @throws EaafException In case of an internal error - */ - public HandleData createHandleData(Throwable throwable, HttpServletRequest req) throws EaafException { - 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) throws EaafException { - - 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 if (action.equals(ActionType.NOTICKET_AUTOREDIRECT.toString())) { - data.actionType = ActionType.NOTICKET_AUTOREDIRECT; - - } else { // ActionType.NOTICKET_NOREDIRECT -> nothing to be done - data.actionType = ActionType.NOTICKET_NOREDIRECT; - - } - - } else { - data.generateSupportTicket(); - throw new EaafException("internal.configuration.00", - new Object[]{data.errorCode + " in on_error_action" + ".properties"}); - } - } - - static 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; - } - - /** - * Logs error to technical log. - */ - 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); - } - } - - /** - * Logs info to technical log. - */ - 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); - } - } - - /** - * Logs warn to technical log. - */ - 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); - } - } - } -} diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/IErrorTicketService.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/IErrorTicketService.java new file mode 100644 index 00000000..15a4c7b1 --- /dev/null +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/IErrorTicketService.java @@ -0,0 +1,92 @@ +package at.gv.egiz.eaaf.core.impl.idp.auth.services; + +import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; +import at.gv.egiz.eaaf.core.exceptions.EaafException; + +import javax.servlet.http.HttpServletRequest; + +public interface IErrorTicketService { + /** + * Describes the kind of action that should be taken. + */ + enum ActionType { + TICKET_REDIRECT("ticket_redirect"), TICKET_NOREDIRECT("ticket_noredirect"), NOTICKET_REDIRECT( + "noticket_redirect"), NOTICKET_NOREDIRECT("noticket_noredirect"), NOTICKET_AUTOREDIRECT( + "noticket_autoredirect"); + + private final String name; + + ActionType(final String text) { + this.name = text; + } + + @Override + public String toString() { + return name; + } + } + + String PARAM_GUI_TICKET = "supportTicket"; + String PARAM_GUI_REDIRECT = "redirectLink"; + + /** + * creates error handling data. + * + * @param throwable error + * @param req http request + * @return eror handle Data + * @throws EaafException In case of an internal error + */ + IHandleData createHandleData(Throwable throwable, HttpServletRequest req) throws EaafException; + + /** + * Displays the error using suitable errordata. + * + * @param c guibuilder + * @param errorData Data to handle + * @throws EaafException In case of an internal error + */ + void displayErrorData(ModifyableGuiBuilderConfiguration c, IErrorTicketService.IHandleData errorData) + throws EaafException; + + /** + * Contains all the Model data for Error Handling. + */ + interface IHandleData { + /** + * Describes the kind of action that should be taken. + * + * @return The appropriate action + */ + ActionType getActionType(); + + /** + * Get internal errorCode describing the problem. + * + * @return internal error Code. + */ + String getInternalErrorCode(); + + /** + * Get the original throwable of the error. + * + * @return causing throwable + */ + Throwable getThrowable(); + + /** + * Logs error to technical log. + */ + void log_error(); + + /** + * Logs info to technical log. + */ + void log_info(); + + /** + * Logs warn to technical log. + */ + void log_warn(); + } +} diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ProtocolAuthenticationService.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ProtocolAuthenticationService.java index 4ac8bba2..c1c2ab00 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ProtocolAuthenticationService.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/services/ProtocolAuthenticationService.java @@ -103,7 +103,7 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer private IConfiguration basicConfig; @Autowired(required = true) - private ErrorTicketService errorTicketService; + private IErrorTicketService errorTicketService; @Autowired(required = false) private ISsoManager ssoManager; @@ -232,10 +232,10 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer public void buildProtocolSpecificErrorResponse(final Throwable throwable, final HttpServletRequest req, final HttpServletResponse resp, final IRequest protocolRequest) throws EaafException, IOException { try { - ErrorTicketService.HandleData errorData = errorTicketService.createHandleData(throwable, req); + IErrorTicketService.IHandleData errorData = errorTicketService.createHandleData(throwable, req); - if (errorData.getActionType().equals(ErrorTicketService.ActionType.TICKET_REDIRECT) || errorData.getActionType() - .equals(ErrorTicketService.ActionType.NOTICKET_REDIRECT)) { + if (errorData.getActionType().equals(IErrorTicketService.ActionType.TICKET_REDIRECT) || errorData.getActionType() + .equals(IErrorTicketService.ActionType.NOTICKET_REDIRECT)) { // Put pending request ExceptionContainer exceptionContainer = new ExceptionContainer(protocolRequest, throwable); @@ -254,7 +254,7 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer displayException(req, resp, errorData); - } else if (errorData.getActionType().equals(ErrorTicketService.ActionType.NOTICKET_AUTOREDIRECT)) { + } else if (errorData.getActionType().equals(IErrorTicketService.ActionType.NOTICKET_AUTOREDIRECT)) { IModulInfo handlingModule = extractShibbolethHandling(protocolRequest, applicationContext); if (handlingModule.generateErrorMessage(throwable, req, resp, protocolRequest)) { @@ -312,7 +312,7 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer public void handleErrorNoRedirect(final Throwable throwable, final HttpServletRequest req, final HttpServletResponse resp, final boolean writeExceptionToStatisticLog) throws EaafException, IOException { - ErrorTicketService.HandleData errorData = null; + IErrorTicketService.IHandleData errorData = null; errorData = errorTicketService.createHandleData(throwable, req); // log Exception into statistic database @@ -323,14 +323,14 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer // write errror to console logExceptionToTechnicalLog(errorData); - if (errorData.getActionType().equals(ErrorTicketService.ActionType.NOTICKET_NOREDIRECT) || errorData.getActionType() - .equals(ErrorTicketService.ActionType.TICKET_NOREDIRECT)) { + if (errorData.getActionType().equals(IErrorTicketService.ActionType.NOTICKET_NOREDIRECT) || errorData + .getActionType().equals(IErrorTicketService.ActionType.TICKET_NOREDIRECT)) { // return error to Web browser displayException(req, resp, errorData); } else { // TODO introduce separate error type? throw new EaafException("internal.configuration.01", new Object[]{ - errorData.getErrorCode() + " in on_error_action" + ".properties", "Erroraction mapping mismatch"}); + errorData.getInternalErrorCode() + " in on_error_action" + ".properties", "Erroraction mapping mismatch"}); } } @@ -418,7 +418,7 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer * * @param data errordata structure */ - protected void logExceptionToTechnicalLog(ErrorTicketService.HandleData data) { + protected void logExceptionToTechnicalLog(IErrorTicketService.IHandleData data) { // In case of a TaskExecutionException, which is only a container for process-errors, // extract internal exception @@ -429,7 +429,7 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer } else { - if (logOnInfoLevel.contains(data.getErrorCode())) { + if (logOnInfoLevel.contains(data.getInternalErrorCode())) { data.log_info(); } else { @@ -492,7 +492,8 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer private void writeHtmlErrorResponse(@NonNull final HttpServletRequest httpReq, @NonNull final HttpServletResponse httpResp, @NonNull final String msg, @NonNull final String errorCode, - @Nullable final Object[] params, String externalErrorCode, String url, String ticket) throws EaafException { + @Nullable final Object[] params, String externalErrorCode, IErrorTicketService.IHandleData errorData) + throws EaafException { try { final IGuiBuilderConfiguration config = guiConfigFactory @@ -523,8 +524,7 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer externalErrorCode); c.putCustomParameterWithOutEscaption(AbstractGuiFormBuilderConfiguration.PARAM_GROUP_MSG, PARAM_GUI_ERRORCODEPARAMS, ArrayUtils.toString(errorCodeParams)); - c.putCustomParameter(AbstractGuiFormBuilderConfiguration.PARAM_GROUP_MSG, PARAM_GUI_TICKET, ticket); - c.putCustomParameter(AbstractGuiFormBuilderConfiguration.PARAM_GROUP_MSG, PARAM_GUI_REDIRECT, url); + errorTicketService.displayErrorData(c, errorData); } else { log.info("Can not ADD error message, because 'GUIBuilderConfiguration' is not modifieable "); @@ -541,9 +541,9 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer } private void displayException(final HttpServletRequest req, final HttpServletResponse resp, - final ErrorTicketService.HandleData errorData) throws IOException, EaafException { + final IErrorTicketService.IHandleData errorData) throws IOException, EaafException { final Throwable e = errorData.getThrowable(); - final String internalErrorCode = errorData.getErrorCode(); + final String internalErrorCode = errorData.getInternalErrorCode(); // send error response if (e instanceof ProtocolNotActiveException) { @@ -556,21 +556,18 @@ public class ProtocolAuthenticationService implements IProtocolAuthenticationSer || e instanceof ProcessExecutionException || e instanceof ConfigurationException) { // write error message writeHtmlErrorResponse(req, resp, e.getMessage(), internalErrorCode, null, - statusMessager.mapInternalErrorToExternalError(internalErrorCode), errorData.getRedirectUrl(), - errorData.getSupportTicket()); + statusMessager.mapInternalErrorToExternalError(internalErrorCode), errorData); } else if (e instanceof EaafException) { // send HTML formated error message writeHtmlErrorResponse(req, resp, e.getMessage(), internalErrorCode, ((EaafException) e).getParams(), - statusMessager.mapInternalErrorToExternalError(internalErrorCode), errorData.getRedirectUrl(), - errorData.getSupportTicket()); + statusMessager.mapInternalErrorToExternalError(internalErrorCode), errorData); } else { // write generic message for general exceptions final String msg = statusMessager.getMessage(IStatusMessenger.CODES_INTERNAL_ERROR_GENERIC, null); writeHtmlErrorResponse(req, resp, msg, internalErrorCode, null, - statusMessager.mapInternalErrorToExternalError(internalErrorCode), errorData.getRedirectUrl(), - errorData.getSupportTicket()); + statusMessager.mapInternalErrorToExternalError(internalErrorCode), errorData); } } -- cgit v1.2.3