Package com.solab.iso8583
Class MessageFactory<T extends IsoMessage>
- java.lang.Object
-
- com.solab.iso8583.MessageFactory<T>
-
public class MessageFactory<T extends IsoMessage> extends java.lang.Object
This class is used to create messages, either from scratch or from an existing String or byte buffer. It can be configured to put default values on newly created messages, and also to know what to expect when reading messages from an InputStream.The factory can be configured to know what values to set for newly created messages, both from a template (useful for fields that must be set with the same value for EVERY message created) and individually (for trace [field 11] and message date [field 7]).
It can also be configured to know what fields to expect in incoming messages (all possible values must be stated, indicating the date type for each). This way the messages can be parsed from a byte buffer.
-
-
Field Summary
Fields Modifier and Type Field Description protected Logger
log
protected java.util.Map<java.lang.Integer,java.util.Map<java.lang.Integer,FieldParseInfo>>
parseMap
Stores the information needed to parse messages sorted by type.protected java.util.Map<java.lang.Integer,java.util.List<java.lang.Integer>>
parseOrder
Stores the field numbers to be parsed, in order of appearance.
-
Constructor Summary
Constructors Constructor Description MessageFactory()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addMessageTemplate(T templ)
Adds a message template to the factory.protected T
createIsoMessage(java.lang.String header)
Creates a Iso message, override this method in the subclass to provide your own implementations of IsoMessage.protected T
createIsoMessageWithBinaryHeader(byte[] binHeader)
Creates a Iso message with the specified binary ISO header.T
createResponse(T request)
Creates a message to respond to a request.void
freeze()
Invoke this method in case you want to freeze the configuration, making message and parsing templates, as well as iso headers and custom fields, immutable.boolean
getAssignDate()
Returns true if the factory is assigning the current date to newly created messages (field 7).byte[]
getBinaryIsoHeader(int type)
Returns the binary ISO header used for the specified type.java.lang.String
getCharacterEncoding()
Returns the encoding used to parse ALPHA, LLVAR and LLLVAR fields.<F> CustomField<F>
getCustomField(int index)
Returns a custom field encoder/decoder for the specified field number, if one is available.<F> CustomField<F>
getCustomField(java.lang.Integer index)
Returns a custom field encoder/decoder for the specified field number, if one is available.int
getEtx()
boolean
getIgnoreLastMissingField()
This flag indicates if the MessageFactory throws an exception if the last field of a message is not really present even though it's specified in the bitmap.java.lang.String
getIsoHeader(int type)
Returns the ISO header used for the specified type.T
getMessageTemplate(int type)
Returns the template for the specified message type.TraceNumberGenerator
getTraceNumberGenerator()
Returns the generator used to assign trace numbers to new messages.boolean
getUseBinaryMessages()
Deprecated.Check the new flags binaryHeader and binaryFields instead.boolean
isBinaryFields()
fields portion of the message is written/parsed in binary, default is falseboolean
isBinaryHeader()
header portion of the message is written/parsed in binary, default is falseboolean
isForceSecondaryBitmap()
boolean
isForceStringEncoding()
boolean
isUseBinaryBitmap()
Returns true if the factory is set to create and parse bitmaps in binary format when the messages are encoded as text.boolean
isVariableLengthFieldsInHex()
T
newMessage(int type)
Creates a new message of the specified type, with optional trace and date values as well as any other values specified in a message template.T
parseMessage(byte[] buf, int isoHeaderLength)
Convenience for parseMessage(buf, isoHeaderLength, false)T
parseMessage(byte[] buf, int isoHeaderLength, boolean binaryIsoHeader)
Creates a new message instance from the buffer, which must contain a valid ISO8583 message.void
removeMessageTemplate(int type)
Removes the message template for the specified type.void
setAssignDate(boolean flag)
Sets whether the factory should set the current date on newly created messages, in field 7.void
setBinaryFields(boolean flag)
fields portion of the message is written/parsed in binary, default is falsevoid
setBinaryHeader(boolean flag)
header portion of the message is written/parsed in binary, default is falsevoid
setBinaryIsoHeader(int type, byte[] value)
Sets the ISO header for a specific message type, in binary format.void
setCharacterEncoding(java.lang.String value)
Sets the character encoding used for parsing ALPHA, LLVAR and LLLVAR fields.void
setConfigPath(java.lang.String path)
Tells the receiver to read the configuration at the specified path.void
setCustomField(int index, CustomField<?> value)
Sets the CustomField encoder for the specified field number.void
setCustomFields(java.util.Map<java.lang.Integer,CustomField> value)
Specifies a map for custom field encoder/decoders.void
setEtx(int value)
Sets the ETX character to be sent at the end of the message.void
setForceSecondaryBitmap(boolean flag)
Sets or clears the flag to pass to new messages, to include a secondary bitmap even if it's not needed.void
setForceStringEncoding(boolean flag)
This flag gets passed on to newly created messages and also sets this value for all field parsers in parsing guides.void
setIgnoreLastMissingField(boolean flag)
Setting this property to true avoids getting a ParseException when parsing messages that don't have the last field specified in the bitmap.void
setIsoHeader(int type, java.lang.String value)
Sets the ISO header for a specific message type.void
setIsoHeaders(java.util.Map<java.lang.Integer,java.lang.String> value)
Sets the ISO header to be used in each message type.void
setParseMap(int type, java.util.Map<java.lang.Integer,FieldParseInfo> map)
Sets a map with the fields that are to be expected when parsing a certain type of message.void
setTimezoneForParseGuide(int messageType, int field, java.util.TimeZone tz)
Sets the timezone for the specified FieldParseInfo, if it's needed for parsing dates.void
setTraceNumberGenerator(TraceNumberGenerator value)
Sets the generator that this factory will get new trace numbers from.void
setUseBinaryBitmap(boolean flag)
Tells the factory to create messages that encode their bitmaps in binary format even when they're encoded as text.void
setUseBinaryMessages(boolean flag)
Tells the receiver to create and parse binary messages if the flag is true.void
setVariableLengthFieldsInHex(boolean flag)
This flag gets passed on to newly created messages and also sets this value for all field parsers in parsing guides.
-
-
-
Field Detail
-
log
protected final Logger log
-
parseMap
protected java.util.Map<java.lang.Integer,java.util.Map<java.lang.Integer,FieldParseInfo>> parseMap
Stores the information needed to parse messages sorted by type.
-
parseOrder
protected java.util.Map<java.lang.Integer,java.util.List<java.lang.Integer>> parseOrder
Stores the field numbers to be parsed, in order of appearance.
-
-
Method Detail
-
setForceStringEncoding
public void setForceStringEncoding(boolean flag)
This flag gets passed on to newly created messages and also sets this value for all field parsers in parsing guides.
-
isForceStringEncoding
public boolean isForceStringEncoding()
-
setVariableLengthFieldsInHex
public void setVariableLengthFieldsInHex(boolean flag)
This flag gets passed on to newly created messages and also sets this value for all field parsers in parsing guides.
-
isVariableLengthFieldsInHex
public boolean isVariableLengthFieldsInHex()
-
setUseBinaryBitmap
public void setUseBinaryBitmap(boolean flag)
Tells the factory to create messages that encode their bitmaps in binary format even when they're encoded as text. Has no effect on binary messages.
-
isUseBinaryBitmap
public boolean isUseBinaryBitmap()
Returns true if the factory is set to create and parse bitmaps in binary format when the messages are encoded as text.
-
setCharacterEncoding
public void setCharacterEncoding(java.lang.String value)
Sets the character encoding used for parsing ALPHA, LLVAR and LLLVAR fields.
-
getCharacterEncoding
public java.lang.String getCharacterEncoding()
Returns the encoding used to parse ALPHA, LLVAR and LLLVAR fields. The default is the file.encoding system property.
-
setForceSecondaryBitmap
public void setForceSecondaryBitmap(boolean flag)
Sets or clears the flag to pass to new messages, to include a secondary bitmap even if it's not needed.
-
isForceSecondaryBitmap
public boolean isForceSecondaryBitmap()
-
setIgnoreLastMissingField
public void setIgnoreLastMissingField(boolean flag)
Setting this property to true avoids getting a ParseException when parsing messages that don't have the last field specified in the bitmap. This is common with certain providers where field 128 is specified in the bitmap but not actually included in the messages. Default is false, which has been the behavior in previous versions when this option didn't exist.
-
getIgnoreLastMissingField
public boolean getIgnoreLastMissingField()
This flag indicates if the MessageFactory throws an exception if the last field of a message is not really present even though it's specified in the bitmap. Default is false which means an exception is thrown.
-
setCustomFields
public void setCustomFields(java.util.Map<java.lang.Integer,CustomField> value)
Specifies a map for custom field encoder/decoders. The keys are the field numbers.
-
setCustomField
public void setCustomField(int index, CustomField<?> value)
Sets the CustomField encoder for the specified field number.
-
getCustomField
public <F> CustomField<F> getCustomField(int index)
Returns a custom field encoder/decoder for the specified field number, if one is available.
-
getCustomField
public <F> CustomField<F> getCustomField(java.lang.Integer index)
Returns a custom field encoder/decoder for the specified field number, if one is available.
-
setConfigPath
public void setConfigPath(java.lang.String path) throws java.io.IOException
Tells the receiver to read the configuration at the specified path. This just calls ConfigParser.configureFromClasspathConfig() with itself and the specified path at arguments, but is really convenient in case the MessageFactory is being configured from within, say, Spring.- Throws:
java.io.IOException
-
setUseBinaryMessages
public void setUseBinaryMessages(boolean flag)
Tells the receiver to create and parse binary messages if the flag is true. Default is false, that is, create and parse ASCII messages. Sets both binaryHeader and fields to the flag.
-
getUseBinaryMessages
@Deprecated public boolean getUseBinaryMessages()
Deprecated.Check the new flags binaryHeader and binaryFields instead.Returns true is the factory is set to create and parse binary messages, false if it uses ASCII messages. Default is false. True if both binaryHeader & binaryFields are set to true
-
setBinaryHeader
public void setBinaryHeader(boolean flag)
header portion of the message is written/parsed in binary, default is false
-
isBinaryHeader
public boolean isBinaryHeader()
header portion of the message is written/parsed in binary, default is false
-
setBinaryFields
public void setBinaryFields(boolean flag)
fields portion of the message is written/parsed in binary, default is false
-
isBinaryFields
public boolean isBinaryFields()
fields portion of the message is written/parsed in binary, default is false
-
setEtx
public void setEtx(int value)
Sets the ETX character to be sent at the end of the message. This is optional and the default is -1, which means nothing should be sent as terminator.- Parameters:
value
- The ASCII value of the ETX character or -1 to indicate no terminator should be used.
-
getEtx
public int getEtx()
-
newMessage
public T newMessage(int type)
Creates a new message of the specified type, with optional trace and date values as well as any other values specified in a message template. If the factory is set to use binary messages, then the returned message will be written using binary coding.- Parameters:
type
- The message type, for example 0x200, 0x400, etc.
-
createResponse
public T createResponse(T request)
Creates a message to respond to a request. Increments the message type by 16, sets all fields from the template if there is one, and copies all values from the request, overwriting fields from the template if they overlap.- Parameters:
request
- An ISO8583 message with a request type (ending in 00).
-
setTimezoneForParseGuide
public void setTimezoneForParseGuide(int messageType, int field, java.util.TimeZone tz)
Sets the timezone for the specified FieldParseInfo, if it's needed for parsing dates.
-
parseMessage
public T parseMessage(byte[] buf, int isoHeaderLength) throws java.text.ParseException, java.io.UnsupportedEncodingException
Convenience for parseMessage(buf, isoHeaderLength, false)- Throws:
java.text.ParseException
java.io.UnsupportedEncodingException
-
parseMessage
public T parseMessage(byte[] buf, int isoHeaderLength, boolean binaryIsoHeader) throws java.text.ParseException, java.io.UnsupportedEncodingException
Creates a new message instance from the buffer, which must contain a valid ISO8583 message. If the factory is set to use binary messages then it will try to parse a binary message.- Parameters:
buf
- The byte buffer containing the message. Must not include the length header.isoHeaderLength
- The expected length of the ISO header, after which the message type and the rest of the message must come.- Throws:
java.text.ParseException
java.io.UnsupportedEncodingException
-
createIsoMessage
protected T createIsoMessage(java.lang.String header)
Creates a Iso message, override this method in the subclass to provide your own implementations of IsoMessage.- Parameters:
header
- The optional ISO header that goes before the message type- Returns:
- IsoMessage
-
createIsoMessageWithBinaryHeader
protected T createIsoMessageWithBinaryHeader(byte[] binHeader)
Creates a Iso message with the specified binary ISO header. Override this method in the subclass to provide your own implementations of IsoMessage.- Parameters:
binHeader
- The optional ISO header that goes before the message type- Returns:
- IsoMessage
-
setAssignDate
public void setAssignDate(boolean flag)
Sets whether the factory should set the current date on newly created messages, in field 7. Default is false.
-
getAssignDate
public boolean getAssignDate()
Returns true if the factory is assigning the current date to newly created messages (field 7). Default is false.
-
setTraceNumberGenerator
public void setTraceNumberGenerator(TraceNumberGenerator value)
Sets the generator that this factory will get new trace numbers from. There is no default generator.
-
getTraceNumberGenerator
public TraceNumberGenerator getTraceNumberGenerator()
Returns the generator used to assign trace numbers to new messages.
-
setIsoHeaders
public void setIsoHeaders(java.util.Map<java.lang.Integer,java.lang.String> value)
Sets the ISO header to be used in each message type.- Parameters:
value
- A map where the keys are the message types and the values are the ISO headers.
-
setIsoHeader
public void setIsoHeader(int type, java.lang.String value)
Sets the ISO header for a specific message type.- Parameters:
type
- The message type, for example 0x200.value
- The ISO header, or NULL to remove any headers for this message type.
-
getIsoHeader
public java.lang.String getIsoHeader(int type)
Returns the ISO header used for the specified type.
-
setBinaryIsoHeader
public void setBinaryIsoHeader(int type, byte[] value)
Sets the ISO header for a specific message type, in binary format.- Parameters:
type
- The message type, for example 0x200.value
- The ISO header, or NULL to remove any headers for this message type.
-
getBinaryIsoHeader
public byte[] getBinaryIsoHeader(int type)
Returns the binary ISO header used for the specified type.
-
addMessageTemplate
public void addMessageTemplate(T templ)
Adds a message template to the factory. If there was a template for the same message type as the new one, it is overwritten.
-
removeMessageTemplate
public void removeMessageTemplate(int type)
Removes the message template for the specified type.
-
getMessageTemplate
public T getMessageTemplate(int type)
Returns the template for the specified message type. This allows templates to be modified programmatically.
-
freeze
public void freeze()
Invoke this method in case you want to freeze the configuration, making message and parsing templates, as well as iso headers and custom fields, immutable.
-
setParseMap
public void setParseMap(int type, java.util.Map<java.lang.Integer,FieldParseInfo> map)
Sets a map with the fields that are to be expected when parsing a certain type of message.- Parameters:
type
- The message type.map
- A map of FieldParseInfo instances, each of which define what type and length of field to expect. The keys will be the field numbers.
-
-