Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* $Id: EchoServlet.java,v 1.5 2003/06/22 12:32:15 fukuda Exp $
- */
- package org.mobicents.servlet.sip.example;
- import java.util.*;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import javax.servlet.sip.Proxy;
- import javax.servlet.sip.URI;
- import javax.servlet.sip.SipFactory;
- import sun.font.CreatedFontTracker;
- import javax.servlet.sip.SipServlet;
- import javax.servlet.sip.SipServletRequest;
- import javax.servlet.sip.SipServletResponse;
- import javax.servlet.sip.SipURI;
- import javax.servlet.ServletException;
- /**
- */
- public class Redirect extends SipServlet {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- static private Map<String, String> Binding;
- static private ArrayList<String> Colaborator;
- static private ArrayList<URI> ColaboradorURI;
- public Redirect() {
- super();
- Binding = new HashMap<String,String>();
- Colaborator= new ArrayList<>();
- ColaboradorURI = new ArrayList<>();
- System.out.println("INICIOUuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");
- }
- /**
- * Acts as a registrar and location service for REGISTER messages
- * @param request The SIP message received by the AS
- */
- protected void doRegister(SipServletRequest request) throws ServletException,
- IOException {
- String aor = getSIPuri(request.getHeader("To"));
- String contact = getSIPuriPort(request.getHeader("Contact"));
- int expires=Integer.parseInt(getString("expires=","", request.getHeader("Contact")));
- if(expires==0){
- String aux = Binding.remove(aor);
- if (aux !=null){
- send(200,request);
- System.out.println("ADEUS" + " "+ aor);
- } else {
- send(400, request);
- }
- }else{
- if (aor.contains("@acme.pt") && !Binding.containsKey(aor)){
- Binding.put(aor, contact);
- System.out.println("OLA "+ aor + " "+getString("expires","", request.getHeader("Contact"))+ " AQUIIIIIIII");
- send(200, request);
- } else{
- send (403, request);
- }
- }
- // Some logs to show the content of the Registrar database.
- log("REGISTER:***");
- Iterator<Map.Entry<String,String>> it = Binding.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<String,String> pairs = (Map.Entry<String,String>)it.next();
- System.out.println(pairs.getKey() + " = " + pairs.getValue());
- }
- log("REGISTER:***");
- }
- //
- /**
- * Sends SIP replies to INVITE messages
- * - 300 if registred
- * - 404 if not registred
- * @param request The SIP message received by the AS
- * @throws IOException
- * @throws UnsupportedEncodingException
- */
- @Override
- protected void doMessage(SipServletRequest request) throws
- ServletException, IOException {
- String sender = getSIPuri(request.getHeader("From"));
- String message = request.getContent().toString();
- String newAor= getSIPuri(message);
- boolean senderConditions= sender.contains("gestor") && !newAor.equals(null);
- System.out.println("MENSAGEMMMMMMM "+ message+" "+Colaborator.contains(newAor) + " "+getString("sip", "@", newAor).contains("colaborador")+" "+Binding.containsKey(newAor) + " "+ newAor );
- if(senderConditions){
- if(message.contains("ADD") && !Colaborator.contains(newAor) && getString("sip", "@", newAor).contains("colaborador") && Binding.containsKey(newAor)){
- Colaborator.add(newAor);
- send(200,request);
- System.out.println("ADDED "+ Colaborator.get(Colaborator.size()-1));
- }else{
- if (message.contains("REMOVE") && Colaborator.contains( newAor)){
- Colaborator.remove(newAor);
- System.out.println("REMOVED");
- send(200,request);
- } else{
- send(403,request);}
- }
- }else
- send(401,request);
- }
- protected void doInvite(SipServletRequest request)
- throws ServletException, IOException {
- Proxy p = request.getProxy();
- SipFactory sipFactory = (SipFactory)getServletContext().getAttribute(SIP_FACTORY);
- URI uri;
- ColaboradorURI.clear();
- for (String aorList : Colaborator) {
- uri = sipFactory.createURI(Binding.get(aorList));
- if(!ColaboradorURI.contains(uri)){
- ColaboradorURI.add(uri);
- }
- }
- if(!ColaboradorURI.isEmpty()){
- p.proxyTo(ColaboradorURI);
- }else{
- uri = sipFactory.createURI("sip:[email protected]:5080");
- p.proxyTo(uri);
- }
- log("INVITE:***");
- Iterator<Map.Entry<String,String>> it = Binding.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry<String,String> pairs = (Map.Entry<String,String>)it.next();
- System.out.println(pairs.getKey() + " = " + pairs.getValue());
- }
- log("INVITE:***");
- String aor = getSIPuri(request.getHeader("To"));
- if (!Binding.containsKey(aor)) {
- SipServletResponse response1;
- response1 = request.createResponse(404);
- } else {
- }
- }
- /**
- * Auxiliary function for extracting SPI URIs
- * @param uri A URI with optional extra attributes
- * @return SIP URI
- */
- protected String getSIPuri(String uri) {
- String f = uri.substring(uri.indexOf("<")+1, uri.indexOf(">"));
- int indexCollon = f.indexOf(":", f.indexOf("@"));
- if (indexCollon != -1) {
- f = f.substring(0,indexCollon);
- }
- return f;
- }
- protected String getService(String uri) {
- String f = uri.substring(uri.indexOf(":")+1, uri.indexOf("@"));
- return f;
- }
- /**
- * Auxiliary function for extracting SPI URIs
- * @param uri A URI with optional extra attributes
- * @return SIP URI and port
- */
- protected String getSIPuriPort(String uri) {
- String f = uri.substring(uri.indexOf("<")+1, uri.indexOf(">"));
- return f;
- }
- protected void send(int i, SipServletRequest request) throws IOException{
- SipServletResponse response = request.createResponse(i);
- response.send();
- }
- private String getString(String init, String end,String s){
- return s.substring(s.indexOf(init)+ init.length(), s.lastIndexOf(end));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment