responseforwarding.java

来自「First of all, the Applet-phone is a SIP 」· Java 代码 · 共 199 行

JAVA
199
字号
/* * ResponseForwarding.java * * Created on April 16, 2003, 11:09 AM */package gov.nist.examples.busy.gateway;import java.util.*;import javax.sip.*;import javax.sip.message.*;import javax.sip.header.*;import javax.sip.address.*;import tools.rtpgateway.*;import  javax.sdp.*;/** * * @author  deruelle */public class ResponseForwarding {        protected Gateway gateway;        /** Creates a new instance of ResponseForwarding */    public ResponseForwarding(Gateway gateway) {        this.gateway=gateway;    }        public void forwardResponse(SipProvider sipProvider,    Response response,ClientTransaction clientTransaction) {        MessageFactory messageFactory=gateway.getMessageFactory();        HeaderFactory headerFactory=gateway.getHeaderFactory();        AddressFactory addressFactory=gateway.getAddressFactory();        Configuration configuration=gateway.getConfiguration();        try{            /*******************************************************************************//***********************  gateway BEHAVIOR    ************************************//*******************************************************************************/                                Response clonedResponse = (Response) response.clone();             Object sdpObjectContent=clonedResponse.getContent();            String sdpContent=null;            if (sdpObjectContent instanceof String)                sdpContent=(String)sdpObjectContent;            else                if (sdpObjectContent instanceof byte[] ) {                    sdpContent=new String(  (byte[])sdpObjectContent);                }                        SessionDescription sd=null;            int sdpPort=-1;            String sdpAddress=null;            try{                SdpFactory factory=SdpFactory.getInstance();                if(sdpContent!=null){                    sd=factory.createSessionDescription(sdpContent);                    Vector timeDescriptions=sd.getTimeDescriptions(false);                                    if(timeDescriptions.size()<1){                        Time time=factory.createTime();                        timeDescriptions=new Vector();                        timeDescriptions.addElement(time);                        sd.setTimeDescriptions(timeDescriptions);                                        ContentTypeHeader newContentTypeHeader=headerFactory.createContentTypeHeader(                                    "application","sdp");                        clonedResponse.setContent(sd.toString(),newContentTypeHeader);                    }                }            }            catch(Exception e) {                e.printStackTrace();                throw new RTPHoleException("ResponseForwarding exception, the SDP content"+                " parameter parsing has failed.");            }                                                                  //if (clientTransaction == null)  {                if (GatewayDebug.debug)                    GatewayDebug.println                    ("ResponseForwarding, forwardResponse(), the client transaction"+                    " does not exist, we"+                    " will forward the response statelessly!");                               // if (gatewayUtilities.hasTopViaHeadergateway(sipStack,response) ) {                    ListIterator viaList=clonedResponse.getHeaders(ViaHeader.NAME);                    clonedResponse.removeHeader(ViaHeader.NAME);                    Vector v=new Vector();                    viaList.next();                    while (viaList!=null && viaList.hasNext()) {                        ViaHeader viaHeader=(ViaHeader) viaList.next();                        v.addElement(viaHeader);                    }                    for (int j=0;j<v.size();j++) {                        ViaHeader viaHeader=(ViaHeader) v.elementAt(j);                        clonedResponse.addHeader(viaHeader);                    }                                        viaList=clonedResponse.getHeaders(ViaHeader.NAME);                    if (viaList==null || !viaList.hasNext()) {                        if (GatewayDebug.debug)                            GatewayDebug.println                            ("ResponseForwarding, forwardResponse(), the response (Via header)does"+                            " not have any Via header, the response is silently discarded");                    }                    else {                        // let's hope the sipProvider will take the Viaheader to                        // forward the response...                                                // We have to open a hole                        CSeqHeader cseqHeader=(CSeqHeader)clonedResponse.getHeader(CSeqHeader.NAME);                        if ( clonedResponse.getStatusCode() == 200 &&                        cseqHeader.getMethod().equals("INVITE") && configuration.enableRTPGateway) {                            try{                                                               Object content=clonedResponse.getContent();                                String text=null;                                if (content instanceof String)                                    text=(String)content;                                else                                    if (content instanceof byte[] ) {                                        text=new String(  (byte[])content  );                                    }                                                                GatewayDebug.println                                ("ResponseForwarding, forwardResponse(), SDP text:"+text);                                if (text!=null) {                                     ContentTypeHeader contentTypeHeader=gateway.                                        headerFactory.createContentTypeHeader(                                        "application","sdp");                                    // The SDP is received let's open a hole:                                    SipStack sipStack=sipProvider.getSipStack();                                    SipStack opp=gateway.getOppositeSipStack(sipStack.getIPAddress());                                                                        CallIdHeader callIdHeader=(CallIdHeader)clonedResponse.                                    getHeader(CallIdHeader.NAME);                                                                                                    RTPHole secondRTPHole=gateway.rtpHolesManager.createRTPHole(                                    callIdHeader.getCallId(),                                    text,                                    opp.getIPAddress(),                                    sipStack.getIPAddress());                                                                        GatewayDebug.println                                    ("ResponseForwarding, forwardResponse(), we open this hole...");                                                                        secondRTPHole.open();                                                                                                            String generatedSDPHole=secondRTPHole.getGeneratedSDPHole();                                    clonedResponse.setContent(generatedSDPHole,contentTypeHeader);                                    clonedResponse.removeHeader(ContactHeader.NAME);                                                                    }                            }                            catch(Exception e){                                e.printStackTrace();                                GatewayDebug.println                                    ("ResponseForwarding, forwardResponse(), problem"+                                    " to open the RTP hole...");                                                                }                        }                                    sipProvider.sendResponse(clonedResponse);                                     if (GatewayDebug.debug)                            GatewayDebug.println                            ("ResponseForwarding, forwardResponse(), the response statelessly forwarded:\n"+                            clonedResponse.toString());                    }                    return;                /*}                else {                    //If the address does not match the                    //gateway, the message MUST be silently discarded.                    if (GatewayDebug.debug)                        GatewayDebug.println                        ("ResponseForwarding, forwardResponse(), the address (Via header)does not"+                        " match the gateway, the message is silently discarded");                    return;                }*/                            //}                     }        catch (Exception ex) {            if (GatewayDebug.debug) {                GatewayDebug.println("ResponseForwarding, forwardResponse(), internal error, "+                "exception raised:");                GatewayDebug.logException(ex);            }        }    }}

⌨️ 快捷键说明

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