1
+ /*
2
+ * JBoss, Home of Professional Open Source
3
+ * Copyright 2011, Red Hat, Inc. and individual contributors
4
+ * by the @authors tag. See the copyright.txt in the distribution for a
5
+ * full listing of individual contributors.
6
+ *
7
+ * This is free software; you can redistribute it and/or modify it
8
+ * under the terms of the GNU Lesser General Public License as
9
+ * published by the Free Software Foundation; either version 2.1 of
10
+ * the License, or (at your option) any later version.
11
+ *
12
+ * This software is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with this software; if not, write to the Free
19
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21
+ */
22
+
23
+ package org .mobicents .servlet .sip .example ;
24
+
25
+ import java .io .IOException ;
26
+
27
+ import javax .servlet .ServletConfig ;
28
+ import javax .servlet .ServletContext ;
29
+ import javax .servlet .ServletException ;
30
+ import javax .servlet .sip .AuthInfo ;
31
+ import javax .servlet .sip .Parameterable ;
32
+ import javax .servlet .sip .ServletParseException ;
33
+ import javax .servlet .sip .SipApplicationSession ;
34
+ import javax .servlet .sip .SipFactory ;
35
+ import javax .servlet .sip .SipServlet ;
36
+ import javax .servlet .sip .SipServletContextEvent ;
37
+ import javax .servlet .sip .SipServletListener ;
38
+ import javax .servlet .sip .SipServletRequest ;
39
+ import javax .servlet .sip .SipServletResponse ;
40
+ import javax .servlet .sip .SipURI ;
41
+ import javax .servlet .sip .annotation .SipListener ;
42
+
43
+ import org .apache .log4j .Logger ;
44
+
45
+ /**
46
+ * This Sip Servlet acts as as a UAC registering to callwithus
47
+ * @author Jean Deruelle
48
+ *
49
+ */
50
+ @ SipListener
51
+ @ javax .servlet .sip .annotation .SipServlet (name ="MessagingSipServlet" , loadOnStartup =1 )
52
+ public class MessagingSipServlet extends SipServlet implements SipServletListener {
53
+ private static final long serialVersionUID = 1L ;
54
+ private static transient final Logger logger = Logger .getLogger (MessagingSipServlet .class );
55
+
56
+ @ Override
57
+ public void init (ServletConfig servletConfig ) throws ServletException {
58
+ logger .info ("the Messaging sip servlet has been started" );
59
+ super .init (servletConfig );
60
+ }
61
+
62
+ @ Override
63
+ protected void doErrorResponse (SipServletResponse response )
64
+ throws ServletException , IOException {
65
+
66
+ logger .info ("Got response: " + response );
67
+ SipFactory sipFactory = (SipFactory ) getServletContext ().getAttribute (SIP_FACTORY );
68
+ if (response .getStatus () == SipServletResponse .SC_UNAUTHORIZED ||
69
+ response .getStatus () == SipServletResponse .SC_PROXY_AUTHENTICATION_REQUIRED ) {
70
+ // Avoid re-sending if the auth repeatedly fails.
71
+ if (!"true" .equals (getServletContext ().getAttribute ("FirstResponseRecieved" )))
72
+ {
73
+ getServletContext ().setAttribute ("FirstResponseRecieved" , "true" );
74
+ AuthInfo authInfo = sipFactory .createAuthInfo ();
75
+ authInfo .addAuthInfo (
76
+ response .getStatus (),
77
+ response .getChallengeRealms ().next (),
78
+ getServletContext ().getInitParameter ("user.name" ),
79
+ getServletContext ().getInitParameter ("password" ));
80
+
81
+ SipServletRequest challengeRequest = response .getSession ().createRequest (
82
+ response .getRequest ().getMethod ());
83
+
84
+ challengeRequest .addAuthHeader (response , authInfo );
85
+ logger .info ("Sending the challenge request " + challengeRequest );
86
+ challengeRequest .send ();
87
+ }
88
+ } else {
89
+ super .doErrorResponse (response );
90
+ }
91
+
92
+ }
93
+
94
+ @ Override
95
+ protected void doSuccessResponse (SipServletResponse resp )
96
+ throws ServletException , IOException {
97
+
98
+ logger .info ("GOT SUCESS RESPONSE HURRAH ! : " + resp );
99
+ }
100
+
101
+ @ Override
102
+ protected void doMessage (SipServletRequest req ) throws ServletException ,
103
+ IOException {
104
+ req .createResponse (200 ).send ();
105
+ }
106
+
107
+ /*
108
+ * (non-Javadoc)
109
+ * @see javax.servlet.sip.SipServletListener#servletInitialized(javax.servlet.sip.SipServletContextEvent)
110
+ */
111
+ public void servletInitialized (SipServletContextEvent ce ) {
112
+ // ServletContext servletContext = ce.getServletContext();
113
+ // SipFactory sipFactory = (SipFactory) servletContext.getAttribute(SIP_FACTORY);
114
+ // String userName = servletContext.getInitParameter("user.name");
115
+ // String domainName = servletContext.getInitParameter("domain.name");
116
+ //
117
+ // SipApplicationSession sipApplicationSession = sipFactory.createApplicationSession();
118
+ // // for REGISTER the from and the to headers are one and the same
119
+ // SipURI fromToURI = sipFactory.createSipURI(userName, domainName);
120
+ // SipServletRequest sipServletRequest =
121
+ // sipFactory.createRequest(sipApplicationSession, "REGISTER", fromToURI, fromToURI);
122
+ // sipServletRequest.setHeader("Expires", "3600");
123
+ // sipServletRequest.setHeader("User-Agent", "MobicentsSipServlets");
124
+ // SipURI requestURI = sipFactory.createSipURI(null, domainName);
125
+ // try {
126
+ // Parameterable parameterable = sipServletRequest.getParameterableHeader("Contact");
127
+ // parameterable.setParameter("expires", "0");
128
+ // } catch (ServletParseException e1) {
129
+ // logger.error("Impossible to set the expires on the contact header",e1);
130
+ // }
131
+ // try {
132
+ // sipServletRequest.setRequestURI(requestURI);
133
+ // sipServletRequest.send();
134
+ // } catch (IOException e) {
135
+ // logger.error("An unexpected exception occured while sending the REGISTER request",e);
136
+ // }
137
+ }
138
+
139
+ }
0 commit comments