Package org.cafesip.jiplet.sip

Source Code of org.cafesip.jiplet.sip.ProxyUtilities

package org.cafesip.jiplet.sip;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ListIterator;

import javax.sip.ClientTransaction;
import javax.sip.ServerTransaction;
import javax.sip.Transaction;
import javax.sip.header.ViaHeader;
import javax.sip.message.Message;

import org.cafesip.jiplet.Jiplet;
import org.cafesip.jiplet.JipletLogger;

/**
* Utilities for the proxy.
*
* @version JAIN-SIP-1.1
*
* @author Olvier Deruelle <deruelle@nist.gov><br/>
* @author M. Ranganathan <mranga@nist.gov><br/><a href=" {@docRoot}
*                   /uncopyright.html">This code is in the public domain. </a>
*/
public class ProxyUtilities
{

    public static final String BRANCH_MAGIC_COOKIE = "z9hG4bK";

    /**
     * Generate a cryptographically random identifier that can be used to
     * generate a branch identifier.
     *
     * @return a cryptographically random gloablly unique string that can be
     *                 used as a branch identifier.
     */
    public static String generateBranchId()
    {
        String b = new Integer((int) (Math.random() * 10000)).toString()
                + System.currentTimeMillis();
        try
        {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte bid[] = messageDigest.digest(b.getBytes());
            // cryptographically random string.
            // prepend with a magic cookie to indicate we
            // are bis09 compatible.
            return BRANCH_MAGIC_COOKIE + toHexString(bid);
        }
        catch (NoSuchAlgorithmException ex)
        {

            return null;
        }
    }

    /**
     * to hex converter
     */
    private static final char[] toHex = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**
     * convert an array of bytes to an hexadecimal string
     *
     * @return a string
     * @param b
     *                       bytes array to convert to a hexadecimal string
     */

    public static String toHexString(byte b[])
    {
        int pos = 0;
        char[] c = new char[b.length * 2];
        for (int i = 0; i < b.length; i++)
        {
            c[pos++] = toHex[(b[i] >> 4) & 0x0F];
            c[pos++] = toHex[b[i] & 0x0f];
        }
        return new String(c);
    }

    public static String generateTag()
    {
        return new Integer((int) (Math.random() * 10000)).toString();
    }

    public static int length(Message message)
    {
        int cpt = 0;
        try
        {
            ListIterator l = message.getHeaders(ViaHeader.NAME);
            while (l.hasNext())
            {
                l.next();
                cpt++;
            }
            return cpt;
        }
        catch (Exception e)
        {
            return cpt;
        }
    }

    public static boolean hasTopViaHeaderProxy(Jiplet jiplet, Message message)
    {
        ListIterator viaList = message.getHeaders(ViaHeader.NAME);
        if (viaList == null || length(message) == 0
                || (viaList.hasNext() == false))
            return false;

        ViaHeader viaHeader = (ViaHeader) viaList.next();
       
        if (jiplet.hasAddress(viaHeader.getHost(), viaHeader.getPort()))
        {
            return true;
        }

        return false;
    }

    public static void printTransaction(Transaction transaction)
    {
        if (transaction == null)
        {
            JipletLogger
                    .debug("DEBUG TRANSACTION INFO: the transaction is null ");
            return;
        }

        if (transaction instanceof ServerTransaction)
        {
            ServerTransaction serverTransaction = (ServerTransaction) transaction;
            JipletLogger.debug("DEBUG TRANSACTION INFO: here is the "
                    + " server transaction: " + serverTransaction);

            JipletLogger.debug("DEBUG INFO: Its dialog is: "
                    + serverTransaction.getDialog());
        }
        else if (transaction instanceof ClientTransaction)
        {
            ClientTransaction clientTransaction = (ClientTransaction) transaction;
            JipletLogger.debug("DEBUG TRANSACTION INFO: here is the "
                    + " client transaction: " + clientTransaction);

            JipletLogger.debug("DEBUG TRANSACTION INFO: Its dialog is: "
                    + clientTransaction.getDialog());
        }
    }

}
TOP

Related Classes of org.cafesip.jiplet.sip.ProxyUtilities

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.