📄 proxy.java
字号:
package gov.nist.sip.proxy;import java.util.*;import javax.sip.*;import javax.sip.message.*;import javax.sip.header.*;import javax.sip.address.*;import gov.nist.sip.proxy.registrar.*;import java.text.ParseException;import java.io.IOException;import gov.nist.sip.proxy.authentication.*;import gov.nist.sip.proxy.presenceserver.*;import gov.nist.sip.proxy.router.*;//ifdef SIMULATION/*import sim.java.net.*;//endif*//** Proxy Entry point. * *@version JAIN-SIP-1.1 * *@author Olivier Deruelle <deruelle@nist.gov> * M. Ranganathan <mranga@nist.gov> (convert to simulation) <br/> * Henrik Leion: Some changes in how SUBSCRIBE and NOTIFY are processed. <br/> *<a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a> * */public class Proxy implements SipListener { protected LinkedList listeningPoints; // Map the server transactions with the client transactions protected SipStack sipStack; protected SipProvider defaultProvider; protected MessageFactory messageFactory; protected HeaderFactory headerFactory; protected AddressFactory addressFactory; protected Configuration configuration; protected PresenceServer presenceServer; protected Registrar registrar; protected ProxyUtilities proxyUtilities; protected Authentication authentication; protected RequestForwarding requestForwarding; protected ResponseForwarding responseForwarding; public RequestForwarding getRequestForwarding() { return requestForwarding; } public ResponseForwarding getResponseForwarding() { return responseForwarding; } public AddressFactory getAddressFactory() { return addressFactory; } public MessageFactory getMessageFactory() { return messageFactory; } public HeaderFactory getHeaderFactory() { return headerFactory; } public Registrar getRegistrar() { return registrar; } public boolean isPresenceServer() { return configuration.enablePresenceServer; } public PresenceServer getPresenceServer() { return presenceServer; } public ProxyUtilities getProxyUtilities() { return proxyUtilities; } public SipStack getSipStack() { return sipStack; } public Configuration getConfiguration() { return configuration; } /** get the first allocated provider. */ public SipProvider getSipProvider() { return this.defaultProvider; } public Authentication getAuthentication() { return authentication; } public boolean managesDomain( String domainAddress ) { return configuration.hasDomain(domainAddress) || registrar.hasRegistration("sip:"+domainAddress); } /** Creates new Proxy */ public Proxy(String confFile) throws Exception{ this.listeningPoints = new LinkedList(); if (confFile==null) { System.out.println ("ERROR: Set the configuration file flag: " + "USE: -cf configuration_file_location.xml" ); } else { try { // First, let's parse the configuration file. ProxyConfigurationHandler handler= new ProxyConfigurationHandler(confFile); configuration=handler.getConfiguration(); if (configuration==null || !configuration.isValidConfiguration()) { System.out.println ("ERROR: the configuration file is not correct!"+ " Correct the errors first."); throw new Exception ("ERROR: the configuration file is not correct!"+ " Correct the errors first."); } else { proxyUtilities=new ProxyUtilities(this); presenceServer=new PresenceServer(this); registrar=new Registrar(this); requestForwarding=new RequestForwarding(this); responseForwarding=new ResponseForwarding(this); } } catch (Exception ex) { System.out.println ("ERROR: exception raised while initializing the proxy"); ex.printStackTrace(); throw new Exception ("ERROR: exception raised while initializing the proxy"); } } } /** This is a listener method. */ public void processRequest(RequestEvent requestEvent) { Request request = requestEvent.getRequest(); SipProvider sipProvider = (SipProvider) requestEvent.getSource(); ServerTransaction serverTransaction=requestEvent.getServerTransaction(); try { if (ProxyDebug.debug) ProxyDebug.println ("\n****************************************************"+ "\nRequest " + request.getMethod() + " received:\n"+request.toString()); if (ProxyDebug.debug) ProxyUtilities.printTransaction(serverTransaction); /*******************************************************************************//*********************** PROXY BEHAVIOR ************************************//*******************************************************************************/ /* RFC 3261: 16.2: * For all new requests, including any with unknown methods, an element * intending to proxy the request MUST: * * 1. Validate the request (Section 16.3) * * 2. Preprocess routing information (Section 16.4) * * 3. Determine target(s) for the request (Section 16.5) * * 4. Forward the request to each target (Section 16.6) * * 5. Process all responses (Section 16.7) */ /*******************************************************************************//***************************** 1. Validate the request (Section 16.3) **********//*******************************************************************************/ /* Before an element can proxy a request, it MUST verify the message's validity */ RequestValidation requestValidation=new RequestValidation(this); if ( !requestValidation.validateRequest (sipProvider,request,serverTransaction) ) { // An appropriate response has been sent back by the request // validation step, so we just return. The request has been // processed! if (ProxyDebug.debug) ProxyDebug.println ("Proxy, processRequest(), the request has not been"+ " validated, so the request is discarded " + " (an error code has normally been"+ " sent back)"); return; } // Let's check if the ACK is for the proxy: if there is no Route // header: it is mandatory for the ACK to be forwarded if ( request.getMethod().equals(Request.ACK) ) { ListIterator routes = request.getHeaders(RouteHeader.NAME); if (routes==null || !routes.hasNext()) { if (ProxyDebug.debug) ProxyDebug.println("Proxy, processRequest(), "+ "the request is an ACK"+ " targeted for the proxy, we ignore it"); return; } } if (serverTransaction==null) { String method=request.getMethod(); // Methods that creates dialogs, so that can // generate transactions if ( method.equals(Request.INVITE) || method.equals(Request.SUBSCRIBE) ) { try{ serverTransaction= sipProvider.getNewServerTransaction(request); TransactionsMapping transactionsMapping= (TransactionsMapping) serverTransaction.getDialog().getApplicationData(); if (transactionsMapping == null) { transactionsMapping = new TransactionsMapping(serverTransaction); } } catch(TransactionAlreadyExistsException e) { if (ProxyDebug.debug) ProxyDebug.println ("Proxy, processRequest(), this request"+ " is a retransmission, we drop it!"); } } } /***************************************************************************//****** 2. Preprocess routing information (Section 16.4) *******************//***************************************************************************/ /* The proxy MUST inspect the Request-URI of the request. If the Request-URI of the request contains a value this proxy previously placed into a Record-Route header field (see Section 16.6 item 4), the proxy MUST replace the Request-URI in the request with the last value from the Route header field, and remove that value from the Route header field. The proxy MUST then proceed as if it received this modified request. ..... (idem to below:) 16.12. The proxy will inspect the URI in the topmost Route header field value. If it indicates this proxy, the proxy removes it from the Route header field (this route node has been reached). */ ListIterator routes = request.getHeaders(RouteHeader.NAME); if (routes!=null) { if ( routes.hasNext() ) { RouteHeader routeHeader = (RouteHeader) routes.next(); Address routeAddress=routeHeader.getAddress(); SipURI routeSipURI=(SipURI)routeAddress.getURI(); String host = routeSipURI.getHost(); int port = routeSipURI.getPort(); if (sipStack.getIPAddress().equals(host) ) { Iterator lps=sipStack.getListeningPoints(); while(lps!=null && lps.hasNext()) { ListeningPoint lp=(ListeningPoint)lps.next(); if (lp.getPort()==port) { if (ProxyDebug.debug) ProxyDebug.println ("Proxy, processRequest(),"+ " we remove the first route form " + " the RouteHeader;"+
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -