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

📄 requestforwarding.java

📁 基于JAINSIP的一个proxy源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**************  8. Add a Via header field value                      **********//*******************************************************************************/                         /* The proxy MUST insert a Via header field value into the copy            before the existing Via header field values.          */                                stackIPAddress=sipStack.getIPAddress();		/*                lps=sipStack.getListeningPoints();                lp=(ListeningPoint)lps.next();		*/		lp = sipProvider.getListeningPoint();                                ViaHeader viaHeader=null;                                if ( clonedRequest.getMethod().equals(Request.CANCEL) ) {                    // Branch Id will be assigned by the stack.                    viaHeader=headerFactory.createViaHeader                    (stackIPAddress,lp.getPort(),lp.getTransport(),null);                                        if (clonedRequest.getMethod().equals(Request.CANCEL) ) {                        // Cancel is hop by hop so remove all other via headers.                        clonedRequest.removeHeader(ViaHeader.NAME);                    }                }                else  {                                      viaHeader=headerFactory.createViaHeader                    (stackIPAddress,lp.getPort(),                    lp.getTransport(),ProxyUtilities.generateBranchId());                }                                if (viaHeader!=null)                    clonedRequest.addHeader(viaHeader);                                                  if (ProxyDebug.debug)                    ProxyDebug.println		    ("RequestForwarding, forwardRequest(), (STEP 8)"+                    " the proxy inserts a Via header field value into the copy"+                    " before the existing Via header field values");                         /* Proxies choosing to detect loops have an additional constraint         in the value they use for construction of the branch parameter.         A proxy choosing to detect loops SHOULD create a branch         parameter separable into two parts by the implementation.  The         first part MUST satisfy the constraints of Section 8.1.1.7 as         described above.  The second is used to perform loop detection         and distinguish loops from spirals.          */                // Not yet implemented                if (ProxyDebug.debug)                    ProxyDebug.println		    ("RequestForwarding, forwardRequest(), (STEP 8)"+                    " Loop detection not implemented");/****************************************************************************//*********** 9. Add a Content-Length header field if necessary     **********//****************************************************************************/                                try {                    ContentTypeHeader contentTypeHeader=(ContentTypeHeader)                    clonedRequest.getHeader(ContentTypeHeader.NAME);                    if (contentTypeHeader==null) {                        if (ProxyDebug.debug)                        ProxyDebug.println			("RequestForwarding, forwardRequest(),"+                        " no Content-Type header,"+                        " we don't stripe any parameters!!!");                    }                    else contentTypeHeader.removeParameter("msgr");                                    }                catch (Exception e) {                    ProxyDebug.println		     ("RequestForwarding, forwardRequest(), Stripe"+                    " Parameter failed!!!!");                }                /****************************************************************************//*********** 10. Forward Request                                   **********//****************************************************************************/                            if (statefullForwarding) {                    forwardRequestStatefully(sipProvider,                    clonedRequest,request,serverTransaction);                }                else {                    forwardRequestStatelessly(sipProvider,                    clonedRequest,request,serverTransaction);                }                /***************************************************************************//********** 11. Set timer C                                       **********//***************************************************************************/                                // Not Implemented....            }        }        catch (Exception ex){            try{                if (ProxyDebug.debug) {                    ProxyDebug.println		    ("RequestForwarding, forwardRequest(), "+                    " internal error, "+                    "exception raised:");                    ProxyDebug.logException(ex);                }                // 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		    ( "RequestForwarding, forwardRequest(), " +		    " 500 SERVER_INTERNAL_ERROR replied:\n"+                     response.toString());            }            catch (Exception e){                e.printStackTrace();            }        }    }   /** Forward the request statefully.    *@param sipProvider -- sip provider to forward request.    *@param clonedRequest -- cloned request to forward.    *@param originalRequest -- incoming request     *@param serverTransaction -- server transaction used to fwd the request.    */            private void forwardRequestStatefully	(SipProvider sipProvider,        Request clonedRequest,	Request originalRequest,	ServerTransaction serverTransaction) {        MessageFactory messageFactory=proxy.getMessageFactory();        HeaderFactory headerFactory=proxy.getHeaderFactory();        AddressFactory addressFactory=proxy.getAddressFactory();        SipStack sipStack=proxy.getSipStack();	if (ProxyDebug.debug) {		ProxyDebug.println("serverTransaction =  " +				serverTransaction);		if (serverTransaction != null) 		    ProxyDebug.println("dialog =  " +				serverTransaction.getDialog());	}                try{           /* A stateful proxy MUST create a new client transaction for this             request as described in Section 17.1 and instructs the             transaction to send the request using the address, port and             transport determined in step 7            *//************************************************************************/ /************************* SERVER TRANSACTION CHECK *********************/ /************************************************************************/             if (serverTransaction==null 		&& ! clonedRequest.getMethod().equals(Request.MESSAGE) ) {		// dont create a server transaction for MESSAGE -		// just forward the request statefully through a new		// client transaction.                if (ProxyDebug.debug)                  ProxyDebug.println                    ("RequestForwarding, forwardRequestStatefully(),"+                   " the cloned request does not have a server transaction,"+                   " so we drop the request!");                 return;            }                         if (originalRequest.getMethod().equals(Request.CANCEL) ) {                // 487 Request Terminated to reply:                Response response=messageFactory.createResponse                (Response.REQUEST_TERMINATED,originalRequest);                CSeqHeader cSeqHeader=(CSeqHeader)response.getHeader(CSeqHeader.NAME);                cSeqHeader.setMethod("INVITE");                serverTransaction.sendResponse(response);                             if (ProxyDebug.debug)                ProxyDebug.println		("Proxy, processRequest(), 487 Request Terminated"+                " replied to the CANCEL:\n" +                response.toString());            }                            /***************************************************************************/ /******* FROM TAG UPDATE FOR METHODS DIALOG CREATOR   **********************/ /***************************************************************************/                  // Important step: update the From tag ONLY for statefull forwarding      FromHeader 	fromHeader=(FromHeader)clonedRequest.getHeader(FromHeader.NAME);     ToHeader toHeader=(ToHeader)clonedRequest.getHeader(ToHeader.NAME);     CSeqHeader cseqHeader=(CSeqHeader)clonedRequest.getHeader(CSeqHeader.NAME);     String method=clonedRequest.getMethod();            /**************************************************************************/ /******               DIALOG CHECK                   **********************/ /**************************************************************************/            	// Note that the proxy server is actually implemented as a back	// to back User Agent.                Dialog dialog= null;	   if (serverTransaction != null) 		dialog = serverTransaction.getDialog();	   DialogState dialogState = null;	   if (dialog != null ) 	      dialogState=dialog.getState();           if ( dialogState==null )  {                 if (ProxyDebug.debug)                    ProxyDebug.println		    ("RequestForwarding, forwardRequestStatefully(),"+                    " the dialog state is null, so we have to"+                    " forward the request using"+                    " a new clientTransaction");                 ClientTransaction clientTransaction =                    sipProvider.getNewClientTransaction                    (clonedRequest);                 clientTransaction.sendRequest();                                     if (ProxyDebug.debug)                        ProxyDebug.println                        ("RequestForwarding, forwardRequestStatefully(),"+                        ", cloned request forwarded statefully:\n "                        +clonedRequest);                 		 if (dialog != null)  {                   TransactionsMapping transactionsMapping=		      (TransactionsMapping)		    serverTransaction.getDialog().				getApplicationData();                    transactionsMapping.addMapping			(serverTransaction,clientTransaction);		}                 return;           }	   // Dialog has already been assigned so transactions map	   // should also be there.           ProxyDebug.println("Dialog checked.");                                      // Special processing because the SUBSCRIBE is 	   // hop by hop for a presence server	   // Henrik: No special processing needed for SUBSCRIBE:	   //   either this presenceserver is the PA of the PUA	   //   and the subscribe ends here, or the subscribe is	   //   forwarded like any other message.	   /*	     // Special processing because the SUBSCRIBE is 	     // hop by hop for a presence server	     if ( proxy.isPresenceServer() )    {		this.forwardIMPresenceRequest ( 				sipProvider, 				serverTransaction,				clonedRequest, 			        originalRequest,				dialog);		return;		} else {	   */		this.forwardRequestThroughDialog(			sipProvider,			serverTransaction,			clonedRequest,			dialog);		return;                   } catch (Exception ex){            try{                if (ProxyDebug.debug) {                    ProxyDebug.println		     ("RequestForwarding, forwardRequestStatefully(), "+                    " internal error, "+                    "exception raised:");                    ProxyDebug.logException(ex);		    ex.printStackTrace();		    System.exit(0);                }                // This is an internal error:                // Let's return a 500 SERVER_INTERNAL_ERROR                Response response=messageFactory.createResponse                (Response.SERVER_INTERNAL_ERROR,originalRequest);                if (serverTransaction!=null)                    serverTransaction.sendResponse(response);                else sipProvider.sendResponse(response);                                if (ProxyDebug.debug)                    ProxyDebug.println(                    "RequestForwarding, forwardRequestStatefully(), " +		    " 500 SERVER_INTERNAL_ERROR replied:\n"+                    response.toString());            }            catch (Exception e){                e.printStackTrace();            }       }    }    /** Special processing for Instant Messaging and Presence     * messages.     *     * @deprecated No special processing needed for SUBSCRIBE,	      either this presenceserver is the PA of the PUA	      and the subscribe ends here, or the subscribe is	      forwarded like any other message.     */

⌨️ 快捷键说明

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