Search in sources :

Example 1 with ISOException

use of org.jpos.iso.ISOException 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 ISOException

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

the class IF_FSTBINARY method unpack.

/**
 * @param c      - the Component to unpack
 * @param b      - binary image
 * @param offset - starting offset within the binary image
 * @return consumed bytes
 * @throws org.jpos.iso.ISOException
 */
public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException {
    if (!(c instanceof ISOField))
        throw new ISOException(c.getClass().getName() + " is not an ISOField");
    int length = -1;
    for (int i = 0; i < getMaxPackedLength(); i++) {
        byte dataByte = b[offset + i];
        if (dataByte == terminator) {
            length = i;
            break;
        }
    }
    if (length >= 0) {
        byte[] value = new byte[length];
        System.arraycopy(b, offset, value, 0, length);
        c.setValue(value);
        return length + 1;
    } else {
        throw new ISOException("Terminating Backslash does not exist");
    }
}
Also used : ISOField(org.jpos.iso.ISOField) ISOException(org.jpos.iso.ISOException)

Example 3 with ISOException

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

the class IF_FSTCHAR method unpack.

/**
 * @param c      - the Component to unpack
 * @param b      - binary image
 * @param offset - starting offset within the binary image
 * @return consumed bytes
 * @throws org.jpos.iso.ISOException
 */
public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException {
    if (!(c instanceof ISOField))
        throw new ISOException(c.getClass().getName() + " is not an ISOField");
    int length = -1;
    for (int i = 0; i < getMaxPackedLength(); i++) {
        byte dataByte = b[offset + i];
        if ((char) dataByte == terminator) {
            length = i;
            break;
        }
    }
    if (length >= 0) {
        String value = new String(b, offset, length);
        c.setValue(value);
        return length + 1;
    } else {
        throw new ISOException("Terminating Backslash does not exist");
    }
}
Also used : ISOField(org.jpos.iso.ISOField) ISOException(org.jpos.iso.ISOException)

Example 4 with ISOException

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

the class IF_FSTCHAR method unpack.

public void unpack(ISOComponent c, InputStream in) throws IOException, ISOException {
    if (!(c instanceof ISOField))
        throw new ISOException(c.getClass().getName() + " is not an ISOField");
    boolean endFound = false;
    if (in.markSupported()) {
        in.mark(getMaxPackedLength());
    }
    ByteBuffer buf = ByteBuffer.allocate(getMaxPackedLength());
    for (int i = 0; i < getMaxPackedLength() && in.available() > 0; i++) {
        byte dataByte = (byte) in.read();
        if ((char) dataByte == terminator) {
            endFound = true;
            break;
        } else {
            buf.put(dataByte);
        }
    }
    if (endFound) {
        byte[] data = byteBufferToBytes(buf);
        String value = new String(data);
        c.setValue(value);
    } else {
        if (in.markSupported()) {
            in.reset();
        }
        throw new ISOException("Terminating Backslash does not exist");
    }
}
Also used : ISOField(org.jpos.iso.ISOField) ISOException(org.jpos.iso.ISOException) ByteBuffer(java.nio.ByteBuffer)

Example 5 with ISOException

use of org.jpos.iso.ISOException 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)

Aggregations

ISOException (org.jpos.iso.ISOException)66 Test (org.junit.Test)36 ISOMsg (org.jpos.iso.ISOMsg)29 ISOField (org.jpos.iso.ISOField)15 ISOFieldPackager (org.jpos.iso.ISOFieldPackager)12 ISOBaseValidator (org.jpos.iso.ISOBaseValidator)8 IOException (java.io.IOException)7 ISOBinaryField (org.jpos.iso.ISOBinaryField)7 TEST0100 (org.jpos.iso.validator.TEST0100)7 Map (java.util.Map)6 ConfigurationException (org.jpos.core.ConfigurationException)6 ISOComponent (org.jpos.iso.ISOComponent)6 ISOFieldValidator (org.jpos.iso.ISOFieldValidator)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 LogEvent (org.jpos.util.LogEvent)5 SAXParseException (org.xml.sax.SAXParseException)5 IVA_ALPHANUMNOBLANK (org.jpos.iso.IVA_ALPHANUMNOBLANK)4 ISOVException (org.jpos.iso.validator.ISOVException)4 ISOTaggedField (org.jpos.tlv.ISOTaggedField)4 ArrayList (java.util.ArrayList)3