package at.asitplus.eidas.specific.core.controller; 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; /** * 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) 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 EaafException In case of an internal error */ @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()) { writeHtmlErrorResponse(new EaafException(INTERNAL_97), "9099", req, resp); return; } } writeHtmlErrorResponse(new EaafException(INTERNAL_96), "9098", req, resp); } 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 { final IGuiBuilderConfiguration config = guiConfigFactory .getDefaultErrorGui(HttpUtils.extractAuthUrlFromRequest(httpReq)); // 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); } } }