From d89f36b67ea1d838a78523538a24e044518f3587 Mon Sep 17 00:00:00 2001 From: mcentner Date: Tue, 26 Jan 2010 16:22:56 +0000 Subject: MOCCA 1.2.11 with SHA-2 enabled. git-svn-id: https://joinup.ec.europa.eu/svn/mocca/branches/mocca-1.2.11-sha2@599 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java | 192 +++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 mocca-1.2.11/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java (limited to 'mocca-1.2.11/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java') diff --git a/mocca-1.2.11/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java b/mocca-1.2.11/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java new file mode 100644 index 00000000..d0762da9 --- /dev/null +++ b/mocca-1.2.11/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java @@ -0,0 +1,192 @@ +/* + * Copyright 2008 Federal Chancellery Austria and + * Graz University of Technology + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package at.gv.egiz.bku.smccstal; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +//import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import at.gv.egiz.bku.gui.BKUGUIFacade; +import at.gv.egiz.smcc.SignatureCard; +import at.gv.egiz.stal.ErrorResponse; +import at.gv.egiz.stal.InfoboxReadRequest; +import at.gv.egiz.stal.STAL; +import at.gv.egiz.stal.STALRequest; +import at.gv.egiz.stal.STALResponse; +import at.gv.egiz.stal.StatusRequest; + +public abstract class AbstractSMCCSTAL implements STAL { + private static Log log = LogFactory.getLog(AbstractSMCCSTAL.class); + + public final static int DEFAULT_MAX_RETRIES = 1; + +// protected Locale locale = Locale.getDefault(); + protected SignatureCard signatureCard = null; + protected Map handlerMap = new HashMap(); + + protected int maxRetries = DEFAULT_MAX_RETRIES; + protected Set unrecoverableErrors = new HashSet(); + + protected AbstractSMCCSTAL() { + addRequestHandler(InfoboxReadRequest.class, new InfoBoxReadRequestHandler()); + addRequestHandler(StatusRequest.class, new StatusRequestHandler()); + unrecoverableErrors.add(6001); + } + + /** + * Implementations must assign the signature card within this method. + * + * @return if the user canceled + */ + protected abstract boolean waitForCard(); + + protected abstract BKUGUIFacade getGUI(); + + private STALResponse getResponse(STALRequest request) throws InterruptedException { + int retryCounter = 0; + while (retryCounter < maxRetries) { + log.info("Retry #" + retryCounter + " of " + maxRetries); + SMCCSTALRequestHandler handler = null; + handler = handlerMap.get(request.getClass().getSimpleName()); + if (handler != null) { + if (handler.requireCard()) { + if (waitForCard()) { + return new ErrorResponse(6001); + } + } + try { + handler.init(signatureCard, getGUI()); + STALResponse response = handler.handleRequest(request); + if (response != null) { + if (response instanceof ErrorResponse) { + log.info("Got an error response"); + ErrorResponse err = (ErrorResponse) response; + if (unrecoverableErrors.contains(err.getErrorCode())) { + return response; + } + if ((++retryCounter < maxRetries) && (handler.requireCard())) { + signatureCard.disconnect(true); + signatureCard = null; + } else { + log.info("Exceeded max retries, returning error " + + err.getErrorMessage()); + return response; + } + } else { + return response; + } + } else { + log.info("Got null response from handler, assuming quit"); + return null; + } + } catch (InterruptedException e) { + log.info("Interrupt during request handling, do not retry"); + throw e; + } catch (Exception e) { + log.info("Error while handling STAL request:", e); + if (++retryCounter < maxRetries) { + signatureCard.disconnect(true); + signatureCard = null; + } else { + log.info("Exceeded max retries, returning error."); + return new ErrorResponse(6000); + } + } + } else { + log.error("Cannot find a handler for STAL request: " + request); + return new ErrorResponse(); + } + } + return new ErrorResponse(6000); + } + + /** + * + * @param requestList + * @return + * @throws RuntimeException with cause InterruptedException if interrupted + */ + @Override + public List handleRequest(List requestList) { + log.debug("Got request list containing " + requestList.size() + + " STAL requests"); + List responseList = new ArrayList(requestList + .size()); + for (STALRequest request : requestList) { + log.info("Processing: " + request.getClass()); + STALResponse response; + try { + response = getResponse(request); + if (response != null) { + responseList.add(response); + if (response instanceof ErrorResponse) { + log.info("Got an error response, don't process remaining requests"); + break; + } + } + } catch (InterruptedException ex) { + log.error("interrupted during request handling"); + throw new RuntimeException("interrupted during request handling", ex); + } + + } + return responseList; + } + + public void addRequestHandler(Class id, + SMCCSTALRequestHandler handler) { + log.debug("Registering STAL request handler: " + id.getSimpleName()); + handlerMap.put(id.getSimpleName(), handler); + } + + public void removeRequestHandler(Class id) { + log.debug("De-registering STAL request handler: " + id.getSimpleName()); + handlerMap.remove(id.getSimpleName()); + } + + public SMCCSTALRequestHandler getRequestHandler( + Class request) { + return handlerMap.get(request.getSimpleName()); + } + +// @Override +// public void setLocale(Locale locale) { +// if (locale == null) { +// throw new NullPointerException("Locale must not be set to null"); +// } +// this.locale = locale; +// } + + public void setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; + } + + public Set getUnrecoverableErrors() { + return unrecoverableErrors; + } + + public void setUnrecoverableErrors(Set unrecoverableErrors) { + this.unrecoverableErrors = unrecoverableErrors; + } +} -- cgit v1.2.3