1 /*
2 * Copyright 2004-2007 Stefan Reuter and others
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.util.Map;
20 import java.util.Set;
21 import java.util.TreeMap;
22 import java.util.Locale;
23
24 /**
25 * Response that is received when sending a GetConfigAction.
26 * <p>
27 * Asterisk's response to the GetConfig command is ugly, and requires some
28 * parsing of attributes. This class lazily parses its own attributes to hide
29 * the ugly details. If the file requested exists but does not contain at least
30 * a line with a category, the ResponseBuilder won't create an instance of
31 * GetConfigResponse, as it won't know what the empty response is.
32 *
33 * @see org.asteriskjava.manager.action.GetConfigAction
34 * @author martins
35 * @since 0.3
36 */
37 public class GetConfigResponse extends ManagerResponse
38 {
39 private static final long serialVersionUID = -2044248427247227390L;
40
41 private Map<Integer, String> categories;
42 private Map<Integer, Map<Integer, String>> lines;
43
44 /**
45 * Returns the map of category numbers to category names.
46 *
47 * @return the map of category numbers to names.
48 * @see org.asteriskjava.manager.response.GetConfigResponse#getLines
49 */
50 public Map<Integer, String> getCategories()
51 {
52 if (categories == null)
53 {
54 categories = new TreeMap<Integer, String>();
55 }
56
57 Map<String, Object> responseMap = super.getAttributes();
58 Set<String> responseKeys = responseMap.keySet();
59 for (String key : responseKeys)
60 {
61 if (key.toLowerCase(Locale.US).contains("category"))
62 {
63 String[] keyParts = key.split("-");
64
65 // if it doesn't have at least category-XXXXXX, skip
66 if (keyParts.length < 2)
67 continue;
68
69 // try to get the number of this category, skip if we mess up
70 Integer categoryNumber;
71 try
72 {
73 categoryNumber = Integer.parseInt(keyParts[1]);
74 }
75 catch (Exception exception)
76 {
77 continue;
78 }
79
80 categories.put(categoryNumber, (String) responseMap.get(key));
81 }
82 }
83
84 return categories;
85 }
86
87 /**
88 * Returns the map of line number to line value for a given category.
89 *
90 * @param categoryNumber a valid category number from getCategories.
91 * @return the map of category numbers to names.
92 * @see org.asteriskjava.manager.response.GetConfigResponse#getCategories
93 */
94 public Map<Integer, String> getLines(int categoryNumber)
95 {
96 if (lines == null)
97 {
98 lines = new TreeMap<Integer, Map<Integer, String>>();
99 }
100
101 Map<String, Object> responseMap = super.getAttributes();
102 Set<String> responseKeys = responseMap.keySet();
103 for (String key : responseKeys)
104 {
105 if (key.toLowerCase(Locale.US).contains("line"))
106 {
107 String[] keyParts = key.split("-");
108
109 // if it doesn't have at least line-XXXXXX-XXXXXX, skip
110 if (keyParts.length < 3)
111 {
112 continue;
113 }
114
115 // try to get the number of this category, skip if we mess up
116 Integer potentialCategoryNumber;
117 try
118 {
119 potentialCategoryNumber = Integer.parseInt(keyParts[1]);
120 }
121 catch (Exception exception)
122 {
123 continue;
124 }
125
126 // try to get the number of this line, skip if we mess up
127 Integer potentialLineNumber;
128 try
129 {
130 potentialLineNumber = Integer.parseInt(keyParts[2]);
131 }
132 catch (Exception exception)
133 {
134 continue;
135 }
136
137 // get the List out for placing stuff in
138 Map<Integer, String> linesForCategory = lines.get(potentialCategoryNumber);
139 if (linesForCategory == null)
140 {
141 linesForCategory = new TreeMap<Integer, String>();
142 }
143
144 // put the line we just parsed into the line map for this category
145 linesForCategory.put(potentialLineNumber, (String) responseMap.get(key));
146 if (!lines.containsKey(potentialCategoryNumber))
147 {
148 lines.put(potentialCategoryNumber, linesForCategory);
149 }
150 }
151 }
152
153 return lines.get(categoryNumber);
154 }
155 }