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++;
}
}
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);
}
}
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;
}
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);
}
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);
}
Aggregations