Example usage for javax.smartcardio TerminalFactory getDefault

List of usage examples for javax.smartcardio TerminalFactory getDefault

Introduction

In this page you can find the example usage for javax.smartcardio TerminalFactory getDefault.

Prototype

public static TerminalFactory getDefault() 

Source Link

Document

Returns the default TerminalFactory instance.

Usage

From source file:Main.java

/**
 * Establishes the connection with the Smart Card (if present)
 * using the first available terminal.//from w w w  . j  a  va 2  s. c  o m
 */
public static Card connectCard() throws CardException {
    // Get available terminals
    List<CardTerminal> terminals = TerminalFactory.getDefault().terminals().list();
    if (terminals.size() < 1) {
        throw new CardException("No attached Smart Card terminals found");
    }

    // Search for connected cards
    Card card = null;
    for (CardTerminal terminal : terminals) {
        if (terminal.isCardPresent()) {
            card = terminal.connect("*");
            break;
        }
    }
    if (card == null) {
        throw new CardException("No connected Smart Cards found");
    }

    return card;
}

From source file:davmail.util.ClientCertificateTest.java

public void testCardReaders() throws CardException {
    for (CardTerminal terminal : TerminalFactory.getDefault().terminals().list()) {
        System.out.println("Card terminal: " + terminal.getName() + " "
                + (terminal.isCardPresent() ? "Card present" : "No card"));
        terminal.waitForCardPresent(10);
        if (terminal.isCardPresent()) {
            Card c = null;//  w w w.j av a 2  s.  c om
            try {
                c = terminal.connect("T=0");
            } catch (Exception e) {
                // failover
                c = terminal.connect("T=1");
            }

            ATR atr = c.getATR();

            byte[] bytes = atr.getBytes();
            System.out.print("card:");

            for (byte b : bytes) {
                System.out.print(" " + Integer.toHexString(b & 0xff));
            }
            System.out.println();

        }
    }
}

From source file:de.willuhn.jameica.hbci.passports.pintan.Controller.java

/**
 * Liefert eine Auswahl verfuegbarer Kartenleser-Bezeichnungen.
 * @return eine Auswahl verfuegbaren Kartenleser-Bezeichnungen.
 * @throws RemoteException//  w  w  w . ja  va2 s.co m
 */
public SelectInput getCardReaders() throws RemoteException {
    if (this.cardReaders != null)
        return this.cardReaders;

    List<String> available = new ArrayList<String>();

    // Erste Zeile Leerer Eintrag.
    // Damit das Feld auch dann leer bleiben kann, wenn der User nur einen
    // Kartenleser hat. Der sollte dann nicht automatisch vorselektiert
    // werden, da dessen Bezeichnung sonst unnoetigerweise hart gespeichert wird
    available.add("");
    try {
        TerminalFactory terminalFactory = TerminalFactory.getDefault();
        CardTerminals terminals = terminalFactory.terminals();
        if (terminals != null) {
            List<CardTerminal> list = terminals.list();
            if (list != null && list.size() > 0) {
                for (CardTerminal t : list) {
                    String name = StringUtils.trimToNull(t.getName());
                    if (name != null)
                        available.add(name);
                }
            }
        }
    } catch (Throwable t) {
        Logger.info("unable to determine card reader list: " + t.getMessage());
        Logger.write(Level.DEBUG, "stacktrace for debugging purpose", t);
    }

    this.cardReaders = new SelectInput(available, this.getConfig().getCardReader());
    this.cardReaders.setEditable(true);
    this.cardReaders.setComment(i18n.tr("nur ntig, wenn mehrere Leser vorhanden"));
    this.cardReaders.setName(i18n.tr("Identifier des PC/SC-Kartenlesers"));
    return this.cardReaders;
}

From source file:src.eidreader.EstEIDUtil.java

public String readCard(final Boolean full) {
    System.err.println("EIDReader.readCard()");
    return AccessController.doPrivileged(new PrivilegedAction<String>() {
        public String run() {
            try {
                TerminalFactory factory = TerminalFactory.getDefault();
                List<CardTerminal> terminals = factory.terminals().list();

                if (terminals.size() == 0) {
                    return "Error: No card reader found";
                }//from   ww  w  .j a v a2 s  .c o  m
                //~ throws java.lang.IndexOutOfBoundsException when there is no reader
                CardTerminal terminal = terminals.get(0);

                if (!terminal.isCardPresent()) {
                    return "Error: No card found on terminal";
                    //~ return new EidReaderResponse(new String[] { "No card found on terminal" });
                }
                // "The best way to go is, as explained by Shane, to use the wildcard protocol."
                // https://forums.oracle.com/message/10531935
                Card card = terminal.connect("T=0");
                // Card card = terminal.connect("T=1");
                // Card card = terminal.connect("*");
                System.err.println("Protocol: " + card.getProtocol());
                ATR atr = card.getATR();

                CardChannel channel = card.getBasicChannel();

                if (BelgianReader.matchesEidAtr(atr)) {
                    System.err.println("It's a Belgian card");
                    BelgianReader pf = new BelgianReader(channel);
                    return pf.resultAsString(full);
                }

                System.err.println("It's an Estonian card");
                PersonalFile pf = new PersonalFile(channel);
                return pf.resultAsString(full);
                //~ return new String[] { pf.toString() };
                //~ return new EidReaderResponse(pf.getData());
                //~ return pf.getSurName();
            } catch (Throwable e) {
                //~ return new EidReaderResponse(new String[] { e.toString() });
                e.printStackTrace();
                return "Error: " + e.toString();
            }
        }
    });
}

From source file:test.be.fedict.eid.applet.PcscTest.java

@Test
public void testReadPhoto() throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    CardTerminals cardTerminals = terminalFactory.terminals();
    CardTerminal cardTerminal = cardTerminals.list().get(0);
    Card card = cardTerminal.connect("T=0");
    CardChannel cardChannel = card.getBasicChannel();
    // select file
    cardChannel.transmit(/* www  . j  a  va 2  s.  co  m*/
            new CommandAPDU(0x00, 0xA4, 0x08, 0x0C, new byte[] { 0x3F, 0x00, (byte) 0xDF, 0x01, 0x40, 0x35 }));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int offset = 0;
    ResponseAPDU responseApdu;
    do {
        // read binary
        responseApdu = cardChannel.transmit(new CommandAPDU(0x00, 0xB0, offset >> 8, offset & 0xFF, 0xff));
        baos.write(responseApdu.getData());
        offset += responseApdu.getData().length;
    } while (responseApdu.getData().length == 0xff);
    BufferedImage photo = ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
    JOptionPane.showMessageDialog(null, new ImageIcon(photo));
}