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   * @see org.asteriskjava.manager.action.ManagerAction
32   * @author srt
33   * @version $Id: ManagerResponse.java 729 2007-05-26 05:16:57Z sprior $
34   */
35  public class ManagerResponse implements Serializable
36  {
37      /***
38       * Serial version identifier
39       */
40      private static final long serialVersionUID = -935845815108584292L;
41  
42      private Date dateReceived;
43      private String actionId;
44      private String response;
45      private String message;
46      private String uniqueId;
47      private Map<String, String> attributes;
48  
49      /***
50       * Returns a Map with all attributes of this response.<p>
51       * The keys are all lower case!
52       * @see #getAttribute(String)
53       */
54      public Map<String, String> getAttributes()
55      {
56          return attributes;
57      }
58  
59      /***
60       * Sets the Map with all attributes.
61       * @param attributes Map with containing the attributes with all lower 
62       * case keys. 
63       */
64      public void setAttributes(Map<String, String> attributes)
65      {
66          this.attributes = attributes;
67      }
68  
69      /***
70       * Returns the value of the attribute with the given key.<p>
71       * This is particulary important when a response contains special 
72       * attributes that are dependent on the action that has been sent.<p>
73       * An example of this is the response to the GetVarAction.
74       * It contains the value of the channel variable as an attribute
75       * stored under the key of the variable name.<p>
76       * Example:
77       * <pre>
78       * GetVarAction action = new GetVarAction();
79       * action.setChannel("SIP/1310-22c3");
80       * action.setVariable("ALERT_INFO");
81       * ManagerResponse response = connection.sendAction(action);
82       * String alertInfo = response.getAttribute("ALERT_INFO");
83       * </pre>
84       * As all attributes are internally stored in lower case the key is
85       * automatically converted to lower case before lookup.
86       * 
87       * @param key the key to lookup.
88       * @return the value of the attribute stored under this key or
89       *         <code>null</code> if there is no such attribute.
90       */
91      public String getAttribute(String key)
92      {
93          return attributes.get(key.toLowerCase(Locale.ENGLISH));
94      }
95  
96      /***
97       * Returns the point in time this response was received from the asterisk
98       * server.
99       */
100     public Date getDateReceived()
101     {
102         return dateReceived;
103     }
104 
105     /***
106      * Sets the point in time this response was received from the asterisk
107      * server.
108      */
109     public void setDateReceived(Date dateReceived)
110     {
111         this.dateReceived = dateReceived;
112     }
113 
114     /***
115      * Returns the user provided action id of the ManagerAction that caused 
116      * this response. If the application did not set an action id this method
117      * returns <code>null</code>.
118      * 
119      * @return the action id of the ManagerAction that caused this response or
120      *         <code>null</code> if none was set.
121      * @see org.asteriskjava.manager.action.ManagerAction#setActionId()
122      */
123     public String getActionId()
124     {
125         return actionId;
126     }
127 
128     /***
129      * Sets the action id of the ManagerAction that caused this response.
130      * 
131      * @param actionId the action id of the ManagerAction that caused this
132      *            response.
133      */
134     public void setActionId(String actionId)
135     {
136         this.actionId = actionId;
137     }
138 
139     /***
140      * Returns the message received with this response. The content depends on
141      * the action that generated this response.
142      */
143     public String getMessage()
144     {
145         return message;
146     }
147 
148     /***
149      * Sets the message.
150      */
151     public void setMessage(String message)
152     {
153         this.message = message;
154     }
155 
156     /***
157      * Returns the value of the "Response:" line. This typically a String like
158      * "Success" or "Error" but depends on the action that generated this
159      * response.
160      */
161     public String getResponse()
162     {
163         return response;
164     }
165 
166     /***
167      * Sets the response.
168      */
169     public void setResponse(String response)
170     {
171         this.response = response;
172     }
173 
174     /***
175      * Returns the unique id received with this response. The unique id is used
176      * to keep track of channels created by the action sent, for example an
177      * OriginateAction.
178      */
179     public String getUniqueId()
180     {
181         return uniqueId;
182     }
183 
184     /***
185      * Sets the unique id received with this response.
186      */
187     public void setUniqueId(String uniqueId)
188     {
189         this.uniqueId = uniqueId;
190     }
191 
192     @Override
193    public String toString()
194     {
195         StringBuffer sb;
196 
197         sb = new StringBuffer(100);
198         sb.append(getClass().getName() + ": ");
199         sb.append("actionId='" + getActionId() + "'; ");
200         sb.append("message='" + getMessage() + "'; ");
201         sb.append("response='" + getResponse() + "'; ");
202         sb.append("uniqueId='" + getUniqueId() + "'; ");
203         sb.append("systemHashcode=" + System.identityHashCode(this));
204 
205         return sb.toString();
206     }
207 }