Package org.openmuc.jship.message
Class MessageUtility
- java.lang.Object
-
- org.openmuc.jship.message.MessageUtility
-
public class MessageUtility extends java.lang.Object
holds static utility methods related to message validation, de-/serialization and parsing preprocess methods separate MessageType (first byte in every message) from MessageValue
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
beginObjectIfHasNext(com.google.gson.stream.JsonReader in)
static void
checkFieldName(com.google.gson.stream.JsonReader in, java.lang.String desiredName)
static byte[]
createErrorMsg(int errorType)
constructs and formats an error messagestatic byte[]
decodeHex(java.lang.String hexString)
decode hexadecimal string to byte arraystatic CDEMsg
deserializeCde(byte[] json)
static CloseMsg
deserializeConnCloseMsg(byte[] json)
static HelloMsg
deserializeHello(byte[] json)
static PinMsg
deserializePin(byte[] json)
static ProtocolHandshakeMsg
deserializeProtH(byte[] json)
static java.lang.String
encodeHex(byte[] bytes)
encode byte array to hexadecimal string, not needed if restrict pinstatic java.lang.String
hexToPrettyFormat(java.lang.String hexString)
Inserts a whitespace after every 4 characters for better graphical presentation.static boolean
isHexDigits(java.lang.String s)
static boolean
isValidPin(java.lang.String pin)
checks if PIN is valid, removes all spaces before checkingstatic java.lang.String
parseCmiMsgToString(byte[] msg)
parse a cmi message for logging.static java.lang.String
parseShipMsgToString(byte[] msg)
parse a message for loggingstatic CDEMsg
preprocessCDEMsg(byte[] msg)
static CloseMsg
preprocessConnCloseMsg(byte[] msg)
static HelloMsg
preprocessHelloMsg(byte[] msg)
static PinMsg
preprocessPinMsg(byte[] msg)
static ProtocolHandshakeMsg
preprocessProtHMsg(byte[] msg)
If there are messages in the message queue, removes the message from the message queue and splits header from body and parses it into a ProtocolHandshake object.static boolean
readWrappedBoolean(com.google.gson.stream.JsonReader in)
static int
readWrappedInt(com.google.gson.stream.JsonReader in)
static java.lang.String
readWrappedString(com.google.gson.stream.JsonReader in)
static io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame
wrapInBinaryFrame(byte[] msg)
static void
writeBooleanToObject(com.google.gson.stream.JsonWriter out, java.lang.String property, java.lang.Boolean value)
static void
writeIntToObject(com.google.gson.stream.JsonWriter out, java.lang.String property, java.lang.Integer value)
static void
writeStringToObject(com.google.gson.stream.JsonWriter out, java.lang.String property, java.lang.String value)
-
-
-
Method Detail
-
wrapInBinaryFrame
public static io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame wrapInBinaryFrame(byte[] msg)
-
deserializeHello
public static HelloMsg deserializeHello(byte[] json)
-
preprocessHelloMsg
public static HelloMsg preprocessHelloMsg(byte[] msg)
-
deserializeProtH
public static ProtocolHandshakeMsg deserializeProtH(byte[] json)
-
preprocessProtHMsg
public static ProtocolHandshakeMsg preprocessProtHMsg(byte[] msg)
If there are messages in the message queue, removes the message from the message queue and splits header from body and parses it into a ProtocolHandshake object.- Returns:
- the parsed ProtocolHandshake object
- Throws:
java.lang.IllegalStateException
- if something went wrong
-
deserializePin
public static PinMsg deserializePin(byte[] json)
-
preprocessPinMsg
public static PinMsg preprocessPinMsg(byte[] msg)
-
deserializeCde
public static CDEMsg deserializeCde(byte[] json)
-
preprocessCDEMsg
public static CDEMsg preprocessCDEMsg(byte[] msg)
-
deserializeConnCloseMsg
public static CloseMsg deserializeConnCloseMsg(byte[] json)
-
preprocessConnCloseMsg
public static CloseMsg preprocessConnCloseMsg(byte[] msg)
-
isHexDigits
public static boolean isHexDigits(java.lang.String s)
-
encodeHex
public static java.lang.String encodeHex(byte[] bytes)
encode byte array to hexadecimal string, not needed if restrict pin- Parameters:
bytes
- byte array containing the hexadecimal values- Returns:
- the encoded hex
-
decodeHex
public static byte[] decodeHex(java.lang.String hexString)
decode hexadecimal string to byte array- Parameters:
hexString
- the hexadecimal string to decode- Returns:
- the decoded hexadecimal string
-
hexToPrettyFormat
public static java.lang.String hexToPrettyFormat(java.lang.String hexString)
Inserts a whitespace after every 4 characters for better graphical presentation. Also cuts off '0x' at the beginning of the String if it exists.- Parameters:
hexString
- the hexadecimal string to convert- Returns:
- the pretty print version of the string
-
createErrorMsg
public static byte[] createErrorMsg(int errorType)
constructs and formats an error message- Parameters:
errorType
- the error type- Returns:
- the error message as a byte array
-
isValidPin
public static boolean isValidPin(java.lang.String pin)
checks if PIN is valid, removes all spaces before checking- Parameters:
pin
- the PIN to be checked- Returns:
true
if PIN is valid,false
otherwise
-
parseShipMsgToString
public static java.lang.String parseShipMsgToString(byte[] msg)
parse a message for logging- Parameters:
msg
- the received message- Returns:
- the parsed message
-
parseCmiMsgToString
public static java.lang.String parseCmiMsgToString(byte[] msg)
parse a cmi message for logging. Requires special handling because it only contains binary data- Parameters:
msg
- the error message or CMI_STATE msg- Returns:
- the parsed message
-
writeStringToObject
public static void writeStringToObject(com.google.gson.stream.JsonWriter out, java.lang.String property, java.lang.String value) throws java.io.IOException
- Throws:
java.io.IOException
-
writeIntToObject
public static void writeIntToObject(com.google.gson.stream.JsonWriter out, java.lang.String property, java.lang.Integer value) throws java.io.IOException
- Throws:
java.io.IOException
-
writeBooleanToObject
public static void writeBooleanToObject(com.google.gson.stream.JsonWriter out, java.lang.String property, java.lang.Boolean value) throws java.io.IOException
- Throws:
java.io.IOException
-
readWrappedString
public static java.lang.String readWrappedString(com.google.gson.stream.JsonReader in) throws java.io.IOException
- Throws:
java.io.IOException
-
readWrappedInt
public static int readWrappedInt(com.google.gson.stream.JsonReader in) throws java.io.IOException
- Throws:
java.io.IOException
-
readWrappedBoolean
public static boolean readWrappedBoolean(com.google.gson.stream.JsonReader in) throws java.io.IOException
- Throws:
java.io.IOException
-
beginObjectIfHasNext
public static void beginObjectIfHasNext(com.google.gson.stream.JsonReader in) throws java.io.IOException
- Throws:
java.io.IOException
-
checkFieldName
public static void checkFieldName(com.google.gson.stream.JsonReader in, java.lang.String desiredName) throws java.io.IOException
- Throws:
java.io.IOException
-
-