Search in sources :

Example 1 with ISOFieldPackager

use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.

the class BERTLVPackager method unpack.

public int unpack(ISOComponent m, byte[] b, boolean nested) throws ISOException {
    LogEvent evt = new LogEvent(this, "unpack");
    try {
        if (m.getComposite() == null)
            throw new ISOException("Can't call packager on non Composite");
        if (b.length == 0)
            // nothing to do
            return 0;
        if (// save a few CPU cycle if no logger available
        logger != null)
            evt.addMessage(ISOUtil.hexString(b));
        int tlvDataLength = b.length;
        int consumed = 0;
        int subFieldNumber = 1;
        if (!nested && fld.length > 1) {
            ISOFieldPackager packager = fld[1];
            if (packager != null) {
                ISOComponent subField = packager.createComponent(1);
                consumed = consumed + packager.unpack(subField, b, consumed);
                m.set(subField);
            }
            subFieldNumber++;
        }
        while (consumed < tlvDataLength) {
            ISOFieldPackager packager;
            if (!nested && fld.length > 1 && (packager = fld[fld.length - 1]) != null && packager.getLength() == tlvDataLength - consumed) {
                ISOComponent subField = packager.createComponent(fld.length - 1);
                consumed = consumed + packager.unpack(subField, b, consumed);
                m.set(subField);
                subFieldNumber++;
            } else {
                // Read the Tag per BER
                UnpackResult tagUnpackResult = unpackTag(b, consumed);
                consumed = consumed + tagUnpackResult.consumed;
                final byte[] tagBytes = tagUnpackResult.value;
                String tag = ISOUtil.byte2hex(tagBytes).toUpperCase();
                UnpackResult lengthUnpackResult = unpackLength(b, consumed);
                consumed = consumed + lengthUnpackResult.consumed;
                int length = ISOUtil.byte2int(lengthUnpackResult.value);
                final ISOComponent tlvSubFieldData;
                byte[] value = new byte[length];
                if (length > 0) {
                    System.arraycopy(b, consumed, value, 0, value.length);
                }
                int uninterpretLength = getUninterpretLength(length, valueInterpreter);
                byte[] rawValueBytes = valueInterpreter.uninterpret(value, 0, uninterpretLength);
                tlvSubFieldData = unpackValue(tag, rawValueBytes, subFieldNumber, length);
                consumed = consumed + length;
                ISOTaggedField tlv = new ISOTaggedField(tag, tlvSubFieldData);
                m.set(tlv);
                subFieldNumber++;
            }
        }
        if (b.length != consumed) {
            evt.addMessage("WARNING: unpack len=" + b.length + " consumed=" + consumed);
        }
        return consumed;
    } catch (ISOException e) {
        evt.addMessage(e);
        throw e;
    } catch (Exception e) {
        evt.addMessage(e);
        throw new ISOException(e);
    } finally {
        Logger.log(evt);
    }
}
Also used : ISOComponent(org.jpos.iso.ISOComponent) ISOException(org.jpos.iso.ISOException) LogEvent(org.jpos.util.LogEvent) ISOTaggedField(org.jpos.tlv.ISOTaggedField) ISOFieldPackager(org.jpos.iso.ISOFieldPackager) ConfigurationException(org.jpos.core.ConfigurationException) UnknownTagNumberException(org.jpos.emv.UnknownTagNumberException) ISOException(org.jpos.iso.ISOException) IOException(java.io.IOException)

Example 2 with ISOFieldPackager

use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.

the class Base1SubFieldPackagerTest method testGetFirstField.

@Test
public void testGetFirstField() throws Throwable {
    Base1SubFieldPackager base1SubFieldPackager = new Base1SubFieldPackager();
    ISOFieldPackager[] fld = new ISOFieldPackager[2];
    fld[0] = new X92_BITMAP(100, "testBase1SubFieldPackagerDescription");
    base1SubFieldPackager.setFieldPackager(fld);
    int result = base1SubFieldPackager.getFirstField();
    assertEquals("result", 1, result);
}
Also used : ISOFieldPackager(org.jpos.iso.ISOFieldPackager) X92_BITMAP(org.jpos.iso.X92_BITMAP) Test(org.junit.Test)

Example 3 with ISOFieldPackager

use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.

the class CTCSubElementPackagerTest method testUnpackThrowsISOException.

@Test
public void testUnpackThrowsISOException() throws Throwable {
    CTCSubElementPackager cTCSubElementPackager = new CTCSubElementPackager();
    ISOFieldPackager[] fld = new ISOFieldPackager[2];
    cTCSubElementPackager.setFieldPackager(fld);
    cTCSubElementPackager.setLogger(new Logger(), "testCTCSubElementPackagerRealm");
    cTCSubElementPackager.setFieldPackager(0, new IFE_CHAR());
    byte[] b = new byte[2];
    try {
        cTCSubElementPackager.unpack(new ISOVField(new ISOField(100, "testCTCSubElementPackagerv"), null), b);
        fail("Expected ISOException to be thrown");
    } catch (ISOException ex) {
        assertEquals("ex.getMessage()", "Can't add to Leaf", ex.getMessage());
        assertNull("ex.getNested()", ex.getNested());
    }
}
Also used : ISOField(org.jpos.iso.ISOField) ISOException(org.jpos.iso.ISOException) ISOVField(org.jpos.iso.ISOVField) ISOFieldPackager(org.jpos.iso.ISOFieldPackager) Logger(org.jpos.util.Logger) IFE_CHAR(org.jpos.iso.IFE_CHAR) Test(org.junit.Test)

Example 4 with ISOFieldPackager

use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.

the class CTCSubElementPackagerTest method testPackThrowsISOException.

@Test
public void testPackThrowsISOException() throws Throwable {
    CTCSubElementPackager cTCSubElementPackager = new CTCSubElementPackager();
    ISOFieldPackager[] fld = new ISOFieldPackager[2];
    cTCSubElementPackager.setFieldPackager(fld);
    try {
        cTCSubElementPackager.pack(new ISOMsg("testCTCSubElementPackagerMti"));
        fail("Expected ISOException to be thrown");
    } catch (ISOException ex) {
        assertEquals("ex.getNested().getClass()", ClassCastException.class, ex.getNested().getClass());
    }
}
Also used : ISOException(org.jpos.iso.ISOException) ISOMsg(org.jpos.iso.ISOMsg) ISOFieldPackager(org.jpos.iso.ISOFieldPackager) Test(org.junit.Test)

Example 5 with ISOFieldPackager

use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.

the class CTCSubElementPackagerTest method testPack1.

@Test
public void testPack1() throws Throwable {
    CTCSubElementPackager cTCSubElementPackager = new CTCSubElementPackager();
    ISOFieldPackager[] fld = new ISOFieldPackager[0];
    cTCSubElementPackager.setFieldPackager(fld);
    byte[] result = cTCSubElementPackager.pack(new ISOMsg());
    assertEquals("result.length", 0, result.length);
}
Also used : ISOMsg(org.jpos.iso.ISOMsg) ISOFieldPackager(org.jpos.iso.ISOFieldPackager) Test(org.junit.Test)

Aggregations

ISOFieldPackager (org.jpos.iso.ISOFieldPackager)28 Test (org.junit.Test)24 ISOException (org.jpos.iso.ISOException)12 ISOMsg (org.jpos.iso.ISOMsg)12 ISOField (org.jpos.iso.ISOField)5 Logger (org.jpos.util.Logger)4 IOException (java.io.IOException)3 IFA_LCHAR (org.jpos.iso.IFA_LCHAR)3 ISOBinaryField (org.jpos.iso.ISOBinaryField)3 ISOComponent (org.jpos.iso.ISOComponent)3 ISOTaggedField (org.jpos.tlv.ISOTaggedField)3 LogEvent (org.jpos.util.LogEvent)3 IFA_AMOUNT (org.jpos.iso.IFA_AMOUNT)2 IFA_LLLLCHAR (org.jpos.iso.IFA_LLLLCHAR)2 IFE_CHAR (org.jpos.iso.IFE_CHAR)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConfigurationException (org.jpos.core.ConfigurationException)1