Search in sources :

Example 56 with ISOException

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

the class IF_FSTBINARY 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 (dataByte == terminator) {
            endFound = true;
            break;
        } else {
            buf.put(dataByte);
        }
    }
    if (endFound) {
        byte[] data = byteBufferToBytes(buf);
        c.setValue(data);
    } 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 57 with ISOException

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

the class TaggedSequencePackager method unpack.

@Override
public int unpack(ISOComponent m, byte[] b) throws ISOException {
    LogEvent evt = new LogEvent(this, "unpack");
    try {
        if (m.getComposite() != m)
            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));
        // Read any non-tlv fields present at beginning of data
        PrefixUnpackResult prefixUnpackResult = unpackPrefixes(m, b);
        int subFieldId = prefixUnpackResult.getSubFieldId();
        int consumed = prefixUnpackResult.getConsumed();
        if (subFieldId == 0) {
            subFieldId = 1;
        }
        while (consumed < b.length) {
            ISOField tagField = new ISOField(subFieldId);
            tagPackager.unpack(tagField, b, consumed);
            String tag = tagField.getValue().toString();
            ISOFieldPackager fieldPackager = (ISOFieldPackager) packagerMap.get(tag);
            if (fieldPackager == null) {
                fieldPackager = (ISOFieldPackager) packagerMap.get("default");
            }
            if (fieldPackager == null) {
                throw new ISOException("No default tag packager and no field packager configured for tag: " + tag);
            }
            int fieldNumber = subFieldId++;
            ISOTaggedField taggedField = (ISOTaggedField) fieldPackager.createComponent(fieldNumber);
            consumed += fieldPackager.unpack(taggedField, b, consumed);
            m.set(taggedField);
        }
        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 : ISOField(org.jpos.iso.ISOField) ISOException(org.jpos.iso.ISOException) LogEvent(org.jpos.util.LogEvent) ISOTaggedField(org.jpos.tlv.ISOTaggedField) ISOFieldPackager(org.jpos.iso.ISOFieldPackager) ISOException(org.jpos.iso.ISOException) IOException(java.io.IOException)

Example 58 with ISOException

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

the class TaggedSequencePackager method pack.

/**
 * Pack the subfield into a byte array
 */
public byte[] pack(ISOComponent m) throws ISOException {
    LogEvent evt = new LogEvent(this, "pack");
    try {
        ISOComponent c;
        List<byte[]> l = new ArrayList<byte[]>();
        Map fields = m.getChildren();
        fields.remove(new Integer(-1));
        int len = 0;
        boolean tagsStarted = false;
        Iterator iterator = fields.values().iterator();
        if (m instanceof OffsetIndexedComposite) {
            int offset = ((OffsetIndexedComposite) m).getOffset();
            for (int i = 0; i < offset && iterator.hasNext(); i++) {
                iterator.next();
            }
        }
        while (iterator.hasNext() && len < this.length) {
            Object obj = iterator.next();
            c = (ISOComponent) obj;
            byte[] b;
            if (c.getValue() != null) {
                if (c instanceof ISOTaggedField) {
                    tagsStarted = true;
                    String tag = ((ISOTaggedField) c).getTag();
                    if (tag == null) {
                        evt.addMessage("error packing subfield " + c.getKey());
                        evt.addMessage(c);
                        throw new ISOException("Tag should not be null");
                    } else {
                        ISOFieldPackager fieldPackager = (ISOFieldPackager) packagerMap.get(tag);
                        if (fieldPackager == null) {
                            fieldPackager = (ISOFieldPackager) packagerMap.get("default");
                        }
                        if (fieldPackager == null) {
                            throw new ISOException("No default tag packager and no field packager configured for tag: " + tag);
                        }
                        b = fieldPackager.pack(c);
                        if (len + b.length > this.length) {
                            break;
                        }
                        len += b.length;
                        l.add(b);
                    }
                } else if (!tagsStarted && fld.length > (Integer) c.getKey() && fld[(Integer) c.getKey()] != null) {
                    b = fld[(Integer) c.getKey()].pack(c);
                    len += b.length;
                    l.add(b);
                } else {
                    int tagNumber = (Integer) c.getKey();
                    String tag = ISOUtil.padleft(String.valueOf(tagNumber), this.tag.length(), '0');
                    ISOTaggedField isoTaggedField = new ISOTaggedField(tag, c);
                    if (fld.length > tagNumber) {
                        b = fld[(Integer) c.getKey()].pack(isoTaggedField);
                    } else {
                        ISOFieldPackager fieldPackager = (ISOFieldPackager) packagerMap.get(tag);
                        if (fieldPackager == null) {
                            fieldPackager = (ISOFieldPackager) packagerMap.get("default");
                        }
                        if (fieldPackager == null) {
                            throw new ISOException("No default tag packager and no field packager configured for tag: " + tag);
                        }
                        b = fieldPackager.pack(isoTaggedField);
                        if (len + b.length > this.length) {
                            break;
                        }
                    }
                    len += b.length;
                    l.add(b);
                }
            }
            if (m instanceof OffsetIndexedComposite) {
                ((OffsetIndexedComposite) m).incOffset();
            }
        }
        int k = 0;
        byte[] d = new byte[len];
        for (byte[] b : l) {
            System.arraycopy(b, 0, d, k, b.length);
            k += b.length;
        }
        if (// save a few CPU cycle if no logger available
        logger != null)
            evt.addMessage(ISOUtil.hexString(d));
        return d;
    } 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) OffsetIndexedComposite(org.jpos.tlv.OffsetIndexedComposite) LogEvent(org.jpos.util.LogEvent) ArrayList(java.util.ArrayList) ISOFieldPackager(org.jpos.iso.ISOFieldPackager) ISOException(org.jpos.iso.ISOException) IOException(java.io.IOException) ISOException(org.jpos.iso.ISOException) Iterator(java.util.Iterator) ISOTaggedField(org.jpos.tlv.ISOTaggedField) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 59 with ISOException

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

the class BERTLVPackager method pack.

public byte[] pack(ISOComponent m, boolean nested, int startIdx, int endIdx) throws ISOException {
    LogEvent evt = new LogEvent(this, "pack");
    try {
        ISOComponent c;
        List<byte[]> l = new ArrayList<byte[]>();
        Map fields = m.getChildren();
        int len = 0;
        for (int i = startIdx; i <= endIdx; i++) {
            c = (ISOComponent) fields.get(i);
            if (c != null) {
                try {
                    final byte[] b;
                    if (c instanceof ISOTaggedField) {
                        b = packTLV((ISOTaggedField) c);
                    } else {
                        if (c.getValue() == null) {
                            b = new byte[0];
                        } else if (!nested && (i == startIdx || i == endIdx) && this.fld.length > i && this.fld[i] != null) {
                            b = this.fld[i].pack(c);
                        } else {
                            throw new ISOException("Field: " + i + " of type: " + c.getClass() + " cannot be packed. Either the object should be of type ISOTagField" + " OR this should be the first or last sub-field and a packager" + " should be configured for the same");
                        }
                    }
                    len += b.length;
                    l.add(b);
                } catch (Exception e) {
                    evt.addMessage("error packing sub-field " + i);
                    evt.addMessage(c);
                    evt.addMessage(e);
                    throw e;
                }
            }
        }
        int k = 0;
        byte[] d = new byte[len];
        for (byte[] b : l) {
            System.arraycopy(b, 0, d, k, b.length);
            k += b.length;
        }
        if (// save a few CPU cycle if no logger available
        logger != null)
            evt.addMessage(ISOUtil.hexString(d));
        return d;
    } 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) ArrayList(java.util.ArrayList) ISOTaggedField(org.jpos.tlv.ISOTaggedField) Map(java.util.Map) ConfigurationException(org.jpos.core.ConfigurationException) UnknownTagNumberException(org.jpos.emv.UnknownTagNumberException) ISOException(org.jpos.iso.ISOException) IOException(java.io.IOException)

Example 60 with ISOException

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

the class GenericValidatingPackager method readFile.

/**
 * It define GenericValidatorContentHandler like handler.
 */
public void readFile(String filename) throws org.jpos.iso.ISOException {
    try {
        XMLReader reader = XMLReaderFactory.createXMLReader(System.getProperty("sax.parser", "org.apache.crimson.parser.XMLReaderImpl"));
        reader.setFeature("http://xml.org/sax/features/validation", true);
        GenericValidatorContentHandler handler = new GenericValidatorContentHandler();
        reader.setContentHandler(handler);
        reader.setErrorHandler(handler);
        reader.setEntityResolver(new GenericEntityResolver());
        reader.parse(filename);
    } catch (Exception e) {
        e.printStackTrace();
        throw new ISOException(e);
    }
}
Also used : ISOException(org.jpos.iso.ISOException) XMLReader(org.xml.sax.XMLReader) ISOVException(org.jpos.iso.validator.ISOVException) ConfigurationException(org.jpos.core.ConfigurationException) ISOException(org.jpos.iso.ISOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException)

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