From 5c1b5b863fe8d6c08cfe0749fed7ce9594827f8a Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Tue, 23 Apr 2019 15:00:13 +0200 Subject: add different strategies for pendingRequestId generation --- .../eaaf/core/impl/idp/auth/RequestStorage.java | 161 +++++++++++++++------ 1 file changed, 118 insertions(+), 43 deletions(-) (limited to 'eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/RequestStorage.java') diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/RequestStorage.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/RequestStorage.java index e4288e62..2115d9b0 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/RequestStorage.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/auth/RequestStorage.java @@ -26,6 +26,7 @@ *******************************************************************************/ package at.gv.egiz.eaaf.core.impl.idp.auth; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -35,35 +36,55 @@ import at.gv.egiz.eaaf.core.api.IRequest; import at.gv.egiz.eaaf.core.api.IRequestStorage; import at.gv.egiz.eaaf.core.api.idp.process.ProcessInstanceStoreDAO; import at.gv.egiz.eaaf.core.api.storage.ITransactionStorage; +import at.gv.egiz.eaaf.core.api.utils.IPendingRequestIdGenerationStrategy; import at.gv.egiz.eaaf.core.exceptions.EAAFException; import at.gv.egiz.eaaf.core.exceptions.EAAFStorageException; +import at.gv.egiz.eaaf.core.exceptions.PendingReqIdValidationException; import at.gv.egiz.eaaf.core.impl.idp.controller.protocols.RequestImpl; -import at.gv.egiz.eaaf.core.impl.utils.Random; import at.gv.egiz.eaaf.core.impl.utils.TransactionIDUtils; @Service("RequestStorage") public class RequestStorage implements IRequestStorage{ private static final Logger log = LoggerFactory.getLogger(RequestStorage.class); - @Autowired ITransactionStorage transactionStorage; - @Autowired ProcessInstanceStoreDAO processInstanceStore; - + @Autowired(required=true) ITransactionStorage transactionStorage; + @Autowired(required=true) ProcessInstanceStoreDAO processInstanceStore; + @Autowired(required=true) IPendingRequestIdGenerationStrategy pendingReqIdGenerationStrategy; + @Override - public IRequest getPendingRequest(String pendingReqID) { + public IRequest getPendingRequest(String pendingReqID) throws PendingReqIdValidationException { try { - IRequest pendingRequest = transactionStorage.get(pendingReqID, IRequest.class); - if (pendingRequest == null) { - log.info("No PendingRequst found with pendingRequestID " + pendingReqID); - return null; - - } + final String internalPendingReqId = + pendingReqIdGenerationStrategy.validateAndGetPendingRequestId(pendingReqID); + log.debug("PendingReqId is valid"); + + //get pending-request from storage + final IRequest pendingRequest = getInternalPendingRequest(internalPendingReqId); //set transactionID and sessionID to Logger TransactionIDUtils.setAllLoggingVariables(pendingRequest); return pendingRequest; - + + } catch (final PendingReqIdValidationException e) { + log.info("PendingRequestId is invalid. Reason: {} ", e.getMessage()); + + // search invalid pending-request for errorHandling + IRequest invalidPendingRequest = null; + try { + if (StringUtils.isNotEmpty(e.getInvalidInternalPendingReqId())) + invalidPendingRequest = transactionStorage.get(e.getInvalidInternalPendingReqId(), IRequest.class); + + } catch (final EAAFException e1) { + log.info("No PendingRequst found with pendingRequestID " + pendingReqID); + return null; + + } + + e.setInvalidPendingReq(invalidPendingRequest); + throw e; + } catch (EAAFException | NullPointerException e) { log.info("No PendingRequst found with pendingRequestID " + pendingReqID); return null; @@ -74,17 +95,27 @@ public class RequestStorage implements IRequestStorage{ @Override public void storePendingRequest(IRequest pendingRequest) throws EAAFException { try { - if (pendingRequest instanceof IRequest) - transactionStorage.put(((IRequest)pendingRequest).getPendingRequestId(), pendingRequest, -1); - - else + if (pendingRequest instanceof IRequest) { + try { + //validate pending-requestId + final String internalPendingRequestId = pendingReqIdGenerationStrategy.validateAndGetPendingRequestId(pendingRequest.getPendingRequestId()); + + //store pending request + transactionStorage.put(internalPendingRequestId, pendingRequest, -1); + + } catch (final PendingReqIdValidationException e) { + log.warn("Invalid pending-request-Id. Reason: {}", e.getMessage()); + log.warn("Do NOT store pending-request with invalid pending-request-Id. The process will break soon!"); + + } + + } else throw new EAAFException("PendigRequest is NOT of type 'IRequest'", null); - - - } catch (EAAFException e) { - log.warn("PendingRequest with ID=" + ((IRequest)pendingRequest).getPendingRequestId() + + + } catch (final EAAFException e) { + log.warn("PendingRequest with ID=" + pendingRequest.getPendingRequestId() + " can not stored.", e); - throw new EAAFStorageException("PendingRequest with Id: " + ((IRequest)pendingRequest).getPendingRequestId() + throw new EAAFStorageException("PendingRequest with Id: " + pendingRequest.getPendingRequestId() + " can not be stored", e); } @@ -92,25 +123,35 @@ public class RequestStorage implements IRequestStorage{ } @Override - public void removePendingRequest(String requestID) { + public void removePendingRequest(String pendingReqID) { - if (requestID != null) { - - //remove process-management execution instance + if (pendingReqID != null) { + String internalPendingReqId = null; try { - IRequest pendingReq = getPendingRequest(requestID); - - if (pendingReq != null && - pendingReq.getProcessInstanceId() != null) - processInstanceStore.remove(pendingReq.getProcessInstanceId()); - - } catch (EAAFException e) { - log.warn("Removing process associated with pending-request:" + requestID + " FAILED.", e); + internalPendingReqId = pendingReqIdGenerationStrategy.validateAndGetPendingRequestId(pendingReqID); + + } catch (final PendingReqIdValidationException e) { + internalPendingReqId = e.getInvalidInternalPendingReqId(); } - - transactionStorage.remove(requestID); + try { + //remove process-management execution instance# + if (internalPendingReqId != null) { + final IRequest pendingReq = getInternalPendingRequest(internalPendingReqId); + if (pendingReq != null && + pendingReq.getProcessInstanceId() != null) + processInstanceStore.remove(pendingReq.getProcessInstanceId()); + + //remove pending-request + transactionStorage.remove(internalPendingReqId); + } + + } catch (final EAAFException e) { + log.warn("Removing process associated with pending-request:" + pendingReqID + " FAILED.", e); + + } + } } @@ -119,25 +160,59 @@ public class RequestStorage implements IRequestStorage{ */ @Override public String changePendingRequestID(IRequest pendingRequest) throws EAAFException { - + + //TODO!!!! + if (pendingRequest instanceof RequestImpl) { - String newRequestID = Random.nextHexRandom32(); - String oldRequestID = pendingRequest.getPendingRequestId(); + //final String newRequestID = Random.nextHexRandom32(); + final String newRequestID = pendingReqIdGenerationStrategy.generateExternalPendingRequestId(); + ((RequestImpl)pendingRequest).setPendingRequestId(newRequestID); - log.debug("Change pendingRequestID from " + pendingRequest.getPendingRequestId() - + " to " + newRequestID); + String newInternalPendingRequestId = null; + try { + newInternalPendingRequestId = pendingReqIdGenerationStrategy.validateAndGetPendingRequestId(newRequestID); + + } catch (final PendingReqIdValidationException e) { + throw new EAAFException("internal.99", new Object[]{"Generate invalid pendingRequestId. Something looks WRONG"}, e); + + } + + String oldInternalRequestID = null; + try { + oldInternalRequestID = + pendingReqIdGenerationStrategy.validateAndGetPendingRequestId(pendingRequest.getPendingRequestId()); - ((RequestImpl)pendingRequest).setPendingRequestId(newRequestID); - transactionStorage.changeKey(oldRequestID, newRequestID, pendingRequest); + } catch (final PendingReqIdValidationException e) { + //it's no problem, because it must be valid before when pending-request was loaded and we change it now + oldInternalRequestID = e.getInvalidInternalPendingReqId(); + + } - //only delete oldRequestID, no change. + log.debug("Change pendingRequestID from " + pendingRequest.getPendingRequestId() + + " to " + newRequestID); + + transactionStorage.changeKey(oldInternalRequestID, newInternalPendingRequestId, pendingRequest); + //only delete oldRequestID, no change. return newRequestID; } else { log.error("PendingRequest object is not of type 'RequestImpl.class'"); throw new EAAFException("PendingRequest object is not of type 'RequestImpl.class'", null); + } } + + private IRequest getInternalPendingRequest(String internalPendingReqId) throws EAAFException { + final IRequest pendingRequest = transactionStorage.get(internalPendingReqId, IRequest.class); + if (pendingRequest == null) { + log.info("No PendingRequst found with pendingRequestID " + internalPendingReqId); + return null; + + } + + return pendingRequest; + + } } -- cgit v1.2.3