aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/common/protocol/ProtocolUtils.java
blob: 37e8c75df1be8a7b1cf5d5af8ea2e1cff69b9c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * ProtocolUtils.java
 * ------------------
 * 
 * Part of the URY Common Packages
 * 
 * V0.00  2011/03/23
 * 
 * (C) 2011 URY Computing
 */

package uk.org.ury.common.protocol;

import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import uk.org.ury.common.protocol.exceptions.DecodeFailureException;
import uk.org.ury.common.protocol.exceptions.EncodeFailureException;
import uk.org.ury.common.protocol.exceptions.InvalidMessageException;

/**
 * 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
 * 
 */
public class ProtocolUtils {
    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * 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.
     * 
     * @param items
     *            The key-value map of items, which should contain strings,
     *            lists and maps. The keys of any map should be protocol
     *            directives.
     * 
     * @return A string containing the encoded representation of the map.
     * 
     * @throws EncodeFailureException
     *             if the encoding engine fails to encode the map as a string.
     */
    public static String encode(Map<String, Object> items)
	    throws EncodeFailureException {
	try {
	    return mapper.writeValueAsString(items);
	} catch (Exception e) {
	    throw new EncodeFailureException(e);
	}
    }

    /**
     * Decodes a protocol string into a key-value map.
     * 
     * @param string
     *            The string to decode.
     * 
     * @return A key-value map mapping directives to strings, lists and maps.
     * 
     * @throws DecodeFailureException
     *             if the decoding engine fails to decode the string.
     */
    public static Map<String, Object> decode(String string) throws DecodeFailureException {
	try {
	    return mapper.readValue(string, new TypeReference<Map<String, Object>>() {});
	} catch (Exception e) {
	    throw new DecodeFailureException(e);
	}
    }

    /**
     * Checks if a response is flagged as having OK status.
     * 
     * @param response
     *            The response message, as a key-value map (eg in decoded
     *            format).
     * 
     * @return 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).
     */
    public static boolean responseIsOK(Map<String, Object> response)
	    throws InvalidMessageException {
	if (response.containsKey(Directive.STATUS.toString()) == false)
	    throw new InvalidMessageException("No status line in response.");

	if ((response.get(Directive.STATUS.toString()) instanceof String) == false)
	    throw new InvalidMessageException("Status is not a string.");

	return (((String) response.get(Directive.STATUS.toString()))
		.equals(Status.OK.toString()));
    }
}