⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proxy.java

📁 基于JAINSIP的一个proxy源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	}					       	            /*              If the target set for the request has not been predetermined as              described above, this implies that the element is responsible for the              domain in the Request-URI, and the element MAY use whatever mechanism              it desires to determine where to send the request.                ...              When accessing the location service constructed by a registrar, the               Request-URI MUST first be canonicalized as described in Section 10.3              before being used as an index.                */             if (requestURI.isSipURI()) {                SipURI requestSipURI=(SipURI)requestURI;                Iterator iterator=requestSipURI.getParameterNames();                if (ProxyDebug.debug)                    ProxyDebug.println("Proxy, processRequest(), we canonicalized"+                    " the request-URI");                while (iterator!=null && iterator.hasNext()) {                    String name=(String)iterator.next();                    requestSipURI.removeParameter(name);                }             }                             if ( registrar.hasRegistration(request)  ) {                               targetURIList=registrar.getContactsURI(request);                                // We fork only INVITE                if (targetURIList!=null && targetURIList.size()>1 			&& !request.getMethod().equals("INVITE") ) {                    if (ProxyDebug.debug)                        ProxyDebug.println			("Proxy, processRequest(), the request "+                        " to fork is not an INVITE, so we will process"+                        " it with the first target as the only target.");                    targetURI= (URI)targetURIList.firstElement();                    targetURIList=new Vector();                    targetURIList.addElement(targetURI);                    // 4. Forward the request statefully to the target:                    requestForwarding.forwardRequest(targetURIList,sipProvider,                    request,serverTransaction,true);                    return;                }                                if (targetURIList!=null && !targetURIList.isEmpty()) {                    if (ProxyDebug.debug)                        ProxyDebug.println			("Proxy, processRequest(), the target set"+                        " is the set of the contacts URI from the " +			" location service");                                        // 4. Forward the request statefully to each target Section 16.6.:                    requestForwarding.forwardRequest			(targetURIList,sipProvider,                    	request,serverTransaction,true);                                        return;                } else {                    // Let's continue and try the default hop.                }            }             // The registrar cannot help to decide the targets, so let's use             // our router: the default hop!            ProxyDebug.println		("Proxy, processRequest(), the registrar cannot help"+            " to decide the targets, so let's use our router: the default hop");            Router router=sipStack.getRouter();            if (router!=null) {                ProxyHop hop =(ProxyHop) router.getOutboundProxy();                if (hop!=null ) {                    if (ProxyDebug.debug)                        ProxyDebug.println			("Proxy, processRequest(), the target set"+                        " is the defaut hop: outbound proxy");		    // Bug fix contributed by Joe Provino		    String user = null;             	    if (requestURI.isSipURI()) {                	SipURI requestSipURI=(SipURI)requestURI;			user = requestSipURI.getUser();		    }                                        SipURI hopURI=addressFactory.createSipURI			(user,hop.getHost());                    hopURI.setTransportParam(hop.getTransport());                    hopURI.setPort(hop.getPort());                    targetURI=hopURI;                    targetURIList.addElement(targetURI);                                        // 4. Forward the request statelessly to each target Section 16.6.:                    requestForwarding.forwardRequest(targetURIList,sipProvider,                    request,serverTransaction,false);                                        return;                }            }                                 /* If the target set remains empty after applying all of the above, the               proxy MUST return an error response, which SHOULD be the 480               (Temporarily Unavailable) response.             */             Response response=messageFactory.createResponse             (Response.TEMPORARILY_UNAVAILABLE,request);             if (serverTransaction!=null)                    serverTransaction.sendResponse(response);             else sipProvider.sendResponse(response);                                if (ProxyDebug.debug)                 ProxyDebug.println("Proxy, processRequest(), unable to set "+                 " the targets, 480 (Temporarily Unavailable) replied:\n"+                 response.toString() );        }        catch (Exception ex){            try{                if (ProxyDebug.debug) {                    ProxyDebug.println("Proxy, processRequest(), internal error, "+                    "exception raised:");                    ProxyDebug.logException(ex);		    ex.printStackTrace();                }                                // This is an internal error:                // Let's return a 500 SERVER_INTERNAL_ERROR                Response response=messageFactory.createResponse                (Response.SERVER_INTERNAL_ERROR,request);                if (serverTransaction!=null)                    serverTransaction.sendResponse(response);                else sipProvider.sendResponse(response);                                   if (ProxyDebug.debug)                    ProxyDebug.println("Proxy, processRequest(),"+                    " 500 SERVER_INTERNAL_ERROR replied:\n"+                    response.toString());            }            catch (Exception e){                e.printStackTrace();            }        }    }        /** This is a listener method.     */    public void processResponse(ResponseEvent responseEvent) {        try{            	                Response response = responseEvent.getResponse();            SipProvider sipProvider = (SipProvider) responseEvent.getSource();            ClientTransaction clientTransaction=responseEvent.getClientTransaction();                        ProxyDebug.println            ("\n***************************************************************"+            "\n***************************************************************"+            "\nResponse "+response.getStatusCode() + " "+response.getReasonPhrase()            +" received:\n"+response.toString() );            ProxyDebug.println("Processing Response in progress");                        if (ProxyDebug.debug)                ProxyUtilities.printTransaction(clientTransaction);            	    //Henrik - added handling of responses addressed to server	    //If we use a presenceserver, and if statuscode was OK...	    CSeqHeader cseqHeader = (CSeqHeader)response.getHeader(CSeqHeader.NAME); 	     if (cseqHeader.getMethod().equals("SUBSCRIBE")) {		    presenceServer.processSubscribeResponse((Response)response.clone(), clientTransaction);	     } else if (cseqHeader.getMethod().equals("NOTIFY")) {		    //presenceServer.processNotifyResponse((Response)response.clone(), clientTransaction);	     }             responseForwarding.forwardResponse(sipProvider, response,clientTransaction);                    } catch (Exception ex) {            if (ProxyDebug.debug) {                ProxyDebug.println("Proxy, processResponse(), internal error, "+                "exception raised:");                ProxyDebug.logException(ex);            }        }    }            /** JAIN Listener method.     */    public void processTimeout(TimeoutEvent timeOutEvent) {        ProxyDebug.println("TimeoutEvent received");	SipProvider sipProvider = (SipProvider)timeOutEvent.getSource();	TransactionsMapping transactionsMapping = null;        if (timeOutEvent.isServerTransaction()) {            ServerTransaction serverTransaction  =            timeOutEvent.getServerTransaction();	    Dialog dialog = serverTransaction.getDialog();	    if (dialog != null) {		transactionsMapping = 			(TransactionsMapping) dialog.getApplicationData();                transactionsMapping.removeMapping(serverTransaction);	    }        } else {            ClientTransaction clientTransaction =            timeOutEvent.getClientTransaction();	    Dialog dialog = clientTransaction.getDialog();	    ServerTransaction st = null;	    if (dialog != null) {		transactionsMapping = 		(TransactionsMapping) dialog.getApplicationData();		if (transactionsMapping != null)  {                   st = transactionsMapping.getServerTransaction		   (clientTransaction);		}               if (st==null) {                  ProxyDebug.println		  ("ERROR, Unable to retrieve the server transaction,"+                  " cannot process timeout!");                  return;               }	    } else {               ProxyDebug.println		  ("ERROR, Unable to retrieve the transaction Mapping,"+                  " cannot process timeout!");                  return;	    }            Request request = st.getRequest();            // This removes the given mapping from the table but not            // necessarily the whole thing.            transactionsMapping.removeMapping(clientTransaction);            if (!transactionsMapping.hasMapping(st)) {                // No more mappings left in the transaction table.                try {                    Response response = messageFactory.createResponse                    (Response.REQUEST_TIMEOUT, request);                    st.sendResponse(response);                } catch (ParseException ex) {                    ex.printStackTrace();                } catch (SipException ex1) {                    ex1.printStackTrace();                }            }        }                    }                    /***********************  Methods for         ***************     *    starting and stopping the proxy          		*     ************************************************************/        /** Start the proxy, this method has to be called after the init method     * throws Exception that which can be caught by the upper application     */    public void start() throws Exception {        if (configuration!=null        && configuration.isValidConfiguration()) {            Properties properties=new Properties();            // LOGGING property:                        if (configuration.enableDebug) {                ProxyDebug.debug=true;                ProxyDebug.setProxyOutputFile(configuration.outputProxy);                ProxyDebug.println("DEBUG properties set!");                if (configuration.badMessageLogFile!=null)                    properties.setProperty("gov.nist.javax.sip.BAD_MESSAGE_LOG",                    configuration.badMessageLogFile);                if (configuration.debugLogFile!=null) {                    properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",                    configuration.debugLogFile);                                   }                if (configuration.serverLogFile!=null)                    properties.setProperty("gov.nist.javax.sip.SERVER_LOG",                    configuration.serverLogFile);                if (configuration.debugLogFile != null)                    properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",                    "32");                else                if (configuration.serverLogFile != null)                    properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",                    "16");                else                    properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",                    "0");                                       }            else {                System.out.println("DEBUG properties not set!");            }             registrar.setExpiresTime(configuration.expiresTime);                        // STOP TIME

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -