summaryrefslogtreecommitdiff
path: root/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/AbstractController.java
diff options
context:
space:
mode:
authorThomas Lenz <thomas.lenz@egiz.gv.at>2019-12-04 19:43:32 +0100
committerThomas Lenz <thomas.lenz@egiz.gv.at>2019-12-04 19:43:32 +0100
commit759ac5f42c6aff901dbeede4fbf1a1d2e08cad0f (patch)
tree2132024fc058b1ef5338bf50df575a3244cc3f9f /eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/AbstractController.java
parent4f15bdc45b08724d20c66c9fd74ea6a43a03c32f (diff)
downloadEAAF-Components-759ac5f42c6aff901dbeede4fbf1a1d2e08cad0f.tar.gz
EAAF-Components-759ac5f42c6aff901dbeede4fbf1a1d2e08cad0f.tar.bz2
EAAF-Components-759ac5f42c6aff901dbeede4fbf1a1d2e08cad0f.zip
common EGIZ code-style refactoring
Diffstat (limited to 'eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/AbstractController.java')
-rw-r--r--eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/AbstractController.java394
1 files changed, 214 insertions, 180 deletions
diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/AbstractController.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/AbstractController.java
index 4505163d..7c42f506 100644
--- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/AbstractController.java
+++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/controller/AbstractController.java
@@ -1,43 +1,27 @@
-/*******************************************************************************
- * Copyright 2017 Graz University of Technology
- * EAAF-Core Components has been developed in a cooperation between EGIZ,
- * A-SIT Plus, A-SIT, and Graz University of Technology.
+/*
+ * Copyright 2017 Graz University of Technology EAAF-Core Components has been developed in a
+ * cooperation between EGIZ, A-SIT Plus, A-SIT, and Graz University of Technology.
*
- * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
- * the European Commission - subsequent versions of the EUPL (the "Licence");
- * You may not use this work except in compliance with the Licence.
- * You may obtain a copy of the Licence at:
+ * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
+ * Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in
+ * compliance with the Licence. You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/news/understanding-eupl-v12
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the Licence is distributed on an "AS IS" basis,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the Licence for the specific language governing permissions and
- * limitations under the Licence.
- *
- * This product combines work with different licenses. See the "NOTICE" text
- * file for details on the various modules and licenses.
- * The "NOTICE" text file is part of the distribution. Any derivative works
- * that you distribute must include a readable copy of the "NOTICE" text file.
- *******************************************************************************/
-/*******************************************************************************
- *******************************************************************************/
-/*******************************************************************************
- *******************************************************************************/
+ * Unless required by applicable law or agreed to in writing, software distributed under the Licence
+ * is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the Licence for the specific language governing permissions and limitations under
+ * the Licence.
+ *
+ * This product combines work with different licenses. See the "NOTICE" text file for details on the
+ * various modules and licenses. The "NOTICE" text file is part of the distribution. Any derivative
+ * works that you distribute must include a readable copy of the "NOTICE" text file.
+*/
+
package at.gv.egiz.eaaf.core.impl.idp.controller;
import java.io.IOException;
-
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-
-import org.apache.commons.text.StringEscapeUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.ApplicationContext;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-
import at.gv.egiz.components.eventlog.api.EventConstants;
import at.gv.egiz.eaaf.core.api.IRequest;
import at.gv.egiz.eaaf.core.api.IStatusMessenger;
@@ -47,165 +31,215 @@ import at.gv.egiz.eaaf.core.api.idp.IConfigurationWithSP;
import at.gv.egiz.eaaf.core.api.idp.auth.services.IProtocolAuthenticationService;
import at.gv.egiz.eaaf.core.api.logging.IRevisionLogger;
import at.gv.egiz.eaaf.core.api.storage.ITransactionStorage;
-import at.gv.egiz.eaaf.core.exceptions.EAAFException;
+import at.gv.egiz.eaaf.core.exceptions.EaafException;
import at.gv.egiz.eaaf.core.exceptions.PendingReqIdValidationException;
import at.gv.egiz.eaaf.core.exceptions.ProcessExecutionException;
import at.gv.egiz.eaaf.core.exceptions.TaskExecutionException;
import at.gv.egiz.eaaf.core.impl.utils.Random;
import at.gv.egiz.eaaf.core.impl.utils.ServletUtils;
+import org.apache.commons.text.StringEscapeUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.web.bind.annotation.ExceptionHandler;
/**
+ * Basic application controller that implements core error-handling.
+ *
* @author tlenz
*
*/
public abstract class AbstractController {
- private static final Logger log = LoggerFactory.getLogger(AbstractController.class);
-
- @Autowired(required=true) protected IProtocolAuthenticationService protAuthService;
- @Autowired(required=true) protected ApplicationContext applicationContext;
- @Autowired(required=true) protected IConfigurationWithSP authConfig;
- @Autowired(required=true) protected ITransactionStorage transactionStorage;
- @Autowired(required=true) protected IStatusMessenger statusMessager;
-
- @Autowired protected IRevisionLogger revisionsLogger;
-
- @ExceptionHandler({EAAFException.class})
- public void EAAFExceptionHandler(final HttpServletRequest req, final HttpServletResponse resp, final Exception e) throws IOException {
- try {
- protAuthService.handleErrorNoRedirect(e, req, resp, true);
-
- } catch (final EAAFException e1) {
- log.warn("Can NOT handle an 'EAAFException'. Forwarding to generic error ... ", e);
- IOExceptionHandler(resp, e);
-
- }
-
- }
-
- @ExceptionHandler({Exception.class})
- public void GenericExceptionHandler(final HttpServletResponse resp, final Exception exception) throws IOException {
- log.error("Internel Server Error." , exception);
- resp.setContentType(EAAFConstants.CONTENTTYPE_HTML_UTF8);
- resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal Server Error!" +
- "(Errorcode=9199"
- +" | Description="
- + StringEscapeUtils.escapeHtml4(StringEscapeUtils.escapeEcmaScript(exception.getMessage()))
- + ")");
- return;
-
- }
-
- @ExceptionHandler({IOException.class})
- public void IOExceptionHandler(final HttpServletResponse resp, final Throwable exception) {
- log.error("Internel Server Error." , exception);
- resp.setContentType(EAAFConstants.CONTENTTYPE_HTML_UTF8);
- resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
-
- }
-
- protected void handleError(final String errorMessage, final Throwable exceptionThrown,
- final HttpServletRequest req, final HttpServletResponse resp, IRequest pendingReq) throws IOException, EAAFException {
-
- Throwable loggedException = null;
- final Throwable extractedException = extractOriginalExceptionFromProcessException(exceptionThrown);
-
- //extract pendingRequestID and originalException if it was a TaskExecutionException
- if (extractedException instanceof TaskExecutionException) {
- //set original exception
- loggedException = ((TaskExecutionException) extractedException).getOriginalException();
-
- } else if (exceptionThrown instanceof PendingReqIdValidationException) {
- log.trace("Find pendingRequestId validation exception. Looking for invalid pending-request ... ");
- if (((PendingReqIdValidationException) exceptionThrown).getInvalidPendingReq() != null)
- pendingReq = ((PendingReqIdValidationException) exceptionThrown).getInvalidPendingReq();
-
- }
-
- //use TaskExecutionException directly, if no Original Exeception is included
- if (loggedException == null)
- loggedException = exceptionThrown;
-
- try {
- //switch to protocol-finalize method to generate a protocol-specific error message
-
- //log error directly in debug mode
- if (log.isDebugEnabled())
- log.warn(loggedException.getMessage(), loggedException);
-
-
- //put exception into transaction store for redirect
- final String key = Random.nextLongRandom();
- if (pendingReq != null) {
- revisionsLogger.logEvent(pendingReq, EventConstants.TRANSACTION_ERROR);
- transactionStorage.put(key,
- new ExceptionContainer(pendingReq, loggedException), -1);
-
- } else {
- transactionStorage.put(key,
- new ExceptionContainer(null, loggedException), -1);
-
- }
-
- //build up redirect URL
- String redirectURL = null;
- redirectURL = ServletUtils.getBaseUrl(req);
- redirectURL += "/"+ProtocolFinalizationController.ENDPOINT_ERRORHANDLING
- + "?" + EAAFConstants.PARAM_HTTP_ERROR_CODE + "=" + key;
-
- resp.setContentType("text/html");
- resp.setStatus(302);
-
- resp.addHeader("Location", redirectURL);
- log.debug("REDIRECT TO: " + redirectURL);
-
- return;
-
- } catch (final Exception e) {
- log.warn("Default error-handling FAILED. Exception can not be stored ....", e);
- log.info("Switch to generic generic backup error-handling ... ");
- protAuthService.handleErrorNoRedirect(loggedException, req, resp, true);
-
- }
-
- }
-
-
-
-
-
- /**
- * Extracts a TaskExecutionException of a ProcessExecutionExeception Stacktrace.
- *
- * @param exception
- * @return Return the latest TaskExecutionExecption if exists, otherwise the latest ProcessExecutionException
- */
- private Throwable extractOriginalExceptionFromProcessException(final Throwable exception) {
- Throwable exholder = exception;
- TaskExecutionException taskExc = null;
-
- while(exholder != null
- && exholder instanceof ProcessExecutionException) {
- final ProcessExecutionException procExc = (ProcessExecutionException) exholder;
- if (procExc.getCause() != null &&
- procExc.getCause() instanceof TaskExecutionException) {
- taskExc = (TaskExecutionException) procExc.getCause();
- exholder = taskExc.getOriginalException();
-
- } else
- break;
-
- }
-
- if (taskExc == null)
- return exholder;
-
- else
- return taskExc;
- }
-
-
-
+ private static final Logger log = LoggerFactory.getLogger(AbstractController.class);
+
+ @Autowired(required = true)
+ protected IProtocolAuthenticationService protAuthService;
+ @Autowired(required = true)
+ protected ApplicationContext applicationContext;
+ @Autowired(required = true)
+ protected IConfigurationWithSP authConfig;
+ @Autowired(required = true)
+ protected ITransactionStorage transactionStorage;
+ @Autowired(required = true)
+ protected IStatusMessenger statusMessager;
+
+ @Autowired
+ protected IRevisionLogger revisionsLogger;
+
+ /**
+ * EAAF framework exception handler.
+ *
+ *<p>
+ *This handler start a protocol-specific error handling.
+ *</p>
+ *
+ * @param req http request
+ * @param resp http response
+ * @param e exception
+ * @throws IOException in case of an exception handling error
+ */
+ @ExceptionHandler({EaafException.class})
+ public void eaafExceptionHandler(final HttpServletRequest req, final HttpServletResponse resp,
+ final Exception e) throws IOException {
+ try {
+ protAuthService.handleErrorNoRedirect(e, req, resp, true);
+
+ } catch (final EaafException e1) {
+ log.warn("Can NOT handle an 'EAAFException'. Forwarding to generic error ... ", e);
+ ioExceptionHandler(resp, e);
+
+ }
+
+ }
+
+ /**
+ * Generic exception handler.
+ *
+ * <p>
+ * This handler wrote an internal server error into http response
+ * </p>
+ *
+ * @param resp http response
+ * @param exception exception
+ * @throws IOException In case of an internal error.
+ */
+ @ExceptionHandler({Exception.class})
+ public void genericExceptionHandler(final HttpServletResponse resp, final Exception exception)
+ throws IOException {
+ log.error("Internel Server Error.", exception);
+ resp.setContentType(EAAFConstants.CONTENTTYPE_HTML_UTF8);
+ resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal Server Error!"
+ + "(Errorcode=9199" + " | Description="
+ + StringEscapeUtils.escapeHtml4(StringEscapeUtils.escapeEcmaScript(exception.getMessage()))
+ + ")");
+ return;
+
+ }
+
+ /**
+ * Generic exception handler.
+ *
+ * <p>
+ * This handler wrote an internal server error into http response
+ * </p>
+ *
+ * @param resp http response
+ * @param exception exception
+ */
+ @ExceptionHandler({IOException.class})
+ public void ioExceptionHandler(final HttpServletResponse resp, final Throwable exception) {
+ log.error("Internel Server Error.", exception);
+ resp.setContentType(EAAFConstants.CONTENTTYPE_HTML_UTF8);
+ resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+ return;
+
+ }
+
+ protected void handleError(final String errorMessage, final Throwable exceptionThrown,
+ final HttpServletRequest req, final HttpServletResponse resp, IRequest pendingReq)
+ throws IOException, EaafException {
+
+ Throwable loggedException = null;
+ final Throwable extractedException =
+ extractOriginalExceptionFromProcessException(exceptionThrown);
+
+ // extract pendingRequestID and originalException if it was a TaskExecutionException
+ if (extractedException instanceof TaskExecutionException) {
+ // set original exception
+ loggedException = ((TaskExecutionException) extractedException).getOriginalException();
+
+ } else if (exceptionThrown instanceof PendingReqIdValidationException) {
+ log.trace(
+ "Find pendingRequestId validation exception. Looking for invalid pending-request ... ");
+ if (((PendingReqIdValidationException) exceptionThrown).getInvalidPendingReq() != null) {
+ pendingReq = ((PendingReqIdValidationException) exceptionThrown).getInvalidPendingReq();
+ }
+
+ }
+
+ // use TaskExecutionException directly, if no Original Exeception is included
+ if (loggedException == null) {
+ loggedException = exceptionThrown;
+ }
+
+ try {
+ // switch to protocol-finalize method to generate a protocol-specific error message
+
+ // log error directly in debug mode
+ if (log.isDebugEnabled()) {
+ log.warn(loggedException.getMessage(), loggedException);
+ }
+
+
+ // put exception into transaction store for redirect
+ final String key = Random.nextLongRandom();
+ if (pendingReq != null) {
+ revisionsLogger.logEvent(pendingReq, EventConstants.TRANSACTION_ERROR);
+ transactionStorage.put(key, new ExceptionContainer(pendingReq, loggedException), -1);
+
+ } else {
+ transactionStorage.put(key, new ExceptionContainer(null, loggedException), -1);
+
+ }
+
+ // build up redirect URL
+ String redirectUrl = null;
+ redirectUrl = ServletUtils.getBaseUrl(req);
+ redirectUrl += "/" + ProtocolFinalizationController.ENDPOINT_ERRORHANDLING + "?"
+ + EAAFConstants.PARAM_HTTP_ERROR_CODE + "=" + key;
+
+ resp.setContentType("text/html");
+ resp.setStatus(302);
+
+ resp.addHeader("Location", redirectUrl);
+ log.debug("REDIRECT TO: " + redirectUrl);
+
+ return;
+
+ } catch (final Exception e) {
+ log.warn("Default error-handling FAILED. Exception can not be stored ....", e);
+ log.info("Switch to generic generic backup error-handling ... ");
+ protAuthService.handleErrorNoRedirect(loggedException, req, resp, true);
+
+ }
+
+ }
+
+
+
+ /**
+ * Extracts a TaskExecutionException of a ProcessExecutionExeception Stacktrace.
+ *
+ * @param exception error
+ * @return Return the latest TaskExecutionExecption if exists, otherwise the latest
+ * ProcessExecutionException
+ */
+ private Throwable extractOriginalExceptionFromProcessException(final Throwable exception) {
+ Throwable exholder = exception;
+ TaskExecutionException taskExc = null;
+
+ while (exholder != null && exholder instanceof ProcessExecutionException) {
+ final ProcessExecutionException procExc = (ProcessExecutionException) exholder;
+ if (procExc.getCause() != null && procExc.getCause() instanceof TaskExecutionException) {
+ taskExc = (TaskExecutionException) procExc.getCause();
+ exholder = taskExc.getOriginalException();
+
+ } else {
+ break;
+ }
+
+ }
+
+ if (taskExc == null) {
+ return exholder;
+ } else {
+ return taskExc;
+ }
+ }
+
+
+
}