1 package org.asteriskjava.fastagi.command;
2
3 /**
4 * Calls a subroutine from dialplan.
5 * <p/>
6 * Example return code: 200 result=0 Gosub complete
7 * <p/>
8 * This command is available since Asterisk 1.6.
9 *
10 * @author fadishei
11 * @since 1.0.0
12 */
13 public class GosubCommand extends AbstractAgiCommand
14 {
15 private static final long serialVersionUID = 1L;
16
17 /**
18 * the context of the called subroutine.
19 */
20 private String context;
21
22 /**
23 * the extension in the called context.
24 */
25 private String extension;
26
27 /**
28 * the priority of the called extension.
29 */
30 private String priority;
31
32 /**
33 * an optional list of arguments to be passed to the subroutine.
34 * They will accessible in the form of ${ARG1}, ${ARG2}, etc in the subroutine body.
35 */
36 private String arguments[];
37
38 /**
39 * Creates a new GosubCommand.
40 *
41 * @param context context of the called subroutine.
42 * @param extension the extension in the called context.
43 * @param priority of the called extension.
44 */
45 public GosubCommand(String context, String extension, String priority)
46 {
47 super();
48 this.context = context;
49 this.extension = extension;
50 this.priority = priority;
51 }
52
53 /**
54 * Creates a new GosubCommand.
55 *
56 * @param context context of the called subroutine.
57 * @param extension the extension in the called context.
58 * @param priority the priority of the called extension.
59 * @param arguments the arguments to be passed to the called subroutine.
60 */
61 public GosubCommand(String context, String extension, String priority, String... arguments)
62 {
63 super();
64 this.context = context;
65 this.extension = extension;
66 this.priority = priority;
67 this.arguments = arguments;
68 }
69
70 /**
71 * Returns the context of the subroutine to call.
72 *
73 * @return the context of the subroutine to call.
74 */
75 public String getContext()
76 {
77 return context;
78 }
79
80 /**
81 * Sets the context of the subroutine to call.
82 *
83 * @param context the context of the subroutine to call.
84 */
85 public void setContext(String context)
86 {
87 this.context = context;
88 }
89
90 /**
91 * Returns the extension within the called context.
92 *
93 * @return the extension within the called context.
94 */
95 public String getExtension()
96 {
97 return extension;
98 }
99
100 /**
101 * Sets the extension within the called context.
102 *
103 * @param extension the extension within the called context.
104 */
105 public void setExtension(String extension)
106 {
107 this.extension = extension;
108 }
109
110 /**
111 * Returns the priority of the called extension.
112 *
113 * @return the priority of the called extension.
114 */
115 public String getPriority()
116 {
117 return priority;
118 }
119
120 /**
121 * Sets the priority of the called extension.
122 *
123 * @param priority the priority of the called extension.
124 */
125 public void setPriority(String priority)
126 {
127 this.priority = priority;
128 }
129
130 /**
131 * Returns the arguments to be passed to the subroutine.
132 *
133 * @return the arguments to be passed to the subroutine.
134 */
135 public String[] getArguments()
136 {
137 return arguments;
138 }
139
140 /**
141 * Sets the arguments to be passed to the subroutine.
142 *
143 * @param arguments the arguments to be passed to the subroutine.
144 */
145 public void setArguments(String[] arguments)
146 {
147 this.arguments = arguments;
148 }
149
150 @Override
151 public String buildCommand()
152 {
153 final StringBuilder sb = new StringBuilder("GOSUB ");
154 sb.append(escapeAndQuote(context)).append(" ");
155 sb.append(escapeAndQuote(extension)).append(" ");
156 sb.append(escapeAndQuote(priority));
157
158 if (arguments != null)
159 {
160 sb.append(" ").append(escapeAndQuote(arguments));
161 }
162
163 return sb.toString();
164 }
165 }