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

📄 messageprocessing.java.svn-base

📁 开源项目openfire的完整源程序
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/**
 * $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.simple;

import net.java.sipmack.common.Log;
import net.java.sipmack.sip.CommunicationsException;
import net.java.sipmack.sip.SIPConfig;
import net.java.sipmack.sip.SipManager;
import net.java.sipmack.sip.security.SipSecurityException;

import javax.sip.ClientTransaction;
import javax.sip.InvalidArgumentException;
import javax.sip.ServerTransaction;
import javax.sip.SipException;
import javax.sip.TransactionUnavailableException;
import javax.sip.address.Address;
import javax.sip.address.URI;
import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ContentLengthHeader;
import javax.sip.header.ContentTypeHeader;
import javax.sip.header.ExpiresHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.ToHeader;
import javax.sip.message.Request;
import javax.sip.message.Response;

import java.net.InetAddress;
import java.text.ParseException;
import java.util.ArrayList;

import gov.nist.javax.sip.message.SIPRequest;
import gov.nist.javax.sip.message.SIPMessage;
import gov.nist.javax.sip.stack.MessageChannel;
import gov.nist.javax.sip.stack.SIPClientTransaction;
import gov.nist.javax.sip.ListeningPointImpl;


public class MessageProcessing {
    protected SipManager sipManCallback = null;

    public MessageProcessing() {
    }

    public MessageProcessing(SipManager sipManCallback) {
        this.sipManCallback = sipManCallback;
    }

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

    /**
     * Attempts to re-generate the corresponding request with the proper
     * credentials and terminates the call if it fails.
     *
     * @param clientTransaction the corresponding transaction
     * @param response          the challenge
     */
    void processAuthenticationChallenge(ClientTransaction clientTransaction,
                                        Response response) {
        try {

            Request challengedRequest = clientTransaction.getRequest();
            Request reoriginatedRequest = null;

            ClientTransaction retryTran = sipManCallback.sipSecurityManager
                    .handleChallenge(response, clientTransaction);

            retryTran.sendRequest();
        }
        catch (SipSecurityException exc) {
            sipManCallback.fireCommunicationsError(new CommunicationsException(
                    "Authorization failed!", exc));
        }
        catch (Exception exc) {
            sipManCallback.fireCommunicationsError(new CommunicationsException(
                    "Failed to resend a request "
                            + "after a security challenge!", exc));
        }
        finally {

        }
    }

    /**
     * Process MESSAGE requests and send OK response.
     *
     * @param serverTransaction
     * @param request
     */
    public void processMessageRequest(ServerTransaction serverTransaction,
                                      Request request) {
        try {

            // Send OK
            Response ok = null;
            try {
                ok = sipManCallback.messageFactory.createResponse(Response.OK,
                        request);
                // sipManCallback.attachToTag(ok,
                // serverTransaction.getDialog());
            }
            catch (ParseException ex) {
                sipManCallback
                        .fireCommunicationsError(new CommunicationsException(
                                "Failed to construct an OK response to a MESSAGE request!",
                                ex));
                return;
            }
            try {
                serverTransaction.sendResponse(ok);

            }
            catch (SipException ex) {
                // This is not really a problem according to the RFC
                // so just dump to stdout should someone be interested
            } catch (InvalidArgumentException e) {

            }
        }
        finally {

        }
    }

    public void sendKeepAlive() throws CommunicationsException {

        String to = "";
        byte[] messageBody = "".getBytes();

        try {

            to = to.trim();
            // Handle default domain name (i.e. transform 1234 -> 1234@sip.com
            String defaultDomainName = SIPConfig.getDefaultDomain();
            if (defaultDomainName != null // no sip scheme
                    && !to.trim().startsWith("tel:") && to.indexOf('@') == -1) {
                to = to + "@" + defaultDomainName;
            }

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

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

                throw new CommunicationsException(to
                        + " is not a legal SIP uri!", ex);
            }
            // Call ID
            CallIdHeader callIdHeader = sipManCallback.sipProvider
                    .getNewCallId();
            // CSeq
            CSeqHeader cSeqHeader;
            try {
                cSeqHeader = sipManCallback.headerFactory.createCSeqHeader(1,
                        Request.MESSAGE);
            }
            catch (Exception ex) {
                // Shouldn't happen
                throw new CommunicationsException(
                        "An unexpected erro occurred while"
                                + "constructing the CSeqHeadder", ex);
            }
            // FromHeader
            FromHeader fromHeader = sipManCallback.getFromHeader();
            // ToHeader
            Address toAddress = sipManCallback.addressFactory
                    .createAddress(requestURI);
            ToHeader toHeader;
            try {
                toHeader = sipManCallback.headerFactory.createToHeader(
                        toAddress, null);
            }
            catch (ParseException ex) {
                // Shouldn't happen
                throw new CommunicationsException(
                        "Null is not an allowed tag for the to header!", ex);
            }

            ContentLengthHeader contentLengthHeader = null;
            try {
                contentLengthHeader = sipManCallback.headerFactory
                        .createContentLengthHeader(messageBody.length);
            }
            catch (InvalidArgumentException ex) {
                throw new CommunicationsException(
                        "Cseq Header must contain a integer value!", ex);
            }

            ExpiresHeader expiresHeader = null;
            try {
                expiresHeader = sipManCallback.headerFactory
                        .createExpiresHeader(30);
            }
            catch (InvalidArgumentException ex) {
                throw new CommunicationsException(
                        "Expires Header must be an integer!", ex);
            }

            String contentType = "text/plain";
            ContentTypeHeader contentTypeHeader = null;
            try {
                String[] contentTypeTab = contentType.split("/");
                contentTypeHeader = sipManCallback.headerFactory
                        .createContentTypeHeader(contentTypeTab[0],
                                contentTypeTab[1]);
            }
            catch (ParseException ex) {
                throw new CommunicationsException(
                        "ContentType Header must look like type/subtype!", ex);

⌨️ 快捷键说明

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