From c8dfbc7f0b55a4f8c5238452055e5087fe46d675 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Mon, 22 Aug 2022 17:10:28 +0200 Subject: feat(core): add default error-handler to show custom error-page on any error --- .../core/controller/DefaultErrorController.java | 75 ++++++++++++++++++++++ .../main/resources/specific_eIDAS_core.beans.xml | 3 + 2 files changed, 78 insertions(+) create mode 100644 modules/core_common_webapp/src/main/java/at/asitplus/eidas/specific/core/controller/DefaultErrorController.java (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 new file mode 100644 index 00000000..a9944674 --- /dev/null +++ b/modules/core_common_webapp/src/main/java/at/asitplus/eidas/specific/core/controller/DefaultErrorController.java @@ -0,0 +1,75 @@ +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.servlet.error.ErrorController; +import org.springframework.http.HttpStatus; +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.idp.auth.services.IProtocolAuthenticationService; +import at.gv.egiz.eaaf.core.exceptions.EaafException; +import at.gv.egiz.eaaf.core.impl.idp.controller.AbstractController; +import lombok.extern.slf4j.Slf4j; + +/** + * Default error handler for any request that is not part of MS-eIDAS-Node. + * + * @author tlenz + * + */ +@Slf4j +@Controller +public class DefaultErrorController extends AbstractController implements ErrorController { + + private static final String INTERNAL_97 = "internal.97"; + private static final String INTERNAL_96 = "internal.96"; + + @Autowired(required = true) + protected IProtocolAuthenticationService protAuthService; + + /** + * 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 + */ + @RequestMapping(EaafConstants.ENDPOINT_PREFIX_PUBLIC + "/error") + public void handleGenericErrors(final HttpServletRequest req, final HttpServletResponse resp) + throws IOException { + 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); + return; + + } + } + + forwardToCentralErrorHandling(new EaafException(INTERNAL_96), req, resp); + + } + + private void forwardToCentralErrorHandling(Throwable e, HttpServletRequest req, HttpServletResponse resp) + throws IOException { + try { + protAuthService.handleErrorNoRedirect(e, req, resp, false); + + } 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); + + } + } + +} 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 af3594a5..04694ef1 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 @@ -28,6 +28,9 @@ + + -- cgit v1.2.3