diff options
-rw-r--r-- | smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java | 127 | ||||
-rw-r--r-- | smccSTAL/src/test/java/at/gv/egiz/smcc/AbstractSMCCSTALTest.java | 110 |
2 files changed, 183 insertions, 54 deletions
diff --git a/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java b/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java index cf524737..f310dd42 100644 --- a/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java +++ b/smccSTAL/src/main/java/at/gv/egiz/bku/smccstal/AbstractSMCCSTAL.java @@ -18,22 +18,22 @@ 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.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); @@ -45,9 +45,11 @@ public abstract class AbstractSMCCSTAL implements STAL { protected Map<String, SMCCSTALRequestHandler> handlerMap = new HashMap<String, SMCCSTALRequestHandler>(); protected int maxRetries = DEFAULT_MAX_RETRIES; + protected Set<Integer> unrecoverableErrors = new HashSet<Integer>(); protected AbstractSMCCSTAL() { addRequestHandler(InfoboxReadRequest.class, new InfoBoxReadRequestHandler()); + unrecoverableErrors.add(6001); } /** @@ -59,66 +61,76 @@ public abstract class AbstractSMCCSTAL implements STAL { protected abstract BKUGUIFacade getGUI(); + private STALResponse getRespone(STALRequest request) { + log.info("Processing: " + request.getClass()); + 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 (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); + } + @Override public List<STALResponse> handleRequest(List<STALRequest> requestList) { log.debug("Got request list containing " + requestList.size() + " STAL requests"); List<STALResponse> responseList = new ArrayList<STALResponse>(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.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()); + STALResponse response = getRespone(request); + if (response != null) { + responseList.add(response); + if (response instanceof ErrorResponse) { + log.info("Got an error response, don't process remaining requests"); + break; } } } @@ -148,4 +160,11 @@ public abstract class AbstractSMCCSTAL implements STAL { this.maxRetries = maxRetries; } + public Set<Integer> getUnrecoverableErrors() { + return unrecoverableErrors; + } + + public void setUnrecoverableErrors(Set<Integer> unrecoverableErrors) { + this.unrecoverableErrors = unrecoverableErrors; + } } diff --git a/smccSTAL/src/test/java/at/gv/egiz/smcc/AbstractSMCCSTALTest.java b/smccSTAL/src/test/java/at/gv/egiz/smcc/AbstractSMCCSTALTest.java new file mode 100644 index 00000000..59ea141c --- /dev/null +++ b/smccSTAL/src/test/java/at/gv/egiz/smcc/AbstractSMCCSTALTest.java @@ -0,0 +1,110 @@ +package at.gv.egiz.smcc;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import javax.smartcardio.Card;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import at.gv.egiz.bku.gui.BKUGUIFacade;
+import at.gv.egiz.bku.smccstal.AbstractSMCCSTAL;
+import at.gv.egiz.bku.smccstal.SMCCSTALRequestHandler;
+import at.gv.egiz.stal.ErrorResponse;
+import at.gv.egiz.stal.InfoboxReadRequest;
+import at.gv.egiz.stal.InfoboxReadResponse;
+import at.gv.egiz.stal.STALRequest;
+import at.gv.egiz.stal.STALResponse;
+
+public class AbstractSMCCSTALTest extends AbstractSMCCSTAL implements
+ SMCCSTALRequestHandler {
+ private int errorConter;
+
+ @Override
+ protected BKUGUIFacade getGUI() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ protected boolean waitForCard() {
+ signatureCard = new SignatureCard() {
+
+ @Override
+ public byte[] createSignature(byte[] hash, KeyboxName keyboxName,
+ PINProvider provider) throws SignatureCardException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void disconnect(boolean reset) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public byte[] getCertificate(KeyboxName keyboxName)
+ throws SignatureCardException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public byte[] getInfobox(String infobox, PINProvider provider,
+ String domainId) throws SignatureCardException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void init(Card card) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void setLocale(Locale locale) {
+ // TODO Auto-generated method stub
+
+ }
+
+ };
+ return false;
+ }
+
+ @Before
+ public void setUp() {
+ addRequestHandler(InfoboxReadRequest.class, this);
+ }
+
+ @Test
+ public void testRetry() {
+ InfoboxReadRequest irr = new InfoboxReadRequest();
+ List<STALRequest> irrl = new ArrayList<STALRequest>();
+ irrl.add(irr);
+ List<STALResponse> list = handleRequest(irrl);
+ Assert.assertFalse(list.get(0) instanceof ErrorResponse);
+ }
+
+ @Override
+ public STALResponse handleRequest(STALRequest request) {
+ if (++errorConter < 3) {
+ return new ErrorResponse(400);
+ }
+ return new InfoboxReadResponse();
+ }
+
+ @Override
+ public void init(SignatureCard sc, BKUGUIFacade gui) {
+ }
+
+ @Override
+ public boolean requireCard() {
+ return true;
+ }
+
+}
|