Package org.jpos.iso

Examples of org.jpos.iso.ISOException


    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)
                return 0; // nothing to do
            if (logger != null) // save a few CPU cycle if no logger available
                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);
        }
    }
View Full Code Here


    public byte[] pack(ISOComponent c) throws ISOException {
        int len;
        String s = (String) c.getValue();

        if ((len = s.length()) > getLength()) {
            throw new ISOException(
                    "Invalid length " + len + " packing IF_FSTCHAR field "
                            + c.getKey() + " max length=" + getLength()
            );
        }
View Full Code Here

     * @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");
        }
    }
View Full Code Here

    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");
        }
    }
View Full Code Here

    public TLVDataFormat getFormat(Integer tagNumber) throws ISOException {
        EMVTagType tagType;
        try {
            tagType = getTagType(tagNumber);
        } catch (UnknownTagNumberException e) {
            throw new ISOException(e);
        }
        final TLVDataFormat dataFormat = tagType.getFormat();
        return dataFormat;
    }
View Full Code Here

    @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)
                return 0; // nothing to do
            if (logger != null// save a few CPU cycle if no logger available
                evt.addMessage(ISOUtil.hexString(b));
            int consumed = 0;
            int subFieldId = 0;
            while (fld.length > subFieldId) {
                if (fld[subFieldId] instanceof TaggedFieldPackager) {
                    break;
                } else if (fld[subFieldId] != null) {
                    ISOComponent subField = fld[subFieldId].createComponent(subFieldId);
                    consumed += fld[subFieldId].unpack(subField, b, consumed);
                    m.set(subField);
                }
                subFieldId++;
            }
            if (subFieldId == 0 && !((fld[subFieldId] instanceof TaggedFieldPackager))) {
                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);
                }
                //Numeric field numbering is helpful in accessing them - e.g.: using m.getComponent("path")
                ISOTaggedField taggedField = (ISOTaggedField) fieldPackager.createComponent(numericTag ? Integer.parseInt(tag) : subFieldId);
                consumed += fieldPackager.unpack(taggedField, b, consumed);
                //ISOTaggedField taggedField = new ISOTaggedField(tag, subField);
                m.set(taggedField);
                subFieldId++;
            }
            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);
        }
    }
View Full Code Here

                        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 (numericTag) {
                        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);
                    } else if (!tagsStarted) {
                        if (fld.length > (Integer) c.getKey()) {
                            b = fld[(Integer) c.getKey()].pack(c);
                        } else {
                            throw new ISOException("Non ISOTagField without packager definition. Cannot pack as tag is non-numeric");
                        }
                        len += b.length;
                        l.add(b);
                    } else {
                        evt.addMessage("error packing sub-field " + c.getKey() + ". Sub-field should be of type ISOTaggedField when tag is non-numeric");
                        evt.addMessage(c);
                        throw new ISOException("error packing sub-field " + c.getKey() + ". Sub-field should be of type ISOTaggedField when tag is non-numeric");
                    }
                }
                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 (logger != null// save a few CPU cycle if no logger available
                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);
        }
    }
View Full Code Here

            ISOMsg m = (ISOMsg) obj;
            if ("LAST".equals(sendMethod)) {
                try {
                    ISOChannel c = server.getLastConnectedISOChannel();
                    if (c == null) {
                        throw new ISOException("Server has no active connections");
                    }
                    if (!c.isConnected()) {
                        throw new ISOException("Client disconnected");
                    }
                    c.send(m);
                }
                catch (Exception e) {
                    getLog().warn("notify", e);
                }
            }
            else if ("ALL".equals(sendMethod)) {
                String channelNames = getISOChannelNames();
                if (channelNames != null) {
                    StringTokenizer tok = new StringTokenizer(channelNames, " ");
                    while (tok.hasMoreTokens()) {
                        try {
                            ISOChannel c = server.getISOChannel(tok.nextToken());
                            if (c == null) {
                                throw new ISOException("Server has no active connections");
                            }
                            if (!c.isConnected()) {
                                throw new ISOException("Client disconnected");
                            }
                            c.send(m);
                        } catch (Exception e) {
                            getLog().warn("notify", e);
                        }
View Full Code Here

                }
            }
            return sb.toString().getBytes();
        }
        catch ( Exception ex ) {
            throw new ISOException ( this.getRealm() + ": " +  ex.getMessage(), ex );
        }
    }
View Full Code Here

                }
            }
            return sb.toString().getBytes();
        }
        catch ( Exception ex ) {
            throw new ISOException ( this.getRealm() + ":" + ex.getMessage(), ex );
        }
    }
View Full Code Here

TOP

Related Classes of org.jpos.iso.ISOException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.