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.fastagi.command;
18  
19  /***
20   * Stream the given file, and recieve DTMF data. The user may interrupt the streaming
21   * by starting to enter digits.<br>
22   * Returns the digits recieved from the channel at the other end.<br>
23   * Input ends when the timeout is reached, the maximum number of digits is read
24   * or the user presses #.
25   * 
26   * @author srt
27   * @version $Id: GetDataCommand.java,v 1.7 2005/11/17 17:41:35 srt Exp $
28   */
29  public class GetDataCommand extends AGICommand
30  {
31      /***
32       * Serial version identifier.
33       */
34      private static final long serialVersionUID = 3978141041352128820L;
35  
36      private static final int DEFAULT_MAX_DIGITS = 1024;
37      private static final int DEFAULT_TIMEOUT = 0;
38  
39      
40      /***
41       * The name of the file to stream.
42       */
43      private String file;
44  
45      /***
46       * The timeout in milliseconds to wait for data.<br>
47       * 0 means standard timeout value, -1 means "ludicrous time" (essentially
48       * never times out).
49       */
50      private long timeout;
51  
52      /***
53       * The maximum number of digits to read.<br>
54       * Must be in [1..1024].
55       */
56      private int maxDigits;
57  
58      /***
59       * Creates a new GetDataCommand with default timeout and maxDigits set to
60       * 1024.
61       * 
62       * @param file the name of the file to stream, must not include extension.
63       */
64      public GetDataCommand(String file)
65      {
66          this.file = file;
67          this.timeout = DEFAULT_TIMEOUT;
68          this.maxDigits = DEFAULT_MAX_DIGITS;
69      }
70  
71      /***
72       * Creates a new GetDataCommand with the given timeout and maxDigits set to
73       * 1024.
74       * 
75       * @param file the name of the file to stream, must not include extension.
76       * @param timeout the timeout in milliseconds to wait for data.<br>
77       *            0 means standard timeout value, -1 means "ludicrous time"
78       *            (essentially never times out).
79       */
80      public GetDataCommand(String file, long timeout)
81      {
82          this.file = file;
83          this.timeout = timeout;
84          this.maxDigits = DEFAULT_MAX_DIGITS;
85      }
86  
87      /***
88       * Creates a new GetDataCommand with the given timeout and maxDigits.
89       * 
90       * @param file the name of the file to stream, must not include extension.
91       * @param timeout the timeout in milliseconds to wait for data.<br>
92       *            0 means standard timeout value, -1 means "ludicrous time"
93       *            (essentially never times out).
94       * @param maxDigits the maximum number of digits to read.<br>
95       *            Must be in [1..1024].
96       * 
97       * @throws IllegalArgumentException if maxDigits is not in [1..1024]
98       */
99      public GetDataCommand(String file, long timeout, int maxDigits)
100             throws IllegalArgumentException
101     {
102         if (maxDigits < 1 || maxDigits > 1024)
103         {
104             throw new IllegalArgumentException("maxDigits must be in [1..1024]");
105         }
106 
107         this.file = file;
108         this.timeout = timeout;
109         this.maxDigits = maxDigits;
110     }
111 
112     /***
113      * Returns the name of the file to stream.
114      * 
115      * @return the name of the file to stream.
116      */
117     public String getFile()
118     {
119         return file;
120     }
121 
122     /***
123      * Sets the name of the file to stream.<br>
124      * This attribute is mandatory.
125      * 
126      * @param file the name of the file to stream, must not include extension.
127      */
128     public void setFile(String file)
129     {
130         this.file = file;
131     }
132 
133     /***
134      * Returns the timeout to wait for data.
135      * 
136      * @return the timeout in milliseconds to wait for data.
137      */
138     public long getTimeout()
139     {
140         return timeout;
141     }
142 
143     /***
144      * Sets the timeout to wait for data.
145      * 
146      * @param timeout the timeout in milliseconds to wait for data.<br>
147      *            0 means standard timeout value, -1 means "ludicrous time"
148      *            (essentially never times out).
149      */
150     public void setTimeout(long timeout)
151     {
152         this.timeout = timeout;
153     }
154 
155     /***
156      * Returns the maximum number of digits to read.
157      * 
158      * @return the maximum number of digits to read.
159      */
160     public int getMaxDigits()
161     {
162         return maxDigits;
163     }
164 
165     /***
166      * Sets the maximum number of digits to read.
167      * 
168      * @param maxDigits the maximum number of digits to read.<br>
169      *            Must be in [1..1024].
170      * 
171      * @throws IllegalArgumentException if maxDigits is not in [1..1024]
172      */
173     public void setMaxDigits(int maxDigits) throws IllegalArgumentException
174     {
175         if (maxDigits < 1 || maxDigits > 1024)
176         {
177             throw new IllegalArgumentException("maxDigits must be in [1..1024]");
178         }
179 
180         this.maxDigits = maxDigits;
181     }
182 
183     public String buildCommand()
184     {
185         if (maxDigits == DEFAULT_MAX_DIGITS)
186         {
187             if (timeout == DEFAULT_TIMEOUT)
188             {
189                 return "GET DATA " + escapeAndQuote(file);
190             }
191             else
192             {
193                 return "GET DATA " + escapeAndQuote(file) + " " + timeout;
194             }
195         }
196         return "GET DATA " + escapeAndQuote(file) + " " + timeout + " "
197                 + maxDigits;
198     }
199 }