1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.asterisk.manager.response;
18
19 import java.io.Serializable;
20 import java.util.Date;
21 import java.util.Map;
22
23 /***
24 * Represents a response received from the Asterisk server as the result of a
25 * previously sent ManagerAction.<br>
26 * The response can be linked with the action that caused it by looking the
27 * action id attribute that will match the action id of the corresponding
28 * action.
29 *
30 * @see net.sf.asterisk.manager.action.ManagerAction
31 * @author srt
32 * @version $Id: ManagerResponse.java,v 1.5 2005/04/24 13:39:40 srt Exp $
33 */
34 public class ManagerResponse implements Serializable
35 {
36 /***
37 * Serial version identifier
38 */
39 static final long serialVersionUID = -935845815108584292L;
40
41 private Date dateReceived;
42 private String actionId;
43 private String response;
44 private String message;
45 private String uniqueId;
46 private Map attributes;
47
48 /***
49 * Returns a Map with all attributes of this response.<br>
50 * The keys are all lower case!
51 * @see #getAttribute(String)
52 */
53 public Map getAttributes()
54 {
55 return attributes;
56 }
57
58 /***
59 * Sets the Map with all attributes.
60 * @param attributes Map with containing the attributes with all lower
61 * case keys.
62 */
63 public void setAttributes(Map attributes)
64 {
65 this.attributes = attributes;
66 }
67
68 /***
69 * Returns the value of the attribute with the given key.<br>
70 * This is particulary important when a response contains special
71 * attributes that are dependent on the action that has been sent.<br>
72 * An example of this is the response to the GetVarAction.
73 * It contains the value of the channel variable as an attribute
74 * stored under the key of the variable name.<br>
75 * Example:
76 * <pre>
77 * GetVarAction action = new GetVarAction();
78 * action.setChannel("SIP/1310-22c3");
79 * action.setVariable("ALERT_INFO");
80 * ManagerResponse response = connection.sendAction(action);
81 * String alertInfo = response.getAttribute("ALERT_INFO");
82 * </pre>
83 * As all attributes are internally stored in lower case the key is
84 * automatically converted to lower case before lookup.
85 *
86 * @param key the key to lookup.
87 * @return the value of the attribute stored under this key or
88 * <code>null</code> if there is no such attribute.
89 */
90 public String getAttribute(String key)
91 {
92 return (String) attributes.get(key.toLowerCase());
93 }
94
95 /***
96 * Returns the point in time this response was received from the asterisk
97 * server.
98 */
99 public Date getDateReceived()
100 {
101 return dateReceived;
102 }
103
104 /***
105 * Sets the point in time this response was received from the asterisk
106 * server.
107 */
108 public void setDateReceived(Date dateReceived)
109 {
110 this.dateReceived = dateReceived;
111 }
112
113 /***
114 * Returns the action id received with this response referencing the action
115 * that generated this response.
116 */
117 public String getActionId()
118 {
119 return actionId;
120 }
121
122 /***
123 * Sets the action id.
124 */
125 public void setActionId(String actionId)
126 {
127 this.actionId = actionId;
128 }
129
130 /***
131 * Returns the message received with this response. The content depends on
132 * the action that generated this response.
133 */
134 public String getMessage()
135 {
136 return message;
137 }
138
139 /***
140 * Sets the message.
141 */
142 public void setMessage(String message)
143 {
144 this.message = message;
145 }
146
147 /***
148 * Returns the value of the "Response:" line. This typically a String like
149 * "Success" or "Error" but depends on the action that generated this
150 * response.
151 */
152 public String getResponse()
153 {
154 return response;
155 }
156
157 /***
158 * Sets the response.
159 */
160 public void setResponse(String response)
161 {
162 this.response = response;
163 }
164
165 /***
166 * Returns the unique id received with this response. The unique id is used
167 * to keep track of channels created by the action sent, for example an
168 * OriginateAction.
169 */
170 public String getUniqueId()
171 {
172 return uniqueId;
173 }
174
175 /***
176 * Sets the unique id received with this response.
177 */
178 public void setUniqueId(String uniqueId)
179 {
180 this.uniqueId = uniqueId;
181 }
182
183 public String toString()
184 {
185 StringBuffer sb;
186
187 sb = new StringBuffer(getClass().getName() + ": ");
188 sb.append("actionId='" + getActionId() + "'; ");
189 sb.append("message='" + getMessage() + "'; ");
190 sb.append("response='" + getResponse() + "'; ");
191 sb.append("uniqueId='" + getUniqueId() + "'; ");
192 sb.append("systemHashcode=" + System.identityHashCode(this));
193
194 return sb.toString();
195 }
196 }