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); } } }