/*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.mobicents.servlet.sip.example;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.sip.Address;
import javax.servlet.sip.AuthInfo;
import javax.servlet.sip.SipApplicationSession;
import javax.servlet.sip.SipErrorEvent;
import javax.servlet.sip.SipErrorListener;
import javax.servlet.sip.SipFactory;
import javax.servlet.sip.SipServlet;
import javax.servlet.sip.SipServletRequest;
import javax.servlet.sip.SipServletResponse;
import javax.servlet.sip.SipSession;
import org.apache.log4j.Logger;
public class Sip3pccHandlerServlet extends SipServlet implements SipErrorListener,
Servlet {
private static Logger logger = Logger.getLogger(Sip3pccHandlerServlet.class);
private static final String CONTACT_HEADER = "Contact";
private SipFactory sipFactory;
public Sip3pccHandlerServlet() {
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
logger.info("the simple sip servlet has been started");
try {
// Getting the Sip factory from the JNDI Context
Properties jndiProps = new Properties();
Context initCtx = new InitialContext(jndiProps);
Context envCtx = (Context) initCtx.lookup("java:comp/env");
sipFactory = (SipFactory) envCtx.lookup("sip/FacebookClickToCallSip/SipFactory");
logger.info("Sip Factory ref from JNDI : " + sipFactory);
} catch (NamingException e) {
throw new ServletException("Uh oh -- JNDI problem !", e);
}
}
@Override
protected void doInvite(SipServletRequest req) throws ServletException,
IOException {
logger.info("Click2Dial don't handle INVITE. Here's the one we got : " + req.toString());
}
@Override
protected void doOptions(SipServletRequest req) throws ServletException,
IOException {
logger.info("Got : " + req.toString());
req.createResponse(SipServletResponse.SC_OK).send();
}
@Override
protected void doSuccessResponse(SipServletResponse resp)
throws ServletException, IOException {
logger.info("Got OK");
SipSession session = resp.getSession();
if (resp.getStatus() == SipServletResponse.SC_OK) {
Boolean inviteSent = (Boolean) session.getAttribute("InviteSent");
if (inviteSent != null && inviteSent.booleanValue()) {
return;
}
if(resp.getSession().getAttribute("OK") != null) {
return;
}
resp.getSession().setAttribute("OK", new Boolean(true));
Address secondPartyAddress = (Address) resp.getSession()
.getAttribute("SecondPartyAddress");
String fromAddr = "sip:"+getServletContext().getInitParameter("user2")+"@"+getServletContext().getInitParameter("domain.name");
Address fromAddress = sipFactory.createAddress(fromAddr);
if (secondPartyAddress != null) {
SipApplicationSession newCallId = sipFactory.createApplicationSession();
SipServletRequest invite = sipFactory.createRequest(newCallId, "INVITE", fromAddress, secondPartyAddress);
logger.info("Found second party -- sending INVITE to "
+ secondPartyAddress);
String contentType = resp.getContentType();
if (contentType.trim().equals("application/sdp")) {
invite.setContent(resp.getContent(), "application/sdp");
}
invite.getSession().setAttribute("user", getServletContext().getInitParameter("user2"));
invite.getSession().setAttribute("pass", getServletContext().getInitParameter("pass2"));
session.setAttribute("LinkedSession", invite.getSession());
invite.getSession().setAttribute("LinkedSession", session);
SipServletRequest ack = resp.createAck();
invite.getSession().setAttribute("FirstPartyAck", ack);
invite.getSession().setAttribute("FirstPartyContent", resp.getContent());
invite.send();
session.setAttribute("InviteSent", Boolean.TRUE);
} else {
System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
String cSeqValue = resp.getHeader("CSeq");
if(cSeqValue.indexOf("INVITE") != -1) {
logger.info("Got OK from second party -- sending ACK");
SipServletRequest secondPartyAck = resp.createAck();
SipServletRequest firstPartyAck = (SipServletRequest) resp
.getSession().getAttribute("FirstPartyAck");
// if (resp.getContentType() != null && resp.getContentType().equals("application/sdp")) {
firstPartyAck.setContent(resp.getContent(),
"application/sdp");
secondPartyAck.setContent(resp.getSession().getAttribute("FirstPartyContent"),
"application/sdp");
// }
firstPartyAck.send();
secondPartyAck.send();
}
}
}
}
@Override
protected void doBye(SipServletRequest request) throws ServletException,
IOException {
logger.info("Got bye");
SipSession session = request.getSession();
SipSession linkedSession = (SipSession) session
.getAttribute("LinkedSession");
if (linkedSession != null) {
SipServletRequest bye = linkedSession.createRequest("BYE");
logger.info("Sending bye to " + linkedSession.getRemoteParty());
bye.send();
}
SipServletResponse ok = request
.createResponse(SipServletResponse.SC_OK);
ok.send();
}
/**
* {@inheritDoc}
*/
@Override
protected void doResponse(SipServletResponse response)
throws ServletException, IOException {
logger.info("SimpleProxyServlet: Got response:\n" + response);
super.doResponse(response);
}
@Override
protected void doErrorResponse(SipServletResponse response)
throws ServletException, IOException {
logger.info("Got response: " + response);
SipFactory sipFactory = (SipFactory) getServletContext().getAttribute(SIP_FACTORY);
if(response.getStatus() == SipServletResponse.SC_UNAUTHORIZED ||
response.getStatus() == SipServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED) {
// Avoid re-sending if the auth repeatedly fails.
if(!"true".equals(response.getApplicationSession().getAttribute("FirstResponseRecieved")))
{
SipApplicationSession sipApplicationSession = response.getApplicationSession();
sipApplicationSession.setAttribute("FirstResponseRecieved", "true");
AuthInfo authInfo = sipFactory.createAuthInfo();
authInfo.addAuthInfo(
response.getStatus(),
response.getChallengeRealms().next(),
(String)response.getSession().getAttribute("user"),
(String)response.getSession().getAttribute("pass"));
SipServletRequest challengeRequest = response.getSession().createRequest(
response.getRequest().getMethod());
challengeRequest.addAuthHeader(response, authInfo);
logger.info("Sending the challenge request " + challengeRequest);
try {
logger.info("sending challenge request " + challengeRequest);
challengeRequest.send();
} catch (IOException e) {
logger.error("An unexpected exception occured while sending the request", e);
}
}
} else {
// If someone rejects it remove the call from the table
}
}
/**
* {@inheritDoc}
*/
public void noAckReceived(SipErrorEvent ee) {
logger.info("SimpleProxyServlet: Error: noAckReceived.");
}
/**
* {@inheritDoc}
*/
public void noPrackReceived(SipErrorEvent ee) {
logger.info("SimpleProxyServlet: Error: noPrackReceived.");
}
protected void doRegister(SipServletRequest req) throws ServletException, IOException {
logger.info("Received register request: " + req.getTo());
int response = SipServletResponse.SC_OK;
SipServletResponse resp = req.createResponse(response);
HashMap<String, String> users = (HashMap<String, String>) getServletContext().getAttribute("registeredUsersMap");
if(users == null) users = new HashMap<String, String>();
getServletContext().setAttribute("registeredUsersMap", users);
Address address = req.getAddressHeader(CONTACT_HEADER);
String fromURI = req.getFrom().getURI().toString();
int expires = address.getExpires();
if(expires < 0) {
expires = req.getExpires();
}
if(expires == 0) {
users.remove(fromURI);
logger.info("User " + fromURI + " unregistered");
} else {
resp.setAddressHeader(CONTACT_HEADER, address);
users.put(fromURI, address.getURI().toString());
logger.info("User " + fromURI +
" registered with an Expire time of " + expires);
}
resp.send();
}
}