📄 registrar.java
字号:
Response response= messageFactory.createResponse(Response.OK,request); try{ if ( hasExpiresZero(request) ) { response.addHeader(request.getHeader(ExpiresHeader.NAME)); } } catch(Exception e){ e.printStackTrace(); } if ( contactHeaders!=null ) { for (int i = 0; i < contactHeaders.size(); i++) { ContactHeader contact = (ContactHeader) contactHeaders.elementAt(i); response.addHeader(contact); } } if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (ProxyDebug.debug) { ProxyDebug.println ("Registrar, processRegister(), response sent:"); ProxyDebug.print(response.toString()); } } else { // Let's check the Expires header: if ( hasExpiresZero(request) ) { // This message is lost.... proxy.getPresenceServer(); ProxyDebug.println("Registrar, processRegister(), "+ "we don't have any record for this REGISTER."); Response response=messageFactory.createResponse (Response.OK,request); if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (ProxyDebug.debug) { ProxyDebug.println ("Registrar, processRegister(), response sent:"); ProxyDebug.print(response.toString()); } return; } registrationsTable.addRegistration(key,request); if (proxy.getConfiguration().rfc2543Compatible && key.indexOf(":5060") < 0) { // // Hack for Cisco IP Phone which registers incorrectly // by not specifying :5060. // key += ":5060"; System.out.println("CISCO IP PHONE FIX: " + "adding proper registration for " + key); registrationsTable.addRegistration(key, request); } // we have to forward SUBSCRIBE if the presence server // is enabled: if (proxy.isPresenceServer()) { PresenceServer presenceServer= proxy.getPresenceServer(); ProxyDebug.println("Registrar, processRegister(), "+ " we have to check if we have some SUBSCRIBE stored."); } Vector contactHeaders=getContactHeaders(key); Response response= messageFactory.createResponse(Response.OK,request); if ( contactHeaders!=null ) { for (int i = 0; i < contactHeaders.size(); i++) { ContactHeader contact = (ContactHeader) contactHeaders.elementAt(i); response.addHeader(contact); } } if (serverTransaction!=null) serverTransaction.sendResponse(response); else sipProvider.sendResponse(response); if (ProxyDebug.debug) { ProxyDebug.println ("Registrar, processRegister(), response sent:"); ProxyDebug.print(response.toString()); } } } catch (IOException ex) { if (ProxyDebug.debug) { ProxyDebug.println("Registrar exception raised:"); ProxyDebug.logException(ex); } } catch (SipException ex) { if (ProxyDebug.debug) { ProxyDebug.println("Registrar exception raised:"); ProxyDebug.logException(ex); } } catch(Exception ex) { if (ProxyDebug.debug) { ProxyDebug.println ("Registrar, processRegister(), internal error, "+ "exception raised:"); ProxyDebug.logException(ex); } } } public static URI getCleanUri(URI uri) { if (uri instanceof SipURI) { SipURI sipURI=(SipURI)uri.clone(); Iterator iterator=sipURI.getParameterNames(); while (iterator!=null && iterator.hasNext()) { String name=(String)iterator.next(); sipURI.removeParameter(name); } return sipURI; } else return uri; } /** The key is built following this rule: * The registrar extracts the address-of-record from the To header * field of the request. The URI * MUST then be converted to a canonical form. To do that, all * URI parameters MUST be removed (including the user-param), and * any escaped characters MUST be converted to their unescaped * form. The result serves as an index into the list of bindings */ public String getKey(Request request) { // Let's see if we already have a binding for this request: try{ ToHeader toHeader=(ToHeader)request.getHeader(ToHeader.NAME); Address address=null; address = toHeader.getAddress(); javax.sip.address.URI cleanedUri; if (address==null) { cleanedUri= getCleanUri(request.getRequestURI()); } else { // We have to build the key, all // URI parameters MUST be removed: cleanedUri = getCleanUri(address.getURI()); } String keyresult=cleanedUri.toString(); return keyresult.toLowerCase(); } catch(Exception ex) { if (ProxyDebug.debug) { ProxyDebug.println("Registrar, hasDomainRegistered(), internal error, "+ "exception raised:"); ProxyDebug.logException(ex); } return null; } } public boolean hasRegistration(String key) { return registrationsTable.hasRegistration(key); } public boolean hasDomainRegistered(Request request) { try{ URI uri=request.getRequestURI(); URI cleanedURI=getCleanUri(uri); if (! (cleanedURI instanceof SipURI) ) return false; // We have to check the host part: String host=((SipURI)cleanedURI).getHost(); return hasRegistration("sip:"+host ); } catch (Exception ex) { if (ProxyDebug.debug) { ProxyDebug.println("Registrar, hasDomainRegistered(), internal error, "+ "exception raised:"); ProxyDebug.logException(ex); } return false; } } public boolean hasDomainRegistered(URI uri) { try{ URI cleanedURI=getCleanUri(uri); if (! (cleanedURI instanceof SipURI) ) return false; // We have to check the host part: String host=((SipURI)cleanedURI).getHost(); return hasRegistration("sip:"+host ); } catch (Exception ex) { if (ProxyDebug.debug) { ProxyDebug.println("Registrar, hasDomainRegistered(), internal error, "+ "exception raised:"); ProxyDebug.logException(ex); } return false; } } public Vector getDomainContactsURI(Request request) { try{ URI uri=request.getRequestURI(); URI cleanedURI=getCleanUri(uri); if (! (cleanedURI instanceof SipURI) ) return null; // We have to check the host part: String host=((SipURI)cleanedURI).getHost(); Vector contacts=getContactHeaders("sip:"+ host ); if (contacts==null) return null; Vector results=new Vector(); for (int i=0;i<contacts.size();i++) { ContactHeader contact = (ContactHeader) contacts.elementAt(i); Address address=contact.getAddress(); uri=address.getURI(); cleanedURI=getCleanUri(uri); results.addElement(cleanedURI); } return results; } catch (Exception ex) { if (ProxyDebug.debug) { ProxyDebug.println("Registrar, getDomainContacts(), internal error, "+ "exception raised:"); ProxyDebug.logException(ex); } return null; } } public boolean hasRegistration(Request request) { try{ String key = getKey(request); return hasRegistration(key); } catch (Exception ex) { if (ProxyDebug.debug) { ProxyDebug.println("Registrar, hasRegistration(), internal error, "+ "exception raised:"); ProxyDebug.logException(ex); } return false; } } /* * The result is a list of URI that we kept from a registration related * to the ToHeader URI from this request. */ public Vector getContactsURI(Request request) { try{ String key=getKey(request); Vector contacts=getContactHeaders(key); if (contacts==null) return null; Vector results=new Vector(); for (int i=0;i<contacts.size();i++) { ContactHeader contact = (ContactHeader) contacts.elementAt(i); Address address=contact.getAddress(); URI uri=address.getURI(); URI cleanedURI=getCleanUri(uri); results.addElement(cleanedURI); } return results; } catch (Exception ex) { if (ProxyDebug.debug) { ProxyDebug.println ("Registrar, getContactsURI(), internal error, exception raised:"); ProxyDebug.logException(ex); } return null; } } /* * Matches a Sip URI "sip:user@domain" with a list of Contacts * @param key The sip URI found in the To-header of a request * @author Henrik Leion */ public Vector getContactsURI(String key) { try{ Vector contacts=getContactHeaders(key); if (contacts==null) return null; Vector results=new Vector(); for (int i=0;i<contacts.size();i++) { ContactHeader contact = (ContactHeader) contacts.elementAt(i); Address address=contact.getAddress(); URI uri=address.getURI(); URI cleanedURI=getCleanUri(uri); results.addElement(cleanedURI); } return results; } catch (Exception ex) { if (ProxyDebug.debug) { ProxyDebug.println ("Registrar, getContactsURI(), internal error, exception raised:"); ProxyDebug.logException(ex); } return null; } } public boolean hasContactHeaders(Request request) { ListIterator list=(ListIterator)request.getHeaders(ContactHeader.NAME); return list!=null; } private boolean hasStar(Request request) throws Exception{ ListIterator list=(ListIterator)request.getHeaders(ContactHeader.NAME); if (list==null) return false; while( list.hasNext() ) { ContactHeader contactHeader=(ContactHeader)list.next(); if (contactHeader.getAddress().isWildcard() ) { return true; } } return false; } private boolean hasExpiresZero(Request request) { try{ ExpiresHeader expiresHeader= (ExpiresHeader)request.getHeader(ExpiresHeader.NAME); if (expiresHeader==null) { ProxyDebug.println ("Registrar, hasExpiresZero(), the REGISTER does not have an Expires Header"); return false; } else { ProxyDebug.println ("Registrar, hasExpiresZero(), the REGISTER has an Expires Header with"+ " expires time:" +expiresHeader.getExpires()); return expiresHeader.getExpires()==0; } } catch(Exception e){ if (ProxyDebug.debug) { ProxyDebug.println ("Registrar, hasExpiresZero(), internal error, exception raised:"); ProxyDebug.logException(e); } return false; } } public Vector getContactHeaders(String key) { return registrationsTable.getContactHeaders(key); } public static Vector getContactHeaders(Request request){ Vector contacts =new Vector(); try{ ListIterator list= (ListIterator)request.getHeaders(ContactHeader.NAME); if (list==null) return contacts; while( list.hasNext() ) { ContactHeader contactHeader=(ContactHeader)list.next(); contacts.addElement(contactHeader); } // We will sort out the contacts following the "q" parameter return contacts; } catch(Exception e){ if (ProxyDebug.debug) { ProxyDebug.println ("Registrar, getContactHeaders(), internal error, exception raised:"); ProxyDebug.logException(e); } return contacts; } } protected void printRegistrations(){ registrationsTable.printRegistrations(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -