package at.gv.egiz.eaaf.core.impl.idp.auth.dummy; import java.text.MessageFormat; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import at.gv.egiz.eaaf.core.api.IRequest; import at.gv.egiz.eaaf.core.api.IStatusMessenger; import at.gv.egiz.eaaf.core.api.gui.ModifyableGuiBuilderConfiguration; 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.auth.services.IErrorService; import jakarta.servlet.http.HttpServletRequest; import lombok.Builder; import lombok.Getter; import lombok.Setter; public class DummyDefaultErrorService implements IErrorService { private static final String TECH_LOG_MSG = "errorCode={0} Message={1}"; public static final String JUNIT_EL_SPREDIRECT = "spRedirect"; @Autowired IConfiguration basicConfig; @Autowired IStatusMessenger statusMessager; @Setter private ActionType ticketType = ActionType.NO_TICKET; @Setter private LogLevel logLevel = LogLevel.WARN; @Setter private String errorIdTokenForRedirect; @Override public String getExternalCodeFromInternal(String internalCode) { return statusMessager.mapInternalErrorToExternalError(internalCode); } @Override public IHandleData createHandleData(Throwable throwable, IRequest pendingReq) throws EaafException { String internalErrorId = extractInternalErrorCode(throwable); return HandleData.builder() .throwable(throwable) .internalErrorCode(internalErrorId) .actionType(ticketType) .logLevel(logLevel) .errorIdTokenForRedirect(errorIdTokenForRedirect) .allowSpRedirct(pendingReq != null) .build(); } @Override public void displayErrorData(ModifyableGuiBuilderConfiguration c, IHandleData errorData, HttpServletRequest httpReq) throws EaafException { if (((HandleData)errorData).isAllowSpRedirct()) { c.putCustomParameter(null, JUNIT_EL_SPREDIRECT, "toSpWithToken:" + errorData.getErrorIdTokenForRedirect()); } } private String extractInternalErrorCode(Throwable throwable) { Throwable originalException; if (throwable instanceof TaskExecutionException && ((TaskExecutionException) throwable).getOriginalException() != null) { originalException = ((TaskExecutionException) throwable).getOriginalException(); } else { originalException = throwable; } if (!(originalException instanceof EaafException)) { return IStatusMessenger.CODES_INTERNAL_ERROR_GENERIC; } else { return ((EaafException) originalException).getErrorId(); } } @Builder static class HandleData implements IHandleData { @Getter private boolean allowSpRedirct; @Getter private String errorIdTokenForRedirect; @Getter private final Throwable throwable; @Getter private String internalErrorCode; @Getter private ActionType actionType; @Getter private LogLevel logLevel; @Getter private Map additionalGuiModelElements; public String getPreFormatedErrorMessage() { return MessageFormat.format(TECH_LOG_MSG, internalErrorCode, throwable.getMessage()); } } }