-
Notifications
You must be signed in to change notification settings - Fork 291
/
AgiConnectionHandler.java
252 lines (221 loc) · 7.67 KB
/
AgiConnectionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright 2004-2006 Stefan Reuter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.asteriskjava.fastagi.internal;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.asteriskjava.fastagi.AgiChannel;
import org.asteriskjava.fastagi.AgiChannelFactory;
import org.asteriskjava.fastagi.AgiException;
import org.asteriskjava.fastagi.AgiReader;
import org.asteriskjava.fastagi.AgiRequest;
import org.asteriskjava.fastagi.AgiScript;
import org.asteriskjava.fastagi.AgiWriter;
import org.asteriskjava.fastagi.MappingStrategy;
import org.asteriskjava.fastagi.NamedAgiScript;
import org.asteriskjava.fastagi.command.VerboseCommand;
import org.asteriskjava.util.Log;
import org.asteriskjava.util.LogFactory;
/**
* An AgiConnectionHandler is created and run by the AgiServer whenever a new
* AGI connection from an Asterisk Server is received. <br>
* It reads the request using an AgiReader and runs the AgiScript configured to
* handle this type of request. Finally it closes the AGI connection.
*
* @author srt
* @version $Id$
*/
public abstract class AgiConnectionHandler implements Runnable
{
private static final String AJ_AGISTATUS_VARIABLE = "AJ_AGISTATUS";
private static final String AJ_AGISTATUS_NOT_FOUND = "NOT_FOUND";
private static final String AJ_AGISTATUS_SUCCESS = "SUCCESS";
private static final String AJ_AGISTATUS_FAILED = "FAILED";
private static final ThreadLocal<AgiChannel> channel = new ThreadLocal<>();
private final Log logger = LogFactory.getLog(getClass());
private boolean ignoreMissingScripts = false;
private AgiScript script = null;
private AgiChannelFactory agiChannelFactory;
public static final ConcurrentMap<AgiConnectionHandler, AgiChannel> AGI_CONNECTION_HANDLERS = new ConcurrentHashMap<>(
32);
/**
* The strategy to use to determine which script to run.
*/
private final MappingStrategy mappingStrategy;
/**
* Creates a new AGIConnectionHandler to handle the given socket connection.
*
* @param mappingStrategy the strategy to use to determine which script to
* run.
* @param agiChannelFactory the AgiFactory, that is used to create new
* AgiChannel objects.
*/
protected AgiConnectionHandler(MappingStrategy mappingStrategy, AgiChannelFactory agiChannelFactory)
{
if (mappingStrategy == null)
{
throw new IllegalArgumentException("MappingStrategy must not be null");
}
if (agiChannelFactory == null)
{
throw new IllegalArgumentException("AgiChannelFactory must not be null");
}
this.agiChannelFactory = agiChannelFactory;
this.mappingStrategy = mappingStrategy;
}
protected boolean isIgnoreMissingScripts()
{
return ignoreMissingScripts;
}
protected void setIgnoreMissingScripts(boolean ignoreMissingScripts)
{
this.ignoreMissingScripts = ignoreMissingScripts;
}
protected AgiScript getScript()
{
return script;
}
protected abstract AgiReader createReader();
protected abstract AgiWriter createWriter();
/**
* Release any open resources like closing a socket.
*/
public abstract void release();
@Override
public void run()
{
AgiChannel channel = null;
try
{
AgiReader reader;
AgiWriter writer;
AgiRequest request;
reader = createReader();
writer = createWriter();
request = reader.readRequest();
channel = this.agiChannelFactory.createAgiChannel(request, writer, reader);
AgiConnectionHandler.channel.set(channel);
if (mappingStrategy != null)
{
script = mappingStrategy.determineScript(request, channel);
}
if (script == null && !ignoreMissingScripts)
{
final String errorMessage;
errorMessage = "No script configured for URL '" + request.getRequestURL() + "' (script '"
+ request.getScript() + "')";
logger.error(errorMessage);
setStatusVariable(channel, AJ_AGISTATUS_NOT_FOUND);
logToAsterisk(channel, errorMessage);
}
else if (script != null)
{
AGI_CONNECTION_HANDLERS.put(this, channel);
runScript(script, request, channel);
}
}
catch (AgiException e)
{
setStatusVariable(channel, AJ_AGISTATUS_FAILED);
logger.error("AgiException while handling request", e);
}
catch (Exception e)
{
setStatusVariable(channel, AJ_AGISTATUS_FAILED);
logger.error("Unexpected Exception while handling request", e);
}
finally
{
AGI_CONNECTION_HANDLERS.remove(this);
AgiConnectionHandler.channel.set(null);
release();
}
}// run
private void runScript(AgiScript script, AgiRequest request, AgiChannel channel)
{
String threadName;
threadName = Thread.currentThread().getName();
logger.debug("Begin AgiScript " + getScriptName(script) + " on " + threadName);
try
{
script.service(request, channel);
setStatusVariable(channel, AJ_AGISTATUS_SUCCESS);
}
catch (AgiException e)
{
logger.error("AgiException running AgiScript " + getScriptName(script) + " on " + threadName, e);
setStatusVariable(channel, AJ_AGISTATUS_FAILED);
}
catch (Exception e)
{
logger.error("Exception running AgiScript " + getScriptName(script) + " on " + threadName, e);
setStatusVariable(channel, AJ_AGISTATUS_FAILED);
}
logger.debug("End AgiScript " + getScriptName(script) + " on " + threadName);
}
protected String getScriptName(AgiScript script)
{
if (script == null)
{
return null;
}
if (script instanceof NamedAgiScript)
{
return ((NamedAgiScript) script).getName();
}
return script.getClass().getName();
}
private void setStatusVariable(AgiChannel channel, String value)
{
if (channel == null)
{
return;
}
try
{
channel.setVariable(AJ_AGISTATUS_VARIABLE, value);
}
catch (Exception e) // NOPMD
{
// swallow
}
}
private void logToAsterisk(AgiChannel channel, String message)
{
if (channel == null)
{
return;
}
try
{
channel.sendCommand(new VerboseCommand(message, 1));
}
catch (Exception e) // NOPMD
{
// swallow
}
}
/**
* Returns the AgiChannel associated with the current thread.
*
* @return the AgiChannel associated with the current thread or
* <code>null</code> if none is associated.
*/
public static AgiChannel getChannel()
{
return AgiConnectionHandler.channel.get();
}
}