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  import java.io.Serializable;
20  
21  /***
22   * Abstract base class for all commands supported by asterisk's FastAGI.
23   * 
24   * @author srt
25   * @version $Id: AGICommand.java,v 1.4 2005/03/13 11:26:48 srt Exp $
26   */
27  public abstract class AGICommand implements Serializable
28  {
29      /***
30       * Serial version identifier.
31       */
32      private static final long serialVersionUID = 3257849874518456633L;
33  
34      /***
35       * Returns a string suitable to be sent to asterisk.<br>
36       * 
37       * @return a string suitable to be sent to asterisk.
38       */
39      public abstract String buildCommand();
40  
41      protected String escapeAndQuote(String s)
42      {
43          String tmp;
44  
45          if (s == null)
46          {
47              return "\"\"";
48          }
49  
50          tmp = s;
51          tmp = tmp.replaceAll("//\"", "////\""); // escape quotes
52          tmp = tmp.replaceAll("//\n", ""); // filter newline
53          return "\"" + tmp + "\""; // add quotes
54      }
55  
56      public String toString()
57      {
58          StringBuffer sb;
59  
60          sb = new StringBuffer(getClass().getName() + ": ");
61          sb.append("command='" + buildCommand() + "'; ");
62          sb.append("systemHashcode=" + System.identityHashCode(this));
63  
64          return sb.toString();
65      }
66  }