diff options
Diffstat (limited to '')
-rw-r--r-- | src/main/java/org/uic/barcode/Encoder.java | 217 |
1 files changed, 168 insertions, 49 deletions
diff --git a/src/main/java/org/uic/barcode/Encoder.java b/src/main/java/org/uic/barcode/Encoder.java index f0f8d14..9afddce 100644 --- a/src/main/java/org/uic/barcode/Encoder.java +++ b/src/main/java/org/uic/barcode/Encoder.java @@ -4,15 +4,21 @@ import java.io.IOException; import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
+import java.util.zip.DataFormatException;
-import org.uic.barcode.asn1.datatypesimpl.OctetString;
+import org.uic.barcode.dynamicContent.api.IUicDynamicContent;
import org.uic.barcode.dynamicContent.fdc1.UicDynamicContentDataFDC1;
import org.uic.barcode.dynamicFrame.Constants;
-import org.uic.barcode.dynamicFrame.DataType;
-import org.uic.barcode.dynamicFrame.DynamicFrame;
-import org.uic.barcode.dynamicFrame.Level1DataType;
-import org.uic.barcode.dynamicFrame.Level2DataType;
-import org.uic.barcode.dynamicFrame.SequenceOfDataType;
+import org.uic.barcode.dynamicFrame.api.DynamicFrameCoder;
+import org.uic.barcode.dynamicFrame.api.IData;
+import org.uic.barcode.dynamicFrame.api.IDynamicFrame;
+import org.uic.barcode.dynamicFrame.api.ILevel1Data;
+import org.uic.barcode.dynamicFrame.api.SimpleData;
+import org.uic.barcode.dynamicFrame.api.SimpleDynamicFrame;
+import org.uic.barcode.dynamicFrame.api.SimpleLevel1Data;
+import org.uic.barcode.dynamicFrame.api.SimpleLevel2Data;
+import org.uic.barcode.dynamicFrame.v1.DynamicFrameCoderV1;
+import org.uic.barcode.dynamicFrame.v2.DynamicFrameCoderV2;
import org.uic.barcode.staticFrame.StaticFrame;
import org.uic.barcode.staticFrame.UFLEXDataRecord;
import org.uic.barcode.staticFrame.UHEADDataRecord;
@@ -33,7 +39,7 @@ import org.uic.barcode.ticket.api.spec.IUicRailTicket; public class Encoder {
/** The dynamic frame. */
- private DynamicFrame dynamicFrame = null;
+ private IDynamicFrame dynamicFrame = null;
/** The static frame. */
private StaticFrame staticFrame = null;
@@ -86,18 +92,19 @@ public class Encoder { } else if (barcodeType == UIC_BARCODE_TYPE_DOSIPAS) {
- dynamicFrame = new DynamicFrame();
- dynamicFrame.setLevel2SignedData(new Level2DataType());
- dynamicFrame.getLevel2SignedData().setLevel1Data(new Level1DataType());
- dynamicFrame.getLevel2SignedData().getLevel1Data().setData(new SequenceOfDataType());
+ dynamicFrame = new SimpleDynamicFrame();
+ dynamicFrame.setLevel2Data(new SimpleLevel2Data());
+ dynamicFrame.getLevel2Data().setLevel1Data(new SimpleLevel1Data());
if (ticket != null) {
if (version == 1) {
- dynamicFrame.setFormat("U1");
- }
+ dynamicFrame.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1);
+ } else if (version == 2) {
+ dynamicFrame.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2);
+ }
- DataType ticketData = new DataType();
+ IData ticketData = new SimpleData();
UicRailTicketCoder uicTicketCoder = new UicRailTicketCoder();
if (fcbVersion == 1 || fcbVersion == 13) {
@@ -107,13 +114,106 @@ public class Encoder { } else if (fcbVersion == 3) {
ticketData.setFormat(Constants.DATA_TYPE_FCB_VERSION_3);
}
- ticketData.setData(new OctetString(uicTicketCoder.encode(ticket, fcbVersion)));
- dynamicFrame.getLevel2SignedData().getLevel1Data().getData().add(ticketData);
+ ticketData.setData(uicTicketCoder.encode(ticket, fcbVersion));
+ dynamicFrame.getLevel2Data().getLevel1Data().addData(ticketData);
}
}
}
+ /**
+ * Instantiates a new encoder for a level 2 encoding.
+ *
+ * @param level1Data the level 1 data (binary as signed)
+ * @param signatureLevel1 the signature of the level 1 data
+ * @param version the version of the bar code
+ * @throws IOException Signals that an I/O exception has occurred.
+ * @throws EncodingFormatException the encoding format exception
+ */
+ public Encoder(byte[] level1DataBin, byte[] signatureLevel1, int version) throws IOException, EncodingFormatException {
+
+
+ dynamicFrame = new SimpleDynamicFrame();
+ dynamicFrame.setLevel2Data(new SimpleLevel2Data());
+
+ if (version == 1) {
+
+ dynamicFrame.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1);
+
+ ILevel1Data l1 = DynamicFrameCoderV1.decodeLevel1(level1DataBin);
+
+ dynamicFrame.getLevel2Data().setLevel1Data(l1);
+
+ dynamicFrame.getLevel2Data().setLevel1Signature(signatureLevel1);
+
+ } else if (version == 2) {
+
+ dynamicFrame.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2);
+
+ ILevel1Data l1 = DynamicFrameCoderV2.decodeLevel1(level1DataBin);
+
+ dynamicFrame.getLevel2Data().setLevel1Data(l1);
+
+ dynamicFrame.getLevel2Data().setLevel1Signature(signatureLevel1);
+
+ } else {
+ throw new EncodingFormatException("Version of the dynamic header not supported");
+ }
+
+
+ }
+
+
+ /**
+ * Instantiates a new encoder for a level 2 encoding with tan encoded dynamic frame containing the level 1 data and signature.
+ *
+ * @param level1Data the level 1 data (binary as signed)
+ * @param signatureLevel1 the signature of the level 1 data
+ * @param version the version of the bar code
+ * @throws IOException Signals that an I/O exception has occurred.
+ * @throws EncodingFormatException the encoding format exception
+ * @throws DataFormatException
+ */
+ public Encoder(byte[] encoded, int version) throws IOException, EncodingFormatException, DataFormatException {
+
+ Decoder decoder = new Decoder(encoded);
+
+ if (decoder.getDynamicFrame() == null) {
+ throw new EncodingFormatException("No dynamic frame included");
+ }
+
+
+ dynamicFrame = decoder.getDynamicFrame();
+ byte[] level1DataBin = decoder.getEncodedLevel1Data();
+ byte[] signatureLevel1 = decoder.getLevel1Signature();
+
+ if (version == 1) {
+
+ dynamicFrame.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1);
+
+ ILevel1Data l1 = DynamicFrameCoderV1.decodeLevel1(level1DataBin);
+
+ dynamicFrame.getLevel2Data().setLevel1Data(l1);
+
+ dynamicFrame.getLevel2Data().setLevel1Signature(signatureLevel1);
+
+ } else if (version == 2) {
+
+ dynamicFrame.setFormat(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2);
+
+ ILevel1Data l1 = DynamicFrameCoderV2.decodeLevel1(level1DataBin);
+
+ dynamicFrame.getLevel2Data().setLevel1Data(l1);
+
+ dynamicFrame.getLevel2Data().setLevel1Signature(signatureLevel1);
+
+ } else {
+ throw new EncodingFormatException("Version of the dynamic header not supported");
+ }
+
+
+ }
+
/**
@@ -150,8 +250,8 @@ public class Encoder { */
public void setLevel1Algs(String level1SigningAlg, String level1KeyAlg) {
if (dynamicFrame != null) {
- dynamicFrame.getLevel2SignedData().getLevel1Data().setLevel1SigningAlg(level1SigningAlg);
- dynamicFrame.getLevel2SignedData().getLevel1Data().setLevel1KeyAlg(level1KeyAlg);
+ dynamicFrame.getLevel2Data().getLevel1Data().setLevel1SigningAlg(level1SigningAlg);
+ dynamicFrame.getLevel2Data().getLevel1Data().setLevel1KeyAlg(level1KeyAlg);
}
}
@@ -164,51 +264,62 @@ public class Encoder { */
public void setLevel2Algs(String level2SigningAlg, String level2KeyAlg, PublicKey publicKey) {
if (dynamicFrame != null) {
- if (dynamicFrame.getLevel2SignedData() == null) {
- dynamicFrame.setLevel2SignedData(new Level2DataType());
+ if (dynamicFrame.getLevel2Data() == null) {
+ dynamicFrame.setLevel2Data(new SimpleLevel2Data());
}
- if (dynamicFrame.getLevel2SignedData().getLevel1Data() == null) {
- dynamicFrame.getLevel2SignedData().setLevel1Data(new Level1DataType());
+ if (dynamicFrame.getLevel2Data().getLevel1Data() == null) {
+ dynamicFrame.getLevel2Data().setLevel1Data(new SimpleLevel1Data());
}
- dynamicFrame.getLevel2SignedData().getLevel1Data().setLevel2SigningAlg(level2SigningAlg);
- dynamicFrame.getLevel2SignedData().getLevel1Data().setLevel2KeyAlg(level2KeyAlg);
+ dynamicFrame.getLevel2Data().getLevel1Data().setLevel2SigningAlg(level2SigningAlg);
+ dynamicFrame.getLevel2Data().getLevel1Data().setLevel2KeyAlg(level2KeyAlg);
if (publicKey != null) {
- dynamicFrame.getLevel2SignedData().getLevel1Data().setLevel2publicKey(new OctetString(publicKey.getEncoded()));
+ dynamicFrame.getLevel2Data().getLevel1Data().setLevel2publicKey(publicKey.getEncoded());
}
}
}
- public void setLevel2Data(DataType level2data) {
+ public void setDynamicData(IUicDynamicContent content) throws EncodingFormatException {
if (dynamicFrame != null) {
- if (dynamicFrame.getLevel2SignedData() == null) {
- dynamicFrame.setLevel2SignedData(new Level2DataType());
+ if (dynamicFrame.getLevel2Data() == null) {
+ dynamicFrame.setLevel2Data(new SimpleLevel2Data());
}
- dynamicFrame.getLevel2SignedData().setLevel2Data(level2data);
+ dynamicFrame.addDynamicContent(content);
+ }
+ }
+
+ public void setLevel2Data(IData level2data) {
+ if (dynamicFrame != null) {
+ if (dynamicFrame.getLevel2Data() == null) {
+ dynamicFrame.setLevel2Data(new SimpleLevel2Data());
+ }
+ dynamicFrame.getLevel2Data().setLevel2Data(level2data);
}
}
public void setDynamicContentDataUIC1(UicDynamicContentDataFDC1 dcd) {
if (dynamicFrame != null) {
- if (dynamicFrame.getLevel2SignedData() == null) {
- dynamicFrame.setLevel2SignedData(new Level2DataType());
+ if (dynamicFrame.getLevel2Data() == null) {
+ dynamicFrame.setLevel2Data(new SimpleLevel2Data());
}
- dynamicFrame.getLevel2SignedData().setLevel2Data(dcd.getDataType());
+ dynamicFrame.getLevel2Data().setLevel2Data(dcd.getApiDataType());
}
}
- public DataType getLevel2Data() {
- if (dynamicFrame != null && dynamicFrame.getLevel2SignedData() != null) {
- return dynamicFrame.getLevel2SignedData().getLevel2Data();
+ public IData getLevel2Data() {
+ if (dynamicFrame != null && dynamicFrame.getLevel2Data() != null) {
+ return dynamicFrame.getLevel2Data().getLevel2Data();
}
return null;
}
- public UicDynamicContentDataFDC1 getDynamicContentDataUIC1() {
- if (dynamicFrame != null && dynamicFrame.getLevel2SignedData() != null) {
- return dynamicFrame.getDynamicDataFDC1();
+
+ public IUicDynamicContent getDynamicContent() {
+ if (dynamicFrame != null && dynamicFrame.getLevel2Data() != null) {
+ return dynamicFrame.getDynamicContent();
}
return null;
}
+
/**
@@ -222,10 +333,10 @@ public class Encoder { */
public void signLevel1(String securityProvider,PrivateKey key,String signingAlg, String keyId) throws Exception {
if (dynamicFrame != null) {
- dynamicFrame.getLevel2SignedData().getLevel1Data().setSecurityProvider(securityProvider);
- dynamicFrame.getLevel2SignedData().getLevel1Data().setLevel1SigningAlg(signingAlg);
- dynamicFrame.getLevel2SignedData().getLevel1Data().setKeyId(Long.parseLong(keyId));
- dynamicFrame.getLevel2SignedData().signLevel1(key);
+ dynamicFrame.getLevel2Data().getLevel1Data().setSecurityProvider(securityProvider);
+ dynamicFrame.getLevel2Data().getLevel1Data().setLevel1SigningAlg(signingAlg);
+ dynamicFrame.getLevel2Data().getLevel1Data().setKeyId(Long.parseLong(keyId));
+ dynamicFrame.signLevel1(key);
} else if (staticFrame != null) {
staticFrame.setSignatureKey(keyId);
staticFrame.setSecurityProvider(securityProvider);
@@ -248,10 +359,10 @@ public class Encoder { */
public void signLevel1(String securityProvider,PrivateKey key,String signingAlg, String keyId, Provider prov) throws Exception {
if (dynamicFrame != null) {
- dynamicFrame.getLevel2SignedData().getLevel1Data().setSecurityProvider(securityProvider);
- dynamicFrame.getLevel2SignedData().getLevel1Data().setLevel1SigningAlg(signingAlg);
- dynamicFrame.getLevel2SignedData().getLevel1Data().setKeyId(Long.parseLong(keyId));
- dynamicFrame.getLevel2SignedData().signLevel1(key, prov);
+ dynamicFrame.getLevel2Data().getLevel1Data().setSecurityProvider(securityProvider);
+ dynamicFrame.getLevel2Data().getLevel1Data().setLevel1SigningAlg(signingAlg);
+ dynamicFrame.getLevel2Data().getLevel1Data().setKeyId(Long.parseLong(keyId));
+ dynamicFrame.signLevel1(key,prov);
} else if (staticFrame != null) {
staticFrame.setSignatureKey(keyId);
staticFrame.setSecurityProvider(securityProvider);
@@ -281,7 +392,7 @@ public class Encoder { *
* @return the dynamic frame
*/
- public DynamicFrame getDynamicFrame() {
+ public IDynamicFrame getDynamicFrame() {
return dynamicFrame;
}
@@ -307,7 +418,7 @@ public class Encoder { */
public byte[] encode() throws IOException, Exception {
if (dynamicFrame != null) {
- return dynamicFrame.encode();
+ return DynamicFrameCoder.encode(dynamicFrame);
} else if (staticFrame != null) {
return staticFrame.encode();
}
@@ -315,7 +426,15 @@ public class Encoder { }
-
+ public byte[] getEncodedLevel1Data() throws IOException, EncodingFormatException {
+ if (dynamicFrame != null) {
+ return DynamicFrameCoder.encodeLevel1(dynamicFrame);
+ } else if (staticFrame != null) {
+ return staticFrame.getDataForSignature();
+ } else {
+ throw new EncodingFormatException("Unknown Header");
+ }
+ }
|