Search in sources :

Example 21 with EstablishContext

use of iso.std.iso_iec._24727.tech.schema.EstablishContext in project open-ecard by ecsec.

the class OpeneCardContext method initialize.

// /
// / Initialization & Shutdown
// /
void initialize() throws UnableToInitialize, NfcUnavailable, NfcDisabled, ApduExtLengthNotSupported {
    String errorMsg = SERVICE_RESPONSE_FAILED;
    if (initialized) {
        throw new UnableToInitialize(SERVICE_ALREADY_INITIALIZED);
    }
    if (appCtx == null) {
        throw new IllegalStateException(NO_APPLICATION_CONTEXT);
    }
    // initialize gui
    eacNavFac = new EacNavigatorFactory();
    List<UserConsentNavigatorFactory<?>> factories = Arrays.asList(eacNavFac, new InsertCardNavigatorFactory());
    gui = new AndroidUserConsent(factories);
    // set up nfc and android marshaller
    IFDProperties.setProperty(IFD_FACTORY_KEY, IFD_FACTORY_VALUE);
    WsdefProperties.setProperty(WSDEF_MARSHALLER_KEY, WSDEF_MARSHALLER_VALUE);
    NFCFactory.setContext(appCtx);
    try {
        boolean nfcAvailable = NFCFactory.isNFCAvailable();
        boolean nfcEnabled = NFCFactory.isNFCEnabled();
        boolean nfcExtendedLengthSupport = NfcUtils.supportsExtendedLength(appCtx);
        if (!nfcAvailable) {
            throw new NfcUnavailable();
        } else if (!nfcEnabled) {
            throw new NfcDisabled();
        } else if (!nfcExtendedLengthSupport) {
            throw new ApduExtLengthNotSupported(NFC_NO_EXTENDED_LENGTH_SUPPORT);
        }
        terminalFactory = IFDTerminalFactory.getInstance();
        LOG.info("Terminal factory initialized.");
    } catch (IFDException ex) {
        errorMsg = UNABLE_TO_INITIALIZE_TF;
        throw new UnableToInitialize(errorMsg, ex);
    }
    try {
        // set up client environment
        env = new ClientEnv();
        // set up dispatcher
        dispatcher = new MessageDispatcher(env);
        env.setDispatcher(dispatcher);
        LOG.info("Message Dispatcher initialized.");
        // set up management
        management = new TinyManagement(env);
        env.setManagement(management);
        LOG.info("Management initialized.");
        // set up event dispatcher
        eventDispatcher = new EventDispatcherImpl();
        env.setEventDispatcher(eventDispatcher);
        LOG.info("Event Dispatcher initialized.");
        // set up SALStateCallback
        cardStates = new CardStateMap();
        SALStateCallback salCallback = new SALStateCallback(env, cardStates);
        eventDispatcher.add(salCallback);
        // set up ifd
        ifd = new IFD();
        ifd.addProtocol(ECardConstants.Protocol.PACE, new PACEProtocolFactory());
        ifd.setGUI(gui);
        ifd.setEnvironment(env);
        env.setIFD(ifd);
        LOG.info("IFD initialized.");
        // set up card recognition
        try {
            recognition = new CardRecognitionImpl(env);
            recognition.setGUI(gui);
            env.setRecognition(recognition);
            LOG.info("CardRecognition initialized.");
        } catch (Exception ex) {
            errorMsg = CARD_REC_INIT_FAILED;
            throw ex;
        }
        // set up SAL
        TinySAL mainSAL = new TinySAL(env, cardStates);
        mainSAL.setGUI(gui);
        sal = new SelectorSAL(mainSAL, env);
        env.setSAL(sal);
        env.setCIFProvider(sal);
        LOG.info("SAL initialized.");
        ViewController viewController = new ViewController() {

            @Override
            public void showSettingsUI() {
            }

            @Override
            public void showDefaultViewUI() {
            }
        };
        // set up addon manager
        try {
            manager = new AddonManager(env, gui, cardStates, viewController, new ClasspathRegistry());
            mainSAL.setAddonManager(manager);
        } catch (Exception ex) {
            errorMsg = ADD_ON_INIT_FAILED;
            throw ex;
        }
        // Initialize the Event Dispatcher
        eventDispatcher.add(this, EventType.TERMINAL_ADDED, EventType.TERMINAL_REMOVED, EventType.CARD_INSERTED, EventType.CARD_RECOGNIZED, EventType.CARD_REMOVED);
        // start event dispatcher
        eventDispatcher.start();
        LOG.info("Event dispatcher started.");
        // initialize SAL
        try {
            WSHelper.checkResult(sal.initialize(new Initialize()));
        } catch (WSHelper.WSException ex) {
            errorMsg = ex.getMessage();
            throw ex;
        }
        // establish context
        try {
            EstablishContext establishContext = new EstablishContext();
            EstablishContextResponse establishContextResponse = ifd.establishContext(establishContext);
            WSHelper.checkResult(establishContextResponse);
            contextHandle = establishContextResponse.getContextHandle();
            LOG.info("ContextHandle: {}", ByteUtils.toHexString(contextHandle));
        } catch (WSHelper.WSException ex) {
            errorMsg = ESTABLISH_IFD_CONTEXT_FAILED;
            throw ex;
        }
        initialized = true;
    } catch (Exception ex) {
        LOG.error(errorMsg, ex);
        throw new UnableToInitialize(errorMsg, ex);
    }
}
Also used : SALStateCallback(org.openecard.common.sal.state.SALStateCallback) EventDispatcherImpl(org.openecard.common.event.EventDispatcherImpl) IFD(org.openecard.ifd.scio.IFD) CardRecognitionImpl(org.openecard.recognition.CardRecognitionImpl) UnableToInitialize(org.openecard.android.ex.UnableToInitialize) Initialize(iso.std.iso_iec._24727.tech.schema.Initialize) NfcDisabled(org.openecard.android.ex.NfcDisabled) ClasspathRegistry(org.openecard.android.utils.ClasspathRegistry) MessageDispatcher(org.openecard.transport.dispatcher.MessageDispatcher) EacNavigatorFactory(org.openecard.gui.android.EacNavigatorFactory) UserConsentNavigatorFactory(org.openecard.gui.android.UserConsentNavigatorFactory) CardStateMap(org.openecard.common.sal.state.CardStateMap) EstablishContext(iso.std.iso_iec._24727.tech.schema.EstablishContext) TinySAL(org.openecard.sal.TinySAL) WSHelper(org.openecard.common.WSHelper) AndroidUserConsent(org.openecard.gui.android.AndroidUserConsent) EstablishContextResponse(iso.std.iso_iec._24727.tech.schema.EstablishContextResponse) PACEProtocolFactory(org.openecard.ifd.protocol.pace.PACEProtocolFactory) IFDException(org.openecard.ifd.scio.IFDException) ApduExtLengthNotSupported(org.openecard.android.ex.ApduExtLengthNotSupported) ClientEnv(org.openecard.common.ClientEnv) ViewController(org.openecard.gui.definition.ViewController) TinyManagement(org.openecard.management.TinyManagement) UnableToInitialize(org.openecard.android.ex.UnableToInitialize) IFDException(org.openecard.ifd.scio.IFDException) AddonManager(org.openecard.addon.AddonManager) InsertCardNavigatorFactory(org.openecard.gui.android.InsertCardNavigatorFactory) SelectorSAL(org.openecard.sal.SelectorSAL) NfcUnavailable(org.openecard.android.ex.NfcUnavailable)

Aggregations

EstablishContext (iso.std.iso_iec._24727.tech.schema.EstablishContext)19 EstablishContextResponse (iso.std.iso_iec._24727.tech.schema.EstablishContextResponse)14 ListIFDs (iso.std.iso_iec._24727.tech.schema.ListIFDs)11 Test (org.testng.annotations.Test)11 ClientEnv (org.openecard.common.ClientEnv)10 SwingDialogWrapper (org.openecard.gui.swing.SwingDialogWrapper)8 SwingUserConsent (org.openecard.gui.swing.SwingUserConsent)8 IFD (org.openecard.ifd.scio.IFD)8 Connect (iso.std.iso_iec._24727.tech.schema.Connect)7 MessageDispatcher (org.openecard.transport.dispatcher.MessageDispatcher)7 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)6 ListIFDsResponse (iso.std.iso_iec._24727.tech.schema.ListIFDsResponse)6 CardStateMap (org.openecard.common.sal.state.CardStateMap)6 SALStateCallback (org.openecard.common.sal.state.SALStateCallback)6 CardRecognitionImpl (org.openecard.recognition.CardRecognitionImpl)6 RecognitionInfo (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType.RecognitionInfo)5 EstablishChannel (iso.std.iso_iec._24727.tech.schema.EstablishChannel)5 EstablishChannelResponse (iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse)5 IOException (java.io.IOException)5 AddonManager (org.openecard.addon.AddonManager)5