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.impl;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import net.sf.asterisk.fastagi.AGIException;
24  import net.sf.asterisk.fastagi.AGIHangupException;
25  import net.sf.asterisk.fastagi.AGINetworkException;
26  import net.sf.asterisk.fastagi.AGIReader;
27  import net.sf.asterisk.fastagi.AGIRequest;
28  import net.sf.asterisk.fastagi.reply.AGIReply;
29  import net.sf.asterisk.fastagi.reply.impl.AGIReplyImpl;
30  import net.sf.asterisk.io.SocketConnectionFacade;
31  
32  /***
33   * Default implementation of the AGIReader implementation.
34   * 
35   * @author srt
36   * @version $Id: AGIReaderImpl.java,v 1.3 2005/09/27 21:07:26 srt Exp $
37   */
38  public class AGIReaderImpl implements AGIReader
39  {
40      private SocketConnectionFacade socket;
41  
42      public AGIReaderImpl(SocketConnectionFacade socket)
43      {
44          this.socket = socket;
45      }
46  
47      public AGIRequest readRequest() throws AGIException
48      {
49          AGIRequestImpl request;
50          String line;
51          List lines;
52  
53          lines = new ArrayList();
54  
55          try
56          {
57              while ((line = socket.readLine()) != null)
58              {
59                  if (line.length() == 0)
60                  {
61                      break;
62                  }
63  
64                  lines.add(line);
65              }
66          }
67          catch (IOException e)
68          {
69              throw new AGINetworkException(
70                      "Unable to read request from Asterisk: " + e.getMessage(),
71                      e);
72          }
73  
74          request = new AGIRequestImpl(lines);
75          request.setLocalAddress(socket.getLocalAddress());
76          request.setLocalPort(socket.getLocalPort());
77          request.setRemoteAddress(socket.getRemoteAddress());
78          request.setRemotePort(socket.getRemotePort());
79  
80          return request;
81      }
82  
83      public AGIReply readReply() throws AGIException
84      {
85          AGIReply reply;
86          List lines;
87          String line;
88  
89          lines = new ArrayList();
90  
91          try
92          {
93              line = socket.readLine();
94          }
95          catch (IOException e)
96          {
97              throw new AGINetworkException(
98                      "Unable to read reply from Asterisk: " + e.getMessage(), e);
99          }
100 
101         if (line == null)
102         {
103             throw new AGIHangupException();
104         }
105 
106         lines.add(line);
107 
108         // read synopsis and usage if statuscode is 520
109         if (line.startsWith(Integer
110                 .toString(AGIReply.SC_INVALID_COMMAND_SYNTAX)))
111         {
112             try
113             {
114                 while ((line = socket.readLine()) != null)
115                 {
116                     lines.add(line);
117                     if (line.startsWith(Integer
118                             .toString(AGIReply.SC_INVALID_COMMAND_SYNTAX)))
119                     {
120                         break;
121                     }
122                 }
123             }
124             catch (IOException e)
125             {
126                 throw new AGINetworkException(
127                         "Unable to read reply from Asterisk: " + e.getMessage(),
128                         e);
129             }
130         }
131 
132         reply = new AGIReplyImpl(lines);
133 
134         return reply;
135     }
136 }