📄 registrar.java
字号:
else sipProvider.sendResponse(response); if (GatewayDebug.debug) { GatewayDebug.println ("Registrar, processRegister(), response sent:"); GatewayDebug.print(response.toString()); } } } catch(Exception ex) { if (GatewayDebug.debug) { GatewayDebug.println("Registrar, processRegister(), internal error, "+ "exception raised:"); GatewayDebug.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 (GatewayDebug.debug) { GatewayDebug.println("Registrar, hasDomainRegistered(), internal error, "+ "exception raised:"); GatewayDebug.logException(ex); } return null; } } public boolean hasRegistration(String key) { return registrationsTable.hasRegistration(key); } public boolean hasRegistration(Request request) { try{ String key = getKey(request); // Order: if user registered, forward to this one; otherwise, send to the // pstn gateway... if (hasRegistration(key)) return true; /***************** pstn gateway *************************************************/ Configuration configuration=gateway.getConfiguration(); System.out.println("configuration.pstnGateway:"+configuration.pstnGateway); if ( configuration.pstnGateway!=null && !configuration.pstnGateway.trim().equals("")) { if (key.startsWith("sip:+") && !request.getMethod().equals("REGISTER") ) { GatewayDebug.println ("RegistrationsTable, hasRegistration(), Checking registration for \"" +key.toLowerCase()+"\" : registered for the pstn Gateway"); return true; } } /*********************************************************************************/ return false; } catch (Exception ex) { if (GatewayDebug.debug) { GatewayDebug.println("Registrar, hasRegistration(), internal error, "+ "exception raised:"); GatewayDebug.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) {/***************** PSTN gateway *************************************************/ Configuration configuration=gateway.getConfiguration(); if ( configuration.pstnGateway!=null ) { if (key.startsWith("sip:+") ) { GatewayDebug.println ("Registrar, getContactsURI(), pstn gateway uri..."); URI reqURI=request.getRequestURI(); SipURI reqSipURI=(SipURI)reqURI; SipURI reqSipURICloned=(SipURI)reqSipURI.clone(); reqSipURICloned.setHost(configuration.pstnGateway); reqSipURICloned.setPort(-1); //AddressFactory addressFactory=Gateway.getAddressFactory(); // SipURI sipURI=addressFactory.createSipURI(null,"129.6.55.81"); Vector res=new Vector(); res.addElement(reqSipURICloned); return res; } } /*********************************************************************************/ 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 (GatewayDebug.debug) { GatewayDebug.println ("Registrar, getContactsURI(), internal error, exception raised:"); GatewayDebug.logException(ex); } return null; } } /* * The result is a list of URI that we kept from a registration related * to the ToHeader URI from this request. */ public Vector getContactsURI(String sipURI) { try{ String key=sipURI; Vector contacts=getContactHeaders(key); 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 (GatewayDebug.debug) { GatewayDebug.println ("Registrar, getContactsURI(), internal error, exception raised:"); GatewayDebug.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; } public boolean hasExpiresZero(Request request) { try{ ExpiresHeader expiresHeader= (ExpiresHeader)request.getHeader(ExpiresHeader.NAME); if (expiresHeader==null) { GatewayDebug.println ("Registrar, hasExpiresZero(), the REGISTER does not have an Expires Header"); return false; } else { GatewayDebug.println ("Registrar, hasExpiresZero(), the REGISTER has an Expires Header with"+ " expires time:" +expiresHeader.getExpires()); return expiresHeader.getExpires()==0; } } catch(Exception e){ if (GatewayDebug.debug) { GatewayDebug.println ("Registrar, hasExpiresZero(), internal error, exception raised:"); GatewayDebug.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" parame ter return contacts; } catch(Exception e){ if (GatewayDebug.debug) { GatewayDebug.println ("Registrar, getContactHeaders(), internal error, exception raised:"); GatewayDebug.logException(e); } return contacts; } } protected void printRegistrations(){ registrationsTable.printRegistrations(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -