View Javadoc

1   /*
2    * Copyright  2004-2005 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 net.sf.asterisk.manager.impl;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import net.sf.asterisk.manager.ResponseBuilder;
23  import net.sf.asterisk.manager.response.ChallengeResponse;
24  import net.sf.asterisk.manager.response.ExtensionStateResponse;
25  import net.sf.asterisk.manager.response.MailboxCountResponse;
26  import net.sf.asterisk.manager.response.MailboxStatusResponse;
27  import net.sf.asterisk.manager.response.ManagerError;
28  import net.sf.asterisk.manager.response.ManagerResponse;
29  
30  /***
31   * Default implementation of the ResponseBuilder interface.
32   * 
33   * @see net.sf.asterisk.manager.response.ManagerResponse
34   * @author srt
35   * @version $Id: ResponseBuilderImpl.java,v 1.2 2005/04/23 22:56:29 srt Exp $
36   */
37  public class ResponseBuilderImpl implements ResponseBuilder
38  {
39      /***
40       * Constructs an instance of ManagerResponse based on a map of attributes.
41       * 
42       * @param attributes the attributes and their values. The keys of this map must be all lower
43       * case.
44       * @return the response with the given attributes.
45       */
46      public ManagerResponse buildResponse(final Map attributes)
47      {
48          ManagerResponse response;
49          String responseType;
50          
51          responseType = (String) attributes.get("response");
52  
53          // determine type
54          if ("error".equalsIgnoreCase(responseType))
55          {
56              response = new ManagerError();
57          }
58          else if (attributes.containsKey("challenge"))
59          {
60              ChallengeResponse challengeResponse = new ChallengeResponse();
61              challengeResponse.setChallenge((String) attributes.get("challenge"));
62              response = challengeResponse;
63          }
64          else if (attributes.containsKey("mailbox") && attributes.containsKey("waiting"))
65          {
66              MailboxStatusResponse mailboxStatusResponse = new MailboxStatusResponse();
67              mailboxStatusResponse.setMailbox((String) attributes.get("mailbox"));
68              
69              if ("1".equals((String) attributes.get("waiting")))
70              {
71                  mailboxStatusResponse.setWaiting(Boolean.TRUE);
72              }
73              else
74              {
75                  mailboxStatusResponse.setWaiting(Boolean.FALSE);
76              }
77              
78              response = mailboxStatusResponse;
79          }
80          else if (attributes.containsKey("mailbox") && attributes.containsKey("newmessages")
81                  && attributes.containsKey("oldmessages"))
82          {
83              MailboxCountResponse mailboxCountResponse = new MailboxCountResponse();
84              mailboxCountResponse.setMailbox((String) attributes.get("mailbox"));
85              mailboxCountResponse.setNewMessages(new Integer((String) attributes.get("newmessages")));
86              mailboxCountResponse.setOldMessages(new Integer((String) attributes.get("oldmessages")));
87              response = mailboxCountResponse;
88          }
89          else if (attributes.containsKey("exten") && attributes.containsKey("context") && attributes.containsKey("hint")
90                  && attributes.containsKey("status"))
91          {
92              ExtensionStateResponse extensionStateResponse = new ExtensionStateResponse();
93              extensionStateResponse.setExten((String) attributes.get("exten"));
94              extensionStateResponse.setContext((String) attributes.get("context"));
95              extensionStateResponse.setHint((String) attributes.get("hint"));
96              extensionStateResponse.setStatus(new Integer((String) attributes.get("status")));
97              response = extensionStateResponse;
98          }
99          else
100         {
101             response = new ManagerResponse();
102         }
103 
104         // fill known attributes
105         response.setResponse(responseType);
106 
107         // clone this map as it is reused by the ManagerReader
108         response.setAttributes(new HashMap(attributes));
109 
110         if (attributes.containsKey("actionid"))
111         {
112             response.setActionId((String) attributes.get("actionid"));
113         }
114         if (attributes.containsKey("message"))
115         {
116             response.setMessage((String) attributes.get("message"));
117         }
118         if (attributes.containsKey("uniqueid"))
119         {
120             response.setUniqueId((String) attributes.get("uniqueid"));
121         }
122 
123         return response;
124     }
125 }