| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.response; |
| 2 | |
| 3 | import java.util.LinkedList; |
| 4 | |
| 5 | import com.fasterxml.jackson.annotation.*; |
| 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | import com.fasterxml.jackson.databind.JsonNode; |
| 8 | import com.fasterxml.jackson.databind.node.*; |
| 9 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 10 | /** |
| 11 | * A message for Notifications. |
| 12 | * |
| 13 | * <p> |
| 14 | * <blockquote><pre> |
| 15 | * Message m = new Message(); |
| 16 | * m.setCode(614); |
| 17 | * m.setMessage("This is a new message"); |
| 18 | * m.addParameter("MyClass"); |
| 19 | * </pre></blockquote> |
| 20 | * |
| 21 | * @author Nils Diewald |
| 22 | * @see de.ids_mannheim.korap.response.Messages |
| 23 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 24 | public class Message implements Cloneable { |
| 25 | // Mapper for JSON serialization |
| 26 | ObjectMapper mapper = new ObjectMapper(); |
| 27 | |
| 28 | private String msg; |
| 29 | private int code = 0; |
| 30 | private LinkedList<String> parameters; |
| 31 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 32 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Construct a new message object. |
| 35 | * |
| 36 | * @param code Code number representing the message code |
| 37 | * @param msg String representation of the message |
| 38 | * @return The new message object |
| 39 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 40 | public Message (int code, String msg) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 41 | this.code = code; |
| 42 | this.msg = msg; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Construct a new message object. |
| 47 | * |
| 48 | * @return The new empty message object |
| 49 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 50 | public Message () {}; |
| 51 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 52 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 53 | /** |
| 54 | * Return the string representation of the message. |
| 55 | * |
| 56 | * @return String representation of the message |
| 57 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 58 | @JsonIgnore |
| 59 | public String getMessage () { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 60 | return this.msg; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Set the string representation of the message. |
| 66 | * |
| 67 | * @param msg String representation of the message |
| 68 | * @return Message object for chaining |
| 69 | */ |
| 70 | @JsonIgnore |
| 71 | public Message setMessage (String msg) { |
| 72 | this.msg = msg; |
| 73 | return this; |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * Return the integer code representation of the message. |
| 79 | * |
| 80 | * @return Integer code representation of the message |
| 81 | */ |
| 82 | @JsonIgnore |
| 83 | public int getCode () { |
| 84 | return this.code; |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Set the integer representation of the message. |
| 91 | * |
| 92 | * @param code Integer code representation of the message |
| 93 | * @return Message object for chaining |
| 94 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 95 | @JsonIgnore |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 96 | public Message setCode (int code) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 97 | this.code = code; |
| 98 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Add additional string parameters to the message. |
| 104 | * |
| 105 | * @return Message object for chaining |
| 106 | */ |
| 107 | public Message addParameter (String param) { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 108 | if (this.parameters == null) |
| 109 | this.parameters = new LinkedList<String>(); |
| 110 | this.parameters.add(param); |
| 111 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 114 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 115 | /** |
| 116 | * Create a clone of the Message. |
| 117 | * |
| 118 | * @return The cloned message object |
| 119 | * @throws CloneNotSupportedException if message can't be cloned |
| 120 | */ |
| 121 | public Object clone () throws CloneNotSupportedException { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 122 | Message clone = new Message(); |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 123 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 124 | // Copy message string |
| 125 | if (this.msg != null) |
| 126 | clone.msg = this.msg; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 127 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 128 | // Copy message code |
| 129 | clone.code = this.code; |
| 130 | |
| 131 | // Copy parameters |
| 132 | if (this.parameters != null) { |
| 133 | for (String p : this.parameters) { |
| 134 | clone.addParameter(p); |
| 135 | }; |
| 136 | }; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 137 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 138 | return clone; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 141 | /** |
| 142 | * Serialize Message as a JsonNode. |
| 143 | * |
| 144 | * @return JsonNode representation of the message |
| 145 | */ |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 146 | public JsonNode toJsonNode () { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 147 | ArrayNode message = mapper.createArrayNode(); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 148 | |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 149 | if (this.code != 0) |
| 150 | message.add(this.getCode()); |
| 151 | |
| 152 | message.add(this.getMessage()); |
| 153 | if (parameters != null) |
| 154 | for (String p : parameters) |
| 155 | message.add(p); |
| 156 | return (JsonNode) message; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 159 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 160 | /** |
| Nils Diewald | 2f2b067 | 2014-11-25 20:26:22 +0000 | [diff] [blame] | 161 | * Serialize Message as a JSON string. |
| 162 | * <p> |
| 163 | * <blockquote><pre> |
| 164 | * [123, "You are not allowed to serialize these messages"] |
| 165 | * </pre></blockquote> |
| 166 | * |
| 167 | * @return String representation of the message |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 168 | */ |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 169 | public String toJsonString () { |
| Nils Diewald | 44d5fa1 | 2015-01-15 21:31:52 +0000 | [diff] [blame] | 170 | String msg = ""; |
| 171 | try { |
| 172 | return mapper.writeValueAsString(this.toJsonNode()); |
| 173 | } |
| 174 | catch (Exception e) { |
| 175 | // Bad in case the message contains quotes! |
| 176 | msg = ", \"" + e.getLocalizedMessage() + "\""; |
| 177 | }; |
| 178 | return |
| 179 | "[620, " + |
| 180 | "\"Unable to generate JSON\"" + msg + "]"; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 181 | }; |
| 182 | }; |