-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIPDriver.java
executable file
·340 lines (315 loc) · 10.6 KB
/
SIPDriver.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package com.voxeo.tropo;
import java.io.IOException;
import javax.annotation.Resource;
import javax.management.MBeanServer;
import javax.script.ScriptException;
import javax.sdp.SessionDescription;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.sip.Address;
import javax.servlet.sip.SipApplicationSession;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServlet;
import javax.servlet.sip.SipServletMessage;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipServletResponse;
import javax.servlet.sip.SipURI;
import javax.servlet.sip.TelURL;
import javax.servlet.sip.URI;
import org.apache.log4j.Logger;
import com.voxeo.tropo.app.AbstractApplicationManager;
import com.voxeo.tropo.app.Application;
import com.voxeo.tropo.app.ApplicationInstance;
import com.voxeo.tropo.app.ApplicationManager;
import com.voxeo.tropo.app.InvalidApplicationException;
import com.voxeo.tropo.app.RedirectException;
import com.voxeo.tropo.core.Call;
import com.voxeo.tropo.core.CallImpl;
import com.voxeo.tropo.core.IncomingCall;
import com.voxeo.tropo.core.OutgoingCall;
import com.voxeo.tropo.core.Call.State;
import com.voxeo.tropo.util.DumpHelper;
import com.voxeo.tropo.util.Utils;
@SuppressWarnings("serial")
@javax.servlet.sip.annotation.SipServlet(name = "tropo", loadOnStartup = 1)
public class SIPDriver extends SipServlet {
private static final Logger LOG = Logger.getLogger(SIPDriver.class);
protected ApplicationManager _appMgr;
@Resource
protected SipFactory _sipFactory;
@Resource
protected MBeanServer _server;
@Override
public void init() {
final ServletContext ctx = getServletContext();
try {
System.setProperty(ServletContextConstants.ROOT_PATH, ctx.getRealPath("/"));
Configuration.init(this.getServletConfig());
AbstractApplicationManager.load(Configuration.get(), ctx);
DumpHelper.initialization(_server);
}
catch (final Exception e) {
LOG.error("Unable to initialize Tropo:", e);
throw new RuntimeException(e);
}
_appMgr = (ApplicationManager) ctx.getAttribute(ServletContextConstants.APP_MANAGER);
}
@Override
public void destroy() {
_appMgr.dispose();
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
try {
super.service(req, res);
}
finally {
Utils.clearLogContext();
}
}
@Override
protected void doInvite(final SipServletRequest req) throws ServletException, IOException {
if (req.isInitial()) {
req.createResponse(SipServletResponse.SC_TRYING).send();
LOG.info("A new call is coming from " + req.getFrom().getURI() + " to " + req.getTo().getURI());
final URI token = req.getTo().getURI();
try {
// set guid session id for logging
req.getSession().getApplicationSession().setAttribute(ServletContextConstants.GUID_SESSION_ID, Utils.getGUID());
final Application app = _appMgr.get(token);
Utils.setLogContext(app, req);
req.createResponse(SipServletResponse.SC_RINGING).send();
app.execute(req);
}
catch (final RedirectException e) {
LOG.info("Redirect request for " + req.getTo());
SipServletResponse response = req.createResponse(SipServletResponse.SC_MOVED_TEMPORARILY);
for(SipURI uri : e.getContacts()) {
response.addHeader("Contact", uri.toString());
}
response.send();
}
catch (final InvalidApplicationException e) {
LOG.error("Unknown application name for " + req.getTo() + ". " + e.getMessage(), e);
req.createResponse(SipServletResponse.SC_NOT_FOUND).send();
}
catch (final ScriptException e) {
LOG.error("Invalid script for " + req.getTo() + ". " + e.getMessage(), e);
req.createResponse(SipServletResponse.SC_TEMPORARLY_UNAVAILABLE).send();
}
}
else { // re-invite
final CallImpl call = findCall(req);
if (call != null) {
if (call.getState() == Call.State.ANSWERED) {
updateEndpoint(call, req);
}
}
}
}
@Override
protected void doAck(final SipServletRequest req) throws ServletException, IOException {
final CallImpl call = findCall(req);
if (call != null) {
call.lock();
try {
if (call.getState() == Call.State.ANSWERING) {
updateEndpoint(call, req);
call.setState(Call.State.ANSWERED);
}
else if (call.getState() == Call.State.REJECTING) {
call.setState(Call.State.REJECTED);
}
else if (call.getState() == Call.State.REDIRECTING) {
call.setState(Call.State.REDIRECTED);
}
else {
if (LOG.isDebugEnabled()) {
LOG.debug("No match state: " + call);
}
}
}
finally {
call.unlock();
}
}
}
@Override
protected void doCancel(final SipServletRequest req) throws ServletException, IOException {
final CallImpl call = findCall(req);
if (call != null) {
call.lock();
try {
if (call.getState() == State.RINGING) {
call.setState(Call.State.DISCONNECTED);
}
}
finally {
call.unlock();
}
}
}
@Override
protected void doBye(final SipServletRequest req) throws ServletException, IOException {
final SipApplicationSession as = req.getSession().getApplicationSession();
final ApplicationInstance inst = (ApplicationInstance) as.getAttribute(ApplicationInstance.INST);
final CallImpl call = findCall(req);
if (call != null) {
call.setState(Call.State.DISCONNECTED);
LOG.info("A call just ended: " + req.getFrom().getURI() + "--->" + req.getTo().getURI());
req.createResponse(SipServletResponse.SC_OK).send();
}
else {
req.createResponse(SipServletResponse.SC_DOES_NOT_EXIT_ANYWHERE).send();
}
// check which leg is this???
if (inst != null) {
inst.terminate();
}
}
@Override
protected void doProvisionalResponse(final SipServletResponse res) throws ServletException, IOException {
final CallImpl call = findCall(res);
if (call == null || call instanceof IncomingCall) {
return;
}
if (res.getMethod().equalsIgnoreCase("INVITE")) {
final int code = res.getStatus();
switch (code) {
case SipServletResponse.SC_RINGING:
call.lock();
try {
if (call.getState() == Call.State.ANSWERING) {
call.setState(Call.State.RINGING);
}
}
finally {
call.unlock();
}
break;
case SipServletResponse.SC_SESSION_PROGRESS:
final OutgoingCall out = (OutgoingCall) call;
if (out.isAnswerOnMedia()) {
final Object o = res.getContent();
if (o instanceof SessionDescription) {
call.lock();
try {
final Call.State state = call.getState();
if (state == Call.State.ANSWERING || state == Call.State.RINGING) {
call.setState(Call.State.ANSWERED);
}
}
finally {
call.unlock();
}
}
}
break;
default:
break;
}
}
}
@Override
protected void doSuccessResponse(final SipServletResponse res) throws ServletException, IOException {
if (res.getMethod().equalsIgnoreCase("INVITE")) {
final CallImpl call = findCall(res);
if (call != null) {
call.lock();
try {
final Call.State state = call.getState();
if (state == Call.State.ANSWERING || state == Call.State.RINGING) {
updateEndpoint(call, res);
call.setState(Call.State.ANSWERED);
}
}
finally {
call.unlock();
}
}
res.createAck().send();
}
else if (res.getMethod().equalsIgnoreCase("BYE")) {
// do we do anything?
}
}
@Override
protected void doErrorResponse(final SipServletResponse res) throws ServletException, IOException {
if (res.getMethod().equalsIgnoreCase("INVITE")) {
final CallImpl call = findCall(res);
if (call != null) {
call.lock();
try {
final Call.State state = call.getState();
if (state == Call.State.ANSWERING || state == Call.State.RINGING) {
call.setState(Call.State.DISCONNECTED);
}
else if (state == Call.State.ANSWERED) { // answerOnMedia case
call.hangup();
}
}
finally {
call.unlock();
}
}
}
}
@Override
protected void doRedirectResponse(final SipServletResponse res) throws ServletException, IOException {
if (res.getMethod().equalsIgnoreCase("INVITE")) {
final CallImpl call = findCall(res);
if (call != null) {
call.lock();
try {
final Call.State state = call.getState();
if (state == Call.State.ANSWERING || state == Call.State.RINGING) {
final Address contact = res.getAddressHeader("Contact");
if (contact != null) {
URI target = contact.getURI();
final SipServletRequest req = _sipFactory.createRequest(res.getApplicationSession(), "INVITE", res
.getFrom(), res.getTo());
if (target instanceof TelURL) {
target = _sipFactory.createURI(Utils.prefixNumber(String.valueOf(target)));
}
req.setRequestURI(target);
((OutgoingCall) call).update(req);
req.send();
res.getSession().invalidate();
}
}
}
finally {
call.unlock();
}
}
}
}
protected CallImpl findCall(final SipServletMessage message) {
Utils.setLogContext(message);
final CallImpl call = (CallImpl) message.getSession().getAttribute(CallImpl.INST);
if (call == null) {
LOG.warn("No call is found for " + message);
}
else {
if (LOG.isDebugEnabled()) {
LOG.debug("do" + (message instanceof SipServletResponse ? ((SipServletResponse) message).getStatus() : "")
+ message.getMethod() + " find call: " + call);
}
}
return call;
}
protected void updateEndpoint(final CallImpl call, final SipServletMessage message) {
Object content = null;
try {
content = message.getContent();
}
catch (final Throwable t) {
LOG.warn("getContent error " + t);
}
if (content != null) {
call.updateEndpoint(message.getRemoteAddr(), message.getRemotePort(), content.toString());
}
}
}