Guest User

Untitled

a guest
Dec 7th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. /* $Id: EchoServlet.java,v 1.5 2003/06/22 12:32:15 fukuda Exp $
  2. */
  3. package org.mobicents.servlet.sip.example;
  4. import java.util.*;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import javax.servlet.sip.Proxy;
  8. import javax.servlet.sip.URI;
  9. import javax.servlet.sip.SipFactory;
  10.  
  11. import sun.font.CreatedFontTracker;
  12.  
  13. import javax.servlet.sip.SipServlet;
  14. import javax.servlet.sip.SipServletRequest;
  15. import javax.servlet.sip.SipServletResponse;
  16. import javax.servlet.sip.SipURI;
  17. import javax.servlet.ServletException;
  18.  
  19. /**
  20. */
  21. public class Redirect extends SipServlet {
  22.  
  23. /**
  24. *
  25. */
  26. private static final long serialVersionUID = 1L;
  27. static private Map<String, String> Binding;
  28. static private ArrayList<String> Colaborator;
  29. static private ArrayList<URI> ColaboradorURI;
  30.  
  31. public Redirect() {
  32. super();
  33. Binding = new HashMap<String,String>();
  34. Colaborator= new ArrayList<>();
  35. ColaboradorURI = new ArrayList<>();
  36. System.out.println("INICIOUuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");
  37. }
  38.  
  39. /**
  40. * Acts as a registrar and location service for REGISTER messages
  41. * @param request The SIP message received by the AS
  42. */
  43. protected void doRegister(SipServletRequest request) throws ServletException,
  44. IOException {
  45. String aor = getSIPuri(request.getHeader("To"));
  46. String contact = getSIPuriPort(request.getHeader("Contact"));
  47. int expires=Integer.parseInt(getString("expires=","", request.getHeader("Contact")));
  48. if(expires==0){
  49. String aux = Binding.remove(aor);
  50. if (aux !=null){
  51. send(200,request);
  52. System.out.println("ADEUS" + " "+ aor);
  53. } else {
  54. send(400, request);
  55. }
  56. }else{
  57. if (aor.contains("@acme.pt") && !Binding.containsKey(aor)){
  58. Binding.put(aor, contact);
  59. System.out.println("OLA "+ aor + " "+getString("expires","", request.getHeader("Contact"))+ " AQUIIIIIIII");
  60. send(200, request);
  61. } else{
  62. send (403, request);
  63. }
  64. }
  65. // Some logs to show the content of the Registrar database.
  66. log("REGISTER:***");
  67. Iterator<Map.Entry<String,String>> it = Binding.entrySet().iterator();
  68. while (it.hasNext()) {
  69. Map.Entry<String,String> pairs = (Map.Entry<String,String>)it.next();
  70. System.out.println(pairs.getKey() + " = " + pairs.getValue());
  71. }
  72. log("REGISTER:***");
  73. }
  74. //
  75. /**
  76. * Sends SIP replies to INVITE messages
  77. * - 300 if registred
  78. * - 404 if not registred
  79. * @param request The SIP message received by the AS
  80. * @throws IOException
  81. * @throws UnsupportedEncodingException
  82. */
  83.  
  84.  
  85. @Override
  86. protected void doMessage(SipServletRequest request) throws
  87. ServletException, IOException {
  88. String sender = getSIPuri(request.getHeader("From"));
  89. String message = request.getContent().toString();
  90. String newAor= getSIPuri(message);
  91. boolean senderConditions= sender.contains("gestor") && !newAor.equals(null);
  92. System.out.println("MENSAGEMMMMMMM "+ message+" "+Colaborator.contains(newAor) + " "+getString("sip", "@", newAor).contains("colaborador")+" "+Binding.containsKey(newAor) + " "+ newAor );
  93. if(senderConditions){
  94. if(message.contains("ADD") && !Colaborator.contains(newAor) && getString("sip", "@", newAor).contains("colaborador") && Binding.containsKey(newAor)){
  95. Colaborator.add(newAor);
  96. send(200,request);
  97. System.out.println("ADDED "+ Colaborator.get(Colaborator.size()-1));
  98. }else{
  99. if (message.contains("REMOVE") && Colaborator.contains( newAor)){
  100. Colaborator.remove(newAor);
  101. System.out.println("REMOVED");
  102. send(200,request);
  103. } else{
  104. send(403,request);}
  105. }
  106. }else
  107. send(401,request);
  108. }
  109.  
  110.  
  111.  
  112.  
  113. protected void doInvite(SipServletRequest request)
  114. throws ServletException, IOException {
  115.  
  116. Proxy p = request.getProxy();
  117. SipFactory sipFactory = (SipFactory)getServletContext().getAttribute(SIP_FACTORY);
  118. URI uri;
  119. ColaboradorURI.clear();
  120. for (String aorList : Colaborator) {
  121. uri = sipFactory.createURI(Binding.get(aorList));
  122. if(!ColaboradorURI.contains(uri)){
  123. ColaboradorURI.add(uri);
  124. }
  125. }
  126. if(!ColaboradorURI.isEmpty()){
  127. p.proxyTo(ColaboradorURI);
  128. }else{
  129. uri = sipFactory.createURI("sip:[email protected]:5080");
  130. p.proxyTo(uri);
  131. }
  132.  
  133.  
  134. log("INVITE:***");
  135. Iterator<Map.Entry<String,String>> it = Binding.entrySet().iterator();
  136. while (it.hasNext()) {
  137. Map.Entry<String,String> pairs = (Map.Entry<String,String>)it.next();
  138. System.out.println(pairs.getKey() + " = " + pairs.getValue());
  139. }
  140. log("INVITE:***");
  141.  
  142. String aor = getSIPuri(request.getHeader("To"));
  143. if (!Binding.containsKey(aor)) {
  144. SipServletResponse response1;
  145. response1 = request.createResponse(404);
  146. } else {
  147.  
  148.  
  149. }
  150. }
  151.  
  152. /**
  153. * Auxiliary function for extracting SPI URIs
  154. * @param uri A URI with optional extra attributes
  155. * @return SIP URI
  156. */
  157.  
  158. protected String getSIPuri(String uri) {
  159. String f = uri.substring(uri.indexOf("<")+1, uri.indexOf(">"));
  160. int indexCollon = f.indexOf(":", f.indexOf("@"));
  161. if (indexCollon != -1) {
  162. f = f.substring(0,indexCollon);
  163. }
  164. return f;
  165. }
  166.  
  167.  
  168. protected String getService(String uri) {
  169. String f = uri.substring(uri.indexOf(":")+1, uri.indexOf("@"));
  170. return f;
  171. }
  172.  
  173. /**
  174. * Auxiliary function for extracting SPI URIs
  175. * @param uri A URI with optional extra attributes
  176. * @return SIP URI and port
  177. */
  178. protected String getSIPuriPort(String uri) {
  179. String f = uri.substring(uri.indexOf("<")+1, uri.indexOf(">"));
  180. return f;
  181. }
  182.  
  183. protected void send(int i, SipServletRequest request) throws IOException{
  184. SipServletResponse response = request.createResponse(i);
  185. response.send();
  186. }
  187.  
  188. private String getString(String init, String end,String s){
  189. return s.substring(s.indexOf(init)+ init.length(), s.lastIndexOf(end));
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment