Search in sources :

Example 1 with ISOComponent

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

the class TagSequenceBase method writeTo.

@Override
public synchronized void writeTo(ISOMsg isoMsg) throws ISOException {
    int maxField = isoMsg.getMaxField();
    List<TagValue> tagValueList = getOrderedList();
    int fieldNumber = 0;
    for (TagValue tagValue : tagValueList) {
        Object value = tagValue.getValue();
        if (value != null) {
            ISOComponent subField;
            if (value instanceof byte[]) {
                subField = new ISOBinaryField(fieldNumber + maxField + 1, (byte[]) value);
            } else if (value instanceof String) {
                subField = new ISOField(fieldNumber + maxField + 1, (String) value);
            } else if (value instanceof TagSequence) {
                TagSequence subSequence = (TagSequence) tagValue;
                subField = new ISOMsg(fieldNumber + maxField + 1);
                subSequence.writeTo((ISOMsg) subField);
            } else if (value instanceof ISOMsg) {
                ISOMsgTagValue subSequence = (ISOMsgTagValue) tagValue;
                subField = subSequence.getValue();
                subField.setFieldNumber(fieldNumber + maxField + 1);
            } else {
                throw new ISOException("Unknown TagValue subclass: " + tagValue.getClass());
            }
            isoMsg.set(new ISOTaggedField(tagValue.getTag(), subField));
        }
        fieldNumber++;
    }
}
Also used : ISOField(org.jpos.iso.ISOField) ISOComponent(org.jpos.iso.ISOComponent) ISOBinaryField(org.jpos.iso.ISOBinaryField) ISOException(org.jpos.iso.ISOException) ISOMsg(org.jpos.iso.ISOMsg)

Example 2 with ISOComponent

use of org.jpos.iso.ISOComponent 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 3 with ISOComponent

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

the class BERTLVPackager method unpackValue.

private ISOComponent unpackValue(String tagNameHex, final byte[] tlvData, int subFieldNumber, int dataLength) throws ISOException, UnknownTagNumberException {
    final int tagNumber = Integer.parseInt(tagNameHex, 16);
    final TLVDataFormat dataFormat = getTagFormatMapper().getFormat(tagNumber);
    ISOComponent value;
    String unpackedValue;
    int uninterpretLength;
    switch(dataFormat) {
        case COMPRESSED_NUMERIC:
            uninterpretLength = getUninterpretLength(dataLength, bcdInterpreterRightPaddedF);
            unpackedValue = bcdInterpreterRightPaddedF.uninterpret(tlvData, 0, uninterpretLength);
            if (unpackedValue.length() > 1 && unpackedValue.charAt(unpackedValue.length() - 1) == 'F') {
                unpackedValue = unpackedValue.substring(0, unpackedValue.length() - 1);
            }
            value = new ISOField(subFieldNumber, unpackedValue);
            break;
        case PACKED_NUMERIC:
        case PACKED_NUMERIC_DATE_YYMMDD:
        case PACKED_NUMERIC_TIME_HHMMSS:
            uninterpretLength = getUninterpretLength(dataLength, bcdInterpreterLeftPaddedZero);
            unpackedValue = bcdInterpreterLeftPaddedZero.uninterpret(tlvData, 0, uninterpretLength);
            if (unpackedValue.length() > 1 && unpackedValue.charAt(0) == '0') {
                unpackedValue = unpackedValue.substring(1);
            }
            value = new ISOField(subFieldNumber, unpackedValue);
            break;
        case ASCII_NUMERIC:
        case ASCII_ALPHA:
        case ASCII_ALPHA_NUMERIC:
        case ASCII_ALPHA_NUMERIC_SPACE:
        case ASCII_ALPHA_NUMERIC_SPECIAL:
            uninterpretLength = getUninterpretLength(dataLength, asciiInterpreter);
            unpackedValue = asciiInterpreter.uninterpret(tlvData, 0, uninterpretLength);
            value = new ISOField(subFieldNumber, unpackedValue);
            break;
        case BINARY:
        case PROPRIETARY:
            value = new ISOBinaryField(subFieldNumber, tlvData);
            break;
        case CONSTRUCTED:
            value = new ISOMsg(subFieldNumber);
            unpack(value, tlvData, true);
            break;
        default:
            throw new IllegalArgumentException("Unknown TLVDataFormat: " + dataFormat);
    }
    return value;
}
Also used : ISOField(org.jpos.iso.ISOField) ISOComponent(org.jpos.iso.ISOComponent) ISOBinaryField(org.jpos.iso.ISOBinaryField) ISOMsg(org.jpos.iso.ISOMsg) TLVDataFormat(org.jpos.tlv.TLVDataFormat)

Example 4 with ISOComponent

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

the class VISA1PackagerTest method testUnpack.

@Test
public void testUnpack() throws Throwable {
    int[] sequence = new int[2];
    ISOComponent m = new ISOMsg(100);
    VISA1Packager vISA1Packager = new VISA1Packager(sequence, 100, "testVISA1PackagerBadResultCode", "");
    byte[] b = new byte[1];
    int result = vISA1Packager.unpack(m, b);
    assertEquals("(ISOMsg) m.getMaxField()", 100, m.getMaxField());
    assertEquals("result", 1, result);
    assertSame("vISA1Packager.filter", vISA1Packager, vISA1Packager.filter);
}
Also used : ISOComponent(org.jpos.iso.ISOComponent) ISOMsg(org.jpos.iso.ISOMsg) Test(org.junit.Test)

Example 5 with ISOComponent

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

the class XMLPackagerTest method testGetFieldDescription.

@Test
public void testGetFieldDescription() throws Throwable {
    ISOComponent m = new ISOMsg(100);
    String result = xMLPackager.getFieldDescription(m, 100);
    assertEquals("result", "<notavailable/>", result);
}
Also used : ISOComponent(org.jpos.iso.ISOComponent) ISOMsg(org.jpos.iso.ISOMsg) String(java.lang.String) Test(org.junit.Test)

Aggregations

ISOComponent (org.jpos.iso.ISOComponent)34 ISOMsg (org.jpos.iso.ISOMsg)25 Test (org.junit.Test)23 ISOVMsg (org.jpos.iso.ISOVMsg)8 ISOException (org.jpos.iso.ISOException)6 LogEvent (org.jpos.util.LogEvent)6 Vector (java.util.Vector)5 ISOBaseValidator (org.jpos.iso.ISOBaseValidator)5 ISOField (org.jpos.iso.ISOField)5 ISOVError (org.jpos.iso.ISOVError)5 Map (java.util.Map)4 ISOBinaryField (org.jpos.iso.ISOBinaryField)4 ISOValidator (org.jpos.iso.ISOValidator)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ISOFieldPackager (org.jpos.iso.ISOFieldPackager)3 ISOFieldValidator (org.jpos.iso.ISOFieldValidator)3 ISOTaggedField (org.jpos.tlv.ISOTaggedField)3 TreeMap (java.util.TreeMap)2 ConfigurationException (org.jpos.core.ConfigurationException)2