uk.org.ury.common.protocol
Class ProtocolUtils

java.lang.Object
  extended by uk.org.ury.common.protocol.ProtocolUtils

public class ProtocolUtils
extends java.lang.Object

Utilities for converting between strings encoded in the response protocol and collections of items, as well as validating and unpicking protocol messages.

Author:
Matt Windsor

Constructor Summary
ProtocolUtils()
           
 
Method Summary
static java.util.Map<?,?> decode(java.lang.String string)
          Decode a protocol string into a key-value map.
static java.lang.String encode(java.util.Map<java.lang.String,java.lang.Object> items)
          Encode a key-value map into a protocol string.
static boolean responseIsOK(java.util.Map<?,?> response)
          Check if a response is flagged as having OK status.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProtocolUtils

public ProtocolUtils()
Method Detail

encode

public static java.lang.String encode(java.util.Map<java.lang.String,java.lang.Object> items)
Encode a key-value map into a protocol string. The map can contain strings, lists and other maps. Other types may be accepted by the underlying encoding engine, but the above types are the only ones explicitly accepted.

Parameters:
items - The key-value map of items, which should contain strings, lists and maps. The keys of any map should be protocol directives.
Returns:
A string containing the encoded representation of the map.

decode

public static java.util.Map<?,?> decode(java.lang.String string)
                                 throws DecodeFailureException
Decode a protocol string into a key-value map.

Parameters:
string - The string to decode.
Returns:
A key-value map mapping directives to strings, lists and maps.
Throws:
DecodeFailureException - if the decoding engine returns something other than a map.

responseIsOK

public static boolean responseIsOK(java.util.Map<?,?> response)
                            throws InvalidMessageException
Check if a response is flagged as having OK status.

Parameters:
response - The response message, as a key-value map (eg in decoded format).
Returns:
true if the response is flagged with OK status, false if not (eg ERROR status).
Throws:
InvalidMessageException - if the response is invalid (eg the status is missing).