Search in sources :

Example 31 with ISOComponent

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

the class ISOBaseValidatingPackagerTest method testValidate14.

@Test
public void testValidate14() throws Throwable {
    ISOBaseValidator[] msgVlds = new ISOBaseValidator[2];
    msgVlds[0] = new TEST0100(true);
    ISOBaseValidatingPackager iSOBaseValidatingPackager = new ISOBaseValidatingPackager();
    iSOBaseValidatingPackager.setMsgValidator(msgVlds);
    ISOValidator[] fvlds = new ISOValidator[5];
    fvlds[1] = new ISOFieldValidator();
    iSOBaseValidatingPackager.setFieldValidator(fvlds);
    ISOComponent m = new ISOMsg("testISOBaseValidatingPackagerMti");
    ISOMsg result = (ISOMsg) iSOBaseValidatingPackager.validate(m);
    assertSame("result", m, result);
}
Also used : ISOComponent(org.jpos.iso.ISOComponent) ISOMsg(org.jpos.iso.ISOMsg) ISOValidator(org.jpos.iso.ISOValidator) ISOBaseValidator(org.jpos.iso.ISOBaseValidator) TEST0100(org.jpos.iso.validator.TEST0100) ISOFieldValidator(org.jpos.iso.ISOFieldValidator) Test(org.junit.Test)

Example 32 with ISOComponent

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

the class TagSequenceBase method readFrom.

@Override
public synchronized void readFrom(ISOMsg isoMsg) throws ISOException {
    int maxField = isoMsg.getMaxField();
    int minField = -1;
    for (int i = 0; i <= maxField; i++) {
        ISOComponent child = isoMsg.getComponent(i);
        if (child instanceof ISOTaggedField) {
            minField = i;
            break;
        }
    }
    if (minField == -1) {
        // No TaggedFields to read
        return;
    }
    for (int i = minField; i <= maxField; i++) {
        ISOComponent child = isoMsg.getComponent(i);
        if (child != null) {
            if (child instanceof ISOTaggedField) {
                TagValue tagValue;
                ISOTaggedField taggedSubField = (ISOTaggedField) child;
                ISOComponent delegate = taggedSubField.getDelegate();
                if (delegate instanceof ISOMsg) {
                    Map subChildren = delegate.getChildren();
                    boolean allTaggedValue = true;
                    for (Object subChild : subChildren.values()) {
                        if (!(subChild instanceof ISOTaggedField)) {
                            allTaggedValue = false;
                            break;
                        }
                    }
                    if (allTaggedValue) {
                        tagValue = createTagValueSequence(taggedSubField.getTag());
                        ((TagSequence) tagValue).readFrom((ISOMsg) delegate);
                    } else {
                        tagValue = new ISOMsgTagValue(getTag(), isoMsg);
                    }
                } else if (delegate instanceof ISOBinaryField) {
                    tagValue = createBinaryTagValuePair(taggedSubField.getTag(), taggedSubField.getBytes());
                } else if (delegate instanceof ISOField) {
                    tagValue = createLiteralTagValuePair(taggedSubField.getTag(), taggedSubField.getValue().toString());
                } else {
                    throw new ISOException("Unknown ISOComponent subclass in ISOTaggedField: " + delegate.getClass());
                }
                this.add(tagValue);
            } else {
                throw new ISOException("Children after first ISOTaggedField should be instance of ISOTaggedField." + " Field " + i + " is not an ISOTaggedField");
            }
        }
    }
}
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) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 33 with ISOComponent

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

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

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