001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package org.ejbca.ui.cli.hardtoken.importer;
013:
014: import java.io.IOException;
015: import java.util.Date;
016: import java.util.Properties;
017:
018: import org.ejbca.core.model.SecConst;
019: import org.ejbca.core.model.hardtoken.HardTokenData;
020: import org.ejbca.core.model.hardtoken.types.EnhancedEIDHardToken;
021: import org.ejbca.core.model.hardtoken.types.HardToken;
022: import org.ejbca.core.model.hardtoken.types.SwedishEIDHardToken;
023:
024: /**
025: * The standard file hard token importer, reading the a textfile line by line.
026: *
027: * The Importer have the following properties:
028: * separator : indicates which column separator that should be used.
029: *
030: * columnorder : A list of columnnames separated by ',' of how the columns
031: * are ordered. Valid values are 'tokensn', 'pin1', 'pin2', 'bothpin', 'puk1', 'puk2', 'bothpuk'
032: * Example the line: 'columnorder=tokensn, pin1, pin2, bothpuk' will generate a
033: * hardtokendata with the tokensn from first column, basicpin from the secondcolumn,
034: * signaturepin from third and finally both puk codes from the forth column.
035: *
036: * tokentype : either 'enhancedeid' or 'swedisheid'. And inidcates what type of token that will be created.
037: *
038: *
039: * @author Philip Vendil 2007 apr 23
040: *
041: * @version $Id: StandardFileHardTokenImporter.java,v 1.3 2007/05/07 11:58:55 herrvendil Exp $
042: */
043:
044: public class StandardFileHardTokenImporter extends
045: FileReadHardTokenImporter {
046:
047: public static final String PROPERTY_SEPARATOR = "separator";
048: public static final String PROPERTY_COLUMNORDER = "columnorder";
049: public static final String PROPERTY_TOKENTYPE = "tokentype";
050:
051: private static final int COLUMN_TOKENSN = 1;
052: private static final int COLUMN_PIN1 = 2;
053: private static final int COLUMN_PIN2 = 3;
054: private static final int COLUMN_BOTHPIN = 4;
055: private static final int COLUMN_PUK1 = 5;
056: private static final int COLUMN_PUK2 = 6;
057: private static final int COLUMN_BOTHPUK = 7;
058:
059: private String columnSeparator;
060: private int[] columns = null;
061: private String hardTokenType = null;
062:
063: public void startImport(Properties props) throws IOException {
064: super .startImport(props);
065:
066: getColumns(props);
067:
068: if (props.getProperty(PROPERTY_SEPARATOR) == null) {
069: throw new IOException("Error property "
070: + PROPERTY_SEPARATOR + " not set.");
071: }
072: columnSeparator = props.getProperty(PROPERTY_SEPARATOR);
073:
074: if (props.getProperty(PROPERTY_TOKENTYPE) == null) {
075: throw new IOException("Error property "
076: + PROPERTY_TOKENTYPE + " not set.");
077: }
078: hardTokenType = props.getProperty(PROPERTY_TOKENTYPE);
079: if (!hardTokenType.equalsIgnoreCase("enhancedeid")
080: && !hardTokenType.equalsIgnoreCase("swedisheid")) {
081: throw new IOException(
082: "Error property "
083: + PROPERTY_TOKENTYPE
084: + " must have either the value 'enhancedeid' or 'swedisheid'.");
085: }
086: }
087:
088: private void getColumns(Properties props) throws IOException {
089: if (props.getProperty("columnorder") == null) {
090: throw new IOException(
091: "Error the required property 'columnorder' isn't set.");
092: }
093:
094: String[] c = props.getProperty(PROPERTY_COLUMNORDER).split(",");
095: columns = new int[c.length];
096: for (int i = 0; i < c.length; i++) {
097: columns[i] = getColumn(c[i].trim());
098: }
099:
100: }
101:
102: private int getColumn(String column) throws IOException {
103: if (column.equalsIgnoreCase("tokensn")) {
104: return COLUMN_TOKENSN;
105: }
106: if (column.equalsIgnoreCase("pin1")) {
107: return COLUMN_PIN1;
108: }
109: if (column.equalsIgnoreCase("pin2")) {
110: return COLUMN_PIN2;
111: }
112: if (column.equalsIgnoreCase("bothpin")) {
113: return COLUMN_BOTHPIN;
114: }
115: if (column.equalsIgnoreCase("puk1")) {
116: return COLUMN_PUK1;
117: }
118: if (column.equalsIgnoreCase("puk2")) {
119: return COLUMN_PUK2;
120: }
121: if (column.equalsIgnoreCase("bothpuk")) {
122: return COLUMN_BOTHPUK;
123: }
124: throw new IOException("Error illegal column " + column
125: + " in the " + PROPERTY_COLUMNORDER + " property.");
126: }
127:
128: /**
129: * @see org.ejbca.ui.cli.hardtoken.importer.FileReadHardTokenImporter#readHardTokenData()
130: */
131: public HardTokenData readHardTokenData() throws IOException {
132: HardTokenData retval = null;
133:
134: String line = bufferedReader.readLine();
135: if (line != null) {
136: String basicPIN = "";
137: String signaturePIN = "";
138: String basicPUK = "";
139: String signaturePUK = "";
140: String tokenSN = "";
141:
142: String[] lineColumns = line.split(columnSeparator);
143: for (int i = 0; i < lineColumns.length; i++) {
144: lineColumns[i] = lineColumns[i].trim();
145: switch (columns[i]) {
146: case COLUMN_TOKENSN:
147: tokenSN = lineColumns[i];
148: break;
149: case COLUMN_PIN1:
150: basicPIN = lineColumns[i];
151: break;
152: case COLUMN_PIN2:
153: signaturePIN = lineColumns[i];
154: break;
155: case COLUMN_BOTHPIN:
156: basicPIN = lineColumns[i];
157: signaturePIN = lineColumns[i];
158: break;
159: case COLUMN_PUK1:
160: basicPUK = lineColumns[i];
161: break;
162: case COLUMN_PUK2:
163: signaturePUK = lineColumns[i];
164: break;
165: case COLUMN_BOTHPUK:
166: basicPUK = lineColumns[i];
167: signaturePUK = lineColumns[i];
168: break;
169: default:
170: throw new IOException("Error reading column + " + i
171: + " of hard token import data.");
172: }
173: }
174: int tokenType = SecConst.TOKEN_SWEDISHEID;
175: if (hardTokenType.equalsIgnoreCase("enhancedeid")) {
176: tokenType = SecConst.TOKEN_ENHANCEDEID;
177: }
178: HardToken ht = getHardTokenType(basicPIN, basicPUK,
179: signaturePIN, signaturePUK);
180: retval = new HardTokenData(tokenSN, null, new Date(),
181: new Date(), tokenType, null, ht, null, null);
182: }
183:
184: return retval;
185: }
186:
187: private HardToken getHardTokenType(String basicPIN,
188: String basicPUK, String signaturePIN, String signaturePUK) {
189: if (hardTokenType.equalsIgnoreCase("enhancedeid")) {
190: return new EnhancedEIDHardToken(signaturePIN, signaturePUK,
191: basicPIN, basicPUK, false, 0);
192: }
193: return new SwedishEIDHardToken(basicPIN, basicPUK,
194: signaturePIN, signaturePUK, 0);
195: }
196:
197: }
|