ASN.1 File
Since Camel 2.20
The ASN.1 Data Format is a Camel Framework’s data format implementation based on Bouncy Castle’s bcprov-jdk18on library and jASN.1’s java compiler for the formal notation used for describing data transmitted by telecommunications protocols, regardless of language implementation and physical representation of these data, whatever the application, whether complex or very simple. Messages can be unmarshalled to byte arrays, or, with usingIterator and unmarshalType, to plain Java objects (POJOs) of the classes the jASN.1 compiler generated. With the help of Camel’s routing engine and data transformations, you can then play with POJO(s) and apply customized formatting and call other Camel Components to convert and send messages to upstream systems.
ASN.1 Data Format Options
The ASN.1 File dataformat supports the following options which are listed below.
Message Body
The message body at each side of the ASN.1 File data format: Marshal input and Unmarshal input are the bodies marshal and unmarshal can take, and Marshal output and Unmarshal output the body after them.
| Side | Body | Description |
|---|---|---|
Marshal input |
| BER-encoded data, written as it is. With usingIterator, a byte array or a Bouncy Castle ASN1Primitive, and with unmarshalType as well an object of a class the asn1bean compiler generated.
Not known when |
Marshal output |
| The BER-encoded data in the stream cache, or a byte array when stream caching is off. With usingIterator, and no unmarshalType, nothing is written for a body that is neither a byte array nor an ASN1Primitive. |
Unmarshal input |
| BER-encoded data: one record, or several one after the other. |
Unmarshal output |
| The records, encoded again, in one byte array. With usingIterator, an Iterator for the split EIP: of messages with the bytes of one record each, or of objects of the class unmarshalType names.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers are described in the Camel Catalog.
Unmarshal
There are 3 different ways to unmarshal ASN.1 structured messages. (Usually binary files)
In this first example, we unmarshal BER file payload to a byte array, with the records encoded again, and send it to mock endpoint.
-
Java
-
YAML
from("direct:unmarshal").unmarshal(asn1).to("mock:unmarshal"); - route:
from:
uri: direct:unmarshal
steps:
- unmarshal:
asn1: {}
- to:
uri: mock:unmarshal In the second example, we unmarshal BER file payload to a byte array using Split EIP. The reason for applying Split EIP is that usually each BER file or (ASN.1 structured file) contains multiple records to process and Split EIP helps us to get each record in a file as a message whose body is the bytes of one record, encoded as an ASN1Primitive (by the use of Bouncy Castle’s ASN.1 support in bcprov-jdk18on library) Byte arrays then may be converted to ASN1Primitive by the help of public static method in (ASN1Primitive.fromByteArray) In such example, note that you need to set usingIterator=true
-
Java
-
YAML
from("direct:unmarshal")
.unmarshal(asn1)
.split(bodyAs(Iterator.class)).streaming()
.to("mock:unmarshal"); - route:
from:
uri: direct:unmarshal
steps:
- unmarshal:
asn1:
usingIterator: true
- split:
expression:
simple:
expression: "${body}"
streaming: true
steps:
- to:
uri: mock:unmarshal In the last example, we unmarshalled a BER file payload to plain old Java Objects using Split EIP. The reason for applying Split EIP is already mentioned in the previous example. Please note and keep in mind that reason. In such example we also need to set the fully qualified name of the class or <YourObject>.class reference through data format. The important thing to note here is that your object should have been generated by jasn1 compiler, which is a nice tool to generate java object representations of your ASN.1 structure. For the reference usage of jasn1 compiler, see the JASN.1 Project Page and please also see how the compiler is invoked with the help of maven’s exec plugin. For example, in this data format’s unit tests an example ASN.1 structure(TestSMSBerCdr.asn1) is added in src/test/resources/asn1_structure. jasn1 compiler is invoked, and java object’s representations are generated in ${basedir}/target/generated/src/test/java The nice thing about this example, you will get POJO instance at the mock endpoint or at whatever your endpoint is.
-
Java
-
YAML
from("direct:unmarshaldsl")
.unmarshal()
.asn1("org.apache.camel.dataformat.asn1.model.testsmscbercdr.SmsCdr")
.split(bodyAs(Iterator.class)).streaming()
.to("mock:unmarshaldsl"); - route:
from:
uri: direct:unmarshaldsl
steps:
- unmarshal:
asn1:
unmarshalType: org.apache.camel.dataformat.asn1.model.testsmscbercdr.SmsCdr
usingIterator: true
- split:
expression:
simple:
expression: "${body}"
streaming: true
steps:
- to:
uri: mock:unmarshaldsl Dependencies
To use ASN.1 data format in your camel routes you need to add a dependency on camel-asn1 which implements this data format.
If you use Maven you can add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-asn1</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>