/* * 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.List; import java.util.Locale; import java.util.Map; 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.smcc.util.SMCCHelper; 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.SignRequest; public abstract class AbstractSMCCSTAL implements STAL { private static Log log = LogFactory.getLog(AbstractSMCCSTAL.class); public final static int DEFAULT_MAX_RETRIES = 3; protected Locale locale = Locale.getDefault(); // protected SMCCHelper smccHelper = new SMCCHelper(); protected SignatureCard signatureCard = null; protected static Map handlerMap = new HashMap(); protected int maxRetries = DEFAULT_MAX_RETRIES; static { addRequestHandler(InfoboxReadRequest.class, new InfoBoxReadRequestHandler()); // addRequestHandler(SignRequest.class, new SignRequestHandler()); } /** * Implementations must assign the signature card within this method. * * @return if the user canceled */ protected abstract boolean waitForCard(); protected abstract BKUGUIFacade getGUI(); @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()); int retryCounter = 0; while (retryCounter < maxRetries) { log.info("Number of retries: "+retryCounter); SMCCSTALRequestHandler handler = null; handler = handlerMap.get(request.getClass().getSimpleName()); if (handler != null) { if (handler.requireCard()) { if (waitForCard()) { responseList.add(new ErrorResponse(6001)); break; } } try { handler = handler.newInstance(); 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 (err.getErrorCode() == 6001) { retryCounter = Integer.MAX_VALUE-1; } if (++retryCounter < maxRetries) { log.info("Retrying"); signatureCard.disconnect(true); signatureCard = null; } else { responseList.add(response); } } else { responseList.add(response); retryCounter = Integer.MAX_VALUE; break; } } else { log.info("Got null response from handler, assuming quit"); retryCounter = Integer.MAX_VALUE; } } catch (Exception e) { log.info("Error while handling STAL request:" + e); if (++retryCounter < maxRetries) { log.info("Retrying"); signatureCard.disconnect(true); signatureCard = null; } else { responseList.add(new ErrorResponse(6000)); } } } else { log.error("Cannot find a handler for STAL request: " + request); responseList.add(new ErrorResponse()); } } } return responseList; } public static void addRequestHandler(Class id, SMCCSTALRequestHandler handler) { log.debug("Registering STAL request handler: " + id.getSimpleName()); handlerMap.put(id.getSimpleName(), handler); } public static 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; } }