From 25c58b0a065db4a38610d54539d707ca4aaae326 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Tue, 23 Aug 2022 09:52:54 +0200 Subject: refact(core): optimize default error handler and add root context "/" --- .../core/controller/DefaultErrorController.java | 74 ++++++++++++++++------ .../main/resources/specific_eIDAS_core.beans.xml | 4 +- 2 files changed, 59 insertions(+), 19 deletions(-) (limited to 'modules') diff --git a/modules/core_common_webapp/src/main/java/at/asitplus/eidas/specific/core/controller/DefaultErrorController.java b/modules/core_common_webapp/src/main/java/at/asitplus/eidas/specific/core/controller/DefaultErrorController.java index a9944674..1f88b632 100644 --- a/modules/core_common_webapp/src/main/java/at/asitplus/eidas/specific/core/controller/DefaultErrorController.java +++ b/modules/core_common_webapp/src/main/java/at/asitplus/eidas/specific/core/controller/DefaultErrorController.java @@ -1,20 +1,27 @@ package at.asitplus.eidas.specific.core.controller; -import java.io.IOException; - import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.http.HttpStatus; +import org.springframework.lang.NonNull; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import at.gv.egiz.eaaf.core.api.data.EaafConstants; +import at.gv.egiz.eaaf.core.api.gui.IGuiBuilderConfiguration; +import at.gv.egiz.eaaf.core.api.gui.IGuiBuilderConfigurationFactory; +import at.gv.egiz.eaaf.core.api.gui.IGuiFormBuilder; +import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; import at.gv.egiz.eaaf.core.api.idp.auth.services.IProtocolAuthenticationService; import at.gv.egiz.eaaf.core.exceptions.EaafException; +import at.gv.egiz.eaaf.core.exceptions.GuiBuildException; +import at.gv.egiz.eaaf.core.impl.gui.AbstractGuiFormBuilderConfiguration; +import at.gv.egiz.eaaf.core.impl.http.HttpUtils; import at.gv.egiz.eaaf.core.impl.idp.controller.AbstractController; import lombok.extern.slf4j.Slf4j; @@ -32,44 +39,75 @@ public class DefaultErrorController extends AbstractController implements ErrorC private static final String INTERNAL_96 = "internal.96"; @Autowired(required = true) - protected IProtocolAuthenticationService protAuthService; - + private IGuiBuilderConfigurationFactory guiConfigFactory; + + private IGuiFormBuilder guiBuilder; + /** * generic error-handler for any error that is not handled otherwise. * * @param req Current HTTP request * @param resp Current HTTP response - * @throws IOException In case of an internal error + * @throws EaafException In case of an internal error */ - @RequestMapping(EaafConstants.ENDPOINT_PREFIX_PUBLIC + "/error") - public void handleGenericErrors(final HttpServletRequest req, final HttpServletResponse resp) - throws IOException { + @RequestMapping(value = { + EaafConstants.ENDPOINT_PREFIX_PUBLIC + "/error", + "/"}) + public void handleGenericErrors(final HttpServletRequest req, final HttpServletResponse resp) throws EaafException { final Object status = req.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); - if (status != null) { final Integer statusCode = Integer.valueOf(status.toString()); if (statusCode == HttpStatus.NOT_FOUND.value()) { - forwardToCentralErrorHandling(new EaafException(INTERNAL_97), req, resp); + writeHtmlErrorResponse(new EaafException(INTERNAL_97), "9099", req, resp); return; } } - forwardToCentralErrorHandling(new EaafException(INTERNAL_96), req, resp); + writeHtmlErrorResponse(new EaafException(INTERNAL_96), "9098", req, resp); } - private void forwardToCentralErrorHandling(Throwable e, HttpServletRequest req, HttpServletResponse resp) - throws IOException { + public void setGuiBuilder(final IGuiFormBuilder guiBuilder) { + this.guiBuilder = guiBuilder; + + } + + private void writeHtmlErrorResponse(@NonNull final EaafException error, @NonNull String externalErrorCode, + @NonNull final HttpServletRequest httpReq, @NonNull final HttpServletResponse httpResp) + throws EaafException { + try { - protAuthService.handleErrorNoRedirect(e, req, resp, false); + final IGuiBuilderConfiguration config = guiConfigFactory + .getDefaultErrorGui(HttpUtils.extractAuthUrlFromRequest(httpReq)); - } catch (final EaafException e1) { - log.warn("ErrorHandling failed with error: ", e.getMessage(), e); - log.warn("Can NOT handle an 'EAAFException'. Forwarding to generic error ... ", e); - ioExceptionHandler(resp, e); + // add errorcode and errormessage + if (config instanceof ModifyableGuiBuilderConfiguration) { + final ModifyableGuiBuilderConfiguration c = (ModifyableGuiBuilderConfiguration) config; + c.putCustomParameter(AbstractGuiFormBuilderConfiguration.PARAM_GROUP_MSG, + IProtocolAuthenticationService.PARAM_GUI_ERROMSG, error.getMessage()); + c.putCustomParameter(AbstractGuiFormBuilderConfiguration.PARAM_GROUP_MSG, + IProtocolAuthenticationService.PARAM_GUI_ERRORCODE, error.getErrorId()); + c.putCustomParameter(AbstractGuiFormBuilderConfiguration.PARAM_GROUP_MSG, + IProtocolAuthenticationService.PARAM_GUI_EXTERNAL_ERRORCODE, + externalErrorCode); + c.putCustomParameterWithOutEscaption(AbstractGuiFormBuilderConfiguration.PARAM_GROUP_MSG, + IProtocolAuthenticationService.PARAM_GUI_ERRORCODEPARAMS, + ArrayUtils.toString(new String[] {})); + + + } else { + log.info("Can not ADD error message, because 'GUIBuilderConfiguration' is not modifieable "); + } + + guiBuilder.build(httpReq, httpResp, config, "Error-Message"); + + } catch (final GuiBuildException e) { + log.warn("Can not build error-message GUI.", e); + throw new EaafException("internal.99", new Object[] {e.getMessage()}, e); } + } } diff --git a/modules/core_common_webapp/src/main/resources/specific_eIDAS_core.beans.xml b/modules/core_common_webapp/src/main/resources/specific_eIDAS_core.beans.xml index 04694ef1..30500bbc 100644 --- a/modules/core_common_webapp/src/main/resources/specific_eIDAS_core.beans.xml +++ b/modules/core_common_webapp/src/main/resources/specific_eIDAS_core.beans.xml @@ -29,7 +29,9 @@ + class="at.asitplus.eidas.specific.core.controller.DefaultErrorController"> + + -- cgit v1.2.3