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

📄 transferprocessing.java

📁 开源项目openfire的完整源程序
💻 JAVA
字号:
/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2007 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package net.java.sipmack.sip;

import gov.nist.javax.sip.header.CSeq;
import net.java.sipmack.common.Log;

import javax.sip.*;
import javax.sip.address.URI;
import javax.sip.header.ContactHeader;
import javax.sip.header.ReferToHeader;
import javax.sip.header.ToHeader;
import javax.sip.message.Request;
import javax.sip.message.Response;
import java.text.ParseException;

/**
 * Title: SIPark
 * Description:JAIN-SIP Audio/Video phone application
 *
 * @author Thiago Rocha Camargo (thiago@jivesoftware.com)
 * @version 1.0, 20/07/2006
 */

public class TransferProcessing {
    protected SipManager sipManCallback = null;
    protected CallProcessing callProcessing = null;

    TransferProcessing(SipManager sipManCallback, CallProcessing callProcessing) {
        this.sipManCallback = sipManCallback;
        this.callProcessing = callProcessing;
    }

    void setSipManagerCallBack(SipManager sipManCallback) {
        this.sipManCallback = sipManCallback;
    }

    void processRefer(ServerTransaction serverTransaction, Request request) {
        System.out.println("REFER ANSWER");
    }

    public void transfer(int callID, String callee)
            throws CommunicationsException {

        Call call = callProcessing.getCallDispatcher().getCall(callID);
        Request refer = null;
        refer = call.getDialog().getFirstTransaction().getRequest();

        try {
            refer = call.getDialog().createRequest(Request.REFER);

        } catch (SipException e) {
            Log.error("hold", e);
        }

        int cseq = ((CSeq) (refer.getHeader(CSeq.NAME)))
                .getSequenceNumber() + 1;
        refer.removeHeader(CSeq.NAME);
        try {
            refer.addHeader(sipManCallback.headerFactory.createCSeqHeader(
                    cseq, Request.REFER));
        }
        catch (Exception e) {

        }

        callee = callee.trim();
        // Remove excessive characters from phone numbers such as '
        // ','(',')','-'
        String excessiveChars = SIPConfig.getExcessiveURIChar();

        if (excessiveChars != null) {
            StringBuffer calleeBuff = new StringBuffer(callee);
            for (int i = 0; i < excessiveChars.length(); i++) {
                String charToDeleteStr = excessiveChars.substring(i, i + 1);

                int charIndex = -1;
                while ((charIndex = calleeBuff.indexOf(charToDeleteStr)) != -1)
                    calleeBuff.delete(charIndex, charIndex + 1);
            }
            callee = calleeBuff.toString();
        }

        // Handle default domain name (i.e. transform 1234 -> 1234@sip.com

        String defaultDomainName = SIPConfig.getDefaultDomain();
        if (defaultDomainName != null // no sip scheme
                && !callee.trim().startsWith("tel:")
                && callee.indexOf('@') == -1 // most probably a sip uri
                ) {
            callee = callee + "@" + defaultDomainName;
        }

        // Let's be uri fault tolerant
        if (callee.toLowerCase().indexOf("sip:") == -1 // no sip scheme
                && callee.indexOf('@') != -1 // most probably a sip uri
                ) {
            callee = "sip:" + callee;

        }
        // Request URI
        URI requestURI;
        try {
            requestURI = sipManCallback.addressFactory.createURI(callee);
        }
        catch (ParseException ex) {

            throw new CommunicationsException(callee
                    + " is not a legal SIP uri!", ex);
        }

        // Content
        ReferToHeader referHeader = sipManCallback.headerFactory
                .createReferToHeader(sipManCallback.addressFactory.createAddress(requestURI));

        refer.addHeader(referHeader);

        refer.removeContent();

        try {
            refer.setMethod(Request.REFER.toString());
        } catch (ParseException e) {
            Log.error("transfer", e);
        }

        // Transaction
        ClientTransaction referTransaction;
        try {

            referTransaction = sipManCallback.sipProvider.getNewClientTransaction(refer);

            call.getDialog().sendRequest(referTransaction);

            call.setLastRequest(refer);
        }
        catch (SipException ee) {
            Log.error("transfer", ee);
        }

        return;

    }

    public void sayOK(int callID)
            throws CommunicationsException {
        try {

            Call call = callProcessing.getCallDispatcher().getCall(callID);
            if (call == null) {
                throw new CommunicationsException(
                        "Failed to find call with id=" + callID);
            }

            Dialog dialog = call.getDialog();
            if (dialog == null) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                        "Failed to extract call's associated dialog! Ending Call!");
            }
            Transaction transaction = dialog.getFirstTransaction();
            if (transaction == null || !dialog.isServer()) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                        "Failed to extract a ServerTransaction "
                                + "from the call's associated dialog!");
            }
            ServerTransaction serverTransaction = (ServerTransaction) transaction;
            Response ok = null;
            try {
                ok = sipManCallback.messageFactory.createResponse(Response.OK,
                        dialog.getFirstTransaction().getRequest());
                sipManCallback.attachToTag(ok, dialog);
            }
            catch (ParseException ex) {
                call.setState(Call.DISCONNECTED);
                throw new CommunicationsException(
                        "Failed to construct an OK response to an INVITE request",
                        ex);
            }

            // TODO This is here provisionally as my remote user agent that I am
            // using for
            // testing is not doing it. It is not correct from the protocol
            // point of view
            // and should probably be removed
            if (((ToHeader) ok.getHeader(ToHeader.NAME)).getTag() == null) {
                try {
                    ((ToHeader) ok.getHeader(ToHeader.NAME)).setTag(Integer
                            .toString(dialog.hashCode()));
                }
                catch (ParseException ex) {
                    call.setState(Call.DISCONNECTED);
                    throw new CommunicationsException("Unable to set to tag",
                            ex);
                }
            }
            ContactHeader contactHeader = sipManCallback.getContactHeader();
            ok.addHeader(contactHeader);
            try {
                serverTransaction.sendResponse(ok);

            }
            catch (SipException ex) {
                call.setState(Call.DISCONNECTED);

                throw new CommunicationsException(
                        "Failed to send an OK response to an INVITE request",
                        ex);
            } catch (InvalidArgumentException e) {
                call.setState(Call.DISCONNECTED);

                throw new CommunicationsException(
                        "Failed to send an OK response to an INVITE request",
                        e);
            }
        }
        finally {

        }

    } // answer call

}

⌨️ 快捷键说明

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