View Javadoc

1   /*
2    *  Copyright 2004-2006 Stefan Reuter
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.asteriskjava.manager.response;
18  
19  import java.io.Serializable;
20  import java.util.Date;
21  import java.util.Locale;
22  import java.util.Map;
23  
24  /**
25   * Represents a response received from the Asterisk server as the result of a
26   * previously sent ManagerAction.<p>
27   * The response can be linked with the action that caused it by looking the
28   * action id attribute that will match the action id of the corresponding
29   * action.
30   *
31   * @author srt
32   * @version $Id$
33   * @see org.asteriskjava.manager.action.ManagerAction
34   */
35  public class ManagerResponse implements Serializable
36  {
37      private static final long serialVersionUID = 1L;
38  
39      private Date dateReceived;
40      private String actionId;
41  
42      /**
43       * The server from which this response has been received (only used with AstManProxy).
44       */
45      private String server;
46      private String response;
47      private String eventList;
48      private String message;
49      private String uniqueId;
50      private Map<String, Object> attributes;
51  
52      public ManagerResponse()
53      {
54          this.attributes = null;
55      }
56  
57      /**
58       * Returns a Map with all attributes of this response.<p>
59       * The keys are all lower case!
60       *
61       * @see #getAttribute(String)
62       */
63      public Map<String, Object> getAttributes()
64      {
65          return attributes;
66      }
67  
68      /**
69       * Sets the Map with all attributes.
70       *
71       * @param attributes Map with containing the attributes with all lower
72       *                   case keys.
73       */
74      public void setAttributes(Map<String, Object> attributes)
75      {
76          this.attributes = attributes;
77      }
78  
79      /**
80       * Returns the value of the attribute with the given key.<p>
81       * This is particulary important when a response contains special
82       * attributes that are dependent on the action that has been sent.<p>
83       * An example of this is the response to the GetVarAction.
84       * It contains the value of the channel variable as an attribute
85       * stored under the key of the variable name.<p>
86       * Example:
87       * <pre>
88       * GetVarAction action = new GetVarAction();
89       * action.setChannel("SIP/1310-22c3");
90       * action.setVariable("ALERT_INFO");
91       * ManagerResponse response = connection.sendAction(action);
92       * String alertInfo = response.getAttribute("ALERT_INFO");
93       * </pre>
94       * As all attributes are internally stored in lower case the key is
95       * automatically converted to lower case before lookup.
96       *
97       * @param key the key to lookup.
98       * @return the value of the attribute stored under this key or
99       *         <code>null</code> if there is no such attribute.
100      */
101     public String getAttribute(String key)
102     {
103         return (String) attributes.get(key.toLowerCase(Locale.ENGLISH));
104     }
105 
106     /**
107      * Returns the point in time this response was received from the asterisk
108      * server.
109      */
110     public Date getDateReceived()
111     {
112         return dateReceived;
113     }
114 
115     /**
116      * Sets the point in time this response was received from the asterisk
117      * server.
118      */
119     public void setDateReceived(Date dateReceived)
120     {
121         this.dateReceived = dateReceived;
122     }
123 
124     /**
125      * Returns the user provided action id of the ManagerAction that caused
126      * this response. If the application did not set an action id this method
127      * returns <code>null</code>.
128      *
129      * @return the action id of the ManagerAction that caused this response or
130      *         <code>null</code> if none was set.
131      * @see org.asteriskjava.manager.action.ManagerAction#setActionId(String)
132      */
133     public String getActionId()
134     {
135         return actionId;
136     }
137 
138     /**
139      * Returns the name of the Asterisk server from which this response has been received.
140      * <p/>
141      * This property is only available when using to AstManProxy.
142      *
143      * @return the name of the Asterisk server from which this response has been received
144      *         or <code>null</code> when directly connected to an Asterisk server
145      *         instead of AstManProxy.
146      * @since 1.0.0
147      */
148     public final String getServer()
149     {
150         return server;
151     }
152 
153     /**
154      * Sets the name of the Asterisk server from which this response has been received.
155      *
156      * @param server the name of the Asterisk server from which this response has been received.
157      * @since 1.0.0
158      */
159     public final void setServer(String server)
160     {
161         this.server = server;
162     }
163 
164     /**
165      * Sent for manager events that reply with a list of events.
166      *
167      * @return "start", "stop" or "complete"
168      * @since 1.0.0
169      */
170     public String getEventList()
171     {
172         return eventList;
173     }
174 
175     /**
176      * Sets the eventList.
177      *
178      * @param eventList the eventList.
179      * @since 1.0.0
180      */
181     public void setEventList(String eventList)
182     {
183         this.eventList = eventList;
184     }
185 
186     /**
187      * Sets the action id of the ManagerAction that caused this response.
188      *
189      * @param actionId the action id of the ManagerAction that caused this
190      *                 response.
191      */
192     public void setActionId(String actionId)
193     {
194         this.actionId = actionId;
195     }
196 
197     /**
198      * Returns the message received with this response. The content depends on
199      * the action that generated this response.
200      */
201     public String getMessage()
202     {
203         return message;
204     }
205 
206     /**
207      * Sets the message.
208      */
209     public void setMessage(String message)
210     {
211         this.message = message;
212     }
213 
214     /**
215      * Returns the value of the "Response:" line. This typically a String like
216      * "Success" or "Error" but depends on the action that generated this
217      * response.
218      */
219     public String getResponse()
220     {
221         return response;
222     }
223 
224     /**
225      * Sets the response.
226      */
227     public void setResponse(String response)
228     {
229         this.response = response;
230     }
231 
232     /**
233      * Returns the unique id received with this response. The unique id is used
234      * to keep track of channels created by the action sent, for example an
235      * OriginateAction.
236      */
237     public String getUniqueId()
238     {
239         return uniqueId;
240     }
241 
242     /**
243      * Sets the unique id received with this response.
244      */
245     public void setUniqueId(String uniqueId)
246     {
247         this.uniqueId = uniqueId;
248     }
249 
250     protected Integer stringToInteger(String s, String suffix) throws NumberFormatException
251     {
252         if (s == null || s.length() == 0)
253         {
254             return null;
255         }
256 
257         if (suffix != null && s.endsWith(suffix))
258         {
259             return Integer.parseInt(s.substring(0, s.length() - suffix.length()).trim());
260         }
261 
262         return Integer.parseInt(s.trim());
263     }
264 
265     protected Long stringToLong(String s, String suffix) throws NumberFormatException
266     {
267         if (s == null || s.length() == 0)
268         {
269             return null;
270         }
271 
272         if (suffix != null && s.endsWith(suffix))
273         {
274             return Long.parseLong(s.substring(0, s.length() - suffix.length()).trim());
275         }
276         
277         return Long.parseLong(s.trim());
278     }
279 
280     @Override
281     public String toString()
282     {
283         StringBuffer sb;
284 
285         sb = new StringBuffer(100);
286         sb.append(getClass().getName()).append(": ");
287         sb.append("actionId='").append(getActionId()).append("'; ");
288         sb.append("message='").append(getMessage()).append("'; ");
289         sb.append("response='").append(getResponse()).append("'; ");
290         sb.append("uniqueId='").append(getUniqueId()).append("'; ");
291         sb.append("systemHashcode=").append(System.identityHashCode(this));
292 
293         return sb.toString();
294     }
295 }